diff options
Diffstat (limited to 'spec/inputs/test/with_statement_spec.yue')
| -rw-r--r-- | spec/inputs/test/with_statement_spec.yue | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/spec/inputs/test/with_statement_spec.yue b/spec/inputs/test/with_statement_spec.yue new file mode 100644 index 0000000..c2f9b3b --- /dev/null +++ b/spec/inputs/test/with_statement_spec.yue | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | describe "with statement", -> | ||
| 2 | it "should access properties with dot", -> | ||
| 3 | obj = {x: 10, y: 20} | ||
| 4 | result = nil | ||
| 5 | with obj | ||
| 6 | result = .x + .y | ||
| 7 | assert.same result, 30 | ||
| 8 | |||
| 9 | it "should chain property access", -> | ||
| 10 | obj = {nested: {value: 42}} | ||
| 11 | result = nil | ||
| 12 | with obj | ||
| 13 | result = .nested.value | ||
| 14 | assert.same result, 42 | ||
| 15 | |||
| 16 | it "should work with method calls", -> | ||
| 17 | obj = | ||
| 18 | value: 10 | ||
| 19 | double: => @value * 2 | ||
| 20 | |||
| 21 | result = nil | ||
| 22 | with obj | ||
| 23 | result = \double! | ||
| 24 | assert.same result, 20 | ||
| 25 | |||
| 26 | it "should handle nested with statements", -> | ||
| 27 | obj = {x: 1} | ||
| 28 | with obj | ||
| 29 | .x = 10 | ||
| 30 | with .nested = {y: 2} | ||
| 31 | .y = 20 | ||
| 32 | assert.same obj.x, 10 | ||
| 33 | assert.same obj.nested.y, 20 | ||
| 34 | |||
| 35 | it "should work in expressions", -> | ||
| 36 | obj = {value: 5} | ||
| 37 | result = with obj | ||
| 38 | .value * 2 | ||
| 39 | assert.same result, 10 | ||
| 40 | |||
| 41 | it "should support multiple statements", -> | ||
| 42 | obj = {a: 1, b: 2} | ||
| 43 | sum = nil | ||
| 44 | product = nil | ||
| 45 | with obj | ||
| 46 | sum = .a + .b | ||
| 47 | product = .a * .b | ||
| 48 | assert.same sum, 3 | ||
| 49 | assert.same product, 2 | ||
| 50 | |||
| 51 | it "should work with table manipulation", -> | ||
| 52 | obj = {items: [1, 2, 3]} | ||
| 53 | with obj | ||
| 54 | table.insert .items, 4 | ||
| 55 | assert.same #obj.items, 4 | ||
| 56 | |||
| 57 | it "should handle conditional inside with", -> | ||
| 58 | obj = {value: 10} | ||
| 59 | result = nil | ||
| 60 | with obj | ||
| 61 | if .value > 5 | ||
| 62 | result = "large" | ||
| 63 | else | ||
| 64 | result = "small" | ||
| 65 | assert.same result, "large" | ||
| 66 | |||
| 67 | it "should work with loops", -> | ||
| 68 | obj = {items: [1, 2, 3]} | ||
| 69 | sum = nil | ||
| 70 | with obj | ||
| 71 | sum = 0 | ||
| 72 | for item in *.items | ||
| 73 | sum += item | ||
| 74 | assert.same sum, 6 | ||
| 75 | |||
| 76 | it "should support with in assignment", -> | ||
| 77 | obj = {x: 5, y: 10} | ||
| 78 | result = with obj | ||
| 79 | .x + .y | ||
| 80 | assert.same result, 15 | ||
| 81 | |||
| 82 | it "should work with string methods", -> | ||
| 83 | s = "hello" | ||
| 84 | result = with s | ||
| 85 | \upper! | ||
| 86 | assert.same result, "HELLO" | ||
| 87 | |||
| 88 | it "should handle metatable access", -> | ||
| 89 | obj = setmetatable {value: 10}, { | ||
| 90 | __index: {extra: 5} | ||
| 91 | } | ||
| 92 | sum = nil | ||
| 93 | with obj | ||
| 94 | sum = .value + .<index>.extra | ||
| 95 | assert.same sum, 15 | ||
| 96 | |||
| 97 | it "should work in function", -> | ||
| 98 | fn = -> | ||
| 99 | obj = {x: 10} | ||
| 100 | with obj | ||
| 101 | .x * 2 | ||
| 102 | |||
| 103 | result = fn! | ||
| 104 | assert.same result, 20 | ||
| 105 | |||
| 106 | it "should support with in return", -> | ||
| 107 | get_value = -> | ||
| 108 | obj = {value: 42} | ||
| 109 | with obj | ||
| 110 | .value | ||
| 111 | |||
| 112 | assert.same get_value!, 42 | ||
| 113 | |||
| 114 | it "should work with existential operator", -> | ||
| 115 | obj = {value: 10} | ||
| 116 | result = with obj | ||
| 117 | .value ? 0 | ||
| 118 | assert.same result, 10 | ||
| 119 | |||
| 120 | it "should handle nil object safely", -> | ||
| 121 | result = with nil | ||
| 122 | .value | ||
| 123 | assert.same result, nil | ||
| 124 | |||
| 125 | it "should work with method chaining", -> | ||
| 126 | obj = | ||
| 127 | value: 5 | ||
| 128 | add: (n) => @value += n | ||
| 129 | get: => @value | ||
| 130 | |||
| 131 | result = with obj | ||
| 132 | \add 10 | ||
| 133 | \add 5 | ||
| 134 | \get! | ||
| 135 | assert.same result, 20 | ||
| 136 | |||
| 137 | it "should support nested property access", -> | ||
| 138 | obj = | ||
| 139 | level1: | ||
| 140 | level2: | ||
| 141 | level3: "deep" | ||
| 142 | |||
| 143 | result = with obj | ||
| 144 | .level1.level2.level3 | ||
| 145 | assert.same result, "deep" | ||
