github.com/xiaoxu5271/can-go@v1.0.1/internal/generate/compile_test.go (about) 1 package generate 2 3 import ( 4 "os" 5 "testing" 6 "time" 7 8 "github.com/xiaoxu5271/can-go/pkg/descriptor" 9 examplecan "github.com/xiaoxu5271/can-go/testdata/gen/go/example" 10 "gotest.tools/v3/assert" 11 ) 12 13 func TestCompile_ExampleDBC(t *testing.T) { 14 finish := runTestInDir(t, "../..") 15 defer finish() 16 const exampleDBCFile = "testdata/dbc/example/example.dbc" 17 exampleDatabase := &descriptor.Database{ 18 SourceFile: exampleDBCFile, 19 Version: "", 20 Nodes: []*descriptor.Node{ 21 { 22 Name: "DBG", 23 }, 24 { 25 Name: "DRIVER", 26 Description: "The driver controller driving the car", 27 }, 28 { 29 Name: "IO", 30 }, 31 { 32 Name: "MOTOR", 33 Description: "The motor controller of the car", 34 }, 35 { 36 Name: "SENSOR", 37 Description: "The sensor controller of the car", 38 }, 39 }, 40 41 Messages: []*descriptor.Message{ 42 { 43 ID: 1, 44 Name: "EmptyMessage", 45 SenderNode: "DBG", 46 }, 47 48 { 49 ID: 100, 50 Name: "DriverHeartbeat", 51 Length: 1, 52 SenderNode: "DRIVER", 53 Description: "Sync message used to synchronize the controllers", 54 SendType: descriptor.SendTypeCyclic, 55 CycleTime: time.Second, 56 Signals: []*descriptor.Signal{ 57 { 58 Name: "Command", 59 Start: 0, 60 Length: 8, 61 Scale: 1, 62 ReceiverNodes: []string{"SENSOR", "MOTOR"}, 63 ValueDescriptions: []*descriptor.ValueDescription{ 64 {Value: 0, Description: "None"}, 65 {Value: 1, Description: "Sync"}, 66 {Value: 2, Description: "Reboot"}, 67 {Value: 3, Description: "Headlights On"}, 68 }, 69 }, 70 }, 71 }, 72 73 { 74 ID: 101, 75 Name: "MotorCommand", 76 Length: 1, 77 SenderNode: "DRIVER", 78 SendType: descriptor.SendTypeCyclic, 79 CycleTime: 100 * time.Millisecond, 80 Signals: []*descriptor.Signal{ 81 { 82 Name: "Steer", 83 Start: 0, 84 Length: 4, 85 IsSigned: true, 86 Scale: 1, 87 Offset: -5, 88 Min: -5, 89 Max: 5, 90 ReceiverNodes: []string{"MOTOR"}, 91 }, 92 { 93 Name: "Drive", 94 Start: 4, 95 Length: 4, 96 Scale: 1, 97 Max: 9, 98 ReceiverNodes: []string{"MOTOR"}, 99 }, 100 }, 101 }, 102 103 { 104 ID: 200, 105 Name: "SensorSonars", 106 Length: 8, 107 SenderNode: "SENSOR", 108 SendType: descriptor.SendTypeCyclic, 109 CycleTime: 100 * time.Millisecond, 110 Signals: []*descriptor.Signal{ 111 { 112 Name: "Mux", 113 IsMultiplexer: true, 114 Start: 0, 115 Length: 4, 116 Scale: 1, 117 ReceiverNodes: []string{"DRIVER", "IO"}, 118 }, 119 { 120 Name: "ErrCount", 121 Start: 4, 122 Length: 12, 123 Scale: 1, 124 ReceiverNodes: []string{"DRIVER", "IO"}, 125 }, 126 { 127 Name: "Left", 128 IsMultiplexed: true, 129 MultiplexerValue: 0, 130 Start: 16, 131 Length: 12, 132 Scale: 0.1, 133 ReceiverNodes: []string{"DRIVER", "IO"}, 134 }, 135 { 136 Name: "NoFiltLeft", 137 IsMultiplexed: true, 138 MultiplexerValue: 1, 139 Start: 16, 140 Length: 12, 141 Scale: 0.1, 142 ReceiverNodes: []string{"DBG"}, 143 }, 144 { 145 Name: "Middle", 146 IsMultiplexed: true, 147 MultiplexerValue: 0, 148 Start: 28, 149 Length: 12, 150 Scale: 0.1, 151 ReceiverNodes: []string{"DRIVER", "IO"}, 152 }, 153 { 154 Name: "NoFiltMiddle", 155 IsMultiplexed: true, 156 MultiplexerValue: 1, 157 Start: 28, 158 Length: 12, 159 Scale: 0.1, 160 ReceiverNodes: []string{"DBG"}, 161 }, 162 { 163 Name: "Right", 164 IsMultiplexed: true, 165 MultiplexerValue: 0, 166 Start: 40, 167 Length: 12, 168 Scale: 0.1, 169 ReceiverNodes: []string{"DRIVER", "IO"}, 170 }, 171 { 172 Name: "NoFiltRight", 173 IsMultiplexed: true, 174 MultiplexerValue: 1, 175 Start: 40, 176 Length: 12, 177 Scale: 0.1, 178 ReceiverNodes: []string{"DBG"}, 179 }, 180 { 181 Name: "Rear", 182 IsMultiplexed: true, 183 MultiplexerValue: 0, 184 Start: 52, 185 Length: 12, 186 Scale: 0.1, 187 ReceiverNodes: []string{"DRIVER", "IO"}, 188 }, 189 { 190 Name: "NoFiltRear", 191 IsMultiplexed: true, 192 MultiplexerValue: 1, 193 Start: 52, 194 Length: 12, 195 Scale: 0.1, 196 ReceiverNodes: []string{"DBG"}, 197 }, 198 }, 199 }, 200 201 { 202 ID: 400, 203 Name: "MotorStatus", 204 Length: 3, 205 SenderNode: "MOTOR", 206 SendType: descriptor.SendTypeCyclic, 207 CycleTime: 100 * time.Millisecond, 208 Signals: []*descriptor.Signal{ 209 { 210 Name: "WheelError", 211 Start: 0, 212 Length: 1, 213 Scale: 1, 214 ReceiverNodes: []string{"DRIVER", "IO"}, 215 }, 216 { 217 Name: "SpeedKph", 218 Start: 8, 219 Length: 16, 220 Scale: 0.001, 221 Unit: "km/h", 222 ReceiverNodes: []string{"DRIVER", "IO"}, 223 }, 224 }, 225 }, 226 227 { 228 ID: 500, 229 Name: "IODebug", 230 Length: 6, 231 SenderNode: "IO", 232 SendType: descriptor.SendTypeEvent, 233 Signals: []*descriptor.Signal{ 234 { 235 Name: "TestUnsigned", 236 Start: 0, 237 Length: 8, 238 Scale: 1, 239 ReceiverNodes: []string{"DBG"}, 240 }, 241 { 242 Name: "TestEnum", 243 Start: 8, 244 Length: 6, 245 Scale: 1, 246 ReceiverNodes: []string{"DBG"}, 247 DefaultValue: int(examplecan.IODebug_TestEnum_Two), 248 ValueDescriptions: []*descriptor.ValueDescription{ 249 {Value: 1, Description: "One"}, 250 {Value: 2, Description: "Two"}, 251 }, 252 }, 253 { 254 Name: "TestSigned", 255 Start: 16, 256 Length: 8, 257 IsSigned: true, 258 Scale: 1, 259 ReceiverNodes: []string{"DBG"}, 260 }, 261 { 262 Name: "TestFloat", 263 Start: 24, 264 Length: 8, 265 Scale: 0.5, 266 ReceiverNodes: []string{"DBG"}, 267 }, 268 { 269 Name: "TestBoolEnum", 270 Start: 32, 271 Length: 1, 272 Scale: 1, 273 ReceiverNodes: []string{"DBG"}, 274 ValueDescriptions: []*descriptor.ValueDescription{ 275 {Value: 0, Description: "Zero"}, 276 {Value: 1, Description: "One"}, 277 }, 278 }, 279 { 280 Name: "TestScaledEnum", 281 Start: 40, 282 Length: 2, 283 Scale: 2, 284 Min: 0, 285 Max: 6, 286 ReceiverNodes: []string{"DBG"}, 287 ValueDescriptions: []*descriptor.ValueDescription{ 288 {Value: 0, Description: "Zero"}, 289 {Value: 1, Description: "Two"}, 290 {Value: 2, Description: "Four"}, 291 {Value: 3, Description: "Six"}, 292 }, 293 }, 294 }, 295 }, 296 }, 297 } 298 input, err := os.ReadFile(exampleDBCFile) 299 assert.NilError(t, err) 300 result, err := Compile(exampleDBCFile, input) 301 if err != nil { 302 t.Fatal(err) 303 } 304 if len(result.Warnings) > 0 { 305 t.Fatal(result.Warnings) 306 } 307 assert.DeepEqual(t, exampleDatabase, result.Database) 308 }