-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathScopeValidator.spec.ts
More file actions
7045 lines (6177 loc) · 251 KB
/
ScopeValidator.spec.ts
File metadata and controls
7045 lines (6177 loc) · 251 KB
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
import * as sinonImport from 'sinon';
import { DiagnosticMessages } from '../../DiagnosticMessages';
import { Program } from '../../Program';
import { expectDiagnostics, expectDiagnosticsIncludes, expectTypeToBe, expectZeroDiagnostics, trim } from '../../testHelpers.spec';
import { expect } from 'chai';
import type { TypeCompatibilityData } from '../../interfaces';
import { IntegerType } from '../../types/IntegerType';
import { StringType } from '../../types/StringType';
import type { BrsFile } from '../../files/BrsFile';
import { FloatType, InterfaceType, TypedFunctionType, VoidType, BooleanType, ArrayType } from '../../types';
import { SymbolTypeFlag } from '../../SymbolTypeFlag';
import { AssociativeArrayType } from '../../types/AssociativeArrayType';
import undent from 'undent';
import * as fsExtra from 'fs-extra';
import { tempDir, rootDir } from '../../testHelpers.spec';
import { isReturnStatement } from '../../astUtils/reflection';
import { ScopeValidator } from './ScopeValidator';
import type { ReturnStatement } from '../../parser/Statement';
import { Logger } from '@rokucommunity/logger';
describe('ScopeValidator', () => {
let sinon = sinonImport.createSandbox();
let program: Program;
beforeEach(() => {
fsExtra.emptyDirSync(tempDir);
program = new Program({
rootDir: rootDir
});
program.createSourceScope();
});
afterEach(() => {
sinon.restore();
program.dispose();
});
it('validateReturnStatement does not crash', () => {
program.options.autoImportComponentScript = true;
program.setFile('components/Component.xml', trim`
<component name="Test" extends="Group">
</component>
`);
const file = program.setFile<BrsFile>('components/Component.bs', trim`
function test()
return {
method: function()
return true
end function
}
end function
`);
const returnStatement = file.ast.findChild<ReturnStatement>(isReturnStatement);
delete returnStatement.parent;
const validator = new ScopeValidator();
//should not crash
validator['validateReturnStatement'](file, returnStatement);
});
describe('mismatchArgumentCount', () => {
it('detects calling functions with too many arguments', () => {
program.setFile('source/file.brs', `
sub a()
end sub
sub b()
a(1)
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(0, 1).message
]);
});
it('detects calling class constructors with too many arguments', () => {
program.setFile('source/main.bs', `
function noop0()
end function
function noop1(p1)
end function
sub main()
noop0(1)
noop1(1,2)
noop1()
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(0, 1),
DiagnosticMessages.mismatchArgumentCount(1, 2),
DiagnosticMessages.mismatchArgumentCount(1, 0)
]);
});
it('detects calling functions with too few arguments', () => {
program.setFile('source/file.brs', `
sub a(name)
end sub
sub b()
a()
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(1, 0)
]);
});
it('allows skipping optional parameter', () => {
program.setFile('source/file.brs', `
sub a(name="Bob")
end sub
sub b()
a()
end sub
`);
program.validate();
//should have an error
expectZeroDiagnostics(program);
});
it('shows expected parameter range in error message', () => {
program.setFile('source/file.brs', `
sub a(age, name="Bob")
end sub
sub b()
a()
end sub
`);
program.validate();
//should have an error
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount('1-2', 0)
]);
});
it('handles expressions as arguments to a function', () => {
program.setFile('source/file.brs', `
sub a(age, name="Bob")
end sub
sub b()
a("cat" + "dog" + "mouse")
end sub
`);
program.validate();
expectZeroDiagnostics(program);
});
it('Catches extra arguments for expressions as arguments to a function', () => {
program.setFile('source/file.brs', `
sub a(age)
end sub
sub b()
a(m.lib.movies[0], 1)
end sub
`);
program.validate();
//should have an error
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(1, 2)
]);
});
it('allows any number of parameters in a function used as an argument', () => {
program.setFile('source/file.brs', `
sub tryManyParams(someFunc as function)
someFunc(1, 2, "hello", "world")
end sub
`);
program.validate();
//should have no errors
expectZeroDiagnostics(program);
});
it('checks for at least the number of non-optional args on variadic (callFunc) functions', () => {
program.setFile('components/Widget.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="Widget" extends="Group">
<script uri="Widget.brs"/>
<interface>
<function name="someFunc" />
</interface>
</component>
`);
program.setFile('components/Widget.brs', `
sub someFunc(input as object)
print input
end sub
`);
program.setFile('source/util.bs', `
sub useCallFunc(input as roSGNodeWidget)
input.callFunc()
end sub
`);
program.validate();
//should have an error
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount('1-63', 0)
]);
});
it('any number number of args on variadic (callFunc) functions', () => {
program.setFile('components/Widget.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="Widget" extends="Group">
<script uri="Widget.brs"/>
<interface>
<function name="someFunc" />
</interface>
</component>
`);
program.setFile('components/Widget.brs', `
sub someFunc(input as object)
print input
end sub
`);
program.setFile('source/util.bs', `
sub useCallFunc(input as roSGNodeWidget, funcToCall as string)
input.callFunc(funcToCall, 1, 2, 3, {})
end sub
`);
program.validate();
// no error, because we can't know what function you're actually calling
expectZeroDiagnostics(program);
});
it('checks for target args count on callfunc', () => {
program.setFile('components/Widget.xml', trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="Widget" extends="Group">
<script uri="Widget.brs"/>
<interface>
<function name="someFunc" />
</interface>
</component>
`);
program.setFile('components/Widget.brs', `
sub someFunc(input as object)
print input
end sub
`);
program.setFile('source/util.bs', `
sub useCallFunc(input as roSGNodeWidget)
input.callFunc("someFunc", 1, 2, 3, {})
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(2, 5)
]);
});
it('validates against scope-defined func in inner namespace, when outer namespace has same named func', () => {
program.setFile('source/main.bs', `
namespace alpha
sub foo()
end sub
namespace beta
sub bar()
foo()
end sub
end namespace
end namespace
function foo(x as integer) as integer
return x
end function
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(1, 0).message
]);
});
it('validates against functions defined in intersection types', () => {
program.setFile('source/main.bs', `
interface IFirst
num as integer
end interface
interface ISecond
function doThing2(a as integer, b as string) as void
end interface
sub main(thing as IFirst and ISecond)
thing.doThing2(thing.num)
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(2, 1).message
]);
});
it('validates against typed functions types', () => {
program.setFile('source/main.bs', `
sub main(cb as function(num as integer, name as string) as void)
cb(1)
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(2, 1).message
]);
});
it('validates against typed functions types from type statements', () => {
program.setFile('source/main.bs', `
type Callback = function(num as integer, name as string) as void
sub main(cb as Callback)
cb(1)
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.mismatchArgumentCount(2, 1).message
]);
});
});
describe('argumentTypeMismatch', () => {
it('param `as object` supports all known types', () => {
program.setFile('source/file.bs', `
sub main()
consoleLog(Direction.up)
consoleLog(true)
consoleLog(main)
consoleLog(1.2)
consoleLog({} as Video)
consoleLog("test")
end sub
sub consoleLog(thing as object)
print thing
end sub
interface Video
url as string
end interface
enum Direction
up = "up"
down = "down"
end enum
`);
program.validate();
expectZeroDiagnostics(program);
});
it('`as object` var can be passed to various param types', () => {
program.setFile('source/file.bs', `
sub main()
obj = {} as object
printBoolean(obj)
printClass(obj)
printDouble(obj)
printEnum(obj)
printFloat(obj)
printFunction(obj)
printInteger(obj)
printInterface(obj)
printLongInteger(obj)
printString(obj)
end sub
sub printBoolean(value as boolean)
print value
end sub
class Person
name as string
end class
sub printClass(value as Person)
print value
end sub
sub printDouble(value as double)
print value
end sub
enum Direction
up = "up"
end enum
sub printEnum(value as Direction)
print value
end sub
sub printFloat(value as float)
print value
end sub
sub printFunction(value as function)
print value
print value(1)
end sub
interface Video
url as string
end interface
sub printInterface(value as Video)
print value
end sub
sub printInteger(value as integer)
print value
end sub
sub printLongInteger(value as LongInteger)
print value
end sub
sub printString(value as string)
print value
end sub
`);
program.validate();
expectZeroDiagnostics(program);
});
it('treats string enums as strings when assigned to string vars', () => {
program.setFile('source/file.bs', `
sub main()
printDirection(Direction.up)
end sub
sub printDirection(theDirection as string)
print theDirection
end sub
enum Direction
up = "up"
down = "down"
end enum
`);
program.validate();
expectZeroDiagnostics(program);
});
it('does not treat strings as a string enum', () => {
program.setFile('source/file.bs', `
sub main()
printDirection("up")
end sub
sub printDirection(theDirection as Direction)
print theDirection
end sub
enum Direction
up = "up"
down = "down"
end enum
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.argumentTypeMismatch('string', 'Direction').message
]);
});
it('supports passing enum type as enum type', () => {
program.setFile('source/file.bs', `
sub test(theDirection as Direction)
printDirection(theDirection)
end sub
sub printDirection(theDirection as Direction)
print theDirection
end sub
enum Direction
up = "up"
down = "down"
end enum
`);
program.validate();
expectDiagnostics(program, [
]);
});
it('Catches argument type mismatches on function calls', () => {
program.setFile('source/file.brs', `
sub a(age as integer)
end sub
sub b()
a("hello")
end sub
`);
program.validate();
//should have an error
expect(program.getDiagnostics().map(x => x.message)).to.include(
DiagnosticMessages.argumentTypeMismatch('string', 'integer').message
);
});
it('Catches argument type mismatches on function calls for functions defined in another file', () => {
program.setFile('source/file.brs', `
sub a(age as integer)
end sub
`);
program.setFile('source/file2.brs', `
sub b()
a("hello")
foo = "foo"
a(foo)
end sub
`);
program.validate();
//should have an error
expect(program.getDiagnostics().map(x => x.message)).to.include(
DiagnosticMessages.argumentTypeMismatch('string', 'integer').message
);
});
it('catches argument type mismatches on function calls within namespaces', () => {
program.setFile('source/file.bs', `
namespace Name.Space
sub a(param as integer)
print param
end sub
sub b()
a("hello")
foo = "foo"
a(foo)
end sub
end namespace
`);
program.validate();
//should have an error
expect(program.getDiagnostics().map(x => x.message)).to.include(
DiagnosticMessages.argumentTypeMismatch('string', 'integer').message
);
});
it('catches argument type mismatches on function calls as arguments', () => {
program.setFile('source/file1.bs', `
sub a(param as string)
print param
end sub
function getNum() as integer
return 1
end function
sub b()
a(getNum())
end sub
`);
program.validate();
//should have an error
expect(program.getDiagnostics().map(x => x.message)).to.include(
DiagnosticMessages.argumentTypeMismatch('integer', 'string').message
);
});
it('catches argument type mismatches on function calls within namespaces across files', () => {
program.setFile('source/file1.bs', `
namespace Name.Space
function getNum() as integer
return 1
end function
function getStr() as string
return "hello"
end function
end namespace
`);
program.setFile('source/file2.bs', `
namespace Name.Space
sub needsInt(param as integer)
print param
end sub
sub someFunc()
needsInt(getStr())
needsInt(getNum())
end sub
end namespace
`);
program.validate();
//should have an error
expect(program.getDiagnostics().length).to.equal(1);
expect(program.getDiagnostics().map(x => x.message)).to.include(
DiagnosticMessages.argumentTypeMismatch('string', 'integer').message
);
});
it('correctly validates correct parameters that are class members', () => {
program.setFile('source/main.bs', `
class PiHolder
pi = 3.14
function getPi() as float
return m.pi
end function
end class
sub takesFloat(fl as float)
end sub
sub someFunc()
holder = new PiHolder()
takesFloat(holder.pi)
takesFloat(holder.getPI())
end sub`);
program.validate();
//should have no error
expectZeroDiagnostics(program);
});
it('correctly validates wrong parameters that are class members', () => {
program.setFile('source/main.bs', `
class PiHolder
pi = 3.14
name = "hello"
function getPi() as float
return m.pi
end function
end class
sub takesFloat(fl as float)
end sub
sub someFunc()
holder = new PiHolder()
takesFloat(holder.name)
takesFloat(Str(holder.getPI()))
end sub`);
program.validate();
//should have error: holder.name is string
expect(program.getDiagnostics().length).to.equal(2);
expect(program.getDiagnostics().map(x => x.message)).to.include(
DiagnosticMessages.argumentTypeMismatch('string', 'float').message
);
});
it('correctly validates correct parameters that are interface members', () => {
program.setFile('source/main.bs', `
interface IPerson
height as float
name as string
function getWeight() as float
function getAddress() as string
end interface
sub takesFloat(fl as float)
end sub
sub someFunc(person as IPerson)
takesFloat(person.height)
takesFloat(person.getWeight())
end sub`);
program.validate();
//should have no error
expectZeroDiagnostics(program);
});
it('correctly validates wrong parameters that are interface members', () => {
program.setFile('source/main.bs', `
interface IPerson
isAlive as boolean
function getAddress() as string
end interface
sub takesFloat(fl as float)
end sub
sub someFunc(person as IPerson)
takesFloat(person.isAlive)
takesFloat(person.getAddress())
end sub
`);
program.validate();
//should have 2 errors: person.name is string (not float) and person.getAddress() is object (not float)
expectDiagnostics(program, [
DiagnosticMessages.argumentTypeMismatch('boolean', 'float').message,
DiagnosticMessages.argumentTypeMismatch('string', 'float').message
]);
});
it('`as object` param allows all types', () => {
program.setFile('source/main.bs', `
sub takesObject(obj as Object)
end sub
sub main()
takesObject(true)
takesObject(1)
takesObject(1.2)
takesObject(1.2#)
takesObject("text")
takesObject({})
takesObject([])
end sub
`);
program.validate();
expectZeroDiagnostics(program);
});
it('allows conversions for arguments', () => {
program.setFile('source/main.bs', `
sub takesFloat(fl as float)
end sub
sub someFunc()
takesFloat(1)
end sub`);
program.validate();
//should have no error
expectZeroDiagnostics(program);
});
it('allows subclasses as arguments', () => {
program.setFile('source/main.bs', `
class Animal
end class
class Dog extends Animal
end class
class Retriever extends Dog
end class
class Lab extends Retriever
end class
sub takesAnimal(thing as Animal)
end sub
sub someFunc()
fido = new Lab()
takesAnimal(fido)
end sub`);
program.validate();
//should have no error
expectZeroDiagnostics(program);
});
it('allows subclasses from namespaces as arguments', () => {
program.setFile('source/main.bs', `
class Outside
end class
class ChildOutExtendsInside extends NS.Inside
end class
namespace NS
class Inside
end class
class ChildInExtendsOutside extends Outside
end class
class ChildInExtendsInside extends Inside
sub methodTakesInside(i as Inside)
end sub
end class
sub takesInside(klass as Inside)
end sub
sub testFuncInNamespace()
takesOutside(new Outside())
takesOutside(new NS.ChildInExtendsOutside())
' These call NS.takesInside
takesInside(new NS.Inside())
takesInside(new Inside())
takesInside(new NS.ChildInExtendsInside())
takesInside(new ChildInExtendsInside())
takesInside(new ChildOutExtendsInside())
child = new ChildInExtendsInside()
child.methodTakesInside(new Inside())
child.methodTakesInside(new ChildInExtendsInside())
child.methodTakesInside(new ChildOutExtendsInside())
end sub
end namespace
sub takesOutside(klass as Outside)
end sub
sub takesInside(klass as NS.Inside)
end sub
sub testFunc()
takesOutside(new Outside())
takesOutside(new NS.ChildInExtendsOutside())
takesInside(new NS.Inside())
takesInside(new NS.ChildInExtendsInside())
takesInside(new ChildOutExtendsInside())
NS.takesInside(new NS.Inside())
NS.takesInside(new NS.ChildInExtendsInside())
NS.takesInside(new ChildOutExtendsInside())
child = new NS.ChildInExtendsInside()
child.methodTakesInside(new NS.Inside())
child.methodTakesInside(new NS.ChildInExtendsInside())
child.methodTakesInside(new ChildOutExtendsInside())
end sub`);
program.validate();
//should have no error
expectZeroDiagnostics(program);
});
it('respects union types', () => {
program.setFile('source/main.bs', `
sub takesStringOrKlass(p as string or Klass)
end sub
class Klass
end class
sub someFunc()
myKlass = new Klass()
takesStringOrKlass("test")
takesStringOrKlass(myKlass)
takesStringOrKlass(1)
end sub`);
program.validate();
//should have error when passed an integer
expect(program.getDiagnostics().length).to.equal(1);
expectDiagnostics(program, [
DiagnosticMessages.argumentTypeMismatch('integer', 'string or Klass').message
]);
});
it('validates functions assigned to variables', () => {
program.setFile('source/main.bs', `
sub someFunc()
myFunc = function(i as integer, s as string)
print i+1
print s.len()
end function
myFunc("hello", 2)
end sub`);
program.validate();
//should have error when passed incorrect types
expectDiagnostics(program, [
DiagnosticMessages.argumentTypeMismatch('string', 'integer').message,
DiagnosticMessages.argumentTypeMismatch('integer', 'string').message
]);
});
it('allows any parameter types in a function passed as an argument', () => {
program.setFile('source/file.brs', `
function getStrLength(name as string) as integer
return len(name)
end function
sub tryManyParams(someFunc as function)
print someFunc(1, 2, "hello", "world")
end sub
sub test()
tryManyParams(getStrLength)
end sub
`);
program.validate();
//should have no errors
expectZeroDiagnostics(program);
});
it('allows a inline function as an argument of type function', () => {
program.setFile('source/file.brs', `
sub tryManyParams(someFunc as function)
print someFunc(1, 2, "hello", "world")
end sub
sub test()
tryManyParams(sub (i as integer)
print i
end sub)
end sub
`);
program.validate();
//should have no errors
expectZeroDiagnostics(program);
});
it('validates when a non-function is used as an argument expecting a function', () => {
program.setFile('source/file.brs', `
sub tryManyParams(someFunc as function)
print someFunc(1, 2, "hello", "world")
end sub
sub test()
notAFunction = 3.14
tryManyParams(notAFunction)
end sub
`);
program.validate();
//should have an error that the argument is not a function
expectDiagnostics(program, [
DiagnosticMessages.argumentTypeMismatch('float', 'function').message
]);
});
it('allows a class constructor to be passed as arg to param typed `as function`', () => {
program.setFile('source/file.bs', `
sub callSomeFunc(someFunc as function)
someFunc()
end sub
class MyKlass
end class
sub doStuff()
callSomeFunc(MyKlass)
end sub
`);
program.validate();
expectZeroDiagnostics(program);
});
it('allows a namespaced class constructor to be passed as arg to param typed `as function`', () => {
program.setFile('source/file.bs', `
sub callSomeFunc(someFunc as function)
someFunc()
end sub
namespace Alpha
class MyKlass
end class
sub doStuff()
callSomeFunc(MyKlass)
end sub
end namespace
`);
program.validate();
expectZeroDiagnostics(program);
});
it('allows any variable to passed as argument to an untyped param with default type invalid', () => {
program.setFile('source/util.brs', `
sub doSomething(x = invalid)
print x
end sub
sub tests()
doSomething(1)
doSomething(1.1)
doSomething("Hello")
doSomething(true)
doSomething({test: true})
end sub
`);
program.validate();
//should have no errors
expectZeroDiagnostics(program);
});
it('allows calling future function and save to same variable', () => {
program.setFile('source/util.brs', `
function getSomeInt() as integer
numVal = getUntypedNum()
numVal = cInt(numVal)
return numVal
end function
function getUntypedNum()
return 1
end function
`);
program.validate();
//should have no errors
expectZeroDiagnostics(program);
});
it('allows union types of all compatible types as arg', () => {
program.setFile('source/util.bs', `
sub printIntNum(num as float or double or integer)
print cInt(num)
end sub
`);
program.validate();
//should have no errors
expectZeroDiagnostics(program);
});
it('allows function calls of built-in members of primitives', () => {
program.setFile('source/util.brs', `