-
Notifications
You must be signed in to change notification settings - Fork 7
/
checkassignstmt_gen_test.go
112 lines (101 loc) · 2.9 KB
/
checkassignstmt_gen_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
package eval
import (
"testing"
"reflect"
)
// Test TooMany
func TestCheckAssignStmtTooMany(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `_, _ = 1, 2, 3`, env,
`assignment count mismatch: 2 = 3`,
)
}
// Test TooFew
func TestCheckAssignStmtTooFew(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `_, _ = 1`, env,
`assignment count mismatch: 2 = 1`,
)
}
// Test NoNewIdents1
func TestCheckAssignStmtNoNewIdents1(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `f := nil`, env,
`no new variables on left side of :=`,
)
}
// Test NoNewIdents2
func TestCheckAssignStmtNoNewIdents2(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `f, _ := nil, 1`, env,
`no new variables on left side of :=`,
)
}
// Test UnderscoreNil
func TestCheckAssignStmtUnderscoreNil(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `_ = nil`, env,
`use of untyped nil`,
)
}
// Test Unaddressable
func TestCheckAssignStmtUnaddressable(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `1 = 1`, env,
`cannot assign to 1`,
`cannot use 1 (type int) as type untyped number in assignment`,
)
}
// Test Unaddressable2
func TestCheckAssignStmtUnaddressable2(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `1, 2 = 1, 2`, env,
`cannot assign to 1`,
`cannot assign to 2`,
`cannot use 1 (type int) as type untyped number in assignment`,
`cannot use 2 (type int) as type untyped number in assignment`,
)
}
// Test ToNil
func TestCheckAssignStmtToNil(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `nil = 1`, env,
`cannot assign to nil`,
`cannot use 1 (type int) as type nil in assignment`,
)
}
// Test Mistyped1
func TestCheckAssignStmtMistyped1(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `f = true`, env,
`cannot use true (type bool) as type func() (int, int) in assignment`,
)
}
// Test Mistyped2
func TestCheckAssignStmtMistyped2(t *testing.T) {
env := MakeSimpleEnv()
f := func() (int, int) { return 1, 1 }
env.Vars["f"] = reflect.ValueOf(&f)
expectCheckError(t, `f, f = true, false`, env,
`cannot use true (type bool) as type func() (int, int) in assignment`,
`cannot use false (type bool) as type func() (int, int) in assignment`,
)
}