From 2d6ea6a9ec3d48db6f2598219de1c075c247627c Mon Sep 17 00:00:00 2001 From: saji Date: Fri, 19 May 2023 16:57:05 -0500 Subject: [PATCH] json tests and fixes --- skylab/gen_skylab.go | 63 ++++++--------------------- skylab/templates/golang.go.tmpl | 4 +- skylab/templates/golang_tests.go.tmpl | 51 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 skylab/templates/golang_tests.go.tmpl diff --git a/skylab/gen_skylab.go b/skylab/gen_skylab.go index 9f954cd..80d18a2 100644 --- a/skylab/gen_skylab.go +++ b/skylab/gen_skylab.go @@ -53,55 +53,6 @@ type PacketDef struct { // we need to generate bitfield types. // constants for packet IDs or a map. - -var test = ` -packets: - - name: dashboard_pedal_percentages - description: ADC values from the brake and accelerator pedals. - id: 0x290 - endian: little - frequency: 10 - data: - - name: reason1 - type: bitfield - bits: - - name: OVERVOLT - - name: UNDERVOLT - - name: OVERTEMP - - name: TEMP_DISCONNECT - - name: COMM_FAIL - - name: reason2 - type: bitfield - bits: - - name: HARDWARE - - name: KILL_PACKET - - name: UKNOWN - - name: OVERCURRENT - - name: PRECHARGE_FAIL - - name: AUX_OVER_UNDER - - name: module - type: uint16_t - - name: value - type: float - - name: bms_module - description: Voltage and temperature for a single module - id: 0x01C - endian: little - repeat: 36 - offset: 1 - frequency: 2 - data: - - name: voltage - type: float - units: V - conversion: 1 - - name: temperature - type: float - units: C - conversion: 1 -` - - var typeMap = map[string]string{ "uint16_t": "uint16", "uint32_t": "uint32", @@ -381,14 +332,28 @@ func main() { } f, err := os.Create("skylab_gen.go") + defer f.Close() if err != nil { panic(err) } err = tmpl.Execute(f, v) + if err != nil { panic(err) } + tests := tmpl.Lookup("golang_tests.go.tmpl") + if tests == nil { + panic("tests not found") + } + + testF, err := os.Create("skylab_gen_test.go") + defer f.Close() + if err != nil { + panic(err) + } + tests.Execute(testF, v) + } diff --git a/skylab/templates/golang.go.tmpl b/skylab/templates/golang.go.tmpl index b31919e..42a5eee 100644 --- a/skylab/templates/golang.go.tmpl +++ b/skylab/templates/golang.go.tmpl @@ -145,13 +145,13 @@ func FromJson (raw []byte) (Packet, error) { {{- range $p := .Packets }} {{- if $p.Repeat }} case {{ Nx (int $p.Id) $p.Repeat $p.Offset | mapf "0x%X" | strJoin ", " -}}: - var res *{{camelCase $p.Name true}} + var res = &{{camelCase $p.Name true}}{} err := json.Unmarshal(jp.Data, res) res.Idx = jp.Id - {{ $p.Id | printf "0x%X" }} return res, err {{- else }} case {{ $p.Id | printf "0x%X" }}: - var res *{{camelCase $p.Name true}} + var res = &{{camelCase $p.Name true}}{} err := json.Unmarshal(jp.Data, res) return res, err {{- end }} diff --git a/skylab/templates/golang_tests.go.tmpl b/skylab/templates/golang_tests.go.tmpl new file mode 100644 index 0000000..fedf567 --- /dev/null +++ b/skylab/templates/golang_tests.go.tmpl @@ -0,0 +1,51 @@ + +package skylab + +import ( + "testing" + "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}}{} + jp, err := ToJson(v) + if err != nil { + t.Fatal(err) + } + + rawData, err := json.Marshal(jp) + if err != nil { + t.Fatal(err) + } + + p, err := FromJson(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) + } + + +} + +{{- end }}