Port test suite for regex from fork

This commit is contained in:
Filip Tibell 2024-04-20 23:30:08 +02:00
parent abe7217a58
commit 96eed54a65
No known key found for this signature in database
4 changed files with 111 additions and 0 deletions

View file

@ -83,6 +83,10 @@ create_tests! {
process_spawn_stdin: "process/spawn/stdin",
process_spawn_stdio: "process/spawn/stdio",
regex_general: "regex/general",
regex_metamethods: "regex/metamethods",
regex_replace: "regex/replace",
require_aliases: "require/tests/aliases",
require_async: "require/tests/async",
require_async_concurrent: "require/tests/async_concurrent",

39
tests/regex/general.luau Normal file
View file

@ -0,0 +1,39 @@
--!nocheck
local regex = require("@lune/regex")
local re = regex.new("[0-9]+")
assert(re:isMatch("look, a number: 1241425") == true)
local mtch = re:find("1337 wow")
assert(mtch ~= nil)
assert(mtch.start == 1)
assert(mtch.finish == 4)
assert(mtch.len == 4)
assert(mtch.text == "1337")
assert(#mtch == mtch.len)
re = regex.new([[([0-9]+) (\d+) \D+ \d+ (\d)]])
local captures = re:captures("1337 125600 wow! 1984 0")
assert(captures ~= nil)
assert(#captures == 3)
assert(captures:get(0).text == "1337 125600 wow! 1984 0")
assert(captures:get(1).text == "1337")
assert(captures:get(2).text == "125600")
assert(captures:get(3).text == "0")
assert(captures:get(4) == nil)
assert(captures:format("$0") == "1337 125600 wow! 1984 0")
assert(captures:format("$3 $2 $1") == "0 125600 1337")
re = regex.new("(?P<first>[^ ]+)[ ]+(?P<last>[^ ]+)(?P<space>[ ]*)")
captures = re:captures("w1 w2 w3 w4")
assert(captures:format("$last $first$space") == "w2 w1 ")
local split = regex.new([[,]]):split("1,2,3,4")
assert(#split == 4)
assert(split[1] == "1")
assert(split[2] == "2")
assert(split[3] == "3")
assert(split[4] == "4")

View file

@ -0,0 +1,15 @@
--!nolint
local regex = require("@lune/regex")
local re = regex.new("[0-9]+")
assert(tostring(re) == "Regex([0-9]+)")
assert(typeof(re) == "Regex")
local mtch = re:find("1337 wow")
assert(tostring(mtch) == "RegexMatch(1337)")
assert(typeof(mtch) == "RegexMatch")
local captures = re:captures("1337 125600 wow! 1984 0")
assert(tostring(captures) == "RegexCaptures(3)")
assert(typeof(captures) == "RegexCaptures")

53
tests/regex/replace.luau Normal file
View file

@ -0,0 +1,53 @@
local regex = require("@lune/regex")
-- Tests taken from the Regex crate
local function replace(
name: string,
pattern: string,
find: string,
replace: string,
expected: string
)
local re = regex.new(pattern)
local replaced = re:replace(find, replace)
if replaced ~= expected then
error(`test '{name}' did not return expected result (expected {expected} got {replaced})`)
end
end
local function replaceAll(
name: string,
pattern: string,
find: string,
replace: string,
expected: string
)
local re = regex.new(pattern)
local replaced = re:replaceAll(find, replace)
if replaced ~= expected then
error(`test '{name}' did not return expected result (expected {expected} got {replaced})`)
end
end
replace("first", "[0-9]", "age: 26", "Z", "age: Z6")
replace("plus", "[0-9]+", "age: 26", "Z", "age: Z")
replaceAll("all", "[0-9]", "age: 26", "Z", "age: ZZ")
replace("groups", "([^ ]+)[ ]+([^ ]+)", "w1 w2", "$2 $1", "w2 w1")
replace("double dollar", "([^ ]+)[ ]+([^ ]+)", "w1 w2", "$2 $$1", "w2 $1")
replaceAll(
"named",
"(?P<first>[^ ]+)[ ]+(?P<last>[^ ]+)(?P<space>[ ]*)",
"w1 w2 w3 w4",
"$last $first$space",
"w2 w1 w4 w3"
)
replaceAll("trim", "^[ \t]+|[ \t]+$", " \t trim me\t \t", "", "trim me")
replace("number hypen", "(.)(.)", "ab", "$1-$2", "a-b")
replaceAll("simple expand", "([a-z]) ([a-z])", "a b", "$2 $1", "b a")
replaceAll("literal dollar 1", "([a-z]+) ([a-z]+)", "a b", "$$1", "$1")
replaceAll("literal dollar 2", "([a-z]+) ([a-z]+)", "a b", "$2 $$c $1", "b $c a")
replaceAll("match at start replace with empty", "foo", "foobar", "", "bar")
replace("single empty match", "^", "bar", "foo", "foobar")