-
Notifications
You must be signed in to change notification settings - Fork 552
/
Copy patherror_msg_test.go
1119 lines (1026 loc) · 23.6 KB
/
error_msg_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cl_test
import (
"fmt"
"path/filepath"
"runtime"
"testing"
"github.com/goplus/gop/cl/cltest"
)
func codeErrorTest(t *testing.T, msg, src string) {
cltest.ErrorEx(t, "main", "bar.gop", msg, src)
}
func codeErrorTestEx(t *testing.T, pkgname, filename, msg, src string) {
cltest.ErrorEx(t, pkgname, filename, msg, src)
}
func codeErrorTestAst(t *testing.T, pkgname, filename, msg, src string) {
cltest.ErrorAst(t, pkgname, filename, msg, src)
}
func TestErrTplLit(t *testing.T) {
codeErrorTest(t, `bar.gop:1:18: not enough arguments to return
have ()
want (interface{})`, "tpl`a = INT => { return }`")
}
func TestErrSendStmt(t *testing.T) {
codeErrorTest(t, `bar.gop:3:8: can't send multiple values to a channel`, `
var a chan int
a <- 1, 2
`)
}
func TestErrVargCommand(t *testing.T) {
codeErrorTest(t, `bar.gop:5:1: not enough arguments in call to Ls
have ()
want (int)`, `
func Ls(int) {
}
ls
`)
codeErrorTest(t, `bar.gop:8:1: not enough arguments in call to f.Ls
have ()
want (int)`, `
type foo int
func (f foo) Ls(int) {
}
var f foo
f.ls
`)
}
func TestErrUnsafe(t *testing.T) {
codeErrorTest(t, `bar.gop:2:9: undefined: Sizeof`, `
println Sizeof(0)
`)
}
func TestErrLambdaExpr(t *testing.T) {
codeErrorTest(t,
"bar.gop:7:6: too few arguments in lambda expression\n\thave ()\n\twant (int, int)", `
func foo(func(int, int)) {
}
func main() {
foo(=> {})
}
`)
codeErrorTest(t,
"bar.gop:7:6: too many arguments in lambda expression\n\thave (x, y, z)\n\twant (int, int)", `
func foo(func(int, int)) {
}
func main() {
foo((x, y, z) => {})
}
`)
codeErrorTest(t, "bar.gop:6:8: cannot use lambda literal as type int in field value to Plot", `
type Foo struct {
Plot int
}
foo := &Foo{
Plot: x => (x * 2, x * x),
}
`)
codeErrorTest(t, "bar.gop:6:8: cannot use lambda literal as type int in field value to Plot", `
type Foo struct {
Plot int
}
foo := &Foo{
Plot: x => {
return x * 2, x * x
},
}
`)
codeErrorTest(t,
"bar.gop:4:5: cannot use lambda literal as type int in argument to foo", `
func foo(int) {
}
foo(=> {})
`)
codeErrorTest(t,
"bar.gop:4:5: cannot use lambda literal as type func() in argument to foo", `
func foo(func()) {
}
foo => (100)
`)
codeErrorTest(t,
"bar.gop:6:8: cannot use lambda literal as type func() int in field value to Plot", `
type Foo struct {
Plot func() int
}
foo := &Foo{
Plot: x => (x * 2, x * x),
}
`)
codeErrorTest(t,
"bar.gop:2:18: cannot use lambda literal as type func() in assignment to foo", `
var foo func() = => (100)
`)
codeErrorTest(t,
"bar.gop:3:7: cannot use lambda literal as type func() in assignment to foo", `
var foo func()
foo = => (100)
`)
codeErrorTest(t,
"bar.gop:2:29: lambda unsupport multiple assignment", `
var foo, foo1 func() = nil, => {}
`)
codeErrorTest(t,
"bar.gop:3:15: lambda unsupport multiple assignment", `
var foo func()
_, foo = nil, => {}
`)
codeErrorTest(t,
"bar.gop:4:9: cannot use lambda expression as type int in return statement", `
func intSeq() int {
i := 0
return => {
i++
return i
}
}
`)
codeErrorTest(t,
"bar.gop:6:10: cannot use i (type int) as type string in return argument", `
func intSeq() func() string {
i := 0
return => {
i++
return i
}
}
`)
}
func TestErrErrWrap(t *testing.T) {
codeErrorTest(t,
"bar.gop:2:2: undefined: a", `func main() {
a!
}
`)
}
func TestErrVar(t *testing.T) {
codeErrorTest(t,
"bar.gop:6:5: assignment mismatch: 1 variables but fmt.Println returns 2 values", `import "fmt"
func main() {
}
var a = fmt.Println(1)
`)
codeErrorTest(t,
"bar.gop:4:5: assignment mismatch: 1 variables but 2 values", `func main() {
}
var a = 1, 2
`)
codeErrorTest(t,
"bar.gop:2:2: undefined: foo", `func main() {
foo.x = 1
}
`)
codeErrorTest(t,
"bar.gop:2:2: use of builtin len not in function call", `func main() {
len.x = 1
}
`)
codeErrorTest(t,
"bar.gop:2:10: undefined: foo", `func main() {
println(foo.x)
}
`)
codeErrorTest(t,
"bar.gop:2:10: use of builtin len not in function call", `func main() {
println(len.x)
}
`)
codeErrorTest(t,
"bar.gop:2:10: undefined: foo", `func main() {
println(foo)
}
`)
codeErrorTest(t,
"bar.gop:3:20: use of builtin len not in function call", `package main
func foo(v map[int]len) {
}
`)
codeErrorTest(t,
"bar.gop:5:20: bar is not a type", `package main
var bar = 1
func foo(v map[int]bar) {
}
`)
codeErrorTest(t,
"bar.gop:2:6: use of builtin len not in function call", `func main() {
new(len)
}
`)
codeErrorTest(t,
"bar.gop:2:2: undefined: foo", `func main() {
foo = 1
}
`)
codeErrorTest(t,
"bar.gop:2:9: cannot use _ as value", `func main() {
foo := _
}
`)
codeErrorTest(t,
"bar.gop:2:9: use of builtin len not in function call", `func main() {
foo := len
}
`)
codeErrorTest(t,
"bar.gop:2:2: println is not a variable", `func main() {
println = "hello"
}
`)
}
func TestErrImport(t *testing.T) {
codeErrorTest(t,
`bar.gop:8:2: confliction: NewEncoding declared both in "encoding/base64" and "encoding/base32"`, `
import (
. "encoding/base32"
. "encoding/base64"
)
func foo() {
NewEncoding("Hi")
}`)
codeErrorTest(t,
"bar.gop:5:2: cannot refer to unexported name os.undefined", `
import "os"
func foo() {
os.undefined
}`)
codeErrorTest(t,
"bar.gop:5:2: undefined: os.UndefinedObject", `
import "os"
func foo() {
os.UndefinedObject
}`)
codeErrorTest(t,
"bar.gop:2:13: undefined: testing", `
func foo(t *testing.T) {
}`)
codeErrorTest(t,
"bar.gop:4:12: testing.Verbose is not a type", `
import "testing"
func foo(t testing.Verbose) {
}`)
}
func TestErrConst(t *testing.T) {
codeErrorTest(t,
"bar.gop:3:7: a redeclared in this block\n\tprevious declaration at bar.gop:2:5", `
var a int
const a = 1
`)
codeErrorTest(t,
"bar.gop:4:2: missing value in const declaration", `
const (
a = iota
b, c
)
`)
}
func TestErrNewVar(t *testing.T) {
codeErrorTest(t,
"bar.gop:3:5: a redeclared in this block\n\tprevious declaration at bar.gop:2:5", `
var a int
var a string
`)
}
func TestErrDefineVar(t *testing.T) {
codeErrorTest(t, "bar.gop:3:1: no new variables on left side of :=\n"+
"bar.gop:3:6: cannot use \"Hi\" (type untyped string) as type int in assignment", `
a := 1
a := "Hi"
`)
}
func TestErrAssignMismatchT(t *testing.T) {
codeErrorTest(t,
`bar.gop:2:16: cannot use []string{} (type []string) as type string in assignment`, `
var a string = []string{}
`)
codeErrorTest(t,
`bar.gop:2:16: cannot use [2]string{} (type [2]string) as type string in assignment`, `
var a string = [2]string{}
`)
codeErrorTest(t,
`bar.gop:2:16: cannot use map[int]string{} (type map[int]string) as type string in assignment`, `
var a string = map[int]string{}
`)
codeErrorTest(t,
`bar.gop:3:16: cannot use T{} (type T) as type string in assignment`, `
type T struct{}
var a string = T{}
`)
codeErrorTest(t,
`bar.gop:2:16: cannot use func(){} (type func()) as type string in assignment`, `
var a string = func(){}
`)
}
func TestErrAssign(t *testing.T) {
codeErrorTest(t,
`bar.gop:8:1: assignment mismatch: 1 variables but bar returns 2 values`, `
func bar() (n int, err error) {
return
}
x := 1
x = bar()
`)
codeErrorTest(t,
`bar.gop:4:1: assignment mismatch: 1 variables but 2 values`, `
x := 1
x = 1, "Hi"
`)
}
func TestErrReturn(t *testing.T) {
codeErrorTest(t,
"bar.gop:4:2: too few arguments to return\n\thave (untyped int)\n\twant (int, error)", `
func foo() (int, error) {
return 1
}
`)
codeErrorTest(t,
"bar.gop:4:2: too many arguments to return\n\thave (untyped int, untyped int, untyped string)\n\twant (int, error)", `
func foo() (int, error) {
return 1, 2, "Hi"
}
`)
codeErrorTest(t,
`bar.gop:4:12: cannot use "Hi" (type untyped string) as type error in return argument`, `
func foo() (int, error) {
return 1, "Hi"
}
`)
codeErrorTest(t,
"bar.gop:8:2: too few arguments to return\n\thave (byte)\n\twant (int, error)", `
func bar() (v byte) {
return
}
func foo() (int, error) {
return bar()
}
`)
codeErrorTest(t,
"bar.gop:8:2: too many arguments to return\n\thave (n int, err error)\n\twant (v byte)", `
func bar() (n int, err error) {
return
}
func foo() (v byte) {
return bar()
}
`)
codeErrorTest(t,
`bar.gop:8:2: cannot use byte value as type error in return argument`, `
func bar() (n int, v byte) {
return
}
func foo() (int, error) {
return bar()
}
`)
codeErrorTest(t,
"bar.gop:4:2: not enough arguments to return\n\thave ()\n\twant (byte)", `
func foo() byte {
return
}
`)
}
func TestErrForRange(t *testing.T) {
codeErrorTest(t,
`bar.gop:4:8: cannot assign type string to a (type int) in range`, `
a := 1
var b []string
for _, a = range b {
}
`)
}
func TestErrInitFunc(t *testing.T) {
codeErrorTest(t,
`bar.gop:2:6: func init must have no arguments and no return values`, `
func init(v byte) {
}
`)
}
func TestErrRecv(t *testing.T) {
codeErrorTest(t,
`bar.gop:5:9: invalid receiver type a (a is a pointer type)`, `
type a *int
func (p a) foo() {
}
`)
codeErrorTest(t,
`bar.gop:2:9: invalid receiver type error (error is an interface type)`, `
func (p error) foo() {
}
`)
codeErrorTest(t,
`bar.gop:2:9: invalid receiver type []byte ([]byte is not a defined type)`, `
func (p []byte) foo() {
}
`)
codeErrorTest(t,
`bar.gop:2:10: invalid receiver type []byte ([]byte is not a defined type)`, `
func (p *[]byte) foo() {
}
`)
}
func TestErrEnvOp(t *testing.T) {
codeErrorTest(t, `bar.gop:2:6: operator $name undefined`, `
echo ${name}
`)
codeErrorTest(t, `bar.gop:2:1: operator $id undefined`, `
$id
`)
}
func TestErrStringLit(t *testing.T) {
codeErrorTest(t, `bar.gop:2:9: [].string undefined (type []interface{} has no field or method string)`, `
echo "${[]}"
`)
}
func TestErrStructLit(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:39: too many values in struct{x int; y string}{...}`, `
x := 1
a := struct{x int; y string}{1, "Hi", 2}
`)
codeErrorTest(t,
`bar.gop:3:30: too few values in struct{x int; y string}{...}`, `
x := 1
a := struct{x int; y string}{1}
`)
codeErrorTest(t,
`bar.gop:3:33: cannot use x (type int) as type string in value of field y`, `
x := 1
a := struct{x int; y string}{1, x}
`)
codeErrorTest(t,
`bar.gop:2:30: z undefined (type struct{x int; y string} has no field or method z)`, `
a := struct{x int; y string}{z: 1}
`)
}
func TestErrArray(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:8: non-constant array bound n`, `
var n int
var a [n]int
`)
}
func TestErrArrayLit(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:14: cannot use a as index which must be non-negative integer constant`,
`
a := "Hi"
b := [10]int{a: 1}
`)
codeErrorTest(t,
`bar.gop:3:20: array index 10 out of bounds [0:10]`,
`
a := "Hi"
b := [10]int{9: 1, 3}
`)
codeErrorTest(t,
`bar.gop:3:16: array index 1 out of bounds [0:1]`,
`
a := "Hi"
b := [1]int{1, 2}
`)
codeErrorTest(t,
`bar.gop:3:14: array index 12 (value 12) out of bounds [0:10]`,
`
a := "Hi"
b := [10]int{12: 2}
`)
codeErrorTest(t,
`bar.gop:3:14: cannot use a+"!" (type string) as type int in array literal`,
`
a := "Hi"
b := [10]int{a+"!"}
`)
codeErrorTest(t,
`bar.gop:3:17: cannot use a (type string) as type int in array literal`,
`
a := "Hi"
b := [10]int{2: a}
`)
}
func TestErrSliceLit(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:12: cannot use a as index which must be non-negative integer constant`,
`
a := "Hi"
b := []int{a: 1}
`)
codeErrorTest(t,
`bar.gop:3:12: cannot use a (type string) as type int in slice literal`,
`
a := "Hi"
b := []int{a}
`)
codeErrorTest(t,
`bar.gop:3:15: cannot use a (type string) as type int in slice literal`,
`
a := "Hi"
b := []int{2: a}
`)
}
func TestErrMapLit(t *testing.T) {
codeErrorTest(t, `bar.gop:4:6: cannot use 1 (type untyped int) as type string in map key`, `
func foo(map[string]string) {}
foo {1: 2}
`)
codeErrorTest(t, `bar.gop:2:1: invalid composite literal type int`, `
int{2}
`)
codeErrorTest(t, `bar.gop:2:1: missing key in map literal`, `
map[string]int{2}
`)
codeErrorTest(t, `bar.gop:2:21: cannot use 1+2 (type untyped int) as type string in map key
bar.gop:3:27: cannot use "Go" + "+" (type untyped string) as type int in map value`,
`
a := map[string]int{1+2: 2}
b := map[string]int{"Hi": "Go" + "+"}
`)
codeErrorTest(t, `bar.gop:2:13: invalid map literal`, `
var v any = {1:2,1}
`)
codeErrorTest(t, `bar.gop:2:21: invalid map literal`, `
var v map[int]int = {1:2,1}
`)
}
func TestErrSlice(t *testing.T) {
codeErrorTest(t,
`bar.gop:4:6: cannot slice a (type *byte)`,
`
var a *byte
x := 1
b := a[x:2]
`)
codeErrorTest(t,
`bar.gop:3:6: cannot slice a (type bool)`,
`
a := true
b := a[1:2]
`)
codeErrorTest(t,
`bar.gop:3:6: invalid operation a[1:2:5] (3-index slice of string)`,
`
a := "Hi"
b := a[1:2:5]
`)
}
func TestErrIndex(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:10: assignment mismatch: 2 variables but 1 values`,
`
a := "Hi"
b, ok := a[1]
`)
codeErrorTest(t,
`bar.gop:3:6: invalid operation: a[1] (type bool does not support indexing)`,
`
a := true
b := a[1]
`)
}
func TestErrIndexRef(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:1: cannot assign to a[1] (strings are immutable)`,
`
a := "Hi"
a[1] = 'e'
`)
}
func TestErrStar(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:2: invalid indirect of a (type string)`,
`
a := "Hi"
*a = 'e'
`)
codeErrorTest(t,
`bar.gop:3:7: invalid indirect of a (type string)`,
`
a := "Hi"
b := *a
`)
}
func TestErrMember(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:6: a.x undefined (type string has no field or method x)`,
`
a := "Hello"
b := a.x
`)
}
func TestErrMemberRef(t *testing.T) {
codeErrorTest(t,
`bar.gop:3:1: a.x undefined (type string has no field or method x)`,
`
a := "Hello"
a.x = 1
`)
codeErrorTest(t,
`bar.gop:5:1: a.x undefined (type aaa has no field or method x)`,
`
type aaa byte
a := aaa(0)
a.x = 1
`)
codeErrorTest(t,
`bar.gop:5:1: a.z undefined (type aaa has no field or method z)`,
`
type aaa struct {x int; y string}
a := aaa{}
a.z = 1
`)
codeErrorTest(t,
`bar.gop:3:1: a.z undefined (type struct{x int; y string} has no field or method z)`,
`
a := struct{x int; y string}{}
a.z = 1
`)
}
func TestErrLabel(t *testing.T) {
codeErrorTest(t,
`bar.gop:4:1: label foo already defined at bar.gop:2:1
bar.gop:2:1: label foo defined and not used`,
`x := 1
foo:
i := 1
foo:
i++
`)
codeErrorTest(t,
`bar.gop:2:6: label foo is not defined`,
`x := 1
goto foo`)
codeErrorTest(t,
`bar.gop:2:7: label foo is not defined`,
`x := 1
break foo`)
}
func TestErrBranchStmt(t *testing.T) {
codeErrorTest(t,
`bar.gop:2:2: fallthrough statement out of place`,
`func foo() {
fallthrough
}`)
}
func TestErrNoEntrypoint(t *testing.T) {
codeErrorTest(t,
"bar.gop:2:2: undefined: println1", `func main() {
println1 "hello"
}
`)
codeErrorTest(t,
"bar.gop:1:1: undefined: println1", `println1 "hello"`)
codeErrorTest(t,
"bar.gop:2:2: undefined: println1", `
println1 "hello"
`)
codeErrorTest(t,
"bar.gop:2:2: undefined: println1", `package main
println1 "hello"
`)
codeErrorTest(t,
`bar.gop:1:9: undefined: abc`,
`println abc
`)
codeErrorTestEx(t, "bar", "bar.gop",
`bar.gop:2:9: undefined: abc`,
`package bar
println abc
`)
}
func TestErrTypeRedefine(t *testing.T) {
codeErrorTest(t,
"bar.gop:9:6: Point redeclared in this block\n\tprevious declaration at bar.gop:5:6",
`import "fmt"
func (p *Point) String() string {
return fmt.Sprintf("%v-%v",p.X,p.Y)
}
type Point struct {
X int
Y int
}
type Point struct {
X int
Y int
}
`)
}
func TestErrSwitchDuplicate(t *testing.T) {
codeErrorTest(t,
"bar.gop:4:7: duplicate case 100 in switch\n\tprevious case at bar.gop:3:7",
`var n int
switch n {
case 100:
case 100:
}`)
codeErrorTest(t,
"bar.gop:4:7: duplicate case int(100) (value 100) in switch\n\tprevious case at bar.gop:3:7",
`var n int
switch n {
case 100:
case int(100):
}`)
codeErrorTest(t,
"bar.gop:4:7: duplicate case 50 + 50 (value 100) in switch\n\tprevious case at bar.gop:3:7",
`var n int
switch n {
case 100:
case 50 + 50:
}`)
codeErrorTest(t,
"bar.gop:5:7: duplicate case int(100) (value 100) in switch\n\tprevious case at bar.gop:3:7",
`var n interface{}
switch n {
case 100:
case uint(100):
case int(100):
}`)
codeErrorTest(t,
"bar.gop:4:7: duplicate case 100.0 in switch\n\tprevious case at bar.gop:3:7",
`var n interface{}
switch n {
case 100.0:
case 100.0:
}`)
codeErrorTest(t,
"bar.gop:5:7: duplicate case v (value 100) in switch\n\tprevious case at bar.gop:4:7",
`var n interface{}
const v = 100.0
switch n {
case 100.0:
case v:
}`)
codeErrorTest(t,
"bar.gop:5:7: duplicate case v (value \"hello\") in switch\n\tprevious case at bar.gop:4:7",
`var n interface{}
const v = "hello"
switch n {
case "hello":
case v:
}`)
codeErrorTest(t,
`bar.gop:4:7: duplicate case 100 in switch
previous case at bar.gop:3:7
bar.gop:5:7: duplicate case 50 + 50 (value 100) in switch
previous case at bar.gop:3:7`,
`var n int
switch n {
case 100:
case 100:
case 50 + 50:
}`)
codeErrorTest(t,
"bar.gop:4:2: multiple defaults in switch (first at bar.gop:3:2)",
`var n interface{}
switch n {
default:
default:
}`)
codeErrorTest(t, `bar.gop:4:2: multiple defaults in switch (first at bar.gop:3:2)
bar.gop:5:2: multiple defaults in switch (first at bar.gop:3:2)`,
`var n interface{}
switch n {
default:
default:
default:
}`)
}
func TestErrTypeSwitchDuplicate(t *testing.T) {
codeErrorTest(t, `bar.gop:4:7: duplicate case int in type switch
previous case at bar.gop:3:7
bar.gop:5:7: duplicate case int in type switch
previous case at bar.gop:3:7`,
`var n interface{} = 100
switch n.(type) {
case int:
case int:
case int:
}
`)
codeErrorTest(t, `bar.gop:4:7: multiple nil cases in type switch (first at bar.gop:3:7)
bar.gop:5:7: multiple nil cases in type switch (first at bar.gop:3:7)`,
`var n interface{} = 100
switch n.(type) {
case nil:
case nil:
case nil:
}
`)
codeErrorTest(t, `bar.gop:4:2: multiple defaults in type switch (first at bar.gop:3:2)
bar.gop:5:2: multiple defaults in type switch (first at bar.gop:3:2)`,
`var n interface{} = 100
switch n.(type) {
default:
default:
default:
}
`)
}
func TestErrAutoProperty(t *testing.T) {
codeErrorTest(t, `bar.gop:4:11: cannot refer to unexported name fmt.println`, `
import "fmt"
n, err := fmt.println
`)
}
func TestFiledsNameRedecl(t *testing.T) {
codeErrorTest(t, `bar.gop:6:2: Id redeclared
bar.gop:5:2 other declaration of Id
bar.gop:7:2: Id redeclared
bar.gop:5:2 other declaration of Id
bar.gop:9:2: name redeclared
bar.gop:8:2 other declaration of name`, `
type Id struct {
}
type A struct {
Id int
Id string
Id
name string
name string
}
`)
}
func TestErrImportPkg(t *testing.T) {
root := filepath.Join(runtime.GOROOT(), "src", "fmt2")
where := "GOROOT"
ver := runtime.Version()[:6]
if ver >= "go1.21" {
where = "std"
}
codeErrorTest(t,
fmt.Sprintf(`bar.gop:3:2: package fmt2 is not in `+where+` (%v)
`, root), `
import (
"fmt2"
)
`)
codeErrorTest(t, `bar.gop:3:2: no required module provides package github.com/goplus/gop/fmt2; to add it:
go get github.com/goplus/gop/fmt2
`, `
import (
"github.com/goplus/gop/fmt2"
)
`)
}
func TestErrClassFileGopx(t *testing.T) {
codeErrorTestEx(t, "main", "Rect.gox",
`Rect.gox:5:2: A redeclared
Rect.gox:3:2 other declaration of A`, `
var (
A
i int
A
)
type A struct{}
println "hello"
`)
}
func TestErrVarInFunc(t *testing.T) {
codeErrorTest(t, `bar.gop:6:10: not enough arguments in call to set
have (untyped string)
want (name string, v int)
bar.gop:7:10: undefined: a`, `
func set(name string, v int) string {
return name
}
func test() {
var a = set("box")
println(a)
}
`)
}
func TestErrInt128(t *testing.T) {
codeErrorTest(t, `bar.gop:2:16: cannot use 1<<127 (type untyped int) as type github.com/goplus/gop/builtin/ng.Int128 in assignment`, `
var a int128 = 1<<127
`)
codeErrorTest(t, `bar.gop:2:13: cannot convert 1<<127 (untyped int constant 170141183460469231731687303715884105728) to type Int128`, `
a := int128(1<<127)
`)
codeErrorTest(t, `bar.gop:2:13: cannot convert -1<<127-1 (untyped int constant -170141183460469231731687303715884105729) to type Int128`, `
a := int128(-1<<127-1)
`)
codeErrorTest(t, `bar.gop:3:13: cannot convert b (untyped int constant -170141183460469231731687303715884105729) to type Int128`, `
const b = -1<<127-1