2025-02-06 17:04:45 +08:00
|
|
|
local function assertEqual(a, b)
|
2025-01-16 03:47:58 +08:00
|
|
|
if a ~= b then
|
|
|
|
error("Assertion failed: " .. a .. " is not equal to " .. b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Basic string functions
|
|
|
|
assert(string.len("Hello") == 5)
|
|
|
|
assert(string.byte("Hello", 1) == 72)
|
|
|
|
assert(string.char(72) == "H")
|
|
|
|
assert(string.find("Hello", "l") == 3)
|
|
|
|
assert(string.rep("Hello", 3) == "HelloHelloHello")
|
|
|
|
assert(string.sub("Hello", 2, 4) == "ell")
|
|
|
|
assert(string.upper("Hello") == "HELLO")
|
|
|
|
assert(string.lower("Hello") == "hello")
|
|
|
|
|
2025-02-09 18:06:15 +08:00
|
|
|
-- Invoke string metatable methods
|
|
|
|
assertEqual(("hello"):len(), 5)
|
|
|
|
assertEqual(("hello"):upper(), "HELLO")
|
|
|
|
|
2025-01-16 03:47:58 +08:00
|
|
|
-- Test string.gsub with various replacement types
|
|
|
|
-- Simple string replacement
|
|
|
|
local result, count = string.gsub("hello world", "hello", "hi")
|
|
|
|
assert(result == "hi world", "Basic string replacement failed")
|
|
|
|
assert(count == 1, "Basic replacement count failed")
|
|
|
|
|
|
|
|
-- Multiple replacements
|
|
|
|
result, count = string.gsub("hello hello hello", "hello", "hi")
|
|
|
|
assert(result == "hi hi hi", "Multiple replacements failed")
|
|
|
|
assert(count == 3, "Multiple replacement count failed")
|
|
|
|
|
|
|
|
-- Limited replacements with n parameter
|
|
|
|
result, count = string.gsub("hello hello hello", "hello", "hi", 2)
|
|
|
|
assert(result == "hi hi hello", "Limited replacements failed")
|
|
|
|
assert(count == 2, "Limited replacement count failed")
|
|
|
|
|
|
|
|
-- Function replacement without captures
|
|
|
|
result = string.gsub("hello world", "hello", function(match)
|
|
|
|
assert(match == "hello", "Function received incorrect match")
|
|
|
|
return string.upper(match)
|
|
|
|
end)
|
|
|
|
assert(result == "HELLO world", "Function replacement without captures failed")
|
|
|
|
|
|
|
|
-- Function replacement with single capture
|
|
|
|
result = string.gsub("hello world", "(h)ello", function(h)
|
|
|
|
assert(h == "h", "Function received incorrect capture")
|
|
|
|
return string.upper(h) .. "i"
|
|
|
|
end)
|
|
|
|
assert(result == "Hi world", "Function replacement with single capture failed")
|
|
|
|
|
|
|
|
-- Function replacement with multiple captures
|
|
|
|
result = string.gsub("hello world", "(h)(e)(l)(l)o", function(h, e, l1, l2)
|
|
|
|
assert(h == "h" and e == "e" and l1 == "l" and l2 == "l",
|
|
|
|
"Function received incorrect captures: " .. h .. ", " .. e .. ", " .. l1 .. ", " .. l2)
|
|
|
|
return string.upper(h) .. string.upper(e) .. l1 .. l2 .. "o"
|
|
|
|
end)
|
|
|
|
assert(result == "HEllo world", "Function replacement with multiple captures failed")
|
|
|
|
|
|
|
|
-- Function returning nil (should keep original match)
|
|
|
|
result = string.gsub("hello world", "hello", function() return nil end)
|
|
|
|
assert(result == "hello world", "Function returning nil failed")
|
|
|
|
|
|
|
|
-- Pattern with multiple matches on same position
|
|
|
|
result = string.gsub("hello world", "h?e", "X")
|
|
|
|
assert(result == "Xllo world", "Overlapping matches failed")
|
|
|
|
|
|
|
|
-- Empty captures
|
|
|
|
result = string.gsub("hello", "(h()e)", function(full, empty)
|
|
|
|
assert(full == "he" and empty == "", "Empty capture handling failed")
|
|
|
|
return "XX"
|
|
|
|
end)
|
|
|
|
assert(result == "XXllo", "Empty capture replacement failed")
|
|
|
|
|
|
|
|
-- Patterns with magic characters
|
|
|
|
result = string.gsub("hello.world", "%.", "-")
|
2025-02-06 15:35:34 +08:00
|
|
|
assert(result == "hello-world", "Magic character replacement failed")
|
2025-01-16 03:47:58 +08:00
|
|
|
|
|
|
|
-- Test string.match
|
|
|
|
local m1, m2 = string.match("hello world", "(h)(ello)")
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(m1, "h")
|
|
|
|
assertEqual(m2, "ello")
|
2025-01-16 03:47:58 +08:00
|
|
|
|
2025-02-09 03:32:42 +08:00
|
|
|
-- Test with pattern with character class
|
|
|
|
assertEqual(string.match("c", "[abc]"), "c")
|
|
|
|
|
2025-01-16 03:47:58 +08:00
|
|
|
-- Test match with init position - need to capture the group
|
2025-02-06 17:04:45 +08:00
|
|
|
local initMatch = string.match("hello world", "(world)", 7)
|
|
|
|
assertEqual(initMatch, "world")
|
2025-01-16 03:47:58 +08:00
|
|
|
|
|
|
|
-- Test string.gmatch
|
|
|
|
local words = {}
|
|
|
|
for word in string.gmatch("hello world lua", "%w+") do
|
|
|
|
table.insert(words, word)
|
|
|
|
end
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(words[1], "hello")
|
|
|
|
assertEqual(words[2], "world")
|
|
|
|
assertEqual(words[3], "lua")
|
2025-01-16 03:47:58 +08:00
|
|
|
|
|
|
|
-- Test string.reverse
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(string.reverse("hello"), "olleh")
|
|
|
|
assertEqual(string.reverse(""), "")
|
2025-01-16 03:47:58 +08:00
|
|
|
|
|
|
|
-- Test string.split
|
|
|
|
local parts = string.split("a,b,c", ",")
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(parts[1], "a")
|
|
|
|
assertEqual(parts[2], "b")
|
|
|
|
assertEqual(parts[3], "c")
|
2025-01-16 03:47:58 +08:00
|
|
|
|
|
|
|
-- Test non-standard string extensions
|
2025-02-10 01:37:21 +08:00
|
|
|
assertEqual(string.startsWith("hello world", "hello"), true)
|
|
|
|
assertEqual(string.startsWith("hello world", "world"), false)
|
2025-01-16 03:47:58 +08:00
|
|
|
|
2025-02-10 01:37:21 +08:00
|
|
|
assertEqual(string.endsWith("hello world", "world"), true)
|
|
|
|
assertEqual(string.endsWith("hello world", "hello"), false)
|
2025-01-16 03:47:58 +08:00
|
|
|
|
2025-01-16 22:33:18 +08:00
|
|
|
-- Extended string.match tests
|
|
|
|
-- Basic pattern matching
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(string.match("hello", "h"), "h")
|
|
|
|
assertEqual(string.match("hello", "hello"), "hello")
|
2025-01-16 22:33:18 +08:00
|
|
|
|
|
|
|
-- Test with no matches
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(string.match("hello", "x"), nil)
|
2025-01-16 22:33:18 +08:00
|
|
|
|
|
|
|
-- Test with captures
|
|
|
|
local m1, m2 = string.match("hello", "(h)(ello)")
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(m1, "h")
|
|
|
|
assertEqual(m2, "ello")
|
2025-01-16 22:33:18 +08:00
|
|
|
|
|
|
|
-- Test with init position
|
2025-02-06 17:04:45 +08:00
|
|
|
local initMatch = string.match("hello world", "(world)", 7)
|
|
|
|
assertEqual(initMatch, "world")
|
2025-01-16 22:33:18 +08:00
|
|
|
|
|
|
|
-- Test init position with no match
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(string.match("hello world", "hello", 7), nil)
|
2025-01-16 22:33:18 +08:00
|
|
|
|
|
|
|
-- Test pattern characters
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(string.match("123", "%d+"), "123")
|
|
|
|
assertEqual(string.match("abc123", "%a+"), "abc")
|
|
|
|
assertEqual(string.match(" abc", "%s+"), " ")
|
2025-01-16 22:33:18 +08:00
|
|
|
|
|
|
|
-- Test multiple captures
|
|
|
|
local day, month, year = string.match("2024-03-14", "(%d+)-(%d+)-(%d+)")
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(day, "2024")
|
|
|
|
assertEqual(month, "03")
|
|
|
|
assertEqual(year, "14")
|
2025-01-16 22:33:18 +08:00
|
|
|
|
|
|
|
-- Test optional captures
|
|
|
|
local word = string.match("The quick brown fox", "%s*(%w+)%s*")
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(word, "The")
|
2025-01-16 22:33:18 +08:00
|
|
|
|
2025-02-06 17:04:45 +08:00
|
|
|
-- Test matchRegexAll
|
2025-02-06 15:35:34 +08:00
|
|
|
local matches = {}
|
2025-02-06 17:04:45 +08:00
|
|
|
for match in string.matchRegexAll("hellolllbl", "(l+)") do
|
2025-02-06 15:35:34 +08:00
|
|
|
table.insert(matches, match)
|
|
|
|
end
|
2025-02-06 17:04:45 +08:00
|
|
|
assertEqual(#matches, 3)
|
|
|
|
assertEqual(matches[1][1], "ll")
|
|
|
|
assertEqual(matches[2][1], "lll")
|
|
|
|
assertEqual(matches[3][1], "l")
|