From 8c3d786157ec7fef3072feac55c2d5450800568b Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 30 Jan 2026 18:16:45 +0800 Subject: Added more tests. --- spec/inputs/test/anonymous_class_spec.yue | 27 ++++ spec/inputs/test/class_expression_spec.yue | 27 ++++ spec/inputs/test/constructor_promotion_spec.yue | 26 ++++ spec/inputs/test/continue_spec.yue | 37 ++++++ .../test/export_import_interactions_spec.yue | 19 +++ spec/inputs/test/format_spec.yue | 7 + spec/inputs/test/string_interpolation_spec.yue | 34 +++++ spec/inputs/test/using_spec.yue | 47 +++++++ spec/outputs/test/anonymous_class_spec.lua | 142 +++++++++++++++++++++ spec/outputs/test/class_expression_spec.lua | 110 ++++++++++++++++ spec/outputs/test/constructor_promotion_spec.lua | 96 ++++++++++++++ spec/outputs/test/continue_spec.lua | 104 +++++++++++++++ .../test/export_import_interactions_spec.lua | 35 +++++ spec/outputs/test/format_spec.lua | 7 + spec/outputs/test/string_interpolation_spec.lua | 36 ++++++ spec/outputs/test/using_spec.lua | 50 ++++++++ 16 files changed, 804 insertions(+) create mode 100644 spec/inputs/test/anonymous_class_spec.yue create mode 100644 spec/inputs/test/class_expression_spec.yue create mode 100644 spec/inputs/test/constructor_promotion_spec.yue create mode 100644 spec/inputs/test/continue_spec.yue create mode 100644 spec/inputs/test/export_import_interactions_spec.yue create mode 100644 spec/inputs/test/string_interpolation_spec.yue create mode 100644 spec/inputs/test/using_spec.yue create mode 100644 spec/outputs/test/anonymous_class_spec.lua create mode 100644 spec/outputs/test/class_expression_spec.lua create mode 100644 spec/outputs/test/constructor_promotion_spec.lua create mode 100644 spec/outputs/test/continue_spec.lua create mode 100644 spec/outputs/test/export_import_interactions_spec.lua create mode 100644 spec/outputs/test/string_interpolation_spec.lua create mode 100644 spec/outputs/test/using_spec.lua diff --git a/spec/inputs/test/anonymous_class_spec.yue b/spec/inputs/test/anonymous_class_spec.yue new file mode 100644 index 0000000..72854ee --- /dev/null +++ b/spec/inputs/test/anonymous_class_spec.yue @@ -0,0 +1,27 @@ +describe "anonymous class", -> + it "should create anonymous class", -> + AnonymousClass = class + value: 100 + getValue: => @value + + instance = AnonymousClass! + assert.same instance\getValue!, 100 + + it "should use assigned name", -> + MyClass = class + value: 50 + + instance = MyClass! + assert.is_true MyClass.__name == "MyClass" + assert.same instance.value, 50 + + it "should support anonymous subclass", -> + Base = class + baseMethod: => "base" + + Sub = class extends Base + subMethod: => "sub" + + instance = Sub! + assert.same instance\baseMethod!, "base" + assert.same instance\subMethod!, "sub" diff --git a/spec/inputs/test/class_expression_spec.yue b/spec/inputs/test/class_expression_spec.yue new file mode 100644 index 0000000..13c1d23 --- /dev/null +++ b/spec/inputs/test/class_expression_spec.yue @@ -0,0 +1,27 @@ +describe "class expression", -> + it "should support class expression assignment", -> + MyClass = class + value: 100 + + assert.same MyClass.value, 100 + + it "should support class expression in table", -> + classes = { + Alpha: class + new: => @value = 1 + Beta: class + new: => @value = 2 + } + + a = classes.Alpha! + b = classes.Beta! + assert.same a.value, 1 + assert.same b.value, 2 + + it "should work with return", -> + fn = -> + return class + value: 50 + + Instance = fn! + assert.same Instance!.value, 50 diff --git a/spec/inputs/test/constructor_promotion_spec.yue b/spec/inputs/test/constructor_promotion_spec.yue new file mode 100644 index 0000000..83c9d15 --- /dev/null +++ b/spec/inputs/test/constructor_promotion_spec.yue @@ -0,0 +1,26 @@ +describe "constructor promotion", -> + it "should promote simple arguments to assignment", -> + class Thing + new: (@name, @age) => + + instance = Thing "Alice", 30 + assert.same instance.name, "Alice" + assert.same instance.age, 30 + + it "should promote multiple arguments", -> + class Point + new: (@x, @y, @z) => + + p = Point 1, 2, 3 + assert.same p.x, 1 + assert.same p.y, 2 + assert.same p.z, 3 + + it "should work with multiple parameters", -> + class Container + new: (@a, @b, @c) => + + c = Container! + assert.same c.a, nil + assert.same c.b, nil + assert.same c.c, nil diff --git a/spec/inputs/test/continue_spec.yue b/spec/inputs/test/continue_spec.yue new file mode 100644 index 0000000..bf4e839 --- /dev/null +++ b/spec/inputs/test/continue_spec.yue @@ -0,0 +1,37 @@ +describe "continue statement", -> + it "should skip odd numbers in for loop", -> + numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + even = for n in *numbers + continue if n % 2 == 1 + n + + assert.same even, {2, 4, 6, 8, 10} + + it "should filter values in while loop", -> + i = 0 + result = [] + + while i < 10 + i += 1 + continue if i % 3 == 0 + table.insert result, i + + assert.same result, {1, 2, 4, 5, 7, 8, 10} + + it "should skip with condition in loop expression", -> + items = [1, 2, 3, 4, 5] + odds = for item in *items + continue if item % 2 == 0 + item + + assert.same odds, {1, 3, 5} + + it "should work with nested loops", -> + result = [] + + for i = 1, 5 + for j = 1, 5 + continue if i == j + table.insert result, {i, j} + + assert.same #result, 20 diff --git a/spec/inputs/test/export_import_interactions_spec.yue b/spec/inputs/test/export_import_interactions_spec.yue new file mode 100644 index 0000000..8281246 --- /dev/null +++ b/spec/inputs/test/export_import_interactions_spec.yue @@ -0,0 +1,19 @@ +describe "export import interactions", -> + it "should import with alias and destructuring", -> + source = { + origin: {x: 10, y: 20} + target: "result" + point: {x: 5, y: 15} + } + import x, y from source.origin + import target from source + px, py = do + local x, y + import x, y from source.point + x, y + + assert.same x, 10 + assert.same y, 20 + assert.same target, "result" + assert.same px, 5 + assert.same py, 15 diff --git a/spec/inputs/test/format_spec.yue b/spec/inputs/test/format_spec.yue index 4da3f7e..2069795 100644 --- a/spec/inputs/test/format_spec.yue +++ b/spec/inputs/test/format_spec.yue @@ -55,11 +55,13 @@ files = [ "spec/inputs/syntax.yue" "spec/inputs/global.yue" "spec/inputs/plus.yue" + "spec/inputs/test/string_interpolation_spec.yue" "spec/inputs/test/with_spec.yue" "spec/inputs/test/try_catch_spec.yue" "spec/inputs/test/operator_advanced_spec.yue" "spec/inputs/test/with_statement_spec.yue" "spec/inputs/test/literals_spec.yue" + "spec/inputs/test/continue_spec.yue" "spec/inputs/test/varargs_assignment_spec.yue" "spec/inputs/test/advanced_macro_spec.yue" "spec/inputs/test/pipe_spec.yue" @@ -77,11 +79,13 @@ files = [ "spec/inputs/test/operators_spec.yue" "spec/inputs/test/comprehension_spec.yue" "spec/inputs/test/attrib_spec.yue" + "spec/inputs/test/using_spec.yue" "spec/inputs/test/nil_coalescing_spec.yue" "spec/inputs/test/table_comprehension_spec.yue" "spec/inputs/test/slicing_spec.yue" "spec/inputs/test/close_attribute_spec.yue" "spec/inputs/test/named_varargs_spec.yue" + "spec/inputs/test/export_import_interactions_spec.yue" "spec/inputs/test/table_spreading_spec.yue" "spec/inputs/test/macro_spec.yue" "spec/inputs/test/chaining_comparison_spec.yue" @@ -89,10 +93,13 @@ files = [ "spec/inputs/test/destructure_spec.yue" "spec/inputs/test/vararg_spec.yue" "spec/inputs/test/string_spec.yue" + "spec/inputs/test/anonymous_class_spec.yue" "spec/inputs/test/implicit_object_spec.yue" + "spec/inputs/test/constructor_promotion_spec.yue" "spec/inputs/test/backcall_spec.yue" "spec/inputs/test/while_assignment_spec.yue" "spec/inputs/test/switch_spec.yue" + "spec/inputs/test/class_expression_spec.yue" "spec/inputs/test/functions_advanced_spec.yue" "spec/inputs/test/config_spec.yue" "spec/inputs/test/yaml_string_spec.yue" diff --git a/spec/inputs/test/string_interpolation_spec.yue b/spec/inputs/test/string_interpolation_spec.yue new file mode 100644 index 0000000..02b1606 --- /dev/null +++ b/spec/inputs/test/string_interpolation_spec.yue @@ -0,0 +1,34 @@ +describe "string interpolation", -> + it "should interpolate in double quotes", -> + name = "World" + result = "Hello #{name}!" + assert.same result, "Hello World!" + + it "should interpolate numbers", -> + a, b = 10, 20 + result = "#{a} + #{b} = #{a + b}" + assert.same result, "10 + 20 = 30" + + it "should interpolate expressions", -> + x = 5 + result = "x * 2 = #{x * 2}" + assert.same result, "x * 2 = 10" + + it "should interpolate function calls", -> + result = "result: #{math.floor 5.5}" + assert.same result, "result: 5" + + it "should interpolate in string literals", -> + x = 100 + result = "Value: #{x}" + assert.same result, "Value: 100" + + it "should work with nested interpolation", -> + inner = "inner" + result = "Outer: #{inner}" + assert.same result, "Outer: inner" + + it "should not interpolate in single quotes", -> + name = "World" + result = 'Hello #{name}!' + assert.same result, 'Hello #{name}!' diff --git a/spec/inputs/test/using_spec.yue b/spec/inputs/test/using_spec.yue new file mode 100644 index 0000000..e9ad749 --- /dev/null +++ b/spec/inputs/test/using_spec.yue @@ -0,0 +1,47 @@ +describe "using", -> + it "should prevent variable shadowing in assignment", -> + tmp = 100 + i, k = 100, 50 + + process = (add using k, i) -> + tmp = tmp + add + i += tmp + k += tmp + + process 22 + assert.same i, 222 + assert.same k, 172 + assert.same tmp, 100 + + it "should handle multiple variable names", -> + a, b, c = 1, 2, 3 + + process = (sum using a, b) -> + a += 1 + b += 2 + c = sum + 100 + + process 10 + assert.same a, 2 + assert.same b, 4 + assert.same c, 3 + + it "should work with nil value", -> + local x = 1 + + fn = (val using x) -> + if val ~= nil + x = val + + fn 100 + assert.same x, 100 + assert.is_true x ~= 1 + + it "should work with function calls", -> + local count = 0 + + fn = (n using count) -> + count += n + + fn 5 + assert.same count, 5 diff --git a/spec/outputs/test/anonymous_class_spec.lua b/spec/outputs/test/anonymous_class_spec.lua new file mode 100644 index 0000000..8f6a122 --- /dev/null +++ b/spec/outputs/test/anonymous_class_spec.lua @@ -0,0 +1,142 @@ +return describe("anonymous class", function() + it("should create anonymous class", function() + local AnonymousClass + do + local _class_0 + local _base_0 = { + value = 100, + getValue = function(self) + return self.value + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "AnonymousClass" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + AnonymousClass = _class_0 + end + local instance = AnonymousClass() + return assert.same(instance:getValue(), 100) + end) + it("should use assigned name", function() + local MyClass + do + local _class_0 + local _base_0 = { + value = 50 + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "MyClass" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + MyClass = _class_0 + end + local instance = MyClass() + assert.is_true(MyClass.__name == "MyClass") + return assert.same(instance.value, 50) + end) + return it("should support anonymous subclass", function() + local Base + do + local _class_0 + local _base_0 = { + baseMethod = function(self) + return "base" + end + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "Base" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Base = _class_0 + end + local Sub + do + local _class_0 + local _parent_0 = Base + local _base_0 = { + subMethod = function(self) + return "sub" + end + } + for _key_0, _val_0 in pairs(_parent_0.__base) do + if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then + _base_0[_key_0] = _val_0 + end + end + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, ...) + return _class_0.__parent.__init(self, ...) + end, + __base = _base_0, + __name = "Sub", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + Sub = _class_0 + end + local instance = Sub() + assert.same(instance:baseMethod(), "base") + return assert.same(instance:subMethod(), "sub") + end) +end) diff --git a/spec/outputs/test/class_expression_spec.lua b/spec/outputs/test/class_expression_spec.lua new file mode 100644 index 0000000..fd9083b --- /dev/null +++ b/spec/outputs/test/class_expression_spec.lua @@ -0,0 +1,110 @@ +local _anon_func_0 = function(setmetatable) + local _class_0 + local _base_0 = { } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self) + self.value = 1 + end, + __base = _base_0 + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + return _class_0 +end +local _anon_func_1 = function(setmetatable) + local _class_0 + local _base_0 = { } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self) + self.value = 2 + end, + __base = _base_0 + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + return _class_0 +end +return describe("class expression", function() + it("should support class expression assignment", function() + local MyClass + do + local _class_0 + local _base_0 = { + value = 100 + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "MyClass" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + MyClass = _class_0 + end + return assert.same(MyClass.value, 100) + end) + it("should support class expression in table", function() + local classes = { + Alpha = _anon_func_0(setmetatable), + Beta = _anon_func_1(setmetatable) + } + local a = classes.Alpha() + local b = classes.Beta() + assert.same(a.value, 1) + return assert.same(b.value, 2) + end) + return it("should work with return", function() + local fn + fn = function() + local _class_0 + local _base_0 = { + value = 50 + } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0 + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + return _class_0 + end + local Instance = fn() + return assert.same(Instance().value, 50) + end) +end) diff --git a/spec/outputs/test/constructor_promotion_spec.lua b/spec/outputs/test/constructor_promotion_spec.lua new file mode 100644 index 0000000..5caa762 --- /dev/null +++ b/spec/outputs/test/constructor_promotion_spec.lua @@ -0,0 +1,96 @@ +return describe("constructor promotion", function() + it("should promote simple arguments to assignment", function() + local Thing + do + local _class_0 + local _base_0 = { } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, name, age) + self.name = name + self.age = age + end, + __base = _base_0, + __name = "Thing" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Thing = _class_0 + end + local instance = Thing("Alice", 30) + assert.same(instance.name, "Alice") + return assert.same(instance.age, 30) + end) + it("should promote multiple arguments", function() + local Point + do + local _class_0 + local _base_0 = { } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, x, y, z) + self.x = x + self.y = y + self.z = z + end, + __base = _base_0, + __name = "Point" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Point = _class_0 + end + local p = Point(1, 2, 3) + assert.same(p.x, 1) + assert.same(p.y, 2) + return assert.same(p.z, 3) + end) + return it("should work with multiple parameters", function() + local Container + do + local _class_0 + local _base_0 = { } + if _base_0.__index == nil then + _base_0.__index = _base_0 + end + _class_0 = setmetatable({ + __init = function(self, a, b, c) + self.a = a + self.b = b + self.c = c + end, + __base = _base_0, + __name = "Container" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({ }, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Container = _class_0 + end + local c = Container() + assert.same(c.a, nil) + assert.same(c.b, nil) + return assert.same(c.c, nil) + end) +end) diff --git a/spec/outputs/test/continue_spec.lua b/spec/outputs/test/continue_spec.lua new file mode 100644 index 0000000..8c73aee --- /dev/null +++ b/spec/outputs/test/continue_spec.lua @@ -0,0 +1,104 @@ +return describe("continue statement", function() + it("should skip odd numbers in for loop", function() + local numbers = { + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10 + } + local even + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #numbers do + local n = numbers[_index_0] + if n % 2 == 1 then + goto _continue_0 + end + _accum_0[_len_0] = n + _len_0 = _len_0 + 1 + ::_continue_0:: + end + even = _accum_0 + end + return assert.same(even, { + 2, + 4, + 6, + 8, + 10 + }) + end) + it("should filter values in while loop", function() + local i = 0 + local result = { } + while i < 10 do + i = i + 1 + if i % 3 == 0 then + goto _continue_0 + end + table.insert(result, i) + ::_continue_0:: + end + return assert.same(result, { + 1, + 2, + 4, + 5, + 7, + 8, + 10 + }) + end) + it("should skip with condition in loop expression", function() + local items = { + 1, + 2, + 3, + 4, + 5 + } + local odds + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #items do + local item = items[_index_0] + if item % 2 == 0 then + goto _continue_0 + end + _accum_0[_len_0] = item + _len_0 = _len_0 + 1 + ::_continue_0:: + end + odds = _accum_0 + end + return assert.same(odds, { + 1, + 3, + 5 + }) + end) + return it("should work with nested loops", function() + local result = { } + for i = 1, 5 do + for j = 1, 5 do + if i == j then + goto _continue_0 + end + table.insert(result, { + i, + j + }) + ::_continue_0:: + end + end + return assert.same(#result, 20) + end) +end) diff --git a/spec/outputs/test/export_import_interactions_spec.lua b/spec/outputs/test/export_import_interactions_spec.lua new file mode 100644 index 0000000..774d57a --- /dev/null +++ b/spec/outputs/test/export_import_interactions_spec.lua @@ -0,0 +1,35 @@ +return describe("export import interactions", function() + return it("should import with alias and destructuring", function() + local source = { + origin = { + x = 10, + y = 20 + }, + target = "result", + point = { + x = 5, + y = 15 + } + } + local x, y + do + local _obj_0 = source.origin + x, y = _obj_0.x, _obj_0.y + end + local target = source.target + local px, py + do + local x, y + do + local _obj_0 = source.point + x, y = _obj_0.x, _obj_0.y + end + px, py = x, y + end + assert.same(x, 10) + assert.same(y, 20) + assert.same(target, "result") + assert.same(px, 5) + return assert.same(py, 15) + end) +end) diff --git a/spec/outputs/test/format_spec.lua b/spec/outputs/test/format_spec.lua index 8307f6a..bb31d50 100644 --- a/spec/outputs/test/format_spec.lua +++ b/spec/outputs/test/format_spec.lua @@ -55,11 +55,13 @@ local files = { "spec/inputs/syntax.yue", "spec/inputs/global.yue", "spec/inputs/plus.yue", + "spec/inputs/test/string_interpolation_spec.yue", "spec/inputs/test/with_spec.yue", "spec/inputs/test/try_catch_spec.yue", "spec/inputs/test/operator_advanced_spec.yue", "spec/inputs/test/with_statement_spec.yue", "spec/inputs/test/literals_spec.yue", + "spec/inputs/test/continue_spec.yue", "spec/inputs/test/varargs_assignment_spec.yue", "spec/inputs/test/advanced_macro_spec.yue", "spec/inputs/test/pipe_spec.yue", @@ -77,11 +79,13 @@ local files = { "spec/inputs/test/operators_spec.yue", "spec/inputs/test/comprehension_spec.yue", "spec/inputs/test/attrib_spec.yue", + "spec/inputs/test/using_spec.yue", "spec/inputs/test/nil_coalescing_spec.yue", "spec/inputs/test/table_comprehension_spec.yue", "spec/inputs/test/slicing_spec.yue", "spec/inputs/test/close_attribute_spec.yue", "spec/inputs/test/named_varargs_spec.yue", + "spec/inputs/test/export_import_interactions_spec.yue", "spec/inputs/test/table_spreading_spec.yue", "spec/inputs/test/macro_spec.yue", "spec/inputs/test/chaining_comparison_spec.yue", @@ -89,10 +93,13 @@ local files = { "spec/inputs/test/destructure_spec.yue", "spec/inputs/test/vararg_spec.yue", "spec/inputs/test/string_spec.yue", + "spec/inputs/test/anonymous_class_spec.yue", "spec/inputs/test/implicit_object_spec.yue", + "spec/inputs/test/constructor_promotion_spec.yue", "spec/inputs/test/backcall_spec.yue", "spec/inputs/test/while_assignment_spec.yue", "spec/inputs/test/switch_spec.yue", + "spec/inputs/test/class_expression_spec.yue", "spec/inputs/test/functions_advanced_spec.yue", "spec/inputs/test/config_spec.yue", "spec/inputs/test/yaml_string_spec.yue", diff --git a/spec/outputs/test/string_interpolation_spec.lua b/spec/outputs/test/string_interpolation_spec.lua new file mode 100644 index 0000000..abf23eb --- /dev/null +++ b/spec/outputs/test/string_interpolation_spec.lua @@ -0,0 +1,36 @@ +return describe("string interpolation", function() + it("should interpolate in double quotes", function() + local name = "World" + local result = "Hello " .. tostring(name) .. "!" + return assert.same(result, "Hello World!") + end) + it("should interpolate numbers", function() + local a, b = 10, 20 + local result = tostring(a) .. " + " .. tostring(b) .. " = " .. tostring(a + b) + return assert.same(result, "10 + 20 = 30") + end) + it("should interpolate expressions", function() + local x = 5 + local result = "x * 2 = " .. tostring(x * 2) + return assert.same(result, "x * 2 = 10") + end) + it("should interpolate function calls", function() + local result = "result: " .. tostring(math.floor(5.5)) + return assert.same(result, "result: 5") + end) + it("should interpolate in string literals", function() + local x = 100 + local result = "Value: " .. tostring(x) + return assert.same(result, "Value: 100") + end) + it("should work with nested interpolation", function() + local inner = "inner" + local result = "Outer: " .. tostring(inner) + return assert.same(result, "Outer: inner") + end) + return it("should not interpolate in single quotes", function() + local name = "World" + local result = 'Hello #{name}!' + return assert.same(result, 'Hello #{name}!') + end) +end) diff --git a/spec/outputs/test/using_spec.lua b/spec/outputs/test/using_spec.lua new file mode 100644 index 0000000..5c2f357 --- /dev/null +++ b/spec/outputs/test/using_spec.lua @@ -0,0 +1,50 @@ +return describe("using", function() + it("should prevent variable shadowing in assignment", function() + local tmp = 100 + local i, k = 100, 50 + local process + process = function(add) + local tmp = tmp + add + i = i + tmp + k = k + tmp + end + process(22) + assert.same(i, 222) + assert.same(k, 172) + return assert.same(tmp, 100) + end) + it("should handle multiple variable names", function() + local a, b, c = 1, 2, 3 + local process + process = function(sum) + a = a + 1 + b = b + 2 + local c = sum + 100 + end + process(10) + assert.same(a, 2) + assert.same(b, 4) + return assert.same(c, 3) + end) + it("should work with nil value", function() + local x = 1 + local fn + fn = function(val) + if val ~= nil then + x = val + end + end + fn(100) + assert.same(x, 100) + return assert.is_true(x ~= 1) + end) + return it("should work with function calls", function() + local count = 0 + local fn + fn = function(n) + count = count + n + end + fn(5) + return assert.same(count, 5) + end) +end) -- cgit v1.2.3-55-g6feb