Skip to content

Commit 32aa7e7

Browse files
committed
Add builtin tests
1 parent da13470 commit 32aa7e7

6 files changed

Lines changed: 297 additions & 39 deletions

File tree

tests/builtin.go

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func testLenSliceSuccess(t *testing.T, transpilerFunc transpilerFunc) {
12+
transpilerFunc(t, `
13+
a := []int{1, 2, 3}
14+
15+
print(len(a))
16+
`, func(output string, err error) {
17+
require.Nil(t, err)
18+
require.Equal(t, "3", output)
19+
})
20+
}
21+
22+
func testLenStringSuccess(t *testing.T, transpilerFunc transpilerFunc) {
23+
transpilerFunc(t, `
24+
print(len("abc"))
25+
`, func(output string, err error) {
26+
require.Nil(t, err)
27+
require.Equal(t, "3", output)
28+
})
29+
}
30+
31+
func testCopySuccess(t *testing.T, transpilerFunc transpilerFunc) {
32+
transpilerFunc(t, `
33+
s1 := []int{}
34+
s2 := []int{1, 2, 3}
35+
36+
a := copy(s1, s2)
37+
38+
print(a)
39+
40+
for i := 0; i < len(s1); i++ {
41+
print(s1[i])
42+
}
43+
`, func(output string, err error) {
44+
require.Nil(t, err)
45+
require.Equal(t, "3\n1\n2\n3", output)
46+
})
47+
}
48+
49+
func testReadSuccess(t *testing.T, transpilerFunc transpilerFunc) {
50+
file := "read-test.txt"
51+
content := "Hello World"
52+
os.WriteFile(file, []byte(content), 0x777)
53+
defer os.Remove(file)
54+
55+
transpilerFunc(t, `
56+
`+fmt.Sprintf(`a := read("%s")`, file)+`
57+
print(a)
58+
`, func(output string, err error) {
59+
require.Nil(t, err)
60+
require.Equal(t, content, output)
61+
})
62+
}
63+
64+
func testWriteSuccess(t *testing.T, transpilerFunc transpilerFunc) {
65+
file := "read-test.txt"
66+
content := "Hello Moon"
67+
defer os.Remove(file)
68+
69+
transpilerFunc(t, `
70+
`+fmt.Sprintf(`write("%s", "%s")`, file, content)+`
71+
`+fmt.Sprintf(`a := read("%s")`, file)+`
72+
print(a)
73+
`, func(output string, err error) {
74+
require.Nil(t, err)
75+
require.Equal(t, content, output)
76+
})
77+
}
78+
79+
func testPanicSuccess(t *testing.T, transpilerFunc transpilerFunc) {
80+
transpilerFunc(t, `
81+
var a = 1
82+
83+
if a == 1 {
84+
panic("panic")
85+
}
86+
print("got through")
87+
`, func(output string, err error) {
88+
require.Error(t, err)
89+
require.Equal(t, "panic: panic", output)
90+
})
91+
}
92+
93+
func testLenSliceInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
94+
transpilerFunc(t, `
95+
func test() {
96+
a := []int{1, 2, 3}
97+
98+
print(len(a))
99+
}
100+
test()
101+
`, func(output string, err error) {
102+
require.Nil(t, err)
103+
require.Equal(t, "3", output)
104+
})
105+
}
106+
107+
func testLenStringInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
108+
transpilerFunc(t, `
109+
func test() {
110+
print(len("abc"))
111+
}
112+
test()
113+
`, func(output string, err error) {
114+
require.Nil(t, err)
115+
require.Equal(t, "3", output)
116+
})
117+
}
118+
119+
func testCopyInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
120+
transpilerFunc(t, `
121+
func test() {
122+
s1 := []int{}
123+
s2 := []int{1, 2, 3}
124+
125+
a := copy(s1, s2)
126+
127+
print(a)
128+
129+
for i := 0; i < len(s1); i++ {
130+
print(s1[i])
131+
}
132+
}
133+
test()
134+
`, func(output string, err error) {
135+
require.Nil(t, err)
136+
require.Equal(t, "3\n1\n2\n3", output)
137+
})
138+
}
139+
140+
func testReadInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
141+
file := "read-test.txt"
142+
content := "Hello World"
143+
os.WriteFile(file, []byte(content), 0x777)
144+
defer os.Remove(file)
145+
146+
transpilerFunc(t, `
147+
func test() {
148+
`+fmt.Sprintf(`a := read("%s")`, file)+`
149+
print(a)
150+
}
151+
test()
152+
`, func(output string, err error) {
153+
require.Nil(t, err)
154+
require.Equal(t, content, output)
155+
})
156+
}
157+
158+
func testWriteInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
159+
file := "read-test.txt"
160+
content := "Hello Moon"
161+
defer os.Remove(file)
162+
163+
transpilerFunc(t, `
164+
func test() {
165+
`+fmt.Sprintf(`write("%s", "%s")`, file, content)+`
166+
`+fmt.Sprintf(`a := read("%s")`, file)+`
167+
print(a)
168+
}
169+
test()
170+
`, func(output string, err error) {
171+
require.Nil(t, err)
172+
require.Equal(t, content, output)
173+
})
174+
}
175+
176+
func testPanicInFunctionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
177+
transpilerFunc(t, `
178+
func test() {
179+
var a = 1
180+
181+
if a == 1 {
182+
panic("panic")
183+
}
184+
print("got through")
185+
}
186+
test()
187+
`, func(output string, err error) {
188+
require.Error(t, err)
189+
require.Equal(t, "panic: panic", output)
190+
})
191+
}

