-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscoping_test.go
115 lines (105 loc) · 2.14 KB
/
scoping_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package testing
import "testing"
func Test_Scoping_ChangingVarInIfBlockIsRemembered(t *testing.T) {
rsl := `
a = 1
if true:
a = 2
print(a)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "2\n")
assertNoErrors(t)
}
func Test_Scoping_DefiningVarInIfBlockIsRemembered(t *testing.T) {
rsl := `
if true:
a = 1
print(a)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "1\n")
assertNoErrors(t)
}
func Test_Scoping_ChangingVarInForBlockIsRemembered(t *testing.T) {
rsl := `
a = 1
for i in range(3):
a = i
print(a)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "2\n")
assertNoErrors(t)
}
func Test_Scoping_DefiningVarInForBlockIsRemembered(t *testing.T) {
rsl := `
for i in range(3):
// do nothing
print(i)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "2\n")
assertNoErrors(t)
}
func Test_Scoping_DefiningItemVarInForBlockIsRemembered(t *testing.T) {
rsl := `
for i, item in ["a", "b", "c"]:
// do nothing
print("i", i)
print("item", item)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "i 2\nitem c\n")
assertNoErrors(t)
}
func Test_Scoping_DefiningKeyValueVarsInForBlockIsRemembered(t *testing.T) {
rsl := `
for k, v in {"a": 1, "b": 2, "c": 3}:
// do nothing
print("k", k)
print("v", v)
`
setupAndRunCode(t, rsl, "--color=never")
assertOnlyOutput(t, stdOutBuffer, "k c\nv 3\n")
assertNoErrors(t)
}
func Test_Scoping_LastValueOfRadLambdaIsNotRemembered(t *testing.T) {
rsl := `
nums = [10]
i = 0
display:
fields nums
nums:
map i -> i * 2
print("i", i)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `nums
20
i 0
`
assertOnlyOutput(t, stdOutBuffer, expected)
assertNoErrors(t)
}
func Test_Scoping_LastValueOfRadLambdaIsNotDefined(t *testing.T) {
rsl := `
nums = [10]
display:
fields nums
nums:
map i -> i * 2
print("i", i)
`
expected := `nums
20
`
setupAndRunCode(t, rsl, "--color=never")
assertOutput(t, stdOutBuffer, expected)
expected = `Error at L7:12
print("i", i)
^ Undefined variable: i
`
assertOutput(t, stdErrBuffer, expected)
assertExitCode(t, 1)
}