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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
|
" Test Vim9 enums
source check.vim
import './vim9.vim' as v9
" Test for parsing an enum definition
def Test_enum_parse()
# enum supported only in a Vim9 script
var lines =<< trim END
enum Foo
endenum
END
v9.CheckSourceFailure(lines, 'E1414: Enum can only be defined in Vim9 script', 1)
# First character in an enum name should be capitalized.
lines =<< trim END
vim9script
enum foo
endenum
END
v9.CheckSourceFailure(lines, 'E1415: Enum name must start with an uppercase letter: foo', 2)
# Only alphanumeric characters are supported in an enum name
lines =<< trim END
vim9script
enum Foo@bar
endenum
END
v9.CheckSourceFailure(lines, 'E1315: White space required after name: Foo@bar', 2)
# Unsupported keyword (instead of enum)
lines =<< trim END
vim9script
noenum Something
endenum
END
v9.CheckSourceFailure(lines, 'E492: Not an editor command: noenum Something', 2)
# Only the complete word "enum" should be recognized
lines =<< trim END
vim9script
enums Something
endenum
END
v9.CheckSourceFailure(lines, 'E492: Not an editor command: enums Something', 2)
# The complete "endenum" should be specified.
lines =<< trim END
vim9script
enum Something
enden
END
v9.CheckSourceFailure(lines, 'E1065: Command cannot be shortened: enden', 3)
# Only the complete word "endenum" should be recognized
lines =<< trim END
vim9script
enum Something
endenums
END
v9.CheckSourceFailure(lines, 'E1420: Missing :endenum', 4)
# all lower case should be used for "enum"
lines =<< trim END
vim9script
Enum Something
endenum
END
v9.CheckSourceFailure(lines, 'E492: Not an editor command: Enum Something', 2)
# all lower case should be used for "endenum"
lines =<< trim END
vim9script
enum Something
Endenum
END
v9.CheckSourceFailure(lines, 'E1420: Missing :endenum', 4)
# Additional words after "endenum"
lines =<< trim END
vim9script
enum Something
endenum school's out
END
v9.CheckSourceFailure(lines, "E488: Trailing characters: school's out", 3)
# Additional commands after "endenum"
lines =<< trim END
vim9script
enum Something
endenum | echo 'done'
END
v9.CheckSourceFailure(lines, "E488: Trailing characters: | echo 'done'", 3)
# Try to define enum in a single command
lines =<< trim END
vim9script
enum Something | endenum
END
v9.CheckSourceFailure(lines, 'E488: Trailing characters: | endenum', 2)
# another command follows the enum name
lines =<< trim END
vim9script
enum Something | var x = 10
Foo
endenum
END
v9.CheckSourceFailure(lines, 'E488: Trailing characters: | var x = 10', 2)
# Try to define an enum with the same name as an existing variable
lines =<< trim END
vim9script
var Something: list<number> = [1]
enum Something
endenum
END
v9.CheckSourceFailure(lines, 'E1041: Redefining script item: "Something"', 3)
# Unsupported special character following enum name
lines =<< trim END
vim9script
enum Foo
first,
second : 20
endenum
END
v9.CheckSourceFailure(lines, 'E1123: Missing comma before argument: : 20', 4)
# Try initializing an enum item with a number
lines =<< trim END
vim9script
enum Foo
first,
second = 2
endenum
END
v9.CheckSourceFailure(lines, 'E1123: Missing comma before argument: = 2', 4)
# Try initializing an enum item with a String
lines =<< trim END
vim9script
enum Foo
first,
second = 'second'
endenum
defcompile
END
v9.CheckSourceFailure(lines, "E1123: Missing comma before argument: = 'second'", 4)
# Try initializing an enum item with a List
lines =<< trim END
vim9script
enum Foo
first,
second = []
endenum
END
v9.CheckSourceFailure(lines, 'E1123: Missing comma before argument: = []', 4)
# Use a colon after name
lines =<< trim END
vim9script
enum Foo
# first
first:
second
endenum
END
v9.CheckSourceFailure(lines, 'E1123: Missing comma before argument: :', 5)
# Use a '=='
lines =<< trim END
vim9script
enum Foo
first == 1
endenum
defcompile
END
v9.CheckSourceFailure(lines, 'E1123: Missing comma before argument: == 1', 3)
# Missing comma after an enum item
lines =<< trim END
vim9script
enum Planet
mercury
venus
endenum
END
v9.CheckSourceFailure(lines, 'E1419: Not a valid command in an Enum: venus', 4)
# Comma at the beginning of an item
lines =<< trim END
vim9script
enum Planet
mercury
,venus
endenum
END
v9.CheckSourceFailure(lines, 'E1419: Not a valid command in an Enum: ,venus', 4)
# Space before comma
lines =<< trim END
vim9script
enum Planet
mercury ,
venus
endenum
END
v9.CheckSourceFailure(lines, "E1068: No white space allowed before ','", 3)
# No space after comma
lines =<< trim END
vim9script
enum Planet
mercury,venus
endenum
END
v9.CheckSourceFailure(lines, "E1069: White space required after ',': mercury,venus", 3)
# no comma between items in the same line
lines =<< trim END
vim9script
enum Planet
mercury venus earth
endenum
defcompile
END
v9.CheckSourceFailure(lines, 'E1123: Missing comma before argument: venus earth', 3)
# No space after an item and comment between items
lines =<< trim END
vim9script
enum Planet
mercury
# Venus
venus
endenum
END
v9.CheckSourceFailure(lines, 'E1419: Not a valid command in an Enum: venus', 6)
# Comma is supported for the last item
lines =<< trim END
vim9script
enum Planet
mercury,
venus,
endenum
var p: Planet
p = Planet.mercury
p = Planet.venus
END
v9.CheckSourceSuccess(lines)
# invalid enum value declaration
lines =<< trim END
vim9script
enum Fruit
Apple,
$%@
endenum
defcompile
END
v9.CheckSourceFailure(lines, 'E1418: Invalid enum value declaration: $%@', 4)
# Duplicate enum value
lines =<< trim END
vim9script
enum A
Foo,
Bar,
Foo
endenum
END
v9.CheckSourceFailure(lines, 'E1428: Duplicate enum value: Foo', 5)
# Duplicate enum value in the same line
lines =<< trim END
vim9script
enum A
Foo, Bar, Foo,
Bar
endenum
END
v9.CheckSourceFailure(lines, 'E1428: Duplicate enum value: Foo', 3)
# Try extending a class when defining an enum
lines =<< trim END
vim9script
class Foo
endclass
enum Bar extends Foo
endenum
END
v9.CheckSourceFailure(lines, 'E1416: Enum cannot extend a class or enum', 4)
# Try extending an enum
lines =<< trim END
vim9script
enum Foo
endenum
enum Bar extends Foo
endenum
END
v9.CheckSourceFailure(lines, 'E1416: Enum cannot extend a class or enum', 4)
# Try extending an enum using a class
lines =<< trim END
vim9script
enum Foo
endenum
class Bar extends Foo
endclass
END
v9.CheckSourceFailure(lines, 'E1354: Cannot extend Foo', 5)
# Try implementing an enum using a class
lines =<< trim END
vim9script
enum Foo
endenum
class Bar implements Foo
endclass
END
v9.CheckSourceFailure(lines, 'E1347: Not a valid interface: Foo', 5)
# abstract method is not supported in an enum
lines =<< trim END
vim9script
enum Foo
Apple
abstract def Bar()
endenum
END
v9.CheckSourceFailure(lines, 'E1417: Abstract cannot be used in an Enum', 4)
# Define an enum without any enum values but only with an object variable
lines =<< trim END
vim9script
enum Foo
final n: number = 10
endenum
END
v9.CheckSourceFailure(lines, 'E1123: Missing comma before argument: n: number = 10', 3)
enddef
def Test_basic_enum()
# Declare a simple enum
var lines =<< trim END
vim9script
enum Foo
apple,
orange
endenum
var a: Foo = Foo.apple
var b: Foo = Foo.orange
assert_equal(a, Foo.apple)
assert_equal(b, Foo.orange)
END
v9.CheckSourceSuccess(lines)
# Multiple enums in a single line
lines =<< trim END
vim9script
enum Foo
apple, orange
endenum
assert_equal('enum<Foo>', typename(Foo.apple))
assert_equal('enum<Foo>', typename(Foo.orange))
END
v9.CheckSourceSuccess(lines)
# Comments and empty lines are supported between enum items
lines =<< trim END
vim9script
enum Foo
# Apple
apple,
# Orange
orange
endenum
def Fn()
var a: Foo = Foo.apple
var b: Foo = Foo.orange
assert_equal(a, Foo.apple)
assert_equal(b, Foo.orange)
enddef
END
v9.CheckSourceSuccess(lines)
# Try using a non-existing enum value
lines =<< trim END
vim9script
enum Foo
apple,
orange
endenum
var a: Foo = Foo.pear
END
v9.CheckSourceFailure(lines, 'E1422: Enum value "pear" not found in enum "Foo"', 6)
# Enum function argument
lines =<< trim END
vim9script
enum Foo
apple,
orange
endenum
def Fn(a: Foo): Foo
return a
enddef
assert_equal(Foo.apple, Fn(Foo.apple))
END
v9.CheckSourceSuccess(lines)
# Enum function argument
lines =<< trim END
vim9script
enum Foo
apple,
orange
endenum
def Fn(a: Foo): Foo
return a
enddef
Fn({})
END
v9.CheckSourceFailure(lines, 'E1013: Argument 1: type mismatch, expected enum<Foo> but got dict<any>', 9)
# Returning an enum in a function returning number
lines =<< trim END
vim9script
enum Foo
apple,
orange
endenum
def Fn(): number
return Foo.orange
enddef
Fn()
END
v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected number but got enum<Foo>', 1)
# Returning a number in a function returning enum
lines =<< trim END
vim9script
enum Foo
apple,
orange
endenum
def Fn(): Foo
return 20
enddef
Fn()
END
v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected enum<Foo> but got number', 1)
# Use a List of enums
lines =<< trim END
vim9script
enum Planet
Mercury,
Venus,
Earth
endenum
var l1: list<Planet> = [Planet.Mercury, Planet.Venus]
assert_equal(Planet.Venus, l1[1])
def Fn()
var l2: list<Planet> = [Planet.Mercury, Planet.Venus]
assert_equal(Planet.Venus, l2[1])
enddef
END
v9.CheckSourceSuccess(lines)
# Try using an enum as a value
lines =<< trim END
vim9script
enum Fruit
Apple,
Orange
endenum
var a = Fruit
END
v9.CheckSourceFailure(lines, 'E1421: Enum "Fruit" cannot be used as a value', 6)
enddef
" Test for type() and typename() of an enum
def Test_enum_type()
var lines =<< trim END
vim9script
enum Fruit
Apple,
Orange
endenum
assert_equal('enum<Fruit>', typename(Fruit))
assert_equal('enum<Fruit>', typename(Fruit.Apple))
assert_equal(v:t_enum, type(Fruit))
assert_equal(v:t_enumvalue, type(Fruit.Apple))
assert_equal(15, type(Fruit))
assert_equal(16, type(Fruit.Apple))
END
v9.CheckSourceSuccess(lines)
# Assign an enum to a variable with any type
lines =<< trim END
vim9script
enum Fruit
Apple,
Orange
endenum
var a = Fruit.Apple
var b: any = Fruit.Orange
assert_equal('enum<Fruit>', typename(a))
assert_equal('enum<Fruit>', typename(b))
END
v9.CheckSourceSuccess(lines)
enddef
" Try modifying an enum or an enum item
def Test_enum_modify()
# Try assigning an unsupported value to an enum
var lines =<< trim END
vim9script
enum Foo
apple
endenum
var a: Foo = 30
END
v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected enum<Foo> but got number', 5)
# Try assigning an unsupported value to an enum in a function
lines =<< trim END
vim9script
enum Foo
apple
endenum
def Fn()
var a: Foo = 30
enddef
defcompile
END
v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected enum<Foo> but got number', 1)
# Try assigning a number to an enum
lines =<< trim END
vim9script
enum Foo
apple,
orange
endenum
Foo = 10
END
v9.CheckSourceFailure(lines, 'E1421: Enum "Foo" cannot be used as a value', 6)
# Try assigning a number to an enum in a function
lines =<< trim END
vim9script
enum Foo
apple
endenum
def Fn()
Foo = 10
enddef
defcompile
END
v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected enum<Foo> but got number', 1)
# Try assigning a number to an enum value
lines =<< trim END
vim9script
enum Foo
apple
endenum
Foo.apple = 20
END
v9.CheckSourceFailure(lines, 'E1423: Enum value "Foo.apple" cannot be modified', 5)
# Try assigning a number to an enum value in a function
lines =<< trim END
vim9script
enum Foo
apple
endenum
def Fn()
Foo.apple = 20
enddef
defcompile
END
v9.CheckSourceFailure(lines, 'E1423: Enum value "Foo.apple" cannot be modified', 1)
# Try assigning one enum to another
lines =<< trim END
vim9script
enum Foo
endenum
enum Bar
endenum
Foo = Bar
END
v9.CheckSourceFailure(lines, 'E1421: Enum "Bar" cannot be used as a value', 6)
# Try assigning one enum to another in a function
lines =<< trim END
vim9script
enum Foo
endenum
enum Bar
endenum
def Fn()
Foo = Bar
enddef
defcompile
END
v9.CheckSourceFailure(lines, 'E1421: Enum "Bar" cannot be used as a value', 1)
# Try assigning one enum item to another enum item
lines =<< trim END
vim9script
enum Foo
Apple
endenum
enum Bar
Orange
endenum
Foo.Apple = Bar.Orange
END
v9.CheckSourceFailure(lines, 'E1423: Enum value "Foo.Apple" cannot be modified', 8)
# Try assigning one enum item to another enum item in a function
lines =<< trim END
vim9script
enum Foo
Apple
endenum
enum Bar
Orange
endenum
def Fn()
Foo.Apple = Bar.Orange
enddef
defcompile
END
v9.CheckSourceFailure(lines, 'E1423: Enum value "Foo.Apple" cannot be modified', 1)
enddef
" Test for using enum in an expression
def Test_enum_expr()
var lines =<< trim END
vim9script
enum Color
Red, Blue, Green
endenum
var a: number = 1 + Color
END
v9.CheckSourceFailure(lines, 'E1421: Enum "Color" cannot be used as a value', 5)
lines =<< trim END
vim9script
enum Color
Red, Blue, Green
endenum
var a: number = 1 + Color.Red
END
v9.CheckSourceFailure(lines, 'E1424: Using an Enum "Color" as a Number', 5)
lines =<< trim END
vim9script
enum Color
Red, Blue, Green
endenum
var s: string = "abc" .. Color
END
v9.CheckSourceFailure(lines, 'E1421: Enum "Color" cannot be used as a value', 5)
lines =<< trim END
vim9script
enum Color
Red, Blue, Green
endenum
var s: string = "abc" .. Color.Red
END
v9.CheckSourceFailure(lines, 'E1425: Using an Enum "Color" as a String', 5)
enddef
" Using an enum in a lambda function
def Test_enum_lambda()
var lines =<< trim END
vim9script
enum Planet
Mercury,
Venus,
Earth,
endenum
var Fn = (p: Planet): Planet => p
for [idx, v] in items([Planet.Mercury, Planet.Venus, Planet.Earth])
assert_equal(idx, Fn(v).ordinal)
endfor
END
v9.CheckSourceSuccess(lines)
enddef
" Comparison using enums
def Test_enum_compare()
var lines =<< trim END
vim9script
enum Planet
Mercury,
Venus,
Earth,
endenum
enum Fruit
Apple,
Orange
endenum
var p: Planet = Planet.Venus
var f: Fruit = Fruit.Orange
assert_equal(true, p == Planet.Venus)
assert_equal(false, p == Planet.Earth)
assert_equal(false, p == f)
assert_equal(true, Planet.Mercury == Planet.Mercury)
assert_equal(true, Planet.Venus != Planet.Earth)
assert_equal(true, Planet.Mercury != Fruit.Apple)
def Fn1()
var p2: Planet = Planet.Venus
var f2: Fruit = Fruit.Orange
assert_equal(true, p2 == Planet.Venus)
assert_equal(false, p2 == Planet.Earth)
assert_equal(false, p2 == f2)
enddef
Fn1()
# comparison using "is" and "isnot"
assert_equal(true, p is Planet.Venus)
assert_equal(true, p isnot Planet.Earth)
assert_equal(false, p is Fruit.Orange)
assert_equal(true, p isnot Fruit.Orange)
def Fn2(arg: Planet)
assert_equal(true, arg is Planet.Venus)
assert_equal(true, arg isnot Planet.Earth)
assert_equal(false, arg is Fruit.Orange)
assert_equal(true, arg isnot Fruit.Orange)
enddef
Fn2(p)
class A
endclass
var o: A = A.new()
assert_equal(false, p == o)
END
v9.CheckSourceSuccess(lines)
enddef
" Test for using an enum as a default argument to a function
def Test_enum_default_arg()
var lines =<< trim END
vim9script
enum Day
Monday, Tuesday, Wednesday
endenum
def Fn(d: Day = Day.Tuesday): Day
return d
enddef
assert_equal(Day.Tuesday, Fn())
assert_equal(Day.Wednesday, Fn(Day.Wednesday))
END
v9.CheckSourceSuccess(lines)
enddef
" Test for enum garbage collection
func Test_enum_garbagecollect()
let lines =<< trim END
vim9script
enum Car
Honda, Ford, Tesla
endenum
assert_equal(1, Car.Ford.ordinal)
call test_garbagecollect_now()
assert_equal(1, Car.Ford.ordinal)
var c: Car = Car.Tesla
assert_equal(2, c.ordinal)
call test_garbagecollect_now()
assert_equal(2, c.ordinal)
END
call v9.CheckSourceSuccess(lines)
" garbage collection with a variable of type any
let lines =<< trim END
vim9script
enum Car
Honda, Ford, Tesla
endenum
call test_garbagecollect_now()
var c: any = Car.Tesla
call test_garbagecollect_now()
assert_equal(Car.Tesla, c)
END
call v9.CheckSourceSuccess(lines)
" garbage collection with function arguments and return types
let lines =<< trim END
vim9script
enum Car
Honda, Ford, Tesla
endenum
def Fn(a: Car): Car
assert_equal(Car.Ford, a)
return Car.Tesla
enddef
call test_garbagecollect_now()
var b: Car = Car.Ford
call test_garbagecollect_now()
assert_equal(Car.Tesla, Fn(b))
call test_garbagecollect_now()
END
call v9.CheckSourceSuccess(lines)
endfunc
" Test for the enum values class variable
def Test_enum_values()
var lines =<< trim END
vim9script
enum Car
Honda, Ford, Tesla
endenum
var l: list<Car> = Car.values
assert_equal(Car.Ford, l[1])
END
v9.CheckSourceSuccess(lines)
# empty enum
lines =<< trim END
vim9script
enum Car
endenum
assert_equal([], Car.values)
END
v9.CheckSourceSuccess(lines)
# single value
lines =<< trim END
vim9script
enum Car
Honda
endenum
assert_equal([Car.Honda], Car.values)
END
v9.CheckSourceSuccess(lines)
lines =<< trim END
vim9script
enum A
Red,
Blue
static def GetValues(): list<A>
return values
enddef
endenum
assert_equal([A.Red, A.Blue], A.GetValues())
END
v9.CheckSourceSuccess(lines)
# Other class variables in an enum should not be added to 'values'
lines =<< trim END
vim9script
enum LogLevel
Error, Warn
static const x: number = 22
endenum
assert_equal([LogLevel.Error, LogLevel.Warn], LogLevel.values)
END
v9.CheckSourceSuccess(lines)
# Other class variable of enum type should not be added to 'values'
lines =<< trim END
vim9script
enum LogLevel
Error, Warn
static const x: LogLevel = LogLevel.Warn
endenum
assert_equal([LogLevel.Error, LogLevel.Warn], LogLevel.values)
END
v9.CheckSourceSuccess(lines)
enddef
" Test comments in enums
def Test_enum_comments()
var lines =<< trim END
vim9script
enum Car # cars
# before enum
Honda, # honda
# before enum
Ford # ford
endenum
assert_equal(1, Car.Ford.ordinal)
END
v9.CheckSourceSuccess(lines)
# Test for using an unsupported comment
lines =<< trim END
vim9script
enum Car
Honda,
Ford,
#{
endenum
defcompile
END
v9.CheckSourceFailure(lines, 'E1170: Cannot use #{ to start a comment', 4)
enddef
" Test string() with enums
def Test_enum_string()
var lines =<< trim END
vim9script
enum Car
Honda,
Ford
endenum
assert_equal("enum Car", string(Car))
assert_equal("enum Car.Honda {name: 'Honda', ordinal: 0}", string(Car.Honda))
END
v9.CheckSourceSuccess(lines)
# customized string function
lines =<< trim END
vim9script
enum Dir
North,
South
def string(): string
return $'Dir.{this.name}'
enddef
endenum
assert_equal('Dir.North', string(Dir.North))
assert_equal('Dir.South', string(Dir.South))
END
v9.CheckSourceSuccess(lines)
enddef
" Test for importing an enum
def Test_enum_import()
var lines =<< trim END
vim9script
export enum Star
Gemini,
Orion,
Pisces
endenum
END
writefile(lines, 'Xenumexport.vim', 'D')
lines =<< trim END
vim9script
import './Xenumexport.vim' as mod
var s1: mod.Star = mod.Star.Orion
assert_equal(true, s1 == mod.Star.Orion)
assert_equal(2, mod.Star.Pisces.ordinal)
var l1: list<mod.Star> = mod.Star.values
assert_equal("enum Star.Orion {name: 'Orion', ordinal: 1}", string(l1[1]))
assert_equal(s1, l1[1])
def Fn()
var s2: mod.Star = mod.Star.Orion
assert_equal(true, s2 == mod.Star.Orion)
assert_equal(2, mod.Star.Pisces.ordinal)
var l2: list<mod.Star> = mod.Star.values
assert_equal("enum Star.Orion {name: 'Orion', ordinal: 1}", string(l2[1]))
assert_equal(s2, l2[1])
enddef
Fn()
END
v9.CheckScriptSuccess(lines)
enddef
" Test for using test_refcount() with enum
def Test_enum_refcount()
var lines =<< trim END
vim9script
enum Foo
endenum
assert_equal(1, test_refcount(Foo))
enum Star
Gemini,
Orion
endenum
assert_equal(3, test_refcount(Star))
assert_equal(2, test_refcount(Star.Gemini))
assert_equal(2, test_refcount(Star.Orion))
var s: Star
assert_equal(3, test_refcount(Star))
assert_equal(-1, test_refcount(s))
s = Star.Orion
assert_equal(3, test_refcount(Star))
assert_equal(3, test_refcount(s))
assert_equal(2, test_refcount(Star.Gemini))
var t = s
assert_equal(3, test_refcount(Star))
assert_equal(4, test_refcount(s))
assert_equal(4, test_refcount(Star.Orion))
END
v9.CheckSourceSuccess(lines)
enddef
" Test for defining an enum with additional object variables and methods
def Test_enum_enhanced()
var lines =<< trim END
vim9script
enum Vehicle
car(4, 5, 400),
bus(6, 50, 800),
bicycle(2, 1, 0)
final tires: number
final passengers: number
final carbonPerKilometer: number
def new(t: number, p: number, cpk: number)
this.tires = t
this.passengers = p
this.carbonPerKilometer = cpk
enddef
def CarbonFootprint(): float
return round(this.carbonPerKilometer / this.passengers)
enddef
def IsTwoWheeled(): bool
return this == Vehicle.bicycle
enddef
def CompareTo(other: Vehicle): float
return this.CarbonFootprint() - other.CarbonFootprint()
enddef
endenum
var v: Vehicle = Vehicle.bus
assert_equal([6, 50, 800], [v.tires, v.passengers, v.carbonPerKilometer])
assert_equal(true, Vehicle.bicycle.IsTwoWheeled())
assert_equal(false, Vehicle.car.IsTwoWheeled())
assert_equal(16.0, Vehicle.bus.CarbonFootprint())
END
v9.CheckSourceSuccess(lines)
enddef
" Test for the enum value 'name' variable
def Test_enum_name()
# Check the names of enum values
var lines =<< trim END
vim9script
enum Planet
Mercury,
Venus,
Earth
endenum
assert_equal('Mercury', Planet.Mercury.name)
assert_equal('Venus', Planet.Venus.name)
assert_equal('Earth', Planet.Earth.name)
assert_equal('string', typename(Planet.Earth.name))
END
v9.CheckSourceSuccess(lines)
# Check the name of enum items in the constructor
lines =<< trim END
vim9script
enum Planet
Mercury("Mercury"),
Venus("Venus"),
Earth("Earth")
def new(s: string)
assert_equal(s, this.name)
enddef
endenum
defcompile
END
v9.CheckSourceSuccess(lines)
# Try assigning to the name of an enum
lines =<< trim END
vim9script
enum Fruit
Apple
endenum
Fruit.Apple.name = 'foo'
END
v9.CheckSourceFailure(lines, 'E1335: Variable "name" in class "Fruit" is not writable', 5)
# Try assigning to the name of an enum in a function
lines =<< trim END
vim9script
enum Fruit
Apple
endenum
def Fn()
Fruit.Apple.name = 'bar'
enddef
defcompile
END
v9.CheckSourceFailure(lines, 'E1423: Enum value "Fruit.name" cannot be modified', 1)
# Try to overwrite an enum value name in the enum constructor
lines =<< trim END
vim9script
enum Planet
Mercury,
Venus
def new()
this.name = 'foo'
enddef
endenum
END
v9.CheckSourceFailure(lines, 'E1427: Enum "Planet" name cannot be modified', 1)
# Try to declare an object variable named 'name'
lines =<< trim END
vim9script
enum Planet
Mercury
var name: string
endenum
END
v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: name', 4)
enddef
" Test for the enum value 'ordinal' variable
def Test_enum_ordinal()
# Check the ordinal values of enum items
var lines =<< trim END
vim9script
enum Planet
Mercury,
Venus,
Earth
endenum
assert_equal(0, Planet.Mercury.ordinal)
assert_equal(1, Planet.Venus.ordinal)
assert_equal(2, Planet.Earth.ordinal)
assert_equal('number', typename(Planet.Earth.ordinal))
END
v9.CheckSourceSuccess(lines)
# Check the ordinal value of enum items in the constructor
lines =<< trim END
vim9script
enum Planet
Mercury(0),
Venus(1),
Earth(2)
def new(v: number)
assert_equal(v, this.ordinal)
enddef
endenum
defcompile
END
v9.CheckSourceSuccess(lines)
# Try assigning to the ordinal value of an enum
lines =<< trim END
vim9script
enum Fruit
Apple
endenum
Fruit.Apple.ordinal = 20
END
v9.CheckSourceFailure(lines, 'E1335: Variable "ordinal" in class "Fruit" is not writable', 5)
# Try assigning to the ordinal value of an enum in a function
lines =<< trim END
vim9script
enum Fruit
Apple
endenum
def Fn()
Fruit.Apple.ordinal = 20
enddef
defcompile
END
v9.CheckSourceFailure(lines, 'E1423: Enum value "Fruit.ordinal" cannot be modified', 1)
# Try to overwrite an enum value ordinal in the enum constructor
lines =<< trim END
vim9script
enum Planet
Mercury,
Venus
def new()
this.ordinal = 20
enddef
endenum
END
v9.CheckSourceFailure(lines, 'E1426: Enum "Planet" ordinal value cannot be modified', 1)
# Try to declare an object variable named 'ordinal'
lines =<< trim END
vim9script
enum Planet
Mercury
var ordinal: number
endenum
END
v9.CheckSourceFailure(lines, 'E1369: Duplicate variable: ordinal', 4)
enddef
" Test for trying to create a new enum object using the constructor
def Test_enum_invoke_constructor()
var lines =<< trim END
vim9script
enum Foo
endenum
var f: Foo = Foo.new()
END
v9.CheckSourceFailure(lines, 'E1325: Method "new" not found in class "Foo"', 4)
lines =<< trim END
vim9script
enum Fruit
Apple,
Orange
endenum
var f: Fruit = Fruit.new()
END
v9.CheckSourceFailure(lines, 'E1325: Method "new" not found in class "Fruit"', 6)
lines =<< trim END
vim9script
enum Fruit
Apple,
Orange
def newFruit()
enddef
endenum
var f: Fruit = Fruit.newFruit()
END
v9.CheckSourceFailure(lines, 'E1325: Method "newFruit" not found in class "Fruit"', 8)
lines =<< trim END
vim9script
enum Fruit
Apple,
Orange
endenum
def Fn()
var f: Fruit = Fruit.new()
enddef
Fn()
END
v9.CheckSourceFailure(lines, 'E1325: Method "new" not found in class "Fruit"', 1)
# error in the enum constructor
lines =<< trim END
vim9script
enum Planet
earth
def new()
x = 123
enddef
endenum
END
v9.CheckSourceFailureList(lines, ['E1100:', 'E1100:'], 1)
enddef
" Test for checking "this" in an enum constructor
def Test_enum_this_in_constructor()
var lines =<< trim END
vim9script
enum A
Red("enum A.Red {name: 'Red', ordinal: 0}"),
Blue("enum A.Blue {name: 'Blue', ordinal: 1}"),
Green("enum A.Green {name: 'Green', ordinal: 2}")
def new(s: string)
assert_equal(s, string(this))
enddef
endenum
defcompile
END
v9.CheckSourceSuccess(lines)
enddef
" Test for using member variables in an enum object
def Test_enum_object_variable()
var lines =<< trim END
vim9script
enum Planet
Jupiter(95),
Saturn(146)
var moons: number
endenum
assert_equal(95, Planet.Jupiter.moons)
assert_equal(146, Planet.Saturn.moons)
END
v9.CheckSourceSuccess(lines)
# Use a final object variable
lines =<< trim END
vim9script
enum Planet
Jupiter(95),
Saturn(146)
final moons: number
def new(n: number)
this.moons = n
enddef
endenum
assert_equal(95, Planet.Jupiter.moons)
assert_equal(146, Planet.Saturn.moons)
END
v9.CheckSourceSuccess(lines)
# Use a const object variable
lines =<< trim END
vim9script
enum Planet
Mars(false),
Jupiter(true)
const has_ring: bool
def new(r: bool)
this.has_ring = r
enddef
endenum
assert_equal(false, Planet.Mars.has_ring)
assert_equal(true, Planet.Jupiter.has_ring)
END
v9.CheckSourceSuccess(lines)
# Use a regular object variable
lines =<< trim END
vim9script
enum Fruit
Apple,
Orange
final farm: string = 'SunValley'
endenum
assert_equal('SunValley', Fruit.Apple.farm)
assert_equal('SunValley', Fruit.Apple.farm)
END
v9.CheckSourceSuccess(lines)
# Invoke the default constructor with an object variable
lines =<< trim END
vim9script
enum Fruit
Apple('foo'),
Orange('bar')
final t: string
endenum
assert_equal('foo', Fruit.Apple.t)
assert_equal('bar', Fruit.Orange.t)
END
v9.CheckSourceSuccess(lines)
# Invoke the default constructor with an argument but without the object
# variable
lines =<< trim END
vim9script
enum Fruit
Apple,
Orange('bar')
endenum
defcompile
END
v9.CheckSourceFailure(lines, 'E118: Too many arguments for function: new', 5)
# Define a default constructor with an argument, but don't pass it in when
# defining the enum value
lines =<< trim END
vim9script
enum Fruit
Apple(5),
Orange
def new(t: number)
enddef
endenum
defcompile
END
v9.CheckSourceFailure(lines, 'E119: Not enough arguments for function: new', 8)
enddef
" Test for using a custom constructor with an enum
def Test_enum_custom_constructor()
# space before "("
var lines =<< trim END
vim9script
enum Fruit
Apple(10),
Orange (20)
def new(t: number)
enddef
endenum
defcompile
END
v9.CheckSourceFailure(lines, "E1068: No white space allowed before '(': Orange (20)", 4)
# no closing ")"
lines =<< trim END
vim9script
enum Fruit
Apple(10),
Orange (20
def new(t: number)
enddef
endenum
defcompile
END
v9.CheckSourceFailure(lines, "E1068: No white space allowed before '(': Orange (20", 4)
# Specify constructor arguments split across multiple lines
lines =<< trim END
vim9script
enum Fruit
Apple(10,
'foo'), Orange(20,
'bar'),
Pear(30,
'baz'), Mango(40,
'qux')
final n: number
final s: string
def new(t: number, str: string)
this.n = t
this.s = str
enddef
endenum
defcompile
assert_equal([10, 'foo'], [Fruit.Apple.n, Fruit.Apple.s])
assert_equal([20, 'bar'], [Fruit.Orange.n, Fruit.Orange.s])
assert_equal([30, 'baz'], [Fruit.Pear.n, Fruit.Pear.s])
assert_equal([40, 'qux'], [Fruit.Mango.n, Fruit.Mango.s])
END
v9.CheckSourceSuccess(lines)
# specify multiple enums with constructor arguments in a single line
lines =<< trim END
vim9script
enum Fruit
Apple(10, 'foo'), Orange(20, 'bar'), Pear(30, 'baz'), Mango(40, 'qux')
const n: number
const s: string
endenum
defcompile
assert_equal([10, 'foo'], [Fruit.Apple.n, Fruit.Apple.s])
assert_equal([20, 'bar'], [Fruit.Orange.n, Fruit.Orange.s])
assert_equal([30, 'baz'], [Fruit.Pear.n, Fruit.Pear.s])
assert_equal([40, 'qux'], [Fruit.Mango.n, Fruit.Mango.s])
END
v9.CheckSourceSuccess(lines)
enddef
" Test for using class variables in an enum class
def Test_enum_class_variable()
var lines =<< trim END
vim9script
enum Fruit
Apple,
Orange
static var farm: string = 'SunValley'
endenum
assert_equal('SunValley', Fruit.farm)
END
v9.CheckSourceSuccess(lines)
enddef
" Test for converting a string to an enum value
def Test_enum_eval()
var lines =<< trim END
vim9script
enum Color
Red,
Blue
endenum
var e = eval('Color.Blue')
assert_equal(Color.Blue, e)
assert_equal(1, e.ordinal)
assert_fails("eval('Color.Green')", 'E1422: Enum value "Green" not found in enum "Color"')
assert_fails("var x = eval('Color')", 'E1421: Enum "Color" cannot be used as a value')
END
v9.CheckSourceSuccess(lines)
enddef
" Test for using "values" in an enum class variable
def Test_use_enum_values_in_class_variable()
var lines =<< trim END
vim9script
enum Dir
North, South
static const dirs: list<Dir> = Dir.values
endenum
assert_equal([Dir.North, Dir.South], Dir.dirs)
END
v9.CheckSourceSuccess(lines)
enddef
" Test for using lambda block in enums
def Test_lambda_block_in_enum()
# This used to crash Vim
var lines =<< trim END
vim9script
enum IdEnum1
ID1
const Id: func(number): number = (num: number): number => {
# Return a ID
return num / 2
}
endenum
assert_equal(5, IdEnum1.ID1.Id(10))
END
v9.CheckScriptSuccess(lines)
# This used to crash Vim
lines =<< trim END
vim9script
enum IdEnum2
ID1
static const Id: func(number): number = (num: number): number => {
# Return a ID
return num + 2
}
endenum
assert_equal(12, IdEnum2.Id(10))
END
v9.CheckScriptSuccess(lines)
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|