gotelem/skylab/templates/golang_tests.go.tmpl
2024-03-07 13:29:50 -06:00

65 lines
1.1 KiB
Cheetah

package skylab
import (
"testing"
"reflect"
"encoding/json"
)
{{ range .Packets }}
{{- $structName := camelCase .Name true}}
func TestMarshalUnmarshal{{$structName}}(t *testing.T) {
v := &{{$structName}}{}
bin, err := v.MarshalPacket()
if err != nil {
t.Fatal(err)
}
err = v.UnmarshalPacket(bin)
if err != nil {
t.Fatal(err)
}
}
func TestJSON{{$structName}}(t *testing.T) {
v := &{{$structName}}{}
rawData, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
p, err := FromJson(v.String(), rawData)
if err != nil {
t.Fatal(err)
}
switch underlying := p.(type) {
case *{{$structName}}:
break
default:
t.Fatalf("didn't match type: %T, %v", underlying, underlying)
}
}
func TestCanFrame{{$structName}}(t *testing.T) {
v := &{{$structName}}{}
frame, err := ToCanFrame(v)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
retpkt, err := FromCanFrame(frame)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if !reflect.DeepEqual(v, retpkt) {
t.Fatalf("decoded packet did not match sent %v got %v", v, retpkt)
}
}
{{- end }}