Сконвертировано в тесты Ginkgo

Этот коммит содержится в:
Softonik 2021-10-04 02:05:48 +03:00 коммит произвёл Nobody
родитель 27bc01bb63
коммит 54fb10eb6a

Просмотреть файл

@ -9,89 +9,61 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
// Trim removes all the whitespaces and returns a new string. func TestUtils(t *testing.T) {
func Trim(s string) string { RegisterFailHandler(Fail)
s = strings.Replace(s, " ", "", -1) RunSpecs(t, "Go Translator")
s = strings.Replace(s, "\n", "", -1)
s = strings.Replace(s, "\r", "", -1)
s = strings.Replace(s, "\t", "", -1)
return s
} }
// Validate the content of a given source with an expected outcome by using a string compare. var _ = Describe("Go Translator", func() {
// The defaultService will be started and used to transform the source into an Arduino sketch format. Describe("All", func() {
func Validate(source, expected string, t *testing.T) { It("Empty_Package", func() {
RegisterTestingT(t)
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 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 Test_Empty_Package(t *testing.T) {
source := `package test` source := `package test`
expected := `void loop(){} expected := `void loop(){}
void setup() {} ` void setup(){}`
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_Declaration(t *testing.T) { It("Function_Declaration", func() {
source := `package test source := `package test
func foo() {} func foo() {}
func bar() {} func bar() {}
` `
expected := `void foo(){} expected := `void foo(){}
void bar() {} ` void bar() {}`
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_Declaration_With_Args(t *testing.T) { It("Function_Declaration_With_Args", func() {
source := `package test source := `package test
func foo(x int) {} func foo(x int) {}
func bar(y int) {} func bar(y int) {}
` `
expected := `void foo(int x){} expected := `void foo(int x){}
void bar(int y) {} ` void bar(int y) {}`
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Const_String_Declaration(t *testing.T) { It("Const_String_Declaration", func() {
source := `package test source := `package test
const foo string = "bar" const foo string = "bar"
` `
expected := ` expected := `
const char* foo = "bar"; const char* foo = "bar";
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Var_String_Declaration(t *testing.T) { It("Var_String_Declaration", func() {
source := `package test source := `package test
var client wifi.Client var client wifi.Client
` `
expected := ` expected := `
WiFiClient client; WiFiClient client;
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Const_String_Declaration(t *testing.T) { It("Function_With_Const_String_Declaration", func() {
source := `package test source := `package test
func foo() { func foo() {
const foo string = "bar" const foo string = "bar"
@ -102,9 +74,10 @@ func Test_Function_With_Const_String_Declaration(t *testing.T) {
const char* foo = "bar"; const char* foo = "bar";
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Var_String_Declaration(t *testing.T) {
It("Function_With_Var_String_Declaration", func() {
source := `package test source := `package test
func foo() { func foo() {
var foo string = "bar" var foo string = "bar"
@ -115,9 +88,10 @@ func Test_Function_With_Var_String_Declaration(t *testing.T) {
char* foo = "bar"; char* foo = "bar";
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Function_Call(t *testing.T) {
It("Function_With_Function_Call", func() {
source := `package test source := `package test
func foo() { func foo() {
bar() bar()
@ -128,9 +102,10 @@ func Test_Function_With_Function_Call(t *testing.T) {
bar(); bar();
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Function_Call_With_Args(t *testing.T) {
It("Function_With_Function_Call_With_Args", func() {
source := `package test source := `package test
func foo() { func foo() {
bar(1,2,3) bar(1,2,3)
@ -141,9 +116,10 @@ func Test_Function_With_Function_Call_With_Args(t *testing.T) {
bar(1,2,3); bar(1,2,3);
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Function_Call_With_String(t *testing.T) {
It("Function_With_Function_Call_With_String", func() {
source := `package test source := `package test
func foo() { func foo() {
bar("foo") bar("foo")
@ -154,10 +130,10 @@ func Test_Function_With_Function_Call_With_String(t *testing.T) {
bar("foo"); bar("foo");
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Package_Function_Call(t *testing.T) { It("Function_With_Package_Function_Call", func() {
source := `package test source := `package test
func foo() { func foo() {
foo.Bar(1,"2") foo.Bar(1,"2")
@ -168,9 +144,10 @@ func Test_Function_With_Package_Function_Call(t *testing.T) {
foo.Bar(1,"2"); foo.Bar(1,"2");
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Assignments(t *testing.T) {
It("Function_With_Assignments", func() {
source := `package test source := `package test
func foo() { func foo() {
x = 1 x = 1
@ -185,10 +162,10 @@ func Test_Function_With_Assignments(t *testing.T) {
z = x + y; z = x + y;
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Package_Selector_Assignments(t *testing.T) { It("Function_With_Package_Selector_Assignments", func() {
source := `package test source := `package test
func foo() { func foo() {
x = bar() x = bar()
@ -203,10 +180,10 @@ func Test_Function_With_Package_Selector_Assignments(t *testing.T) {
z = x + y; z = x + y;
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_Ident_Mapping(t *testing.T) { It("Function_Ident_Mapping", func() {
source := `package test source := `package test
func foo() { func foo() {
serial.Begin() serial.Begin()
@ -217,9 +194,10 @@ func Test_Function_Ident_Mapping(t *testing.T) {
Serial.begin(); Serial.begin();
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Ident_Param(t *testing.T) {
It("Function_With_Ident_Param", func() {
source := `package test source := `package test
func foo() { func foo() {
foo.Bar(1,"2",digital.Low) foo.Bar(1,"2",digital.Low)
@ -230,10 +208,10 @@ func Test_Function_With_Ident_Param(t *testing.T) {
foo.Bar(1,"2",LOW); foo.Bar(1,"2",LOW);
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Function_With_Function_Param(t *testing.T) { It("Function_With_Function_Param", func() {
source := `package test source := `package test
func foo() { func foo() {
serial.Println(wifi.LocalIP()) serial.Println(wifi.LocalIP())
@ -244,10 +222,10 @@ func Test_Function_With_Function_Param(t *testing.T) {
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Package_Import(t *testing.T) { It("Package_Import", func() {
source := `package test source := `package test
import "github.com/andygeiss/esp32-mqtt/api/controller" import "github.com/andygeiss/esp32-mqtt/api/controller"
import "github.com/andygeiss/esp32-mqtt/api/controller/serial" import "github.com/andygeiss/esp32-mqtt/api/controller/serial"
@ -257,10 +235,10 @@ func Test_Package_Import(t *testing.T) {
expected := ` expected := `
#include <WiFi.h> #include <WiFi.h>
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_Package_Import_But_Ignore_Controller(t *testing.T) { It("Package_Import_But_Ignore_Controller", func() {
source := `package test source := `package test
import controller "github.com/andygeiss/esp32-controller" import controller "github.com/andygeiss/esp32-controller"
import "github.com/andygeiss/esp32-mqtt/api/controller/serial" import "github.com/andygeiss/esp32-mqtt/api/controller/serial"
@ -270,10 +248,10 @@ func Test_Package_Import_But_Ignore_Controller(t *testing.T) {
expected := ` expected := `
#include <WiFi.h> #include <WiFi.h>
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_IfStmt_With_Condition_BasicLit_And_BasicLit(t *testing.T) { It("IfStmt_With_Condition_BasicLit_And_BasicLit", func() {
source := `package test source := `package test
func Setup() {} func Setup() {}
func Loop() { func Loop() {
@ -281,7 +259,7 @@ func Test_IfStmt_With_Condition_BasicLit_And_BasicLit(t *testing.T) {
serial.Println("1") serial.Println("1")
} }
} }
` `
expected := ` expected := `
void setup() {} void setup() {}
void loop() { void loop() {
@ -289,11 +267,11 @@ func Test_IfStmt_With_Condition_BasicLit_And_BasicLit(t *testing.T) {
Serial.println("1"); Serial.println("1");
} }
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_IfStmt_With_Condition_Ident_And_BasicLit(t *testing.T) { It("IfStmt_With_Condition_Ident_And_BasicLit", func() {
source := `package test source := `package test
func Setup() {} func Setup() {}
func Loop() { func Loop() {
@ -301,7 +279,7 @@ func Test_IfStmt_With_Condition_Ident_And_BasicLit(t *testing.T) {
serial.Println("1") serial.Println("1")
} }
} }
` `
expected := ` expected := `
void setup() {} void setup() {}
void loop() { void loop() {
@ -309,11 +287,11 @@ func Test_IfStmt_With_Condition_Ident_And_BasicLit(t *testing.T) {
Serial.println("1"); Serial.println("1");
} }
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_IfStmt_With_Condition_CallExpr_And_BasicLit(t *testing.T) { It("IfStmt_With_Condition_CallExpr_And_BasicLit", func() {
source := `package test source := `package test
func Setup() {} func Setup() {}
func Loop() { func Loop() {
@ -321,7 +299,7 @@ func Test_IfStmt_With_Condition_CallExpr_And_BasicLit(t *testing.T) {
serial.Println("1") serial.Println("1")
} }
} }
` `
expected := ` expected := `
void setup() {} void setup() {}
void loop() { void loop() {
@ -329,11 +307,11 @@ func Test_IfStmt_With_Condition_CallExpr_And_BasicLit(t *testing.T) {
Serial.println("1"); Serial.println("1");
} }
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_IfStmt_With_Condition_Const_And_BasicLit(t *testing.T) { It("IfStmt_With_Condition_Const_And_BasicLit", func() {
source := `package test source := `package test
const maxX = 1 const maxX = 1
func Setup() {} func Setup() {}
@ -342,7 +320,7 @@ func Test_IfStmt_With_Condition_Const_And_BasicLit(t *testing.T) {
serial.Println("1") serial.Println("1")
} }
} }
` `
expected := ` expected := `
const maxX = 1; const maxX = 1;
void setup() {} void setup() {}
@ -351,11 +329,11 @@ func Test_IfStmt_With_Condition_Const_And_BasicLit(t *testing.T) {
Serial.println("1"); Serial.println("1");
} }
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_IfStmt_With_Else(t *testing.T) { It("IfStmt_With_Else", func() {
source := `package test source := `package test
const maxX = 1 const maxX = 1
func Setup() {} func Setup() {}
@ -366,7 +344,7 @@ func Test_IfStmt_With_Else(t *testing.T) {
serial.Println("2") serial.Println("2")
} }
} }
` `
expected := ` expected := `
const maxX = 1; const maxX = 1;
void setup() {} void setup() {}
@ -377,11 +355,11 @@ func Test_IfStmt_With_Else(t *testing.T) {
Serial.println("2"); Serial.println("2");
} }
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_SwitchStmt_With_Ident_And_BasicLit(t *testing.T) { It("SwitchStmt_With_Ident_And_BasicLit", func() {
source := `package test source := `package test
func Setup() {} func Setup() {}
func Loop() { func Loop() {
@ -390,7 +368,7 @@ func Test_SwitchStmt_With_Ident_And_BasicLit(t *testing.T) {
serial.Println("1") serial.Println("1")
} }
} }
` `
expected := ` expected := `
void setup() {} void setup() {}
void loop() { void loop() {
@ -399,11 +377,11 @@ func Test_SwitchStmt_With_Ident_And_BasicLit(t *testing.T) {
Serial.println("1"); Serial.println("1");
} }
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_SwitchStmt_With_Break(t *testing.T) { It("SwitchStmt_With_Break", func() {
source := `package test source := `package test
func Setup() {} func Setup() {}
func Loop() { func Loop() {
@ -415,7 +393,7 @@ func Test_SwitchStmt_With_Break(t *testing.T) {
serial.Println("1") serial.Println("1")
} }
} }
` `
expected := ` expected := `
void setup() {} void setup() {}
void loop() { void loop() {
@ -427,11 +405,11 @@ func Test_SwitchStmt_With_Break(t *testing.T) {
Serial.println("1"); Serial.println("1");
} }
} }
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) { It("ForLoop_WithoutInit_And_Post_Transpiles_To_While", func() {
source := `package test source := `package test
import wifi "github.com/andygeiss/esp32/api/controller/wifi" import wifi "github.com/andygeiss/esp32/api/controller/wifi"
func Setup() { func Setup() {
@ -444,7 +422,7 @@ func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) {
return nil return nil
} }
func Loop() {} func Loop() {}
` `
expected := ` expected := `
#include <WiFi.h> #include <WiFi.h>
void setup() { void setup() {
@ -456,11 +434,11 @@ func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) {
Serial.println("Connected!"); Serial.println("Connected!");
} }
void loop() {} void loop() {}
` `
Validate(source, expected, t) Compare(source, expected)
} })
func Test_WiFiWebClient(t *testing.T) { It("WiFiWebClient", func() {
source := `package test source := `package test
import wifi "github.com/andygeiss/esp32/api/controller/wifi" import wifi "github.com/andygeiss/esp32/api/controller/wifi"
var client wifi.Client var client wifi.Client
@ -475,7 +453,7 @@ func Test_WiFiWebClient(t *testing.T) {
serial.Println(" Failed!") serial.Println(" Failed!")
} }
} }
` `
expected := `#include <WiFi.h> expected := `#include <WiFi.h>
WiFiClient client; WiFiClient client;
voidsetup(){} voidsetup(){}
@ -489,43 +467,10 @@ func Test_WiFiWebClient(t *testing.T) {
Serial.println(" Failed!"); Serial.println(" Failed!");
} }
}` }`
Validate(source, expected, t) Compare(source, expected)
} })
})
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Go Translator")
}
func NDescribe(s string, i func()) int { return 0 }
func NIt(s string, i func()) int { return 0 }
func NIt_async(s string, i func(done Done), timeout float64) int { return 0 }
var Be = Equal
func BeCalled(called bool) {
ExpectWithOffset(1, called).To(BeTrue())
}
func NoErr(err error) {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
var Ok = NoErr
func ErrBe(err error, must error) {
if must != nil {
Ω(err).To(Be(must))
} else {
Ω(err).To(BeNil())
}
}
func ToBeTrue(arg bool) {
Ω(arg).To(BeTrue())
}
var _ = Describe("Go Translator", func() {
Describe("Arduino", func() { Describe("Arduino", func() {
It("Maps constants", func() { It("Maps constants", func() {
source := `package test source := `package test
@ -542,3 +487,33 @@ var _ = Describe("Go Translator", func() {
}) })
}) })
}) })
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