Сконвертировано в тесты Ginkgo
Этот коммит содержится в:
родитель
27bc01bb63
коммит
54fb10eb6a
1 изменённых файлов: 486 добавлений и 511 удалений
|
@ -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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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() {
|
||||
|
@ -290,10 +268,10 @@ func Test_IfStmt_With_Condition_BasicLit_And_BasicLit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
`
|
||||
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() {
|
||||
|
@ -310,10 +288,10 @@ func Test_IfStmt_With_Condition_Ident_And_BasicLit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
`
|
||||
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() {
|
||||
|
@ -330,10 +308,10 @@ func Test_IfStmt_With_Condition_CallExpr_And_BasicLit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
`
|
||||
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() {}
|
||||
|
@ -352,10 +330,10 @@ func Test_IfStmt_With_Condition_Const_And_BasicLit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
`
|
||||
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() {}
|
||||
|
@ -378,10 +356,10 @@ func Test_IfStmt_With_Else(t *testing.T) {
|
|||
}
|
||||
}
|
||||
`
|
||||
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() {
|
||||
|
@ -400,10 +378,10 @@ func Test_SwitchStmt_With_Ident_And_BasicLit(t *testing.T) {
|
|||
}
|
||||
}
|
||||
`
|
||||
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() {
|
||||
|
@ -428,10 +406,10 @@ func Test_SwitchStmt_With_Break(t *testing.T) {
|
|||
}
|
||||
}
|
||||
`
|
||||
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() {
|
||||
|
@ -457,10 +435,10 @@ func Test_ForLoop_WithoutInit_And_Post_Transpiles_To_While(t *testing.T) {
|
|||
}
|
||||
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
|
||||
|
@ -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
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче