diff options
| author | ruki <[email protected]> | 2019-06-05 22:51:26 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-06-05 11:08:41 +0800 |
| commit | 06dba0daf4968b9632ebc00a9018c6af044422df (patch) | |
| tree | 511cb7366de4cfb613e327998f6520a80dffa638 /tests/modules/string/test.lua | |
| parent | ab59ae06a2b2560912446d20fe4eef1e49e3ed55 (diff) | |
add string.split test
Diffstat (limited to 'tests/modules/string/test.lua')
| -rw-r--r-- | tests/modules/string/test.lua | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua index 98c76661f..46689dd0f 100644 --- a/tests/modules/string/test.lua +++ b/tests/modules/string/test.lua @@ -1,5 +1,48 @@ -function main() +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")) +end + +function test_startswith() assert(("aaaccc"):startswith("aaa")) end + +function test_split() + -- 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"}) + + -- 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"}) + + -- 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"}) + + -- 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"}) + + -- 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() +end |
