aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/existential_spec.yue
blob: 2d516a930a4f779f3808f7344dfd324377597904 (plain)
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
describe "existential", ->
	it "should handle ?. with existing object", ->
		obj = {value: 42}
		result = obj?.value
		assert.same result, 42

	it "should handle ?. with nil object", ->
		obj = nil
		result = obj?.value
		assert.same result, nil

	it "should chain ?. calls", ->
		obj = {nested: {value: 100}}
		result = obj?.nested?.value
		assert.same result, 100

	it "should return nil in chain with nil", ->
		obj = nil
		result = obj?.nested?.value
		assert.same result, nil

	it "should handle ?. with method call", ->
		obj = {func: -> "result"}
		result = obj?.func!
		assert.same result, "result"

	it "should handle ? on table index", ->
		tb = {[1]: "first"}
		result = tb?[1]
		assert.same result, "first"

	it "should return nil for missing index", ->
		tb = {}
		result = tb?[99]
		assert.same result, nil

	it "should work with ? in if condition", ->
		obj = {value: 5}
		result = if obj?.value
			"exists"
		assert.same result, "exists"

	it "should combine ?. with and/or", ->
		obj = {value: 10}
		result = obj?.value and 20 or 30
		assert.same result, 20

	it "should handle with? safely", ->
		obj = {x: 1, y: 2}
		sum = obj?.x + obj?.y
		assert.same sum, 3

	it "should return nil with with? on nil", ->
		obj = nil
		result = obj?.x
		assert.same result, nil

	it "should handle false value correctly", ->
		-- false is a valid value, not nil
		obj = {value: false}
		result = obj?.value
		assert.same result, false

	it "should handle 0 value correctly", ->
		-- 0 is a valid value, not nil
		obj = {value: 0}
		result = obj?.value
		assert.same result, 0

	it "should handle empty string correctly", ->
		-- "" is a valid value, not nil
		obj = {value: ""}
		result = obj?.value
		assert.same result, ""

	it "should handle empty table correctly", ->
		-- {} is a valid value, not nil
		obj = {value: {}}
		result = obj?.value
		assert.same type(result), "table"

	it "should work with deep chains", ->
		obj = {a: {b: {c: {d: "deep"}}}}
		result = obj?.a?.b?.c?.d
		assert.same result, "deep"

	it "should break chain on first nil", ->
		obj = {a: nil}
		result = obj?.a?.b?.c
		assert.same result, nil

	it "should handle ?. with string methods", ->
		s = "hello"
		result = s?\upper!
		assert.same result, "HELLO"

	it "should handle ?. with nil string", ->
		s = nil
		result = s?\upper!
		assert.same result, nil