diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-27 00:30:56 +0000 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-27 00:30:56 +0000 |
| commit | 7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (patch) | |
| tree | ceba95c48bd8d5d9fff3d1206483ddf073c0e03d /spec/outputs/test/if_assignment_spec.lua | |
| parent | e70e63a9737ed3a9e72f1329901075498190e6b4 (diff) | |
| download | yuescript-compiler-improvements.tar.gz yuescript-compiler-improvements.tar.bz2 yuescript-compiler-improvements.zip | |
Add compiler improvements and comprehensive test suitecompiler-improvements
- Fixed path option handling to avoid semicolon concatenation issues
- Added exception handling for std::length_error and general exceptions
- Added comprehensive test specifications for advanced language features
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'spec/outputs/test/if_assignment_spec.lua')
| -rw-r--r-- | spec/outputs/test/if_assignment_spec.lua | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/spec/outputs/test/if_assignment_spec.lua b/spec/outputs/test/if_assignment_spec.lua new file mode 100644 index 0000000..7d3b708 --- /dev/null +++ b/spec/outputs/test/if_assignment_spec.lua | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | return describe("if assignment", function() | ||
| 2 | it("should assign and check truthy value", function() | ||
| 3 | local obj = { | ||
| 4 | find_user = function(name) | ||
| 5 | return name == "valid" and { | ||
| 6 | name = name | ||
| 7 | } or nil | ||
| 8 | end | ||
| 9 | } | ||
| 10 | local user = obj:find_user("valid") | ||
| 11 | if user then | ||
| 12 | return assert.same(user.name, "valid") | ||
| 13 | end | ||
| 14 | end) | ||
| 15 | it("should not enter block when nil", function() | ||
| 16 | local obj = { | ||
| 17 | find_user = function() | ||
| 18 | return nil | ||
| 19 | end | ||
| 20 | } | ||
| 21 | local user = obj:find_user() | ||
| 22 | if user then | ||
| 23 | return assert.is_true(false) | ||
| 24 | else | ||
| 25 | return assert.is_true(true) | ||
| 26 | end | ||
| 27 | end) | ||
| 28 | it("should work with elseif", function() | ||
| 29 | local get_value | ||
| 30 | get_value = function(key) | ||
| 31 | if "a" == key then | ||
| 32 | return 1 | ||
| 33 | elseif "b" == key then | ||
| 34 | return 2 | ||
| 35 | else | ||
| 36 | return nil | ||
| 37 | end | ||
| 38 | end | ||
| 39 | local result = nil | ||
| 40 | do | ||
| 41 | local val = get_value("c") | ||
| 42 | if val then | ||
| 43 | result = "c: " .. tostring(val) | ||
| 44 | else | ||
| 45 | val = get_value("b") | ||
| 46 | if val then | ||
| 47 | result = "b: " .. tostring(val) | ||
| 48 | else | ||
| 49 | result = "no match" | ||
| 50 | end | ||
| 51 | end | ||
| 52 | end | ||
| 53 | return assert.same(result, "b: 2") | ||
| 54 | end) | ||
| 55 | it("should scope variable to if block", function() | ||
| 56 | do | ||
| 57 | local x = 10 | ||
| 58 | if x then | ||
| 59 | assert.same(x, 10) | ||
| 60 | end | ||
| 61 | end | ||
| 62 | return assert.is_true(true) | ||
| 63 | end) | ||
| 64 | it("should work with multiple return values", function() | ||
| 65 | local fn | ||
| 66 | fn = function() | ||
| 67 | return true, "success" | ||
| 68 | end | ||
| 69 | local success, result = fn() | ||
| 70 | if success then | ||
| 71 | assert.is_true(success) | ||
| 72 | return assert.same(result, "success") | ||
| 73 | end | ||
| 74 | end) | ||
| 75 | it("should work with table destructuring", function() | ||
| 76 | local get_point | ||
| 77 | get_point = function() | ||
| 78 | return { | ||
| 79 | x = 10, | ||
| 80 | y = 20 | ||
| 81 | } | ||
| 82 | end | ||
| 83 | local _des_0 = get_point() | ||
| 84 | if _des_0 then | ||
| 85 | local x, y = _des_0.x, _des_0.y | ||
| 86 | assert.same(x, 10) | ||
| 87 | return assert.same(y, 20) | ||
| 88 | end | ||
| 89 | end) | ||
| 90 | it("should work with array destructuring", function() | ||
| 91 | local get_coords | ||
| 92 | get_coords = function() | ||
| 93 | return { | ||
| 94 | 1, | ||
| 95 | 2, | ||
| 96 | 3 | ||
| 97 | } | ||
| 98 | end | ||
| 99 | local _des_0 = get_coords() | ||
| 100 | if _des_0 then | ||
| 101 | local a, b, c = _des_0[1], _des_0[2], _des_0[3] | ||
| 102 | assert.same(a, 1) | ||
| 103 | assert.same(b, 2) | ||
| 104 | return assert.same(c, 3) | ||
| 105 | end | ||
| 106 | end) | ||
| 107 | it("should chain multiple assignments", function() | ||
| 108 | local a = 1 | ||
| 109 | if a then | ||
| 110 | local b = a + 1 | ||
| 111 | if b then | ||
| 112 | return assert.same(b, 2) | ||
| 113 | end | ||
| 114 | end | ||
| 115 | end) | ||
| 116 | it("should work in expression context", function() | ||
| 117 | local get_value | ||
| 118 | get_value = function(x) | ||
| 119 | if x > 0 then | ||
| 120 | return x | ||
| 121 | else | ||
| 122 | return nil | ||
| 123 | end | ||
| 124 | end | ||
| 125 | local result | ||
| 126 | do | ||
| 127 | local val = get_value(5) | ||
| 128 | if val then | ||
| 129 | result = val * 2 | ||
| 130 | else | ||
| 131 | result = 0 | ||
| 132 | end | ||
| 133 | end | ||
| 134 | return assert.same(result, 10) | ||
| 135 | end) | ||
| 136 | it("should work with os.getenv", function() | ||
| 137 | local path = os.getenv("PATH") | ||
| 138 | if path then | ||
| 139 | return assert.is_true(type(path) == "string") | ||
| 140 | else | ||
| 141 | return assert.is_true(true) | ||
| 142 | end | ||
| 143 | end) | ||
| 144 | it("should support table access", function() | ||
| 145 | local tb = { | ||
| 146 | key = "value" | ||
| 147 | } | ||
| 148 | local val = tb.key | ||
| 149 | if val then | ||
| 150 | return assert.same(val, "value") | ||
| 151 | end | ||
| 152 | end) | ||
| 153 | it("should work with function call results", function() | ||
| 154 | local fn | ||
| 155 | fn = function() | ||
| 156 | return "result" | ||
| 157 | end | ||
| 158 | local s = fn() | ||
| 159 | if s then | ||
| 160 | return assert.same(s, "result") | ||
| 161 | end | ||
| 162 | end) | ||
| 163 | return it("should handle false values", function() | ||
| 164 | local val = false | ||
| 165 | if val then | ||
| 166 | return assert.is_true(false) | ||
| 167 | else | ||
| 168 | return assert.is_true(true) | ||
| 169 | end | ||
| 170 | end) | ||
| 171 | end) | ||
