Skip to content

Commit 52c5fa8

Browse files
Implement tests of the coderunner.CompileCode() function
1 parent a445d9f commit 52c5fa8

1 file changed

Lines changed: 112 additions & 1 deletion

File tree

compile_code_test.go

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,118 @@ func TestCompileCode(test *testing.T) {
2626
wantPreparedCode string
2727
wantOutput string
2828
}{
29-
// TODO: Add test cases.
29+
{
30+
name: "success",
31+
args: args{
32+
ctx: context.Background(),
33+
code: `
34+
package main
35+
36+
func main() {
37+
var x, y int
38+
if _, err := fmt.Scan(&x, &y); err != nil {
39+
log.Fatal(err)
40+
}
41+
42+
fmt.Println(x + y)
43+
}
44+
`,
45+
allowedImports: nil,
46+
input: "5 12",
47+
},
48+
wantPreparedCode: "package main\n" +
49+
"\n" +
50+
"import (\n" +
51+
"\t\"fmt\"\n" +
52+
"\t\"log\"\n" +
53+
")\n" +
54+
"\n" +
55+
"func main() {\n" +
56+
"\tvar x, y int\n" +
57+
"\tif _, err := fmt.Scan(&x, &y); err != nil {\n" +
58+
"\t\tlog.Fatal(err)\n" +
59+
"\t}\n" +
60+
"\n" +
61+
"\tfmt.Println(x + y)\n" +
62+
"}\n",
63+
wantOutput: "17\n",
64+
wantedErr: assert.NoError,
65+
},
66+
{
67+
name: "error with code preparing",
68+
args: args{
69+
ctx: context.Background(),
70+
code: `
71+
package main
72+
73+
func main() {
74+
`,
75+
allowedImports: nil,
76+
input: "5 12",
77+
},
78+
wantPreparedCode: "",
79+
wantOutput: "",
80+
wantedErr: assert.Error,
81+
},
82+
{
83+
name: "error with import checking",
84+
args: args{
85+
ctx: context.Background(),
86+
code: `
87+
package main
88+
89+
func main() {
90+
var x, y int
91+
if _, err := fmt.Scan(&x, &y); err != nil {
92+
log.Fatal(err)
93+
}
94+
95+
fmt.Println(x + y)
96+
}
97+
`,
98+
allowedImports: mapset.NewSet("log"),
99+
input: "5 12",
100+
},
101+
wantPreparedCode: "package main\n" +
102+
"\n" +
103+
"import (\n" +
104+
"\t\"fmt\"\n" +
105+
"\t\"log\"\n" +
106+
")\n" +
107+
"\n" +
108+
"func main() {\n" +
109+
"\tvar x, y int\n" +
110+
"\tif _, err := fmt.Scan(&x, &y); err != nil {\n" +
111+
"\t\tlog.Fatal(err)\n" +
112+
"\t}\n" +
113+
"\n" +
114+
"\tfmt.Println(x + y)\n" +
115+
"}\n",
116+
wantOutput: "",
117+
wantedErr: assert.Error,
118+
},
119+
{
120+
name: "error with code compiling",
121+
args: args{
122+
ctx: context.Background(),
123+
code: `
124+
package main
125+
126+
func main() {
127+
var x, y int
128+
}
129+
`,
130+
allowedImports: nil,
131+
input: "5 12",
132+
},
133+
wantPreparedCode: "package main\n" +
134+
"\n" +
135+
"func main() {\n" +
136+
"\tvar x, y int\n" +
137+
"}\n",
138+
wantOutput: "",
139+
wantedErr: assert.Error,
140+
},
30141
} {
31142
test.Run(data.name, func(test *testing.T) {
32143
pathToCode, err := systemutils.SaveTemporaryText(data.args.code, ".go")

0 commit comments

Comments
 (0)