json tests and fixes

This commit is contained in:
saji 2023-05-19 16:57:05 -05:00
parent ac5ccaa964
commit 2d6ea6a9ec
3 changed files with 67 additions and 51 deletions

View file

@ -53,55 +53,6 @@ type PacketDef struct {
// we need to generate bitfield types. // we need to generate bitfield types.
// constants for packet IDs or a map. // 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{ var typeMap = map[string]string{
"uint16_t": "uint16", "uint16_t": "uint16",
"uint32_t": "uint32", "uint32_t": "uint32",
@ -381,14 +332,28 @@ func main() {
} }
f, err := os.Create("skylab_gen.go") f, err := os.Create("skylab_gen.go")
defer f.Close()
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = tmpl.Execute(f, v) err = tmpl.Execute(f, v)
if err != nil { if err != nil {
panic(err) 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)
} }

View file

@ -145,13 +145,13 @@ func FromJson (raw []byte) (Packet, error) {
{{- range $p := .Packets }} {{- range $p := .Packets }}
{{- if $p.Repeat }} {{- if $p.Repeat }}
case {{ Nx (int $p.Id) $p.Repeat $p.Offset | mapf "0x%X" | strJoin ", " -}}: 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) err := json.Unmarshal(jp.Data, res)
res.Idx = jp.Id - {{ $p.Id | printf "0x%X" }} res.Idx = jp.Id - {{ $p.Id | printf "0x%X" }}
return res, err return res, err
{{- else }} {{- else }}
case {{ $p.Id | printf "0x%X" }}: case {{ $p.Id | printf "0x%X" }}:
var res *{{camelCase $p.Name true}} var res = &{{camelCase $p.Name true}}{}
err := json.Unmarshal(jp.Data, res) err := json.Unmarshal(jp.Data, res)
return res, err return res, err
{{- end }} {{- end }}

View file

@ -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 }}