Skip to content

Commit

Permalink
Added some more string tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zefhemel committed Feb 8, 2025
1 parent d57792a commit 6699ac7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
26 changes: 19 additions & 7 deletions common/space_lua/language_core_test.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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)")
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)
4 changes: 4 additions & 0 deletions common/space_lua/stdlib/string_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6699ac7

Please sign in to comment.