package service import ( "testing" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" gomega_format "github.com/onsi/gomega/format" ) func testUtils(t *testing.T) { RegisterFailHandler(Fail) gomega_format.CharactersAroundMismatchToInclude = 50 RunSpecs(t, "Go Translator") } var _ = Describe("Go Translator", func() { Describe("All", func() { It("Function_Declaration", func() { source := `package test func foo() {} func bar() {} ` expected := ` void foo(); void bar(); void foo(){} void bar() {}` Compare(source, expected) }) It("Function_Declaration_With_Args", func() { source := `package test func foo(x int) {} func bar(y int) {} ` expected := ` void foo(int x); void bar(int y); void foo(int x){} void bar(int y) {}` Compare(source, expected) }) It("Const_String_Declaration", func() { source := `package test const foo string = "bar" ` expected := ` const char* foo = "bar"; ` Compare(source, expected) }) It("Const Int Calculated Declaration", func() { source := `package test const foo int = 3 - 1 ` expected := ` const int foo = 3 - 1; ` Compare(source, expected) }) It("Var_String_Declaration", func() { source := `package test var client wifi.Client ` expected := ` WiFiClient client; ` Compare(source, expected) }) It("Function_With_Const_String_Declaration", func() { source := `package test func foo() { const foo string = "bar" } ` expected := ` void foo(); void foo() { const char* foo = "bar"; } ` Compare(source, expected) }) It("Function_With_Var_String_Declaration", func() { source := `package test func foo() { var foo string = "bar" } ` expected := ` void foo(); void foo() { char* foo = "bar"; } ` Compare(source, expected) }) It("Function_With_Function_Call", func() { source := `package test func foo() { bar() } ` expected := ` void foo(); void foo() { bar(); } ` Compare(source, expected) }) It("Function_With_Function_Call_With_Args", func() { source := `package test func foo() { bar(1,2,3) } ` expected := ` void foo(); void foo() { bar(1,2,3); } ` Compare(source, expected) }) It("Function_With_Function_Call_With_String", func() { source := `package test func foo() { bar("foo") } ` expected := ` void foo(); void foo() { bar("foo"); } ` Compare(source, expected) }) It("Function_With_Package_Function_Call", func() { source := `package test func foo() { foo.Bar(1,"2") } ` expected := ` void foo(); void foo() { foo.Bar(1,"2"); } ` Compare(source, expected) }) It("Function_With_Assignments", func() { source := `package test func foo() { x = 1 y = 2 z = x + y } ` expected := ` void foo(); void foo() { x = 1; y = 2; z = x + y; } ` Compare(source, expected) }) It("Function_With_Package_Selector_Assignments", func() { source := `package test func foo() { x = bar() y = pkg.Bar() z = x + y } ` expected := ` void foo(); void foo() { x = bar(); y = pkg.Bar(); z = x + y; } ` Compare(source, expected) }) It("Function_Ident_Mapping", func() { source := `package test func foo() { serial.Begin() } ` expected := ` void foo(); void foo() { Serial.begin(); } ` Compare(source, expected) }) It("Function_With_Ident_Param", func() { source := `package test func foo() { foo.Bar(1,"2",digital.Low) } ` expected := ` void foo(); void foo() { foo.Bar(1,"2",LOW); } ` Compare(source, expected) }) It("Function_With_Function_Param", func() { source := `package test func foo() { serial.Println(wifi.LocalIP()) } ` expected := ` void foo(); void foo() { Serial.println(WiFi.localIP()); } ` Compare(source, expected) }) It("Function_Should_Be_Skipped", func() { source := `package test func foo() (s ShouldBeSkipped) { return; } ` expected := `` Compare(source, expected) }) It("Functions get declarations first", func() { source := `package test func foo() { foo2() } func foo2() int { return 0 } ` expected := ` void foo(); int foo2(); void foo() { foo2(); } int foo2() { return 0; } ` Compare(source, expected) }) It("Package_Import", func() { source := `package test import "github.com/andygeiss/esp32-mqtt/api/controller" import "github.com/andygeiss/esp32-mqtt/api/controller/serial" import "github.com/andygeiss/esp32/api/controller/timer" import wifi "github.com/andygeiss/esp32/api/controller/wifi" ` expected := ` #include ` Compare(source, expected) }) It("Package_Import_But_Ignore_Controller", func() { source := `package test import controller "my/go-controller" import "github.com/andygeiss/esp32-mqtt/api/controller/serial" import "github.com/andygeiss/esp32/api/controller/timer" import wifi "github.com/andygeiss/esp32/api/controller/wifi" ` expected := ` #include ` Compare(source, expected) }) It("IfStmt_With_Condition_BasicLit_And_BasicLit", func() { source := `package test func Setup() {} func Loop() { if 1 == 1 { serial.Println("1") } } ` expected := ` void setup(); void loop(); void setup() {} void loop() { if (1 == 1) { Serial.println("1"); } } ` Compare(source, expected) }) It("IfStmt_With_Condition_Ident_And_BasicLit", func() { source := `package test func Setup() {} func Loop() { if x == 1 { serial.Println("1") } } ` expected := ` void setup(); void loop(); void setup() {} void loop() { if (x == 1) { Serial.println("1"); } } ` Compare(source, expected) }) It("IfStmt_With_Condition_CallExpr_And_BasicLit", func() { source := `package test func Setup() {} func Loop() { if x() == 1 { serial.Println("1") } } ` expected := ` void setup(); void loop(); void setup() {} void loop() { if (x() == 1) { Serial.println("1"); } } ` Compare(source, expected) }) It("IfStmt_With_Condition_Const_And_BasicLit", func() { source := `package test const maxX = 1 func Setup() {} func Loop() { if x == maxX { serial.Println("1") } } ` expected := ` void setup(); void loop(); const maxX = 1; void setup() {} void loop() { if (x == maxX) { Serial.println("1"); } } ` Compare(source, expected) }) It("IfStmt_With_Else", func() { source := `package test const maxX = 1 func Setup() {} func Loop() { if x == maxX { serial.Println("1") } else { serial.Println("2") } } ` expected := ` void setup(); void loop(); const maxX = 1; void setup() {} void loop() { if (x == maxX) { Serial.println("1"); } else { Serial.println("2"); } } ` Compare(source, expected) }) It("SwitchStmt_With_Ident_And_BasicLit", func() { source := `package test func Setup() {} func Loop() { switch x { case 1: serial.Println("1") } } ` expected := ` void setup(); void loop(); void setup() {} void loop() { switch (x) { case 1: Serial.println("1"); } } ` Compare(source, expected) }) It("SwitchStmt_With_Break", func() { source := `package test func Setup() {} func Loop() { switch x { case 1: serial.Println("1") break case 2: serial.Println("1") } } ` expected := ` void setup(); void loop(); void setup() {} void loop() { switch (x) { case 1: Serial.println("1"); break; case 2: Serial.println("1"); } } ` Compare(source, expected) }) It("ForLoop_WithoutInit_And_Post_Transpiles_To_While", func() { source := `package test import wifi "github.com/andygeiss/esp32/api/controller/wifi" func Setup() { serial.Begin(serial.BaudRate115200) wifi.BeginEncrypted("SSID", "PASS") for wifi.Status() != wifi.StatusConnected { serial.Println("Connecting ...") } serial.Println("Connected!") } func Loop() {} ` expected := ` void setup(); void loop(); #include void setup() { Serial.begin(115200); WiFi.begin("SSID","PASS"); while(WiFi.status()!=WL_CONNECTED){ Serial.println("Connecting..."); } Serial.println("Connected!"); } void loop() {} ` Compare(source, expected) }) It("WiFiWebClient", func() { source := `package test import wifi "github.com/andygeiss/esp32/api/controller/wifi" var client wifi.Client func Setup() {} func Loop() { serial.Print("Connecting to ") serial.Println(host) serial.Print(" ...") if (client.Connect(host, 443) == true) { serial.Println(" Connected!") } else { serial.Println(" Failed!") } } ` expected := ` void setup(); void loop(); #include WiFiClient client; voidsetup(){} voidloop(){ Serial.print("Connecting to"); Serial.println(host); Serial.print(" ..."); if(client.connect(host, 443) == true){ Serial.println(" Connected!"); } else { Serial.println(" Failed!"); } }` Compare(source, expected) }) }) Describe("Arduino", func() { It("Maps constants", func() { source := `package test var analogInPin int = arduino.A0 func Setup() {} func Loop() {} ` expected := ` void setup(); void loop(); int analogInPin = A0; void setup() {} void loop() {} ` Compare(source, expected) }) }) Describe("Goroutines", func() { It("Go создаёт поток", func() { source := `package test func main() { go myfunction() } func myfunction() { } ` expected := ` void main(); void myfunction(); void __GoHelper_myfunction(void*) { myfunction(); vTaskDelete(NULL); } void main() { xTaskCreate(__GoHelper_myfunction,"",1024,NULL,1,NULL); } void myfunction() { } ` Compare(source, expected) }) It("Go создаёт поток - два раза", func() { source := `package test func main() { go myfunction() go myfunction2() } func myfunction() { } func myfunction2() { } ` expected := ` void main(); void myfunction(); void myfunction2(); void __GoHelper_myfunction(void*) { myfunction(); vTaskDelete(NULL); } void __GoHelper_myfunction2(void*) { myfunction2(); vTaskDelete(NULL); } void main() { xTaskCreate(__GoHelper_myfunction,"",1024,NULL,1,NULL); xTaskCreate(__GoHelper_myfunction2,"",1024,NULL,1,NULL); } void myfunction() { } void myfunction2() { } ` Compare(source, expected) }) }) Describe("Функции", func() { It("Объявление void функции", func() { source := `package test func Setup() {} func Loop() { } func MyFunction() { } ` expected := ` void setup(); void loop(); void MyFunction(); void setup() {} void loop() { } void MyFunction() { } ` Compare(source, expected) }) It("Объявление void функции с return", func() { source := `package test func Setup() {} func Loop() { } func MyFunction() { return } ` expected := ` void setup(); void loop(); void MyFunction(); void setup() {} void loop() { } void MyFunction() { return; } ` Compare(source, expected) }) It("Объявление int функции", func() { source := `package test func Setup() {} func Loop() { } func MyFunction() int { } ` expected := ` void setup(); void loop(); int MyFunction(); void setup() {} void loop() { } int MyFunction() { } ` Compare(source, expected) }) It("Объявление int функции с return 0", func() { source := `package test func Setup() {} func Loop() { } func MyFunction() int { return 0 } ` expected := ` void setup(); void loop(); int MyFunction(); void setup() {} void loop() { } int MyFunction() { return 0; } ` Compare(source, expected) }) It("Объявление int функции с return -1", func() { source := `package test func Setup() {} func Loop() { } func MyFunction() int { return -1 } ` expected := ` void setup(); void loop(); int MyFunction(); void setup() {} void loop() { } int MyFunction() { return -1; } ` Compare(source, expected) }) It("Объявляет и вызывает функцию", func() { source := `package test func Setup() {} func Loop() { var x int = MyFunction() } func MyFunction() int { return 0 } ` expected := ` void setup(); void loop(); int MyFunction(); void setup() {} void loop() { int x = MyFunction(); } int MyFunction() { return 0; } ` Compare(source, expected) }) It("Объявление bool функции с return false", func() { source := `package test func MyFunction() bool { return false } ` expected := ` bool MyFunction(); bool MyFunction() { return false; } ` Compare(source, expected) }) }) Describe("Циклы", func() { It("for {}", func() { source := `package test func Loop() { var i int for { i = i } } ` expected := ` void loop(); void loop() { int i; while (1) { i = i; } } ` Compare(source, expected) }) It("for i=0; i<10; i+=1", func() { source := `package test func Setup() {} func Loop() { var i int for i=0; i<10; i+=1 { i = i } } ` expected := ` void setup(); void loop(); void setup() {} void loop() { int i; for (i=0; i<10; i+=1) { i = i; } } ` Compare(source, expected) }) It("for i=0; i<10; i++", func() { source := `package test func Setup() {} func Loop() { var i int for i=0; i<10; i++ { i = i } } ` expected := ` void setup(); void loop(); void setup() {} void loop() { int i; for (i=0; i<10; i++) { i = i; } } ` Compare(source, expected) }) It("for { continue }", func() { source := `package test func Setup() {} func Loop() { for { if v == 0 { continue } } } ` expected := ` void setup(); void loop(); void setup() {} void loop() { while (1) { if (v == 0) { continue; } } } ` Compare(source, expected) }) }) Describe("Действия", func() { It("nil -> NULL", func() { source := `package test func Loop() { if p == nil { } } ` expected := ` void loop(); void loop() { if (p == NULL) { } } ` Compare(source, expected) }) It("uint32 -> unsigned long", func() { source := `package test func Loop() { var i uint32 } ` expected := ` void loop(); void loop() { unsigned long i; } ` Compare(source, expected) }) It("uint64 -> unsigned long long", func() { source := `package test func Loop() { var i uint64 } ` expected := ` void loop(); void loop() { unsigned long long i; } ` Compare(source, expected) }) It("i++", func() { source := `package test func Loop() { var i int i++ } ` expected := ` void loop(); void loop() { int i; i++; } ` Compare(source, expected) }) }) }) func NDescribe(s string, i func()) int { return 0 } func NIt(s string, i func()) int { return 0 }