diff --git a/src/tests.rs b/src/tests.rs index fad6026..b14566f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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", diff --git a/tests/regex/general.luau b/tests/regex/general.luau new file mode 100644 index 0000000..24a73b5 --- /dev/null +++ b/tests/regex/general.luau @@ -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[^ ]+)[ ]+(?P[^ ]+)(?P[ ]*)") +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") diff --git a/tests/regex/metamethods.luau b/tests/regex/metamethods.luau new file mode 100644 index 0000000..66741d8 --- /dev/null +++ b/tests/regex/metamethods.luau @@ -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") diff --git a/tests/regex/replace.luau b/tests/regex/replace.luau new file mode 100644 index 0000000..dc6b9f4 --- /dev/null +++ b/tests/regex/replace.luau @@ -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[^ ]+)[ ]+(?P[^ ]+)(?P[ ]*)", + "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")