Repository navigation
Expand file tree
/
Copy pathtest_string_utils.mojo
More file actions
91 lines (80 loc) · 3.43 KB
/
Copy pathtest_string_utils.mojo
File metadata and controls
91 lines (80 loc) · 3.43 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from teetest.tee_test import TeeTest
from string_utils.stringutils import su
from std.collections import list
from std.reflection import source_location, SourceLocation
@always_inline
def test_string_split() raises -> Tuple[Bool, String]:
var s = "a,b,c"
var l = su.split(s, ",")
var assert1 = l[0] == "a"
var assert2 = l[1] == "b"
var assert3 = l[2] == "c"
return assert1 and assert2 and assert3, String(source_location())
@always_inline
def test_string_split_empty() raises -> Tuple[Bool, String]:
var s = ""
var assert1 = len(su.split(s, ",")) == 0
return assert1, String(source_location())
@always_inline
def test_string_split_multi_character_separator() raises -> Tuple[Bool, String]:
var parts = su.split("alpha--beta--gamma", "--")
var assert1 = len(parts) == 3
var assert2 = parts[0] == "alpha"
var assert3 = parts[1] == "beta"
var assert4 = parts[2] == "gamma"
return assert1 and assert2 and assert3 and assert4, String(source_location())
@always_inline
def test_string_justification() raises -> Tuple[Bool, String]:
var assert1 = su.rjust("42", 5, "0") == "00042"
var assert2 = su.ljust("go", 4, ".") == "go.."
var assert3 = su.rjust("wide", 2) == "wide"
return assert1 and assert2 and assert3, String(source_location())
@always_inline
def test_string_endswith() raises -> Tuple[Bool, String]:
var assert1 = su.endswith("hello.mojo", ".mojo")
var assert2 = su.endswith("hello.mojo", "hello", 0, 5)
var assert3 = not su.endswith("hello.mojo", ".py")
return assert1 and assert2 and assert3, String(source_location())
@always_inline
def test_string_trim() raises -> Tuple[Bool, String]:
var assert1 = su.trim("[[value]]", "[", "]") == "value"
var assert2 = su.trim("value", "[", "]") == "value"
var assert3 = su.trim("[]", "[", "]") == ""
return assert1 and assert2 and assert3, String(source_location())
@always_inline
def test_string_find() raises -> Tuple[Bool, String]:
var assert1 = su.find("one two one", "one") == 0
var assert2 = su.find("one two one", "one", 1) == 8
var assert3 = su.find("one two", "three") == -1
return assert1 and assert2 and assert3, String(source_location())
@always_inline
def test_string_character_helpers() raises -> Tuple[Bool, String]:
var assert1 = su.remove_char("banana", "a") == "bnn"
var assert2 = su.count_char("banana", "a") == 3
var assert3 = su.remove_char("banana", "ab") == ""
var assert4 = su.count_char("banana", "ab") == 0
return assert1 and assert2 and assert3 and assert4, String(source_location())
@always_inline
def test_string_substr_and_build_string() raises -> Tuple[Bool, String]:
var assert1 = su.substr("abcdef", 2, 3) == "cde"
var assert2 = su.substr("abcdef", 3) == "def"
var assert3 = su.build_string("x", 4) == "xxxx"
return assert1 and assert2 and assert3, String(source_location())
@always_inline
def test_split_to_strings() raises -> Tuple[Bool, String]:
var assert1 = su.split_to_strings("a,b,c", ",") == "a\nb\nc"
var assert2 = su.split_to_strings("", ",") == ""
return assert1 and assert2, String(source_location())
def main() raises:
TeeTest(
test_string_split,
test_string_split_empty,
test_string_split_multi_character_separator,
test_string_justification,
test_string_endswith,
test_string_trim,
test_string_find,
test_string_character_helpers,
test_string_substr_and_build_string,
test_split_to_strings,
).run_tests(False)