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

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

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

@ -9,89 +9,61 @@ import (
. "github.com/onsi/gomega"
)
// Trim removes all the whitespaces and returns a new string.
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 TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Go Translator")
}
// Validate the content of a given source with an expected outcome by using a string compare.
// The defaultService will be started and used to transform the source into an Arduino sketch format.
func Validate(source, expected string, t *testing.T) {
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) {
var _ = Describe("Go Translator", func() {
Describe("All", func() {
It("Empty_Package", func() {
source := `package test`
expected := `void loop(){}
void setup() {} `
Validate(source, expected, t)
}
void setup(){}`
Compare(source, expected)
})
func Test_Function_Declaration(t *testing.T) {
It("Function_Declaration", func() {
source := `package test
func foo() {}
func bar() {}
`
expected := `void foo(){}
void bar() {} `
Validate(source, expected, t)
}
void bar() {}`
Compare(source, expected)
})
func Test_Function_Declaration_With_Args(t *testing.T) {
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) {} `
Validate(source, expected, t)
}
void bar(int y) {}`
Compare(source, expected)
})
func Test_Const_String_Declaration(t *testing.T) {
It("Const_String_Declaration", func() {
source := `package test
const foo string = "bar"
`
expected := `
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
var client wifi.Client
`
expected := `
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
func foo() {
const foo string = "bar"
@ -102,9 +74,10 @@ func Test_Function_With_Const_String_Declaration(t *testing.T) {
const char* foo = "bar";
}
`
Validate(source, expected, t)
}
func Test_Function_With_Var_String_Declaration(t *testing.T) {
Compare(source, expected)
})
It("Function_With_Var_String_Declaration", func() {
source := `package test
func foo() {
var foo string = "bar"
@ -115,9 +88,10 @@ func Test_Function_With_Var_String_Declaration(t *testing.T) {
char* foo = "bar";
}
`
Validate(source, expected, t)
}
func Test_Function_With_Function_Call(t *testing.T) {
Compare(source, expected)
})
It("Function_With_Function_Call", func() {
source := `package test
func foo() {
bar()
@ -128,9 +102,10 @@ func Test_Function_With_Function_Call(t *testing.T) {
bar();
}
`
Validate(source, expected, t)
}
func Test_Function_With_Function_Call_With_Args(t *testing.T) {
Compare(source, expected)
})
It("Function_With_Function_Call_With_Args", func() {
source := `package test
func foo() {
bar(1,2,3)
@ -141,9 +116,10 @@ func Test_Function_With_Function_Call_With_Args(t *testing.T) {
bar(1,2,3);
}
`
Validate(source, expected, t)
}
func Test_Function_With_Function_Call_With_String(t *testing.T) {
Compare(source, expected)
})
It("Function_With_Function_Call_With_String", func() {
source := `package test
func foo() {
bar("foo")
@ -154,10 +130,10 @@ func Test_Function_With_Function_Call_With_String(t *testing.T) {
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
func foo() {
foo.Bar(1,"2")
@ -168,9 +144,10 @@ func Test_Function_With_Package_Function_Call(t *testing.T) {
foo.Bar(1,"2");
}
`
Validate(source, expected, t)
}
func Test_Function_With_Assignments(t *testing.T) {
Compare(source, expected)
})
It("Function_With_Assignments", func() {
source := `package test
func foo() {
x = 1
@ -185,10 +162,10 @@ func Test_Function_With_Assignments(t *testing.T) {
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
func foo() {
x = bar()
@ -203,10 +180,10 @@ func Test_Function_With_Package_Selector_Assignments(t *testing.T) {
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
func foo() {
serial.Begin()
@ -217,9 +194,10 @@ func Test_Function_Ident_Mapping(t *testing.T) {
Serial.begin();
}
`
Validate(source, expected, t)
}
func Test_Function_With_Ident_Param(t *testing.T) {
Compare(source, expected)
})
It("Function_With_Ident_Param", func() {
source := `package test
func foo() {
foo.Bar(1,"2",digital.Low)
@ -230,10 +208,10 @@ func Test_Function_With_Ident_Param(t *testing.T) {
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
func foo() {
serial.Println(wifi.LocalIP())
@ -244,10 +222,10 @@ func Test_Function_With_Function_Param(t *testing.T) {
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
import "github.com/andygeiss/esp32-mqtt/api/controller"
import "github.com/andygeiss/esp32-mqtt/api/controller/serial"
@ -257,10 +235,10 @@ func Test_Package_Import(t *testing.T) {
expected := `
#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
import controller "github.com/andygeiss/esp32-controller"
import "github.com/andygeiss/esp32-mqtt/api/controller/serial"
@ -270,10 +248,10 @@ func Test_Package_Import_But_Ignore_Controller(t *testing.T) {
expected := `
#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
func Setup() {}
func Loop() {
@ -281,7 +259,7 @@ func Test_IfStmt_With_Condition_BasicLit_And_BasicLit(t *testing.T) {
serial.Println("1")
}
}
`
`
expected := `
void setup() {}
void loop() {
@ -289,11 +267,11 @@ func Test_IfStmt_With_Condition_BasicLit_And_BasicLit(t *testing.T) {
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
func Setup() {}
func Loop() {
@ -301,7 +279,7 @@ func Test_IfStmt_With_Condition_Ident_And_BasicLit(t *testing.T) {
serial.Println("1")
}
}
`
`
expected := `
void setup() {}
void loop() {
@ -309,11 +287,11 @@ func Test_IfStmt_With_Condition_Ident_And_BasicLit(t *testing.T) {
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
func Setup() {}
func Loop() {
@ -321,7 +299,7 @@ func Test_IfStmt_With_Condition_CallExpr_And_BasicLit(t *testing.T) {
serial.Println("1")
}
}
`
`
expected := `
void setup() {}
void loop() {
@ -329,11 +307,11 @@ func Test_IfStmt_With_Condition_CallExpr_And_BasicLit(t *testing.T) {
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
const maxX = 1
func Setup() {}
@ -342,7 +320,7 @@ func Test_IfStmt_With_Condition_Const_And_BasicLit(t *testing.T) {
serial.Println("1")
}
}
`
`
expected := `
const maxX = 1;
void setup() {}
@ -351,11 +329,11 @@ func Test_IfStmt_With_Condition_Const_And_BasicLit(t *testing.T) {
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
const maxX = 1
func Setup() {}
@ -366,7 +344,7 @@ func Test_IfStmt_With_Else(t *testing.T) {
serial.Println("2")
}
}
`
`
expected := `
const maxX = 1;
void setup() {}
@ -377,11 +355,11 @@ func Test_IfStmt_With_Else(t *testing.T) {
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
func Setup() {}
func Loop() {
@ -390,7 +368,7 @@ func Test_SwitchStmt_With_Ident_And_BasicLit(t *testing.T) {
serial.Println("1")
}
}
`
`
expected := `
void setup() {}
void loop() {
@ -399,11 +377,11 @@ func Test_SwitchStmt_With_Ident_And_BasicLit(t *testing.T) {
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
func Setup() {}
func Loop() {
@ -415,7 +393,7 @@ func Test_SwitchStmt_With_Break(t *testing.T) {
serial.Println("1")
}
}
`
`
expected := `
void setup() {}
void loop() {
@ -427,11 +405,11 @@ func Test_SwitchStmt_With_Break(t *testing.T) {
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
import wifi "github.com/andygeiss/esp32/api/controller/wifi"
func Setup() {
@ -444,7 +422,7 @@ func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) {
return nil
}
func Loop() {}
`
`
expected := `
#include <WiFi.h>
void setup() {
@ -456,11 +434,11 @@ func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) {
Serial.println("Connected!");
}
void loop() {}
`
Validate(source, expected, t)
}
`
Compare(source, expected)
})
func Test_WiFiWebClient(t *testing.T) {
It("WiFiWebClient", func() {
source := `package test
import wifi "github.com/andygeiss/esp32/api/controller/wifi"
var client wifi.Client
@ -475,7 +453,7 @@ func Test_WiFiWebClient(t *testing.T) {
serial.Println(" Failed!")
}
}
`
`
expected := `#include <WiFi.h>
WiFiClient client;
voidsetup(){}
@ -489,43 +467,10 @@ func Test_WiFiWebClient(t *testing.T) {
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() {
It("Maps constants", func() {
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