From f1454bbbd13a71da2005ff789cde2da0e9eb81f6 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 22 Jan 2026 15:03:12 +0800 Subject: Adding tests. --- spec/inputs/comprehension_nested.yue | 3 +++ spec/inputs/destructure_defaults.yue | 9 +++++++++ spec/inputs/export_mixed.yue | 3 +++ spec/inputs/nil_coalesce_precedence.yue | 8 ++++++++ spec/inputs/pipe_chain_combo.yue | 8 ++++++++ spec/inputs/test/comprehension_spec.yue | 5 +++++ spec/inputs/test/destructure_spec.yue | 5 +++++ spec/inputs/test/format_spec.yue | 12 ++++++++++++ spec/inputs/test/nil_coalescing_spec.yue | 7 +++++++ spec/inputs/test/pipe_spec.yue | 5 +++++ spec/inputs/test/try_catch_spec.yue | 12 ++++++++++++ spec/inputs/unicode/pipe_chain_combo.yue | 8 ++++++++ spec/inputs/with_scope_shadow.yue | 12 ++++++++++++ spec/outputs/comprehension_nested.lua | 24 ++++++++++++++++++++++++ spec/outputs/destructure_defaults.lua | 15 +++++++++++++++ spec/outputs/export_mixed.lua | 11 +++++++++++ spec/outputs/nil_coalesce_precedence.lua | 24 ++++++++++++++++++++++++ spec/outputs/pipe_chain_combo.lua | 9 +++++++++ spec/outputs/test/comprehension_spec.lua | 31 +++++++++++++++++++++++++++++++ spec/outputs/test/destructure_spec.lua | 29 +++++++++++++++++++++++++++++ spec/outputs/test/format_spec.lua | 14 +++++++++++++- spec/outputs/test/nil_coalescing_spec.lua | 23 +++++++++++++++++++++++ spec/outputs/test/pipe_spec.lua | 13 +++++++++++++ spec/outputs/test/try_catch_spec.lua | 17 +++++++++++++++++ spec/outputs/unicode/pipe_chain_combo.lua | 9 +++++++++ spec/outputs/with_scope_shadow.lua | 14 ++++++++++++++ src/yue.cpp | 4 ++-- src/yuescript/yue_compiler.cpp | 10 +++++----- 28 files changed, 336 insertions(+), 8 deletions(-) create mode 100644 spec/inputs/comprehension_nested.yue create mode 100644 spec/inputs/destructure_defaults.yue create mode 100644 spec/inputs/export_mixed.yue create mode 100644 spec/inputs/nil_coalesce_precedence.yue create mode 100644 spec/inputs/pipe_chain_combo.yue create mode 100644 spec/inputs/test/comprehension_spec.yue create mode 100644 spec/inputs/test/destructure_spec.yue create mode 100644 spec/inputs/test/nil_coalescing_spec.yue create mode 100644 spec/inputs/test/pipe_spec.yue create mode 100644 spec/inputs/test/try_catch_spec.yue create mode 100644 spec/inputs/unicode/pipe_chain_combo.yue create mode 100644 spec/inputs/with_scope_shadow.yue create mode 100644 spec/outputs/comprehension_nested.lua create mode 100644 spec/outputs/destructure_defaults.lua create mode 100644 spec/outputs/export_mixed.lua create mode 100644 spec/outputs/nil_coalesce_precedence.lua create mode 100644 spec/outputs/pipe_chain_combo.lua create mode 100644 spec/outputs/test/comprehension_spec.lua create mode 100644 spec/outputs/test/destructure_spec.lua create mode 100644 spec/outputs/test/nil_coalescing_spec.lua create mode 100644 spec/outputs/test/pipe_spec.lua create mode 100644 spec/outputs/test/try_catch_spec.lua create mode 100644 spec/outputs/unicode/pipe_chain_combo.lua create mode 100644 spec/outputs/with_scope_shadow.lua diff --git a/spec/inputs/comprehension_nested.yue b/spec/inputs/comprehension_nested.yue new file mode 100644 index 0000000..c60419d --- /dev/null +++ b/spec/inputs/comprehension_nested.yue @@ -0,0 +1,3 @@ +list = {1, 2, 3} +pairs = ["#{i}-#{j}" for i in *list when i % 2 == 1 for j in *list when j > i] +print table.concat pairs, "," diff --git a/spec/inputs/destructure_defaults.yue b/spec/inputs/destructure_defaults.yue new file mode 100644 index 0000000..112d4a1 --- /dev/null +++ b/spec/inputs/destructure_defaults.yue @@ -0,0 +1,9 @@ +t = { + a: 1 + b: + c: 3 + d: nil +} + +{:a, b: {:c, :d = 4}, :e = 5} = t +print a, c, d, e diff --git a/spec/inputs/export_mixed.yue b/spec/inputs/export_mixed.yue new file mode 100644 index 0000000..5adfdd7 --- /dev/null +++ b/spec/inputs/export_mixed.yue @@ -0,0 +1,3 @@ +export answer = 42 +export foo = -> "bar" +export baz = "baz" diff --git a/spec/inputs/nil_coalesce_precedence.yue b/spec/inputs/nil_coalesce_precedence.yue new file mode 100644 index 0000000..4e7eabf --- /dev/null +++ b/spec/inputs/nil_coalesce_precedence.yue @@ -0,0 +1,8 @@ +a = nil +b = false +c = 0 + +x = a ?? (b and 1) ?? (c or 2) +y = (a ?? b) and 1 or 2 + +print x, y diff --git a/spec/inputs/pipe_chain_combo.yue b/spec/inputs/pipe_chain_combo.yue new file mode 100644 index 0000000..d9265e4 --- /dev/null +++ b/spec/inputs/pipe_chain_combo.yue @@ -0,0 +1,8 @@ +f1 = (x)-> x + 2 +f2 = (x)-> x * 3 + +value = 3 + |> f1 + |> f2 + |> tostring + |> print diff --git a/spec/inputs/test/comprehension_spec.yue b/spec/inputs/test/comprehension_spec.yue new file mode 100644 index 0000000..5f24aba --- /dev/null +++ b/spec/inputs/test/comprehension_spec.yue @@ -0,0 +1,5 @@ +describe "comprehension", -> + it "nested with filter", -> + list = {1, 2, 3} + out = ["#{i}-#{j}" for i in *list when i % 2 == 1 for j in *list when j > i] + assert.same out, {"1-2", "1-3"} diff --git a/spec/inputs/test/destructure_spec.yue b/spec/inputs/test/destructure_spec.yue new file mode 100644 index 0000000..802774c --- /dev/null +++ b/spec/inputs/test/destructure_spec.yue @@ -0,0 +1,5 @@ +describe "destructure", -> + it "defaults and nested", -> + t = { a: 1, b: { c: 3 }, d: nil } + {:a, b: {:c, :d = 4}, :e = 5} = t + assert.same {a, c, d, e}, {1, 3, 4, 5} diff --git a/spec/inputs/test/format_spec.yue b/spec/inputs/test/format_spec.yue index 95f73fc..6b94540 100644 --- a/spec/inputs/test/format_spec.yue +++ b/spec/inputs/test/format_spec.yue @@ -95,6 +95,18 @@ files = [ "spec/inputs/unicode/syntax.yue" "spec/inputs/unicode/global.yue" "spec/inputs/unicode/plus.yue" + "spec/inputs/pipe_chain_combo.yue" + "spec/inputs/destructure_defaults.yue" + "spec/inputs/nil_coalesce_precedence.yue" + "spec/inputs/comprehension_nested.yue" + "spec/inputs/with_scope_shadow.yue" + "spec/inputs/export_mixed.yue" + "spec/inputs/unicode/pipe_chain_combo.yue" + "spec/inputs/test/destructure_spec.yue" + "spec/inputs/test/nil_coalescing_spec.yue" + "spec/inputs/test/pipe_spec.yue" + "spec/inputs/test/try_catch_spec.yue" + "spec/inputs/test/comprehension_spec.yue" ] import "yue" diff --git a/spec/inputs/test/nil_coalescing_spec.yue b/spec/inputs/test/nil_coalescing_spec.yue new file mode 100644 index 0000000..4f845b3 --- /dev/null +++ b/spec/inputs/test/nil_coalescing_spec.yue @@ -0,0 +1,7 @@ +describe "nil coalescing", -> + it "distinguish nil and false", -> + a = nil + b = false + c = 0 + assert.same (a ?? b), false + assert.same (a ?? c), 0 diff --git a/spec/inputs/test/pipe_spec.yue b/spec/inputs/test/pipe_spec.yue new file mode 100644 index 0000000..58d48aa --- /dev/null +++ b/spec/inputs/test/pipe_spec.yue @@ -0,0 +1,5 @@ +describe "pipe", -> + it "pipes through functions", -> + f = (x)-> x + 1 + g = (x)-> x * 2 + assert.same (3 |> f |> g), 8 diff --git a/spec/inputs/test/try_catch_spec.yue b/spec/inputs/test/try_catch_spec.yue new file mode 100644 index 0000000..ed8fef0 --- /dev/null +++ b/spec/inputs/test/try_catch_spec.yue @@ -0,0 +1,12 @@ +describe "try/catch", -> + it "catch and rethrow", -> + ok, success, err = pcall -> + try + error "boom" + catch e + _, result = try + error "wrap:" .. e\match "^.-:%d+:%s*(.*)$" + result + assert.same ok, true + assert.same success, false + assert.is_true err\match("wrap:boom") != nil diff --git a/spec/inputs/unicode/pipe_chain_combo.yue b/spec/inputs/unicode/pipe_chain_combo.yue new file mode 100644 index 0000000..8f85207 --- /dev/null +++ b/spec/inputs/unicode/pipe_chain_combo.yue @@ -0,0 +1,8 @@ +加 = (x)-> x + 2 +乘 = (x)-> x * 3 + +值 = 3 + |> 加 + |> 乘 + |> tostring + |> print diff --git a/spec/inputs/with_scope_shadow.yue b/spec/inputs/with_scope_shadow.yue new file mode 100644 index 0000000..a0d58cc --- /dev/null +++ b/spec/inputs/with_scope_shadow.yue @@ -0,0 +1,12 @@ +target = { + val: 1 + add: (n)=> + @val += n + @val +} + +result = with target + val = 100 + add 2 + +print result, target.val diff --git a/spec/outputs/comprehension_nested.lua b/spec/outputs/comprehension_nested.lua new file mode 100644 index 0000000..abd5792 --- /dev/null +++ b/spec/outputs/comprehension_nested.lua @@ -0,0 +1,24 @@ +local list = { + 1, + 2, + 3 +} +local pairs +do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #list do + local i = list[_index_0] + if i % 2 == 1 then + for _index_1 = 1, #list do + local j = list[_index_1] + if j > i then + _accum_0[_len_0] = tostring(i) .. "-" .. tostring(j) + _len_0 = _len_0 + 1 + end + end + end + end + pairs = _accum_0 +end +return print(table.concat(pairs, ",")) diff --git a/spec/outputs/destructure_defaults.lua b/spec/outputs/destructure_defaults.lua new file mode 100644 index 0000000..938d9b2 --- /dev/null +++ b/spec/outputs/destructure_defaults.lua @@ -0,0 +1,15 @@ +local t = { + a = 1, + b = { + c = 3 + }, + d = nil +} +local a, c, d, e = t.a, t.b.c, t.b.d, t.e +if d == nil then + d = 4 +end +if e == nil then + e = 5 +end +return print(a, c, d, e) diff --git a/spec/outputs/export_mixed.lua b/spec/outputs/export_mixed.lua new file mode 100644 index 0000000..35529e2 --- /dev/null +++ b/spec/outputs/export_mixed.lua @@ -0,0 +1,11 @@ +local _module_0 = { } +local answer = 42 +_module_0["answer"] = answer +local foo +foo = function() + return "bar" +end +_module_0["foo"] = foo +local baz = "baz" +_module_0["baz"] = baz +return _module_0 diff --git a/spec/outputs/nil_coalesce_precedence.lua b/spec/outputs/nil_coalesce_precedence.lua new file mode 100644 index 0000000..6f353ee --- /dev/null +++ b/spec/outputs/nil_coalesce_precedence.lua @@ -0,0 +1,24 @@ +local a = nil +local b = false +local c = 0 +local x +if a ~= nil then + x = a +else + do + local _exp_0 = (b and 1) + if _exp_0 ~= nil then + x = _exp_0 + else + x = (c or 2) + end + end +end +local y = ((function() + if a ~= nil then + return a + else + return b + end +end)()) and 1 or 2 +return print(x, y) diff --git a/spec/outputs/pipe_chain_combo.lua b/spec/outputs/pipe_chain_combo.lua new file mode 100644 index 0000000..6cf40a9 --- /dev/null +++ b/spec/outputs/pipe_chain_combo.lua @@ -0,0 +1,9 @@ +local f1 +f1 = function(x) + return x + 2 +end +local f2 +f2 = function(x) + return x * 3 +end +local value = print(tostring(f2(f1(3)))) diff --git a/spec/outputs/test/comprehension_spec.lua b/spec/outputs/test/comprehension_spec.lua new file mode 100644 index 0000000..a4682d5 --- /dev/null +++ b/spec/outputs/test/comprehension_spec.lua @@ -0,0 +1,31 @@ +return describe("comprehension", function() + return it("nested with filter", function() + local list = { + 1, + 2, + 3 + } + local out + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #list do + local i = list[_index_0] + if i % 2 == 1 then + for _index_1 = 1, #list do + local j = list[_index_1] + if j > i then + _accum_0[_len_0] = tostring(i) .. "-" .. tostring(j) + _len_0 = _len_0 + 1 + end + end + end + end + out = _accum_0 + end + return assert.same(out, { + "1-2", + "1-3" + }) + end) +end) diff --git a/spec/outputs/test/destructure_spec.lua b/spec/outputs/test/destructure_spec.lua new file mode 100644 index 0000000..4e18b02 --- /dev/null +++ b/spec/outputs/test/destructure_spec.lua @@ -0,0 +1,29 @@ +return describe("destructure", function() + return it("defaults and nested", function() + local t = { + a = 1, + b = { + c = 3 + }, + d = nil + } + local a, c, d, e = t.a, t.b.c, t.b.d, t.e + if d == nil then + d = 4 + end + if e == nil then + e = 5 + end + return assert.same({ + a, + c, + d, + e + }, { + 1, + 3, + 4, + 5 + }) + end) +end) diff --git a/spec/outputs/test/format_spec.lua b/spec/outputs/test/format_spec.lua index 898fa03..74a511d 100644 --- a/spec/outputs/test/format_spec.lua +++ b/spec/outputs/test/format_spec.lua @@ -94,7 +94,19 @@ local files = { "spec/inputs/unicode/metatable.yue", "spec/inputs/unicode/syntax.yue", "spec/inputs/unicode/global.yue", - "spec/inputs/unicode/plus.yue" + "spec/inputs/unicode/plus.yue", + "spec/inputs/pipe_chain_combo.yue", + "spec/inputs/destructure_defaults.yue", + "spec/inputs/nil_coalesce_precedence.yue", + "spec/inputs/comprehension_nested.yue", + "spec/inputs/with_scope_shadow.yue", + "spec/inputs/export_mixed.yue", + "spec/inputs/unicode/pipe_chain_combo.yue", + "spec/inputs/test/destructure_spec.yue", + "spec/inputs/test/nil_coalescing_spec.yue", + "spec/inputs/test/pipe_spec.yue", + "spec/inputs/test/try_catch_spec.yue", + "spec/inputs/test/comprehension_spec.yue" } local yue = require("yue") local rewriteLineCol diff --git a/spec/outputs/test/nil_coalescing_spec.lua b/spec/outputs/test/nil_coalescing_spec.lua new file mode 100644 index 0000000..90b2a64 --- /dev/null +++ b/spec/outputs/test/nil_coalescing_spec.lua @@ -0,0 +1,23 @@ +local _anon_func_0 = function(a, b) + if a ~= nil then + return a + else + return b + end +end +local _anon_func_1 = function(a, c) + if a ~= nil then + return a + else + return c + end +end +return describe("nil coalescing", function() + return it("distinguish nil and false", function() + local a = nil + local b = false + local c = 0 + assert.same((_anon_func_0(a, b)), false) + return assert.same((_anon_func_1(a, c)), 0) + end) +end) diff --git a/spec/outputs/test/pipe_spec.lua b/spec/outputs/test/pipe_spec.lua new file mode 100644 index 0000000..3d797ed --- /dev/null +++ b/spec/outputs/test/pipe_spec.lua @@ -0,0 +1,13 @@ +return describe("pipe", function() + return it("pipes through functions", function() + local f + f = function(x) + return x + 1 + end + local g + g = function(x) + return x * 2 + end + return assert.same((g(f(3))), 8) + end) +end) diff --git a/spec/outputs/test/try_catch_spec.lua b/spec/outputs/test/try_catch_spec.lua new file mode 100644 index 0000000..2bcfc16 --- /dev/null +++ b/spec/outputs/test/try_catch_spec.lua @@ -0,0 +1,17 @@ +return describe("try/catch", function() + return it("catch and rethrow", function() + local ok, success, err = pcall(function() + return xpcall(function() + return error("boom") + end, function(e) + local _, result = pcall(function() + return error("wrap:" .. e:match("^.-:%d+:%s*(.*)$")) + end) + return result + end) + end) + assert.same(ok, true) + assert.same(success, false) + return assert.is_true(err:match("wrap:boom") ~= nil) + end) +end) diff --git a/spec/outputs/unicode/pipe_chain_combo.lua b/spec/outputs/unicode/pipe_chain_combo.lua new file mode 100644 index 0000000..f48cb5b --- /dev/null +++ b/spec/outputs/unicode/pipe_chain_combo.lua @@ -0,0 +1,9 @@ +local _u52a0 +_u52a0 = function(x) + return x + 2 +end +local _u4e58 +_u4e58 = function(x) + return x * 3 +end +local _u503c = print(tostring(_u4e58(_u52a0(3)))) diff --git a/spec/outputs/with_scope_shadow.lua b/spec/outputs/with_scope_shadow.lua new file mode 100644 index 0000000..72abe95 --- /dev/null +++ b/spec/outputs/with_scope_shadow.lua @@ -0,0 +1,14 @@ +local target = { + val = 1, + add = function(self, n) + self.val = self.val + n + return self.val + end +} +local result +do + local val = 100 + add(2) + result = target +end +return print(result, target.val) diff --git a/src/yue.cpp b/src/yue.cpp index 26f581e..7e39367 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -57,8 +57,8 @@ std::future async(const std::function& f) { #else template std::future async(const std::function& f) { - // fallback: ignore stack size - return std::async(std::launch::async, f); + // fallback: ignore stack size + return std::async(std::launch::async, f); } #endif diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index c4fc952..2ed0265 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.32.3"sv; +const std::string_view version = "0.32.4"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -10872,13 +10872,13 @@ private: } BLOCK_END if (wrapped) { - auto expList = x->new_ptr(); + auto expList = tryFunc->new_ptr(); expList->exprs.push_back(tryFunc); - auto expListAssign = x->new_ptr(); + auto expListAssign = tryFunc->new_ptr(); expListAssign->expList.set(expList); - auto stmt = x->new_ptr(); + auto stmt = tryFunc->new_ptr(); stmt->content.set(expListAssign); - auto block = x->new_ptr(); + auto block = tryFunc->new_ptr(); block->statementOrComments.push_back(stmt); tryFunc.set(block); } -- cgit v1.2.3-55-g6feb