forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypesystemTests.swift
More file actions
1334 lines (1166 loc) · 41.5 KB
/
TypesystemTests.swift
File metadata and controls
1334 lines (1166 loc) · 41.5 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
//
// TypesystemTests.swift
// ShapeScriptTests
//
// Created by Nick Lockwood on 19/05/2022.
// Copyright © 2022 Nick Lockwood. All rights reserved.
//
@testable import ShapeScript
import XCTest
private func expressionType(_ source: String) throws -> ValueType {
let program = try parse(source)
let context = EvaluationContext(source: "", delegate: nil)
do {
try program.evaluate(in: context)
} catch let error as RuntimeError {
switch error.type {
case .fileNotFound, .unusedValue:
break
default:
throw error
}
}
return try program.statements.last?.staticType(in: context) ?? .void
}
private func symbol(for name: String? = nil, in definition: String) throws -> Symbol? {
let program = try parse(definition)
let context = EvaluationContext(source: "", delegate: nil)
try program.evaluate(in: context)
if let name {
return context.symbol(for: name)
}
guard case let .define(identifier, _) = program.statements.last?.type else {
return nil
}
return context.symbol(for: identifier.name)
}
private func functionType(
for name: String? = nil,
in definition: String,
file: StaticString = #file,
line: UInt = #line
) throws -> FunctionType {
guard case let .function(functionType, _) = try symbol(for: name, in: definition) else {
XCTFail("Function definition not found", file: file, line: line)
return (.any, .any)
}
return functionType
}
private func blockType(
for name: String? = nil,
in definition: String,
file: StaticString = #file,
line: UInt = #line
) throws -> BlockType {
guard case let .block(blockType, _) = try symbol(for: name, in: definition) else {
XCTFail("Block definition not found", file: file, line: line)
return .init([:], [:], .any, .any)
}
return blockType
}
private func evaluate(
_ source: String,
as type: ValueType = .any,
file: StaticString = #file,
line: UInt = #line
) throws -> Value {
var lines = source.split(separator: "\n")
lines[lines.count - 1] = "define foo_ \(lines[lines.count - 1])"
let source = lines.joined(separator: "\n")
let program = try parse(source)
guard case let .define(_, definition) = program.statements.last?.type,
case let .expression(expression) = definition.type
else {
XCTFail("Expression not found", file: file, line: line)
return .void
}
let delegate = TestDelegate()
let context = EvaluationContext(source: source, delegate: delegate)
try program.evaluate(in: context)
return try expression.evaluate(as: type, for: "", in: context)
}
final class TypesystemTests: XCTestCase {
// MARK: Static type
func testNumericLiteralType() throws {
XCTAssertEqual(try expressionType("5"), .number)
}
func testArithmeticExpressionType() {
XCTAssertEqual(try expressionType("1 + 2"), .number)
}
func testArithmeticExpressionType2() {
XCTAssertEqual(try expressionType("(1 + 2) * 3"), .number)
}
func testArithmeticExpressionType3() {
XCTAssertEqual(try expressionType("\"1\" + 2"), .number)
}
func testVectorExpressionType() {
XCTAssertEqual(try expressionType("(1 2) * 3"), .list(.number))
}
func testVectorExpressionType2() {
XCTAssertEqual(try expressionType("2 + (3 4)"), .list(.number))
}
func testVectorExpressionType3() {
XCTAssertEqual(try expressionType("(\"1\" \"2\") * 3"), .list(.number))
}
func testBooleanLiteral() {
XCTAssertEqual(try expressionType("true"), .boolean)
}
func testBooleanExpression() {
XCTAssertEqual(try expressionType("1 < 2"), .boolean)
}
func testStringLiteralType() {
XCTAssertEqual(try expressionType("\"foo\""), .string)
}
func testNumericTupleExpressionType() throws {
// Note: interpreted as list instead of tuple to improve option inference
XCTAssertEqual(try expressionType("1 5"), .list(.number))
}
func testNumericTupleExpressionType2() throws {
// TODO: seems like we can do something better here?
XCTAssertEqual(try expressionType("(pi 5)"), .list(.any))
}
func testNumericTupleExpressionType3() throws {
XCTAssertEqual(try expressionType("(1 5)"), .list(.number))
}
func testMixedTupleExpressionType() throws {
// TODO: should this be a tuple or color list instead?
XCTAssertEqual(try expressionType("1 red"), .list(.any))
}
func testBlockExpressionType() {
XCTAssertEqual(try expressionType("cube"), .mesh)
}
func testBlockExpressionType2() {
XCTAssertEqual(try expressionType("cube { size 1 }"), .mesh)
}
func testBlockExpressionType3() {
XCTAssertEqual(try expressionType("square"), .path)
}
func testFunctionExpressionType() {
XCTAssertEqual(try expressionType("rnd"), .number)
}
func testFunctionExpressionType2() {
XCTAssertEqual(try expressionType("(cos pi)"), .number)
}
func testFunctionExpressionType3() {
XCTAssertEqual(try expressionType("cos(pi)"), .number)
}
func testCustomBlockReturnType() {
XCTAssertEqual(try expressionType("""
define foo { 5 }
foo
"""), .number)
}
func testCustomBlockExpressionReturnType() {
XCTAssertEqual(try expressionType("""
define foo { 5 + 1 }
foo
"""), .number)
}
func testCustomBlockDefineReturnType() {
XCTAssertEqual(try expressionType("""
define foo {
define bar "hello"
bar
}
foo
"""), .string)
}
func testCustomBlockOptionReturnType() {
XCTAssertEqual(try expressionType("""
define foo {
option bar "hello"
bar
}
foo { bar "goodbye" }
"""), .string)
}
func testCustomBlockCallReturnType() {
XCTAssertEqual(try expressionType("""
define foo {
define bar "hello"
bar
}
define bar {
define baz foo()
"dog" baz
}
bar
"""), .list(.string))
}
func testCustomBlockConditionalReturnType() {
XCTAssertEqual(try expressionType("""
define foo {
option bar true
if bar {
"Hello"
} else {
55
}
}
foo { bar true }
"""), .union([.string, .number]))
}
func testCustomBlockLoopReturnType() {
XCTAssertEqual(try expressionType("""
define foo {
option count 1
for i in 1 to count {
i
}
}
foo { count 5 }
"""), .list(.number))
}
func testCustomBlockRecursiveReturnType() {
XCTAssertEqual(try expressionType("""
define foo {
option count 1
if count > 0 {
count
foo { count count - 1 }
}
}
foo { count 5 }
"""), .list(.any)) // TODO: .list(.number)
}
func testCustomFunctionReturnType() {
XCTAssertEqual(try expressionType("""
define foo() { 5 + 1 }
foo
"""), .number)
}
func testCustomFunctionReturnType2() {
XCTAssertEqual(try expressionType("""
define foo(bar) { bar + 1 }
foo(1)
"""), .number)
}
func testCustomFunctionReturnType3() {
XCTAssertEqual(try expressionType("""
define foo(bar baz) { bar < baz }
(foo 1 2)
"""), .boolean)
}
func testPropertySetterReturnsVoid() {
XCTAssertEqual(try expressionType("""
define foo() { color red }
foo()
"""), .void)
}
func testColorPropertyMemberType() {
XCTAssertEqual(try expressionType("color.red"), .number)
}
func testBlockMemberType() {
XCTAssertEqual(try expressionType("cube.bounds"), .bounds)
}
func testCustomConstantMemberType() {
XCTAssertEqual(try expressionType("""
define foo red
foo.green
"""), .number)
}
func testCustomBlockResultMemberType() {
XCTAssertEqual(try expressionType("""
define foo { cube }
foo.bounds
"""), .bounds)
}
func testCustomFunctionResultMemberType() {
XCTAssertEqual(try expressionType("""
define foo() { red }
foo.blue
"""), .number)
}
func testEmptyTupleCountType() {
XCTAssertEqual(try expressionType("""
define foo ()
foo.count
"""), .number)
}
func testUnionMemberType() {
XCTAssertEqual(try expressionType("""
define foo () {
if rnd > 0.5 {
(1 2 1)
} else {
"#f00"
}
}
foo.blue
"""), .number)
}
func testPolygonsMemberType() {
XCTAssertEqual(try expressionType("""
cube.polygons
"""), .list(.polygon))
}
func testPolygonPointsMemberType() {
XCTAssertEqual(try expressionType("""
cube.polygons.first.points
"""), .list(.point))
}
func testPointColorMemberType() {
XCTAssertEqual(try expressionType("""
(square { color red }).points.first.color
"""), .optional(.color))
}
func testPointColorMemberType2() {
XCTAssertEqual(try expressionType("""
square.points.first.color
"""), .optional(.color))
}
func testPointCurvedMemberType() {
XCTAssertEqual(try expressionType("""
square.points.first.isCurved
"""), .boolean)
}
func testImportShapeType() {
XCTAssertEqual(try expressionType("""
import "foo.shape"
"""), .any)
}
func testImportModelType() {
XCTAssertEqual(try expressionType("""
import "foo.obj"
"""), .mesh)
}
func testImportModelType2() {
XCTAssertEqual(try expressionType("""
define foo {
import "foo.obj"
}
foo
"""), .mesh)
}
func testImportTextType() {
XCTAssertEqual(try expressionType("""
import "foo.txt"
"""), .string)
}
func testImportJSONType() {
XCTAssertEqual(try expressionType("""
import "foo.json"
"""), .any)
}
// MARK: Function parameter inference
func testInferSimpleFunctionParameter() throws {
let type = try functionType(in: "define foo(bar) { bar + 1 }")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector]))
XCTAssertEqual(type.returnType, .number)
}
func testInferSimpleFunctionParameters() throws {
let type = try functionType(in: "define foo(bar baz) { bar + baz }")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector, .numberOrVector]))
XCTAssertEqual(type.returnType, .number)
}
func testInferFunctionParameterInBlock() throws {
let type = try functionType(in: """
define foo(bar) {
cube {
color bar
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.color]))
XCTAssertEqual(type.returnType, .mesh)
}
func testInferFunctionParameterUsedInImport() throws {
let type = try functionType(in: """
define foo(bar) {
import bar
}
""")
XCTAssertEqual(type.parameterType, .tuple([.string]))
XCTAssertEqual(type.returnType, .any)
}
func testComplexNestedFunctionParameters() throws {
let type = try functionType(in: """
define foo(bar) { bar + 1 }
define bar(baz quux) {
foo(baz) + 1
print quux
}
""")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector, .list(.any)]))
XCTAssertEqual(type.returnType, .number)
}
func testConditionalFunctionParameter() throws {
let type = try functionType(in: """
define foo(bar) {
if bar {
"hello"
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.boolean]))
XCTAssertEqual(type.returnType, .union([.string, .void]))
}
func testConditionalFunctionParameter2() throws {
let type = try functionType(in: """
define foo(bar) {
if bar < 3 {
"hello"
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.number]))
XCTAssertEqual(type.returnType, .union([.string, .void]))
}
func testConditionalFunctionParameters() throws {
let type = try functionType(in: """
define foo(bar baz) {
if bar = baz {
bar + 1
} else {
print baz
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector, .list(.any)]))
XCTAssertEqual(type.returnType, .union([.number, .void]))
}
func testConditionalFunctionParameters2() throws {
let type = try functionType(in: """
define foo(bar baz) {
if bar > 1 {
print bar + 1
} else {
print not baz
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.number, .any]))
XCTAssertEqual(type.returnType, .void)
}
func testConditionalFunctionParameters3() throws {
let type = try functionType(in: """
define foo(bar baz) {
if baz > 1 {
print bar
} else {
print bar + 1
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.union([.number, .radians, .list(.any)]), .number]))
XCTAssertEqual(type.returnType, .void)
}
func testConditionalFunctionParameters4() throws {
let type = try functionType(in: """
define foo(bar) {
if true {
print bar + 1
texture bar
} else {
print bar + 1
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.union([.numberOrVector, .texture]).simplified()]))
XCTAssertEqual(type.returnType, .void)
}
func testFunctionParameterInferenceInsideTuple() throws {
let type = try functionType(in: """
define foo(bar baz) {
(bar and baz 2)
}
""")
XCTAssertEqual(type.parameterType, .tuple([.boolean, .boolean]))
}
func testFunctionParameterInferenceInsideMember() throws {
let type = try functionType(in: """
define foo(bar) {
(bar + 3 2).count
}
""")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector]))
}
func testFunctionParameterInferenceInsideSubscript() throws {
let type = try functionType(in: """
define foo(bar baz) {
(-bar 2)[baz]
}
""")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector, .union([.number, .string])]))
}
func testFunctionParameterInferenceInsideNestedDefine() throws {
let type = try functionType(in: """
define foo(bar) {
define baz bar + 1
}
""")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector]))
}
func testFunctionParameterInferenceInsideNestedFunctionDefine() throws {
let type = try functionType(in: """
define foo(bar) {
define baz() {
bar + 1
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector]))
}
func testFunctionParameterInferenceInsideNestedBlockDefine() throws {
let type = try functionType(in: """
define foo(bar) {
define baz() {
bar + 1
}
}
""")
XCTAssertEqual(type.parameterType, .tuple([.numberOrVector]))
}
func testErrorThrowingArgumentHasCorrectRange() throws {
let name = "Nope.jpg"
let program = try parse("""
define foo(bar baz) {
if baz {
texture bar
}
}
foo("\(name)" true)
""")
let range = program.source.range(of: "\"\(name)\"")
let context = EvaluationContext(source: "", delegate: nil)
XCTAssertThrowsError(try program.evaluate(in: context)) { error in
let error = try? XCTUnwrap(error as? RuntimeError)
XCTAssertEqual(error?.type, .fileNotFound(for: name, at: nil))
XCTAssertEqual(error?.range, range)
}
}
func testFunctionWithForwardReferenceToConstant() throws {
let type = try functionType(for: "foo", in: """
define foo() {
bar
}
define bar 3
""")
XCTAssertEqual(type.parameterType, .void)
XCTAssertEqual(type.returnType, .number)
}
func testFunctionWithForwardReferenceToFunction() throws {
let type = try functionType(for: "foo", in: """
define foo() {
bar
}
define bar() {
3
}
""")
XCTAssertEqual(type.parameterType, .void)
XCTAssertEqual(type.returnType, .number)
}
func testFunctionWithNestedForwardReferenceToFunction() throws {
let type = try functionType(for: "foo", in: """
define foo() {
bar
}
define bar() {
baz
}
define baz() {
3
}
""")
XCTAssertEqual(type.parameterType, .void)
// TODO: this should actually be a number
XCTAssertEqual(type.returnType, .any)
}
// MARK: Block type inference
func testEmptyBlock() throws {
let type = try blockType(in: """
define foo {}
""")
XCTAssertEqual(type.childTypes, .void)
XCTAssertEqual(type.returnType, .void)
}
func testBlockPrintingChildren() throws {
let type = try blockType(in: """
define foo {
print children
}
""")
XCTAssertEqual(type.childTypes, .any)
XCTAssertEqual(type.returnType, .void)
}
func testBlockWithChildMeshes() throws {
let type = try blockType(in: """
define foo {
union children
}
""")
XCTAssertEqual(type.childTypes, .mesh)
XCTAssertEqual(type.returnType, .mesh)
}
func testBlockWithMixedChildTypes() throws {
let input = """
define foo {
define foo children + 1
union children
}
"""
let type = try blockType(in: input)
XCTAssertEqual(type.childTypes, .union([.mesh, .numberOrVector]).simplified())
XCTAssertEqual(type.returnType, .mesh)
// No arguments
XCTAssertThrowsError(try evaluate("\(input)\nfoo")) { error in
let error = try? XCTUnwrap(error as? RuntimeError)
XCTAssertEqual(error?.message, "Missing argument")
XCTAssertEqual(error?.hint, """
The 'foo' symbol expects an argument of type mesh, number, vector, or block.
""")
}
// Empty block argument
XCTAssertEqual(try evaluate("\(input)\nfoo {}").type, .mesh)
// Empty tuple argument
XCTAssertEqual(try evaluate("\(input)\nfoo ()").type, .mesh)
// Mesh argument
XCTAssertThrowsError(try evaluate("\(input)\nfoo cube")) { error in
let error = try? XCTUnwrap(error as? RuntimeError)
XCTAssertEqual(error?.message, "Type mismatch")
XCTAssertEqual(error?.hint, """
The left operand for '+' should be a number or vector, not a mesh.
""")
}
// Numeric argument
XCTAssertThrowsError(try evaluate("\(input)\nfoo 1")) { error in
let error = try? XCTUnwrap(error as? RuntimeError)
XCTAssertEqual(error?.message, "Type mismatch")
XCTAssertEqual(error?.hint, """
The argument for 'union' should be a mesh or block, not a number.
""")
}
}
func testBlockWithIncompatibleChild() throws {
let input = """
define foo {
define objects children cube
union objects
}
"""
let type = try blockType(in: input)
XCTAssertEqual(type.childTypes, .any)
XCTAssertEqual(type.returnType, .mesh)
XCTAssertThrowsError(try evaluate("\(input)\nfoo 1")) { error in
let error = try? XCTUnwrap(error as? RuntimeError)
XCTAssertEqual(error?.message, "Type mismatch")
XCTAssertEqual(error?.hint, """
The argument for 'union' should be a mesh or block, not a tuple.
""")
}
}
// MARK: Type unions
func testTypeUnionIsOrderIndependent() {
let type = ValueType.union([.string, .number])
XCTAssertEqual(type, .union([.number, .string]))
}
func testSimpleTypeUnion() {
let type = ValueType.string.union(.number)
XCTAssertEqual(type, .union([.string, .number]))
}
func testSubtypeUnion() {
let type = ValueType.string.union(.any)
XCTAssertEqual(type, .any)
}
func testSubtypeUnion2() {
let type = ValueType.any.union(.number)
XCTAssertEqual(type, .any)
}
func testSimpleTypeUnionWithUnion() {
var type = ValueType.string.union(.number)
type.formUnion(.boolean)
XCTAssertEqual(type, .union([.string, .number, .boolean]))
}
func testMergeSimpleTypeUnions() {
var type = ValueType.union([.string, .number])
type.formUnion(.union([.number, .boolean]))
XCTAssertEqual(type, .union([.string, .number, .boolean]))
}
func testMergeTypeUnionsWithSubtypes() {
var type = ValueType.union([.number, .any])
type.formUnion(.union([.number, .boolean]))
XCTAssertEqual(type, .any)
}
func testUnionSubtypeSimplification() {
XCTAssertEqual(ValueType.union([.number, .any]).simplified(), .any)
XCTAssertEqual(ValueType.union([.any, .boolean]).simplified(), .any)
XCTAssertEqual(ValueType.union([.list(.any), .list(.number)]).simplified(), .list(.any))
}
func testNestedUnionSimplification() {
XCTAssertEqual(
ValueType.union([.boolean, .union([.number, .string])]).simplified(),
.union([.boolean, .number, .string])
)
XCTAssertEqual(
ValueType.list(.union([.number, .any])).simplified(),
.list(.any)
)
XCTAssertEqual(
ValueType.tuple([.union([.number, .any]), .union([.any, .boolean])]).simplified(),
.tuple([.any, .any])
)
XCTAssertEqual(
ValueType.object(["foo": .union([.number, .any]), "bar": .union([.any, .boolean])]).simplified(),
.object(["foo": .any, "bar": .any])
)
}
// MARK: Type conversion
func testCastNumberToNumberTuple() {
XCTAssert(Value(1).isConvertible(to: .tuple([.number])))
XCTAssertEqual(try evaluate("1", as: .tuple([.number])), [.number(1)])
}
func testCastNumberToNumberList() {
XCTAssert(Value(1).isConvertible(to: .list(.number)))
XCTAssertEqual(try evaluate("1", as: .list(.number)), [.number(1)])
}
func testCastVectorToSizeAndViceVersa() {
XCTAssert(Value.vector(.one).isConvertible(to: .size))
XCTAssert(Value.size(.one).isConvertible(to: .vector))
XCTAssertEqual(try evaluate("cube.bounds.size", as: .vector), .vector(.one))
}
func testCastNumberToColor() {
XCTAssert(Value(1).isConvertible(to: .color))
XCTAssertEqual(try evaluate("1", as: .color), .color(.white))
}
func testCastNumberToColorList() {
XCTAssert(Value(1).isConvertible(to: .list(.color)))
XCTAssertEqual(try evaluate("1", as: .list(.color)), [.color(.white)])
}
func testCastNumericCoupletToColor() {
XCTAssert(Value(1, 0.5).isConvertible(to: .color))
XCTAssertEqual(
try evaluate("1 0.5", as: .color),
.color(Color.white.withAlpha(0.5))
)
}
func testCastNumericTripletToColor() {
XCTAssert(Value(1, 0.5, 0.1).isConvertible(to: .color))
XCTAssertEqual(
try evaluate("1 0.5 0.1", as: .color),
.color(Color(1, 0.5, 0.1))
)
}
func testCastNumericQuadrupletToColor() {
XCTAssert(Value(1, 0.5, 0.1, 0.2).isConvertible(to: .color))
XCTAssertEqual(
try evaluate("1 0.5 0.1 0.2", as: .color),
.color(Color(1, 0.5, 0.1, 0.2))
)
}
func testCastNumericQuintupletToColor() {
XCTAssertFalse(Value(1, 0.5, 0.1, 0.2, 0.3).isConvertible(to: .color))
XCTAssertThrowsError(try evaluate("1 0.5 0.1 0.2 0.3", as: .color)) { error in
let error = try? XCTUnwrap(error as? RuntimeError)
XCTAssertEqual(error?.type, .unexpectedArgument(for: "", max: 4))
}
}
func testCastEmptyTupleToColor() {
XCTAssertFalse(Value([]).isConvertible(to: .color))
XCTAssertThrowsError(try evaluate("()", as: .color)) { error in
let error = try? XCTUnwrap(error as? RuntimeError)
XCTAssertEqual(error?.type, .typeMismatch(
for: "", expected: "color", got: "empty tuple"
))
}
}
func testCastColorWithAlphaToColor() {
XCTAssert(Value(.color(.red), 0.5).isConvertible(to: .color))
XCTAssertEqual(
try evaluate("red 0.5", as: .color),
.color(Color.red.withAlpha(0.5))
)
}
func testCastColorToNumberList() {
XCTAssert(Value.color(.red).isConvertible(to: .list(.number)))
XCTAssertEqual(try evaluate("red", as: .list(.number)), [1, 0, 0, 1])
}
func testCastColorWithAlphaToNumberList() {
XCTAssert(Value(.color(.red), 0.5).isConvertible(to: .list(.number)))
XCTAssertEqual(
try evaluate("red 0.5", as: .list(.number)),
[1, 0, 0, 0.5]
)
}
func testCastNumericCoupletToNumberList() {
XCTAssert(Value(1, 0.5).isConvertible(to: .list(.number)))
XCTAssertEqual(try evaluate("1 0.5", as: .list(.number)), [1, 0.5])
}
func testCastNumericCoupletToAnyList() {
XCTAssert(Value(1, 0.5).isConvertible(to: .list(.any)))
XCTAssertEqual(try evaluate("1 0.5", as: .list(.any)), [1, 0.5])
}
func testCastMixedTupleToStringList() {
XCTAssert(Value("foo", 0.5, true).isConvertible(to: .list(.string)))
XCTAssertEqual(
try evaluate("\"foo\" 0.5 true", as: .list(.string)),
["foo", "0.5", "true"]
)
}
func testCastNumericCoupletToNumberTuple() {
let type = ValueType.tuple([.number, .number])
XCTAssert(Value(1, 0.5).isConvertible(to: type))
XCTAssertEqual(try evaluate("1 0.5", as: type), [1, 0.5])
}
func testCastNumericCoupletToAnyTuple() {
let type = ValueType.tuple([.any, .any])
XCTAssert(Value(1, 0.5).isConvertible(to: type))
XCTAssertEqual(try evaluate("1 0.5", as: type), [1, 0.5])
}
func testCastMixedTupleToMixedTuple() {
let type = ValueType.tuple([.string, .number, .boolean])
XCTAssert(Value("foo", 0.5, true).isConvertible(to: type))
XCTAssertEqual(
try evaluate("\"foo\" 0.5 true", as: type),
["foo", 0.5, true]
)
}
func testCastMixedTupleToStringTuple() {
let type = ValueType.tuple([.string, .string, .string])
XCTAssert(Value("foo", 0.5, true).isConvertible(to: type))
XCTAssertEqual(
try evaluate("\"foo\" 0.5 true", as: type),
["foo", "0.5", "true"]
)
}
func testCastMixedTupleToString() {
XCTAssert(Value("foo", 0.5, true).isConvertible(to: .string))
XCTAssertEqual(
try evaluate("\"foo\" 0.5 true", as: .string),
"foo0.5 true"
)
}
func testCastMixedNestedTupleToString() {
XCTAssert(Value("foo", Value(0.5, 1), "bar", true).isConvertible(to: .string))
XCTAssertEqual(
try evaluate("\"foo\" (0.5 1) \"bar\" true", as: .string),
"foo0.5 1bartrue"
)
}
func testCastTextureToString() throws {
let url = TestDelegate().resolveURL(for: "Stars1.jpg")
let texture = Texture.file(name: "Stars1.jpg", url: url, intensity: 1)
XCTAssert(Value.texture(texture).isConvertible(to: .string))
XCTAssertEqual(try evaluate("""
texture "Stars1.jpg"
define foo texture
foo
""", as: .string), .string(url.path))
}
func testCastFontToString() throws {
#if canImport(CoreGraphics)
XCTAssert(Value.font("times").isConvertible(to: .string))
XCTAssertEqual(try evaluate("""
font "EdgeOfTheGalaxyRegular-OVEa6.otf"
define foo font
foo
""", as: .string), .string("Edge of the Galaxy Regular"))
#endif
}
func testCastFontToTexture() throws {
#if canImport(CoreGraphics)
XCTAssertFalse(Value.font("times").isConvertible(to: .texture))
XCTAssertThrowsError(try evaluate("font \"times\"\nfont", as: .texture)) { error in
let error = try? XCTUnwrap(error as? RuntimeError)
XCTAssertEqual(error?.type, .typeMismatch(for: "", index: -1, expected: "texture", got: "font"))
}
#endif
}
func testCastTextureToFont() throws {
#if canImport(CoreGraphics)
let url = TestDelegate().resolveURL(for: "Stars1.jpg")
let texture = Texture.file(name: "Stars1.jpg", url: url, intensity: 1)
XCTAssertFalse(Value.texture(texture).isConvertible(to: .font))
XCTAssertThrowsError(try evaluate("texture \"Stars1.jpg\"\ntexture", as: .font)) { error in