github.com/jshiv/can-go@v0.2.1-0.20210224011015-069e90e90bdf/internal/generate/compile_test.go (about)

     1  package generate
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  	"time"
     7  
     8  	"go.einride.tech/can/pkg/descriptor"
     9  	examplecan "go.einride.tech/can/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  						},
    68  					},
    69  				},
    70  			},
    71  
    72  			{
    73  				ID:         101,
    74  				Name:       "MotorCommand",
    75  				Length:     1,
    76  				SenderNode: "DRIVER",
    77  				SendType:   descriptor.SendTypeCyclic,
    78  				CycleTime:  100 * time.Millisecond,
    79  				Signals: []*descriptor.Signal{
    80  					{
    81  						Name:          "Steer",
    82  						Start:         0,
    83  						Length:        4,
    84  						IsSigned:      true,
    85  						Scale:         1,
    86  						Offset:        -5,
    87  						Min:           -5,
    88  						Max:           5,
    89  						ReceiverNodes: []string{"MOTOR"},
    90  					},
    91  					{
    92  						Name:          "Drive",
    93  						Start:         4,
    94  						Length:        4,
    95  						Scale:         1,
    96  						Max:           9,
    97  						ReceiverNodes: []string{"MOTOR"},
    98  					},
    99  				},
   100  			},
   101  
   102  			{
   103  				ID:         200,
   104  				Name:       "SensorSonars",
   105  				Length:     8,
   106  				SenderNode: "SENSOR",
   107  				SendType:   descriptor.SendTypeCyclic,
   108  				CycleTime:  100 * time.Millisecond,
   109  				Signals: []*descriptor.Signal{
   110  					{
   111  						Name:          "Mux",
   112  						IsMultiplexer: true,
   113  						Start:         0,
   114  						Length:        4,
   115  						Scale:         1,
   116  						ReceiverNodes: []string{"DRIVER", "IO"},
   117  					},
   118  					{
   119  						Name:          "ErrCount",
   120  						Start:         4,
   121  						Length:        12,
   122  						Scale:         1,
   123  						ReceiverNodes: []string{"DRIVER", "IO"},
   124  					},
   125  					{
   126  						Name:             "Left",
   127  						IsMultiplexed:    true,
   128  						MultiplexerValue: 0,
   129  						Start:            16,
   130  						Length:           12,
   131  						Scale:            0.1,
   132  						ReceiverNodes:    []string{"DRIVER", "IO"},
   133  					},
   134  					{
   135  						Name:             "NoFiltLeft",
   136  						IsMultiplexed:    true,
   137  						MultiplexerValue: 1,
   138  						Start:            16,
   139  						Length:           12,
   140  						Scale:            0.1,
   141  						ReceiverNodes:    []string{"DBG"},
   142  					},
   143  					{
   144  						Name:             "Middle",
   145  						IsMultiplexed:    true,
   146  						MultiplexerValue: 0,
   147  						Start:            28,
   148  						Length:           12,
   149  						Scale:            0.1,
   150  						ReceiverNodes:    []string{"DRIVER", "IO"},
   151  					},
   152  					{
   153  						Name:             "NoFiltMiddle",
   154  						IsMultiplexed:    true,
   155  						MultiplexerValue: 1,
   156  						Start:            28,
   157  						Length:           12,
   158  						Scale:            0.1,
   159  						ReceiverNodes:    []string{"DBG"},
   160  					},
   161  					{
   162  						Name:             "Right",
   163  						IsMultiplexed:    true,
   164  						MultiplexerValue: 0,
   165  						Start:            40,
   166  						Length:           12,
   167  						Scale:            0.1,
   168  						ReceiverNodes:    []string{"DRIVER", "IO"},
   169  					},
   170  					{
   171  						Name:             "NoFiltRight",
   172  						IsMultiplexed:    true,
   173  						MultiplexerValue: 1,
   174  						Start:            40,
   175  						Length:           12,
   176  						Scale:            0.1,
   177  						ReceiverNodes:    []string{"DBG"},
   178  					},
   179  					{
   180  						Name:             "Rear",
   181  						IsMultiplexed:    true,
   182  						MultiplexerValue: 0,
   183  						Start:            52,
   184  						Length:           12,
   185  						Scale:            0.1,
   186  						ReceiverNodes:    []string{"DRIVER", "IO"},
   187  					},
   188  					{
   189  						Name:             "NoFiltRear",
   190  						IsMultiplexed:    true,
   191  						MultiplexerValue: 1,
   192  						Start:            52,
   193  						Length:           12,
   194  						Scale:            0.1,
   195  						ReceiverNodes:    []string{"DBG"},
   196  					},
   197  				},
   198  			},
   199  
   200  			{
   201  				ID:         400,
   202  				Name:       "MotorStatus",
   203  				Length:     3,
   204  				SenderNode: "MOTOR",
   205  				SendType:   descriptor.SendTypeCyclic,
   206  				CycleTime:  100 * time.Millisecond,
   207  				Signals: []*descriptor.Signal{
   208  					{
   209  						Name:          "WheelError",
   210  						Start:         0,
   211  						Length:        1,
   212  						Scale:         1,
   213  						ReceiverNodes: []string{"DRIVER", "IO"},
   214  					},
   215  					{
   216  						Name:          "SpeedKph",
   217  						Start:         8,
   218  						Length:        16,
   219  						Scale:         0.001,
   220  						Unit:          "km/h",
   221  						ReceiverNodes: []string{"DRIVER", "IO"},
   222  					},
   223  				},
   224  			},
   225  
   226  			{
   227  				ID:         500,
   228  				Name:       "IODebug",
   229  				Length:     6,
   230  				SenderNode: "IO",
   231  				SendType:   descriptor.SendTypeEvent,
   232  				Signals: []*descriptor.Signal{
   233  					{
   234  						Name:          "TestUnsigned",
   235  						Start:         0,
   236  						Length:        8,
   237  						Scale:         1,
   238  						ReceiverNodes: []string{"DBG"},
   239  					},
   240  					{
   241  						Name:          "TestEnum",
   242  						Start:         8,
   243  						Length:        6,
   244  						Scale:         1,
   245  						ReceiverNodes: []string{"DBG"},
   246  						DefaultValue:  int(examplecan.IODebug_TestEnum_Two),
   247  						ValueDescriptions: []*descriptor.ValueDescription{
   248  							{Value: 1, Description: "One"},
   249  							{Value: 2, Description: "Two"},
   250  						},
   251  					},
   252  					{
   253  						Name:          "TestSigned",
   254  						Start:         16,
   255  						Length:        8,
   256  						IsSigned:      true,
   257  						Scale:         1,
   258  						ReceiverNodes: []string{"DBG"},
   259  					},
   260  					{
   261  						Name:          "TestFloat",
   262  						Start:         24,
   263  						Length:        8,
   264  						Scale:         0.5,
   265  						ReceiverNodes: []string{"DBG"},
   266  					},
   267  					{
   268  						Name:          "TestBoolEnum",
   269  						Start:         32,
   270  						Length:        1,
   271  						Scale:         1,
   272  						ReceiverNodes: []string{"DBG"},
   273  						ValueDescriptions: []*descriptor.ValueDescription{
   274  							{Value: 0, Description: "Zero"},
   275  							{Value: 1, Description: "One"},
   276  						},
   277  					},
   278  					{
   279  						Name:          "TestScaledEnum",
   280  						Start:         40,
   281  						Length:        2,
   282  						Scale:         2,
   283  						Min:           0,
   284  						Max:           6,
   285  						ReceiverNodes: []string{"DBG"},
   286  						ValueDescriptions: []*descriptor.ValueDescription{
   287  							{Value: 0, Description: "Zero"},
   288  							{Value: 1, Description: "Two"},
   289  							{Value: 2, Description: "Four"},
   290  							{Value: 3, Description: "Six"},
   291  						},
   292  					},
   293  				},
   294  			},
   295  		},
   296  	}
   297  	input, err := ioutil.ReadFile(exampleDBCFile)
   298  	assert.NilError(t, err)
   299  	result, err := Compile(exampleDBCFile, input)
   300  	if err != nil {
   301  		t.Fatal(err)
   302  	}
   303  	if len(result.Warnings) > 0 {
   304  		t.Fatal(result.Warnings)
   305  	}
   306  	assert.DeepEqual(t, exampleDatabase, result.Database)
   307  }