From 6699ac783fb894cf44625cf1e626e4a8863fbe20 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 8 Feb 2025 20:32:42 +0100 Subject: [PATCH] Added some more string tests --- common/space_lua/language_core_test.lua | 26 ++++++++++++++++++------- common/space_lua/stdlib/string_test.lua | 4 ++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/common/space_lua/language_core_test.lua b/common/space_lua/language_core_test.lua index a97c86a7..b5da8684 100644 --- a/common/space_lua/language_core_test.lua +++ b/common/space_lua/language_core_test.lua @@ -1,4 +1,4 @@ -local function assert_equal(a, b) +local function assertEqual(a, b) if a ~= b then error("Assertion failed: " .. a .. " is not equal to " .. b) end @@ -18,8 +18,8 @@ assert(a + b == 3) -- Basic string stuff assert("Hello " .. "world" == "Hello world") -assert_equal([[Hello world]], "Hello world") -assert_equal([==[Hello [[world]]!]==], "Hello [[world]]!") +assertEqual([[Hello world]], "Hello world") +assertEqual([==[Hello [[world]]!]==], "Hello [[world]]!") -- Various forms of function definitions function f1() @@ -136,7 +136,7 @@ mt = { t = setmetatable({}, mt) t.bar = "bar" assert(t.bar == "bar") -assert_equal(t.foo, "Key not found: foo") +assertEqual(t.foo, "Key not found: foo") -- Test the __newindex metamethod t = setmetatable( @@ -152,8 +152,8 @@ t = setmetatable( t.name = "John" -- rawset ignores the metamethod rawset(t, "age", 100) -assert_equal(t.name, "Value: John") -assert_equal(t.age, 100) +assertEqual(t.name, "Value: John") +assertEqual(t.age, 100) -- Test some of the operator metamethods t = setmetatable( @@ -420,4 +420,16 @@ end assert(#points == 6, "Grid should generate 6 points") assert(points[1][1] == 1 and points[1][2] == 1, "First point should be (1,1)") -assert(points[6][1] == 2 and points[6][2] == 3, "Last point should be (2,3)") \ No newline at end of file +assert(points[6][1] == 2 and points[6][2] == 3, "Last point should be (2,3)") + +-- Test for functions with variable number of arguments +function sum(...) + local total = 0 + for i, v in ipairs({ ... }) do + total = total + v + end + return total +end + +assertEqual(sum(1, 2, 3), 6) +assertEqual(sum(1, 2, 3, 4, 5), 15) diff --git a/common/space_lua/stdlib/string_test.lua b/common/space_lua/stdlib/string_test.lua index 0a532bf3..82a2a2b7 100644 --- a/common/space_lua/stdlib/string_test.lua +++ b/common/space_lua/stdlib/string_test.lua @@ -76,6 +76,10 @@ local m1, m2 = string.match("hello world", "(h)(ello)") assertEqual(m1, "h") assertEqual(m2, "ello") +-- Test with pattern with character class +assertEqual(string.match("c", "[abc]"), "c") + + -- Test match with init position - need to capture the group local initMatch = string.match("hello world", "(world)", 7) assertEqual(initMatch, "world")