Skip to content

Commit 2fe4046

Browse files
committed
Add tests (#45)
1 parent d6a9918 commit 2fe4046

3 files changed

Lines changed: 299 additions & 0 deletions

File tree

tests/type_linux_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestTypeDeclarationAndDefinitionSuccess(t *testing.T) {
8+
testTypeDeclarationAndDefinitionSuccess(t, transpileBash)
9+
}
10+
11+
func TestTypeDeclarationAndAssignmentFail(t *testing.T) {
12+
testTypeDeclarationAndAssignmentFail(t, transpileBash)
13+
}
14+
15+
func TestTypeAliasAndAssignmentSuccess(t *testing.T) {
16+
testTypeAliasAndAssignmentSuccess(t, transpileBash)
17+
}
18+
19+
func TestTypeDeclarationAndDefinitionInFunctionSuccess(t *testing.T) {
20+
testTypeDeclarationAndDefinitionInFunctionSuccess(t, transpileBash)
21+
}
22+
23+
func TestTypeDeclarationAndAssignmentInFunctionFail(t *testing.T) {
24+
testTypeDeclarationAndAssignmentInFunctionFail(t, transpileBash)
25+
}
26+
27+
func TestTypeAliasAndAssignmentInFunctionSuccess(t *testing.T) {
28+
testTypeAliasAndAssignmentInFunctionSuccess(t, transpileBash)
29+
}
30+
31+
func TestTypeDeclaredInFunctionUsedOutsideFail(t *testing.T) {
32+
testTypeDeclaredInFunctionUsedOutsideFail(t, transpileBash)
33+
}
34+
35+
func TestTypeDeclaredInIfUsedOutsideFail(t *testing.T) {
36+
testTypeDeclaredInIfUsedOutsideFail(t, transpileBash)
37+
}
38+
39+
func TestDeclareTypeTwiceFail(t *testing.T) {
40+
testDeclareTypeTwiceFail(t, transpileBash)
41+
}
42+
43+
func TestPassDeclaredTypeToFunctionSuccess(t *testing.T) {
44+
testPassDeclaredTypeToFunctionSuccess(t, transpileBash)
45+
}
46+
47+
func TestPassBaseTypeToFunctionFail(t *testing.T) {
48+
testPassBaseTypeToFunctionFail(t, transpileBash)
49+
}
50+
51+
func TestPassValueWithSameBaseTypeToFunctionSuccess(t *testing.T) {
52+
testPassValueWithSameBaseTypeToFunctionSuccess(t, transpileBash)
53+
}
54+
55+
func TtestAssignDifferentDefinedTypeFail(t *testing.T) {
56+
testAssignDifferentDefinedTypeFail(t, transpileBash)
57+
}

tests/type_test.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func testTypeDeclarationAndDefinitionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
10+
transpilerFunc(t, `
11+
type testType int
12+
13+
var a testType
14+
a = testType(1)
15+
16+
print(a)
17+
`, func(output string, err error) {
18+
require.Nil(t, err)
19+
require.Equal(t, "1", output)
20+
})
21+
}
22+
23+
func testTypeDeclarationAndAssignmentFail(t *testing.T, transpilerFunc transpilerFunc) {
24+
transpilerFunc(t, `
25+
type testType int
26+
27+
var a testType
28+
a = 1
29+
30+
print(a)
31+
`, func(output string, err error) {
32+
require.EqualError(t, shortenError(err), "expected testType but got int")
33+
})
34+
}
35+
36+
func testTypeAliasAndAssignmentSuccess(t *testing.T, transpilerFunc transpilerFunc) {
37+
transpilerFunc(t, `
38+
type testType = int
39+
40+
var a testType
41+
a = 1
42+
43+
print(a)
44+
`, func(output string, err error) {
45+
require.Nil(t, err)
46+
require.Equal(t, "1", output)
47+
})
48+
}
49+
50+
func testTypeDeclarationAndDefinitionInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
51+
transpilerFunc(t, `
52+
func main() {
53+
type testType int
54+
55+
var a testType
56+
a = testType(1)
57+
58+
print(a)
59+
}
60+
main()
61+
`, func(output string, err error) {
62+
require.Nil(t, err)
63+
require.Equal(t, "1", output)
64+
})
65+
}
66+
67+
func testTypeDeclarationAndAssignmentInFunctionFail(t *testing.T, transpilerFunc transpilerFunc) {
68+
transpilerFunc(t, `
69+
func main() {
70+
type testType int
71+
72+
var a testType
73+
a = 1
74+
75+
print(a)
76+
}
77+
main()
78+
`, func(output string, err error) {
79+
require.EqualError(t, shortenError(err), "expected testType but got int")
80+
})
81+
}
82+
83+
func testTypeAliasAndAssignmentInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
84+
transpilerFunc(t, `
85+
func main() {
86+
type testType = int
87+
88+
var a testType
89+
a = 1
90+
91+
print(a)
92+
}
93+
main()
94+
`, func(output string, err error) {
95+
require.Nil(t, err)
96+
require.Equal(t, "1", output)
97+
})
98+
}
99+
100+
func testTypeDeclaredInFunctionUsedOutsideFail(t *testing.T, transpilerFunc transpilerFunc) {
101+
transpilerFunc(t, `
102+
func main() {
103+
type testType int
104+
}
105+
var a testType
106+
`, func(output string, err error) {
107+
require.EqualError(t, shortenError(err), "expected valid data type")
108+
})
109+
}
110+
111+
func testTypeDeclaredInIfUsedOutsideFail(t *testing.T, transpilerFunc transpilerFunc) {
112+
transpilerFunc(t, `
113+
if true {
114+
type testType int
115+
}
116+
var a testType
117+
`, func(output string, err error) {
118+
require.EqualError(t, shortenError(err), "expected valid data type")
119+
})
120+
}
121+
122+
func testDeclareTypeTwiceFail(t *testing.T, transpilerFunc transpilerFunc) {
123+
transpilerFunc(t, `
124+
type myType int
125+
type myType string
126+
`, func(output string, err error) {
127+
require.EqualError(t, shortenError(err), "myType has already been defined")
128+
})
129+
}
130+
131+
func testPassDeclaredTypeToFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
132+
transpilerFunc(t, `
133+
type myType string
134+
135+
func test(param myType) {
136+
print(param)
137+
}
138+
test(myType("test"))
139+
`, func(output string, err error) {
140+
require.Nil(t, err)
141+
require.Equal(t, "test", output)
142+
})
143+
}
144+
145+
func testPassBaseTypeToFunctionFail(t *testing.T, transpilerFunc transpilerFunc) {
146+
transpilerFunc(t, `
147+
type myType string
148+
149+
func test(param myType) {
150+
print(param)
151+
}
152+
test("test")
153+
`, func(output string, err error) {
154+
require.EqualError(t, shortenError(err), "expected parameter of type myType (param) but got string")
155+
})
156+
}
157+
158+
func testPassValueWithSameBaseTypeToFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
159+
transpilerFunc(t, `
160+
type myType1 = string
161+
type myType2 = string
162+
type myType3 = myType2
163+
type myType4 = myType3
164+
165+
func test(param myType2) {
166+
print(param)
167+
}
168+
test(myType4("test"))
169+
`, func(output string, err error) {
170+
require.Nil(t, err)
171+
require.Equal(t, "test", output)
172+
})
173+
}
174+
175+
func testAssignDifferentDefinedTypeFail(t *testing.T, transpilerFunc transpilerFunc) {
176+
transpilerFunc(t, `
177+
type myType1 string
178+
type myType2 string
179+
180+
var a myType1
181+
a = myType2("test")
182+
`, func(output string, err error) {
183+
require.EqualError(t, shortenError(err), "expected myType1 but got myType2")
184+
})
185+
}

