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/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 +++++++++++++++++ 6 files changed, 126 insertions(+), 1 deletion(-) 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 (limited to 'spec/outputs/test') 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) -- cgit v1.2.3-55-g6feb