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 ++++++++++++++++++++++ 8 files changed, 224 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 (limited to 'spec/inputs') 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 -- cgit v1.2.3-55-g6feb