package transpile import ( "bytes" "strings" "testing" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) func TestUtils(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "Go Translator") } var _ = Describe("Go Translator", func() { Describe("All", func() { It("Empty_Package", func() { source := `package test` expected := `void loop(){} void setup(){}` Compare(source, expected) }) It("Function_Declaration", func() { source := `package test func foo() {} func bar() {} ` expected := `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) {}` Compare(source, expected) }) It("Const_String_Declaration", func() { source := `package test const foo string = "bar" ` expected := ` const char* foo = "bar"; ` 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() { 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() { char* foo = "bar"; } ` Compare(source, expected) }) It("Function_With_Function_Call", func() { source := `package test func foo() { bar() } ` expected := ` 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() { 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() { bar("foo"); } ` Compare(source, expected) }) It("Function_With_Package_Function_Call", func() { source := `package test func foo() { foo.Bar(1,"2") } ` expected := ` 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() { 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() { 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() { 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() { 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() { Serial.println(WiFi.localIP()); } ` 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() { 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() { 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() { 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 := ` 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 := ` 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() { 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() { 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 := ` #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 := `#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 := ` int analogInPin = A0; void setup() {} void loop() {} ` Compare(source, expected) }) }) Describe("Функции", func() { It("Объявление void функции", func() { source := `package test func Setup() {} func Loop() { } func MyFunction() { } ` expected := ` 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() { return; } ` Compare(source, expected) }) It("Объявление int функции", func() { source := `package test func Setup() {} func Loop() { } func MyFunction() int { } ` expected := ` void setup() {} void loop() { } 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() { } MyFunction() { return 0; } ` 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 x = MyFunction(); } MyFunction() { return 0; } ` Compare(source, expected) }) }) }) func Compare(source, expected string) { var in, out bytes.Buffer in.WriteString(source) service := NewService(&in, &out) err := service.Start() got := out.String() tgot, texpected := Trim(got), Trim(expected) ExpectWithOffset(1, err).NotTo(HaveOccurred()) ExpectWithOffset(1, tgot).To(Be(texpected)) } func Trim(s string) string { s = strings.Replace(s, " ", "", -1) s = strings.Replace(s, "\n", "", -1) s = strings.Replace(s, "\r", "", -1) s = strings.Replace(s, "\t", "", -1) return s } func NDescribe(s string, i func()) int { return 0 } func NIt(s string, i func()) int { return 0 } var Be = Equal func NoErr(err error) { ExpectWithOffset(1, err).NotTo(HaveOccurred()) } var Ok = NoErr