tests/builtin_linux_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLenSliceSuccess(t *testing.T) {
8+
testLenSliceSuccess(t, transpileBash)
9+
}
10+
11+
func TestLenStringSuccess(t *testing.T) {
12+
testLenStringSuccess(t, transpileBash)
13+
}
14+
15+
func TestCopySuccess(t *testing.T) {
16+
testCopySuccess(t, transpileBash)
17+
}
18+
19+
func TestReadSuccess(t *testing.T) {
20+
testReadSuccess(t, transpileBash)
21+
}
22+
23+
func TestWriteSuccess(t *testing.T) {
24+
testWriteSuccess(t, transpileBash)
25+
}
26+
27+
func TestPanicSuccess(t *testing.T) {
28+
testPanicSuccess(t, transpileBash)
29+
}
30+
31+
func TestLenSliceInFunctionSuccess(t *testing.T) {
32+
testLenSliceInFunctionSuccess(t, transpileBash)
33+
}
34+
35+
func TestLenStringInFunctionSuccess(t *testing.T) {
36+
testLenStringInFunctionSuccess(t, transpileBash)
37+
}
38+
39+
func TestCopyInFunctionSuccess(t *testing.T) {
40+
testCopyInFunctionSuccess(t, transpileBash)
41+
}
42+
43+
func TestReadInFunctionSuccess(t *testing.T) {
44+
testReadInFunctionSuccess(t, transpileBash)
45+
}
46+
47+
func TestWriteInFunctionSuccess(t *testing.T) {
48+
testWriteInFunctionSuccess(t, transpileBash)
49+
}
50+
51+
func TestPanicInFunctionSuccess(t *testing.T) {
52+
testPanicInFunctionSuccess(t, transpileBash)
53+
}

tests/builtin_windows_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLenSliceSuccess(t *testing.T) {
8+
testLenSliceSuccess(t, transpileBatch)
9+
}
10+
11+
func TestLenStringSuccess(t *testing.T) {
12+
testLenStringSuccess(t, transpileBatch)
13+
}
14+
15+
func TestCopySuccess(t *testing.T) {
16+
testCopySuccess(t, transpileBatch)
17+
}
18+
19+
func TestReadSuccess(t *testing.T) {
20+
testReadSuccess(t, transpileBatch)
21+
}
22+
23+
func TestWriteSuccess(t *testing.T) {
24+
testWriteSuccess(t, transpileBatch)
25+
}
26+
27+
func TestPanicSuccess(t *testing.T) {
28+
testPanicSuccess(t, transpileBatch)
29+
}
30+
31+
func TestLenSliceInFunctionSuccess(t *testing.T) {
32+
testLenSliceInFunctionSuccess(t, transpileBatch)
33+
}
34+
35+
func TestLenStringInFunctionSuccess(t *testing.T) {
36+
testLenStringInFunctionSuccess(t, transpileBatch)
37+
}
38+
39+
func TestCopyInFunctionSuccess(t *testing.T) {
40+
testCopyInFunctionSuccess(t, transpileBatch)
41+
}
42+
43+
func TestReadInFunctionSuccess(t *testing.T) {
44+
testReadInFunctionSuccess(t, transpileBatch)
45+
}
46+
47+
func TestWriteInFunctionSuccess(t *testing.T) {
48+
testWriteInFunctionSuccess(t, transpileBatch)
49+
}
50+
51+
func TestPanicInFunctionSuccess(t *testing.T) {
52+
testPanicInFunctionSuccess(t, transpileBatch)
53+
}

tests/panic.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/panic_linux_test.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/panic_windows_test.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)