github.com/jshiv/can-go@v0.2.1-0.20210224011015-069e90e90bdf/pkg/cantext/encode_test.go (about) 1 package cantext 2 3 import ( 4 "strings" 5 "testing" 6 "time" 7 8 "go.einride.tech/can" 9 "go.einride.tech/can/pkg/descriptor" 10 "go.einride.tech/can/pkg/generated" 11 "gotest.tools/v3/assert" 12 ) 13 14 func TestMarshal(t *testing.T) { 15 for _, tt := range []struct { 16 name string 17 msg generated.Message 18 expected string 19 expectedCompact string 20 }{ 21 { 22 name: "with enum", 23 msg: &testMessage{ 24 frame: can.Frame{ID: 100, Length: 1, Data: can.Data{2}}, 25 descriptor: newDriverHeartbeatDescriptor(), 26 }, 27 expected: ` 28 DriverHeartbeat 29 Command: 2 (0x2) Reboot 30 `, 31 expectedCompact: `{Command: Reboot}`, 32 }, 33 { 34 name: "with unit", 35 msg: &testMessage{ 36 frame: can.Frame{ID: 100, Length: 3, Data: can.Data{1, 0x7b}}, 37 descriptor: newMotorStatusDescriptor(), 38 }, 39 expected: ` 40 MotorStatus 41 WheelError: true 42 SpeedKph: 0.123km/h (0x7b) 43 `, 44 expectedCompact: `{WheelError: true, SpeedKph: 0.123km/h}`, 45 }, 46 } { 47 tt := tt 48 t.Run(tt.name, func(t *testing.T) { 49 t.Run("standard", func(t *testing.T) { 50 txt := Marshal(tt.msg) 51 assert.Equal(t, strings.TrimSpace(tt.expected), string(txt)) 52 }) 53 t.Run("compact", func(t *testing.T) { 54 txt := MarshalCompact(tt.msg) 55 assert.Equal(t, strings.TrimSpace(tt.expectedCompact), string(txt)) 56 }) 57 }) 58 } 59 } 60 61 func TestAppendID(t *testing.T) { 62 const expected = "ID: 100 (0x64)" 63 actual := string(AppendID([]byte{}, newDriverHeartbeatDescriptor())) 64 assert.Equal(t, expected, actual) 65 } 66 67 func TestAppendSender(t *testing.T) { 68 const expected = "Sender: DRIVER" 69 actual := string(AppendSender([]byte{}, newDriverHeartbeatDescriptor())) 70 assert.Equal(t, expected, actual) 71 } 72 73 func TestAppendSendType(t *testing.T) { 74 const expected = "SendType: Cyclic" 75 actual := string(AppendSendType([]byte{}, newDriverHeartbeatDescriptor())) 76 assert.Equal(t, expected, actual) 77 } 78 79 func TestAppendCycleTime(t *testing.T) { 80 const expected = "CycleTime: 100ms" 81 actual := string(AppendCycleTime([]byte{}, newDriverHeartbeatDescriptor())) 82 assert.Equal(t, expected, actual) 83 } 84 85 func TestAppendDelayTime(t *testing.T) { 86 const expected = "DelayTime: 2s" 87 actual := string(AppendDelayTime([]byte{}, newDriverHeartbeatDescriptor())) 88 assert.Equal(t, expected, actual) 89 } 90 91 func TestAppendFrame(t *testing.T) { 92 const expected = "Frame: 042#123456" 93 actual := string(AppendFrame([]byte{}, can.Frame{ID: 0x42, Length: 3, Data: can.Data{0x12, 0x34, 0x56}})) 94 assert.Equal(t, expected, actual) 95 } 96 97 func newDriverHeartbeatDescriptor() *descriptor.Message { 98 return &descriptor.Message{ 99 Name: (string)("DriverHeartbeat"), 100 ID: (uint32)(100), 101 IsExtended: (bool)(false), 102 Length: (uint8)(1), 103 Description: (string)("Sync message used to synchronize the controllers"), 104 SendType: descriptor.SendTypeCyclic, 105 CycleTime: 100 * time.Millisecond, 106 DelayTime: 2 * time.Second, 107 Signals: []*descriptor.Signal{ 108 { 109 Name: (string)("Command"), 110 Start: (uint8)(0), 111 Length: (uint8)(8), 112 IsBigEndian: (bool)(false), 113 IsSigned: (bool)(false), 114 IsMultiplexer: (bool)(false), 115 IsMultiplexed: (bool)(false), 116 MultiplexerValue: (uint)(0), 117 Offset: (float64)(0), 118 Scale: (float64)(1), 119 Min: (float64)(0), 120 Max: (float64)(0), 121 Unit: (string)(""), 122 Description: (string)(""), 123 ValueDescriptions: []*descriptor.ValueDescription{ 124 { 125 Value: (int)(0), 126 Description: (string)("None"), 127 }, 128 { 129 Value: (int)(1), 130 Description: (string)("Sync"), 131 }, 132 { 133 Value: (int)(2), 134 Description: (string)("Reboot"), 135 }, 136 }, 137 ReceiverNodes: []string{ 138 (string)("SENSOR"), 139 (string)("MOTOR"), 140 }, 141 DefaultValue: (int)(0), 142 }, 143 }, 144 SenderNode: (string)("DRIVER"), 145 } 146 } 147 148 func newMotorStatusDescriptor() *descriptor.Message { 149 return &descriptor.Message{ 150 Name: (string)("MotorStatus"), 151 ID: (uint32)(400), 152 IsExtended: (bool)(false), 153 Length: (uint8)(3), 154 Description: (string)(""), 155 Signals: []*descriptor.Signal{ 156 { 157 Name: (string)("WheelError"), 158 Start: (uint8)(0), 159 Length: (uint8)(1), 160 IsBigEndian: (bool)(false), 161 IsSigned: (bool)(false), 162 IsMultiplexer: (bool)(false), 163 IsMultiplexed: (bool)(false), 164 MultiplexerValue: (uint)(0), 165 Offset: (float64)(0), 166 Scale: (float64)(1), 167 Min: (float64)(0), 168 Max: (float64)(0), 169 Unit: (string)(""), 170 Description: (string)(""), 171 ValueDescriptions: ([]*descriptor.ValueDescription)(nil), 172 ReceiverNodes: []string{ 173 (string)("DRIVER"), 174 (string)("IO"), 175 }, 176 DefaultValue: (int)(0), 177 }, 178 { 179 Name: (string)("SpeedKph"), 180 Start: (uint8)(8), 181 Length: (uint8)(16), 182 IsBigEndian: (bool)(false), 183 IsSigned: (bool)(false), 184 IsMultiplexer: (bool)(false), 185 IsMultiplexed: (bool)(false), 186 MultiplexerValue: (uint)(0), 187 Offset: (float64)(0), 188 Scale: (float64)(0.001), 189 Min: (float64)(0), 190 Max: (float64)(0), 191 Unit: (string)("km/h"), 192 Description: (string)(""), 193 ValueDescriptions: ([]*descriptor.ValueDescription)(nil), 194 ReceiverNodes: []string{ 195 (string)("DRIVER"), 196 (string)("IO"), 197 }, 198 DefaultValue: (int)(0), 199 }, 200 }, 201 SenderNode: (string)("MOTOR"), 202 CycleTime: (time.Duration)(100000000), 203 DelayTime: (time.Duration)(0), 204 } 205 } 206 207 type testMessage struct { 208 frame can.Frame 209 descriptor *descriptor.Message 210 } 211 212 func (m *testMessage) Frame() can.Frame { 213 return m.frame 214 } 215 216 func (m *testMessage) Descriptor() *descriptor.Message { 217 return m.descriptor 218 } 219 220 func (m *testMessage) MarshalFrame() (can.Frame, error) { 221 panic("should not be called") 222 } 223 224 func (testMessage) Reset() { 225 panic("should not be called") 226 } 227 228 func (testMessage) String() string { 229 panic("should not be called") 230 } 231 232 func (testMessage) UnmarshalFrame(can.Frame) error { 233 panic("should not be called") 234 }