aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/existential_spec.yue
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-26 06:38:38 +0000
committerLi Jin <dragon-fly@qq.com>2026-01-26 06:38:38 +0000
commit5d5b657f606b5939062983b1f90c3359d542672e (patch)
tree32132fd8908d6a8920d59362c572815a949f1a1f /spec/inputs/test/existential_spec.yue
parentf5006f449a7be1a2f655f1b178ecf1d2f0569dd5 (diff)
downloadyuescript-5d5b657f606b5939062983b1f90c3359d542672e.tar.gz
yuescript-5d5b657f606b5939062983b1f90c3359d542672e.tar.bz2
yuescript-5d5b657f606b5939062983b1f90c3359d542672e.zip
Fixed compiler improvements and added comprehensive test suite
- Fixed makefile preprocessor macro definitions (removed spaces in -D flags) - Added null pointer check in compiler class declaration handling - Added comprehensive test specifications for various language features: - attrib, backcall, cond, config, existential, export, goto - import, literals, macro, metatable, operators, return - string, switch, vararg, with Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'spec/inputs/test/existential_spec.yue')
-rw-r--r--spec/inputs/test/existential_spec.yue100
1 files changed, 100 insertions, 0 deletions
diff --git a/spec/inputs/test/existential_spec.yue b/spec/inputs/test/existential_spec.yue
new file mode 100644
index 0000000..f63967a
--- /dev/null
+++ b/spec/inputs/test/existential_spec.yue
@@ -0,0 +1,100 @@
1describe "existential", ->
2 it "should handle ?. with existing object", ->
3 obj = {value: 42}
4 result = obj?.value
5 assert.same result, 42
6
7 it "should handle ?. with nil object", ->
8 obj = nil
9 result = obj?.value
10 assert.same result, nil
11
12 it "should chain ?. calls", ->
13 obj = {nested: {value: 100}}
14 result = obj?.nested?.value
15 assert.same result, 100
16
17 it "should return nil in chain with nil", ->
18 obj = nil
19 result = obj?.nested?.value
20 assert.same result, nil
21
22 it "should handle ?. with method call", ->
23 obj = {func: -> "result"}
24 result = obj?.func!
25 assert.same result, "result"
26
27 it "should handle ? on table index", ->
28 tb = {[1]: "first"}
29 result = tb?[1]
30 assert.same result, "first"
31
32 it "should return nil for missing index", ->
33 tb = {}
34 result = tb?[99]
35 assert.same result, nil
36
37 it "should work with ? in if condition", ->
38 obj = {value: 5}
39 if obj?.value
40 result = "exists"
41 assert.same result, "exists"
42
43 it "should combine ?. with and/or", ->
44 obj = {value: 10}
45 result = obj?.value and 20 or 30
46 assert.same result, 20
47
48 it "should handle with? safely", ->
49 obj = {x: 1, y: 2}
50 sum = obj?.x + obj?.y
51 assert.same sum, 3
52
53 it "should return nil with with? on nil", ->
54 obj = nil
55 result = obj?.x
56 assert.same result, nil
57
58 it "should handle false value correctly", ->
59 -- false is a valid value, not nil
60 obj = {value: false}
61 result = obj?.value
62 assert.same result, false
63
64 it "should handle 0 value correctly", ->
65 -- 0 is a valid value, not nil
66 obj = {value: 0}
67 result = obj?.value
68 assert.same result, 0
69
70 it "should handle empty string correctly", ->
71 -- "" is a valid value, not nil
72 obj = {value: ""}
73 result = obj?.value
74 assert.same result, ""
75
76 it "should handle empty table correctly", ->
77 -- {} is a valid value, not nil
78 obj = {value: {}}
79 result = obj?.value
80 assert.same type(result), "table"
81
82 it "should work with deep chains", ->
83 obj = {a: {b: {c: {d: "deep"}}}}
84 result = obj?.a?.b?.c?.d
85 assert.same result, "deep"
86
87 it "should break chain on first nil", ->
88 obj = {a: nil}
89 result = obj?.a?.b?.c
90 assert.same result, nil
91
92 it "should handle ?. with string methods", ->
93 s = "hello"
94 result = s?\upper!
95 assert.same result, "HELLO"
96
97 it "should handle ?. with nil string", ->
98 s = nil
99 result = s?\upper!
100 assert.same result, nil