From 5e5bcf37450d07f7f2812255bbd1df35d8e6ce75 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 27 Oct 2025 08:43:22 +0100 Subject: verbose_errors improvement * Use std::format instead of sprintf for verbose errors when decoding table keys * Add a unit test for the different table key types --- unit_tests/UnitTests.vcxproj | 1 + unit_tests/UnitTests.vcxproj.filters | 3 +++ unit_tests/legacy_tests.cpp | 3 ++- unit_tests/misc_tests.cpp | 9 ++++++++ unit_tests/scripts/misc/verbose_errors.lua | 34 ++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 unit_tests/scripts/misc/verbose_errors.lua (limited to 'unit_tests') diff --git a/unit_tests/UnitTests.vcxproj b/unit_tests/UnitTests.vcxproj index 300eb9b..bb0a2af 100644 --- a/unit_tests/UnitTests.vcxproj +++ b/unit_tests/UnitTests.vcxproj @@ -1163,6 +1163,7 @@ + diff --git a/unit_tests/UnitTests.vcxproj.filters b/unit_tests/UnitTests.vcxproj.filters index 57d8918..9b3d023 100644 --- a/unit_tests/UnitTests.vcxproj.filters +++ b/unit_tests/UnitTests.vcxproj.filters @@ -159,5 +159,8 @@ Scripts\linda + + Scripts\misc + \ No newline at end of file diff --git a/unit_tests/legacy_tests.cpp b/unit_tests/legacy_tests.cpp index 84581d2..d66937c 100644 --- a/unit_tests/legacy_tests.cpp +++ b/unit_tests/legacy_tests.cpp @@ -33,7 +33,8 @@ MAKE_TEST_CASE(func_is_string) MAKE_TEST_CASE(irayo_closure) MAKE_TEST_CASE(irayo_recursive) MAKE_TEST_CASE(keeper) -//MAKE_TEST_CASE(linda_perf) +MAKE_TEST_CASE(lanes_as_upvalue) +// MAKE_TEST_CASE(linda_perf) MAKE_TEST_CASE(manual_register) MAKE_TEST_CASE(nameof) MAKE_TEST_CASE(objects) diff --git a/unit_tests/misc_tests.cpp b/unit_tests/misc_tests.cpp index a2199aa..3848a75 100644 --- a/unit_tests/misc_tests.cpp +++ b/unit_tests/misc_tests.cpp @@ -191,3 +191,12 @@ TEST_CASE("misc.convert_max_attempts.is_respected") " l:send('k', t)" // send the table, it should raise an error because the converter retries too many times ); } + +#define MAKE_TEST_CASE(DIR, FILE, CONDITION) \ + TEST_CASE("scripted_tests." #DIR "." #FILE) \ + { \ + FileRunner _runner(R"(.\unit_tests\scripts)"); \ + _runner.performTest(FileRunnerParam{ #DIR "/" #FILE, TestType::CONDITION }); \ + } + +MAKE_TEST_CASE(misc, verbose_errors, AssertNoLuaError) diff --git a/unit_tests/scripts/misc/verbose_errors.lua b/unit_tests/scripts/misc/verbose_errors.lua new file mode 100644 index 0000000..40948ca --- /dev/null +++ b/unit_tests/scripts/misc/verbose_errors.lua @@ -0,0 +1,34 @@ +local fixture = require "fixture" -- require fixture before lanes so that it is not registered and will cause transfer errors +local lanes = require("lanes").configure{verbose_errors = true} +local l = lanes.linda{name = "my linda"} + + +local do_test = function(key_) + local t = {[key_] = fixture.newuserdata()} + local b, e = pcall(l.send, l, "k", t) + assert(b == false and type(e) == "string") + local t_key = type(key_) + if t_key == "string" then + local x, y = string.find(e, "arg#2." .. key_, 1, true) + assert(x and y, "got " .. e) + elseif t_key == "boolean" then + local x, y = string.find(e, "arg#2[" .. tostring(key_) .. "]", 1, true) + assert(x and y, "got " .. e) + elseif t_key == "number" then + local x, y = string.find(e, "arg#2[" .. key_ .. "]", 1, true) + assert(x and y, "got " .. e) + elseif t_key == "userdata" then + local t_name + -- light userdata is formatted by std::format, where the pointer is written as a lowercase hex literal + local expected = "arg#2[U:0x" .. string.lower(string.match(tostring(key_), "userdata: (%x+)")) .. "]" + local x, y = string.find(e, expected, 1, true) + assert(x and y, "expecting " .. expected .. " got " .. e) + end +end + +do_test("bob") +do_test(true) +do_test(false) +do_test(42) +do_test(42.44) +do_test(lanes.null) -- cgit v1.2.3-55-g6feb