aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/operator_advanced_spec.yue
diff options
context:
space:
mode:
Diffstat (limited to 'spec/inputs/test/operator_advanced_spec.yue')
-rw-r--r--spec/inputs/test/operator_advanced_spec.yue136
1 files changed, 136 insertions, 0 deletions
diff --git a/spec/inputs/test/operator_advanced_spec.yue b/spec/inputs/test/operator_advanced_spec.yue
new file mode 100644
index 0000000..8127fd4
--- /dev/null
+++ b/spec/inputs/test/operator_advanced_spec.yue
@@ -0,0 +1,136 @@
1describe "advanced operators", ->
2 it "should support chaining comparisons with functions", ->
3 v = (x) -> x
4 assert.is_true v(1) < v(2) <= v(3)
5
6 it "should handle compound assignment with or", ->
7 x = nil
8 x or= "default"
9 assert.same x, "default"
10
11 it "should not overwrite existing value with or", ->
12 x = "existing"
13 x or= "default"
14 assert.same x, "existing"
15
16 it "should support compound string concatenation", ->
17 s = "hello"
18 s ..= " world"
19 assert.same s, "hello world"
20
21 it "should work with table appending", ->
22 tab = [1, 2]
23 tab[] = 3
24 tab[] = 4
25 assert.same tab, {1, 2, 3, 4}
26
27 it "should handle spread append", ->
28 tbA = [1, 2]
29 tbB = [3, 4]
30 tbA[] = ...tbB
31 assert.same tbA, {1, 2, 3, 4}
32
33 it "should support reverse indexing", ->
34 items = [1, 2, 3, 4, 5]
35 assert.same items[#], 5
36 assert.same items[#-1], 4
37 assert.same items[#-2], 3
38
39 it "should work with nil coalescing assignment", ->
40 x = nil
41 x ??= "default"
42 assert.same x, "default"
43
44 it "should not assign with ??= when value exists", ->
45 x = "existing"
46 x ??= "default"
47 assert.same x, "existing"
48
49 it "should chain nil coalescing", ->
50 a = nil
51 b = nil
52 c = "value"
53 result = a ?? b ?? c
54 assert.same result, "value"
55
56 it "should support compound modulo", ->
57 x = 20
58 x %= 3
59 assert.same x, 2
60
61 it "should handle compound exponentiation", ->
62 x = 2
63 x ^= 3
64 assert.same x, 8
65
66 it "should work with compound bitwise and", ->
67 x = 15 -- 1111 in binary
68 x &= 7 -- 0111 in binary
69 assert.same x, 7
70
71 it "should support compound bitwise or", ->
72 x = 8 -- 1000 in binary
73 x |= 3 -- 0011 in binary
74 assert.same x, 11 -- 1011 in binary
75
76 it "should handle compound bitwise xor", ->
77 x = 12 -- 1100 in binary
78 x ~= 10 -- 1010 in binary
79 assert.same x, 6 -- 0110 in binary
80
81 it "should work with compound left shift", ->
82 x = 2
83 x <<= 3
84 assert.same x, 16
85
86 it "should support compound right shift", ->
87 x = 16
88 x >>= 2
89 assert.same x, 4
90
91 it "should handle negation operator", ->
92 assert.same -10, -10
93 assert.same --5, 5
94
95 it "should work with length operator on tables", ->
96 tab = {1, 2, 3, 4, 5}
97 assert.same #tab, 5
98
99 it "should support length on strings", ->
100 s = "hello"
101 assert.same #s, 5
102
103 it "should handle chaining assignment", ->
104 a = b = c = d = 0
105 assert.same a, 0
106 assert.same b, 0
107 assert.same c, 0
108 assert.same d, 0
109
110 it "should work with chaining assignment with functions", ->
111 f = -> 42
112 x = y = z = f!
113 assert.same x, 42
114 assert.same y, 42
115 assert.same z, 42
116
117 it "should support != as alias for ~=", ->
118 assert.is_true 1 != 2
119 assert.is_false 1 != 1
120
121 it "should work with :: for method chaining", ->
122 obj =
123 value: 10
124 add: (n) => @value += n
125 get: => @value
126
127 result = obj::add 5::get!
128 assert.same result, 15
129
130 it "should handle complex expressions with precedence", ->
131 result = 1 + 2 * 3 - 4 / 2
132 assert.same result, 5
133
134 it "should support mixed operator types", ->
135 result = 10 + 20 * 2 - 5 / 5
136 assert.same result, 49