@@ -12,11 +12,92 @@ import (
1212 "errors"
1313 "io"
1414 "os"
15+ "path/filepath"
1516 "reflect"
17+ "strings"
1618 "testing"
1719 "time"
1820)
1921
22+ func Test_MXDeviceContexts (t * testing.T ) {
23+
24+ tests := []struct {
25+ name string
26+ cfgPath string
27+ cfgName string
28+ cfgRefName string
29+ wantErr bool
30+ }{
31+ {"STM32H7_SC" , "STM32H7_SC/STM32CubeMX/device/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
32+ {"STM32H7_DC:CM4" , "STM32H7_DC/STM32CubeMX/STM32H745BGTx/MX_Device/CM4/" , "MX_Device.h" , "MX_Device_ref.h" , false },
33+ {"STM32H7_DC:CM7" , "STM32H7_DC/STM32CubeMX/STM32H745BGTx/MX_Device/CM7/" , "MX_Device.h" , "MX_Device_ref.h" , false },
34+ {"STM32U5_noTZ" , "STM32U5_noTZ/STM32CubeMX/Board/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
35+ {"STM32U5_TZ:NonSecure" , "STM32U5_TZ/STM32CubeMX/Board/MX_Device/NonSecure/" , "MX_Device.h" , "MX_Device_ref.h" , false },
36+ {"STM32U5_TZ:Secure" , "STM32U5_TZ/STM32CubeMX/Board/MX_Device/Secure/" , "MX_Device.h" , "MX_Device_ref.h" , false },
37+ {"STM32WL_DC:CM0PLUS" , "STM32WL_DC/test/STM32CubeMX/STM32WL54CCUx/MX_Device/CM0PLUS/" , "MX_Device.h" , "MX_Device_ref.h" , false },
38+ {"STM32WL_DC:CM4" , "STM32WL_DC/test/STM32CubeMX/STM32WL54CCUx/MX_Device/CM4/" , "MX_Device.h" , "MX_Device_ref.h" , false },
39+ {"STM32H5" , "STM32H5/STM32CubeMX/STM32H573IIKxQ/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
40+ {"STM32H7" , "STM32H7/STM32CubeMX/STM32H743XIHx/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
41+ {"STM32F7" , "STM32F7/STM32CubeMX/STM32F746NGHx/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
42+ {"STM32U5" , "STM32U5/STM32CubeMX/STM32U5G9ZJTxQ/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
43+ {"STM32F2" , "STM32F2/STM32CubeMX/STM32F217IGHx/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
44+ {"STM32F4" , "STM32F4/STM32CubeMX/STM32F469NIHx/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
45+ {"STM32G4" , "STM32G4/STM32CubeMX/STM32G474QETx/MX_Device/" , "MX_Device.h" , "MX_Device_ref.h" , false },
46+ }
47+ for _ , tt := range tests {
48+ t .Run (tt .name , func (t * testing.T ) {
49+
50+ tt .cfgPath = filepath .Join ("../../testdata/testExamples/" , tt .cfgPath )
51+
52+ data , err := os .ReadFile (filepath .Join (tt .cfgPath , tt .cfgName ))
53+ if err != nil {
54+ t .Errorf (" %s; cannot open MX_Device.h file" , tt .name )
55+ return
56+ }
57+
58+ dataRef , errRef := os .ReadFile (filepath .Join (tt .cfgPath , tt .cfgRefName ))
59+ if errRef != nil {
60+ t .Errorf (" %s; cannot open MX_Device_ref.h file" , tt .name )
61+ return
62+ }
63+
64+ // Normalize line endings inline
65+ content := strings .ReplaceAll (string (data ), "\r \n " , "\n " )
66+ contentRef := strings .ReplaceAll (string (dataRef ), "\r \n " , "\n " )
67+
68+ lines := strings .Split (content , "\n " )
69+ linesRef := strings .Split (contentRef , "\n " )
70+
71+ content = strings .Join (lines [3 :], "\n " )
72+ contentRef = strings .Join (linesRef [3 :], "\n " )
73+
74+ if reflect .DeepEqual ((content != contentRef ), ! tt .wantErr ) {
75+ t .Errorf (" %s; MX_Device.h file content mismatch" , tt .name )
76+ }
77+ })
78+ }
79+ }
80+
81+ func Test_ReadContexts (t * testing.T ) {
82+
83+ tests := []struct {
84+ name string
85+ iocFile string
86+ params BridgeParamType
87+ wantErr bool
88+ }{
89+ {"STM32H7_SC" , "../../testdata/testExamples/STM32H7_SC/STM32CubeMX/device/STM32CubeMX/STM32CubeMX.ioc" , BridgeParamType {"device" , "" , "STM32H743AGIx" , "" , "test" , "single-core" , "" , "" , "AC6" , "" , "test" , "" , "" }, false },
90+ }
91+ for _ , tt := range tests {
92+ t .Run (tt .name , func (t * testing.T ) {
93+ argsSlice := []BridgeParamType {tt .params }
94+ if err := ReadContexts (tt .iocFile , argsSlice ); (err != nil ) != tt .wantErr {
95+ t .Errorf ("ReadContexts() %s error = %v, wantErr %v" , tt .name , err , tt .wantErr )
96+ }
97+ })
98+ }
99+ }
100+
20101func Test_createContextMap (t * testing.T ) {
21102 t .Parallel ()
22103
0 commit comments