summaryrefslogtreecommitdiff
path: root/tests/modules/string/test.lua
blob: 04ee715cae28b8aaf2be746b36f9fbc65fb53990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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(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(t)
    -- pattern match and ignore empty string
    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
    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
    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
    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
    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