tests/type_windows_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestTypeDeclarationAndDefinitionSuccess(t *testing.T) {
8+
testTypeDeclarationAndDefinitionSuccess(t, transpileBatch)
9+
}
10+
11+
func TestTypeDeclarationAndAssignmentFail(t *testing.T) {
12+
testTypeDeclarationAndAssignmentFail(t, transpileBatch)
13+
}
14+
15+
func TestTypeAliasAndAssignmentSuccess(t *testing.T) {
16+
testTypeAliasAndAssignmentSuccess(t, transpileBatch)
17+
}
18+
19+
func TestTypeDeclarationAndDefinitionInFunctionSuccess(t *testing.T) {
20+
testTypeDeclarationAndDefinitionInFunctionSuccess(t, transpileBatch)
21+
}
22+
23+
func TestTypeDeclarationAndAssignmentInFunctionFail(t *testing.T) {
24+
testTypeDeclarationAndAssignmentInFunctionFail(t, transpileBatch)
25+
}
26+
27+
func TestTypeAliasAndAssignmentInFunctionSuccess(t *testing.T) {
28+
testTypeAliasAndAssignmentInFunctionSuccess(t, transpileBatch)
29+
}
30+
31+
func TestTypeDeclaredInFunctionUsedOutsideFail(t *testing.T) {
32+
testTypeDeclaredInFunctionUsedOutsideFail(t, transpileBatch)
33+
}
34+
35+
func TestTypeDeclaredInIfUsedOutsideFail(t *testing.T) {
36+
testTypeDeclaredInIfUsedOutsideFail(t, transpileBatch)
37+
}
38+
39+
func TestDeclareTypeTwiceFail(t *testing.T) {
40+
testDeclareTypeTwiceFail(t, transpileBatch)
41+
}
42+
43+
func TestPassDeclaredTypeToFunctionSuccess(t *testing.T) {
44+
testPassDeclaredTypeToFunctionSuccess(t, transpileBatch)
45+
}
46+
47+
func TestPassBaseTypeToFunctionFail(t *testing.T) {
48+
testPassBaseTypeToFunctionFail(t, transpileBatch)
49+
}
50+
51+
func TestPassValueWithSameBaseTypeToFunctionSuccess(t *testing.T) {
52+
testPassValueWithSameBaseTypeToFunctionSuccess(t, transpileBatch)
53+
}
54+
55+
func TtestAssignDifferentDefinedTypeFail(t *testing.T) {
56+
testAssignDifferentDefinedTypeFail(t, transpileBatch)
57+
}

0 commit comments

Comments
 (0)