summaryrefslogtreecommitdiff
path: root/tests/modules/string/test.lua
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-06-08 13:34:02 +0800
committerOpportunityLiu <[email protected]>2019-06-08 13:34:02 +0800
commit1af88102687e493cab40e62b0a2b8b7d42417ae6 (patch)
tree272a80b424e68024a9ecda138159864d9659fe5a /tests/modules/string/test.lua
parente165a8e43abed4eb2eca64c3b5adefb57cd65600 (diff)
improve test lib
Diffstat (limited to 'tests/modules/string/test.lua')
-rw-r--r--tests/modules/string/test.lua60
1 files changed, 26 insertions, 34 deletions
diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua
index 46689dd0f..04ee715ca 100644
--- a/tests/modules/string/test.lua
+++ b/tests/modules/string/test.lua
@@ -1,48 +1,40 @@
-function assert_array_eq(t1, t2)
- assert(table.is_array(t1))
- assert(table.is_array(t2))
- assert(#t1 == #t2, "#t1(%d) != #t2(%d)", #t1, #t2)
- for idx, value in ipairs(t1) do
- assert(value == t2[idx], "value[%d]: %s != %s", idx, value, t2[idx])
- end
-end
-function test_endswith()
- assert(not ("rc"):endswith("xcadas"))
- assert(("aaaccc"):endswith("ccc"))
+function test_endswith(t)
+ t:require(("aaaccc"):endswith("ccc"))
+ t:require(("aaaccc"):endswith("aaaccc"))
+ t:require_not(("rc"):endswith("xcadas"))
+ t:require_not(("aaaccc "):endswith("%s"))
end
-function test_startswith()
- assert(("aaaccc"):startswith("aaa"))
+function test_startswith(t)
+ t:require(("aaaccc"):startswith("aaa"))
+ t:require(("aaaccc"):startswith("aaaccc"))
+ t:require_not(("rc"):startswith("xcadas"))
+ t:require_not((" aaaccc"):startswith("%s"))
end
-function test_split()
+function test_split(t)
-- pattern match and ignore empty string
- assert_array_eq(("1\n\n2\n3"):split('\n'), {"1", "2", "3"})
- assert_array_eq(("abc123123xyz123abc"):split('123'), {"abc", "xyz", "abc"})
- assert_array_eq(("abc123123xyz123abc"):split('[123]+'), {"abc", "xyz", "abc"})
-
+ t:are_equal(("1\n\n2\n3"):split('\n'), {"1", "2", "3"})
+ t:are_equal(("abc123123xyz123abc"):split('123'), {"abc", "xyz", "abc"})
+ t:are_equal(("abc123123xyz123abc"):split('[123]+'), {"abc", "xyz", "abc"})
+
-- plain match and ignore empty string
- assert_array_eq(("1\n\n2\n3"):split('\n', {plain = true}), {"1", "2", "3"})
- assert_array_eq(("abc123123xyz123abc"):split('123', {plain = true}), {"abc", "xyz", "abc"})
+ t:are_equal(("1\n\n2\n3"):split('\n', {plain = true}), {"1", "2", "3"})
+ t:are_equal(("abc123123xyz123abc"):split('123', {plain = true}), {"abc", "xyz", "abc"})
-- pattern match and contains empty string
- assert_array_eq(("1\n\n2\n3"):split('\n', {strict = true}), {"1", "", "2", "3"})
- assert_array_eq(("abc123123xyz123abc"):split('123', {strict = true}), {"abc", "", "xyz", "abc"})
- assert_array_eq(("abc123123xyz123abc"):split('[123]+', {strict = true}), {"abc", "xyz", "abc"})
+ t:are_equal(("1\n\n2\n3"):split('\n', {strict = true}), {"1", "", "2", "3"})
+ t:are_equal(("abc123123xyz123abc"):split('123', {strict = true}), {"abc", "", "xyz", "abc"})
+ t:are_equal(("abc123123xyz123abc"):split('[123]+', {strict = true}), {"abc", "xyz", "abc"})
-- plain match and contains empty string
- assert_array_eq(("1\n\n2\n3"):split('\n', {plain = true, strict = true}), {"1", "", "2", "3"})
- assert_array_eq(("abc123123xyz123abc"):split('123', {plain = true, strict = true}), {"abc", "", "xyz", "abc"})
+ t:are_equal(("1\n\n2\n3"):split('\n', {plain = true, strict = true}), {"1", "", "2", "3"})
+ t:are_equal(("abc123123xyz123abc"):split('123', {plain = true, strict = true}), {"abc", "", "xyz", "abc"})
-- limit split count
- assert_array_eq(("1\n\n2\n3"):split('\n', {limit = 2}), {"1", "2\n3"})
- assert_array_eq(("1\n\n2\n3"):split('\n', {limit = 2, strict = true}), {"1", "\n2\n3"})
- assert_array_eq(("1.2.3.4.5"):split('%.', {limit = 3}), {"1", "2", "3.4.5"})
-end
-
-function main()
- test_split()
- test_startswith()
- test_endswith()
+ t:are_equal(("1\n\n2\n3"):split('\n', {limit = 2}), {"1", "2\n3"})
+ t:are_equal(("1\n\n2\n3"):split('\n', {limit = 2, strict = true}), {"1", "\n2\n3"})
+ t:are_equal(("1.2.3.4.5"):split('%.', {limit = 3}), {"1", "2", "3.4.5"})
+ t:are_equal(("123.45"):split('%.', {limit = 3}), {"123", "45"})
end