3 say("Testing strings like $str")
5 assert str.upper() == "HELLO AMÉLIE!"
6 assert str.lower() == "hello amélie!"
7 assert str.lower().title() == "Hello Amélie!"
10 assert "I".lower() == "i"
11 assert "I".lower(language="tr_TR") == "ı"
13 assert "i".upper() == "I"
14 assert "i".upper(language="tr_TR") == "İ"
16 assert "ian".title() == "Ian"
17 assert "ian".title(language="tr_TR") == "İan"
19 assert "I".caseless_equals("ı") == no
20 assert "I".caseless_equals("ı", language="tr_TR") == yes
24 assert str[99] == none
26 assert "\{UE9}" == "é"
28 assert "\{U65}\{U301}" == "é"
30 assert "\{Penguin}".codepoint_names() == ["PENGUIN"]
32 assert "\[31;1]" == "\e[31;1m"
34 assert "\{UE9}" == "\{U65}\{U301}"
36 amelie := "Am\{UE9}lie"
37 assert amelie.split() == ["A", "m", "é", "l", "i", "e"]
38 assert amelie.utf32() == [65, 109, 233, 108, 105, 101]
39 assert amelie.utf8() == [0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65]
40 assert Text.from_utf8([0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65])! == "Amélie"
41 assert Text.from_utf8([Byte(0xFF)]) == none
43 amelie2 := "Am\{U65}\{U301}lie"
44 assert amelie2.split() == ["A", "m", "é", "l", "i", "e"]
45 assert amelie2.utf32() == [65, 109, 233, 108, 105, 101]
46 assert amelie2.utf8() == [0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65]
48 assert amelie.codepoint_names() == ["LATIN CAPITAL LETTER A", "LATIN SMALL LETTER M", "LATIN SMALL LETTER E WITH ACUTE", "LATIN SMALL LETTER L", "LATIN SMALL LETTER I", "LATIN SMALL LETTER E"]
49 assert amelie2.codepoint_names() == ["LATIN CAPITAL LETTER A", "LATIN SMALL LETTER M", "LATIN SMALL LETTER E WITH ACUTE", "LATIN SMALL LETTER L", "LATIN SMALL LETTER I", "LATIN SMALL LETTER E"]
51 assert "Hello".replace("e", "X") == "HXllo"
53 assert "Hello".has("l") == yes
54 assert "Hello".has("x") == no
56 assert "Hello".replace("l", "") == "Heo"
57 assert "xxxx".replace("x", "") == ""
58 assert "xxxx".replace("y", "") == "xxxx"
59 assert "One two three four five six".replace("e ", "") == "Ontwo threfour fivsix"
60 assert "Hello".replace("", "xxx") == "Hello"
61 assert "".replace("", "xxx") == ""
63 assert amelie.has(amelie2) == yes
69 assert multiline == "line one\nline two"
71 say("Interpolation tests:")
72 assert "A $(1+2)" == "A 3"
73 assert "A \$(1+2)" == "A \$(1+2)"
74 assert 'A $(1+2)' == "A 3"
75 assert `A @(1+2)` == "A 3"
78 assert c.codepoint_names() == ["LATIN CAPITAL LETTER E WITH ACUTE", "COMBINING VERTICAL LINE BELOW"]
79 assert c == Text.from_codepoint_names(c.codepoint_names())!
80 assert c == Text.from_utf32(c.utf32())!
81 assert c == Text.from_utf8(c.utf8())!
83 assert "one\ntwo\nthree".lines() == ["one", "two", "three"]
84 assert "one\ntwo\nthree\n".lines() == ["one", "two", "three"]
85 assert "one\ntwo\nthree\n\n".lines() == ["one", "two", "three", ""]
86 assert "one\r\ntwo\r\nthree\r\n".lines() == ["one", "two", "three"]
87 assert "".lines() == []
89 say("Test splitting and joining text:")
90 assert "one,, two,three".split(",") == ["one", "", " two", "three"]
91 assert [t for t in "one,, two,three".by_split(",")] == ["one", "", " two", "three"]
92 assert "one,, two,three".split_any(", ") == ["one", "two", "three"]
93 assert [t for t in "one,, two,three".by_split_any(", ")] == ["one", "two", "three"]
94 assert ",one,, two,three,".split(",") == ["", "one", "", " two", "three", ""]
95 assert [t for t in ",one,, two,three,".by_split(",")] == ["", "one", "", " two", "three", ""]
96 assert ",one,, two,three,".split_any(", ") == ["", "one", "two", "three", ""]
97 assert [t for t in ",one,, two,three,".by_split_any(", ")] == ["", "one", "two", "three", ""]
99 assert "abc".split() == ["a", "b", "c"]
101 assert "one two three".split_any() == ["one", "two", "three"]
103 assert ", ".join(["one", "two", "three"]) == "one, two, three"
105 assert "".join(["one", "two", "three"]) == "onetwothree"
107 assert "+".join(["one"]) == "one"
109 assert "+".join([]) == ""
111 assert "".split() == []
113 say("Test text slicing:")
114 assert "abcdef".slice() == "abcdef"
115 assert "abcdef".slice(from=3) == "cdef"
116 assert "abcdef".slice(to=-2) == "abcde"
117 assert "abcdef".slice(from=2, to=4) == "bcd"
118 assert "abcdef".slice(from=5, to=1) == ""
122 assert house.length == 1
123 assert house.codepoint_names() == ["CJK Unified Ideographs-5BB6"]
124 assert house.utf32() == [23478]
126 assert "🐧".codepoint_names() == ["PENGUIN"]
128 assert Text.from_codepoint_names(["not a valid name here buddy"]) == none
130 assert "Hello".replace("ello", "i") == "Hi"
132 assert "<tag>".translate({"<": "<", ">": ">"}) == "<tag>"
134 assert "Abc".repeat(3) == "AbcAbcAbc"
136 assert "abcde".starts_with("ab") == yes
137 assert "abcde".starts_with("bc") == no
139 assert "abcde".ends_with("de") == yes
140 assert "abcde".starts_with("cd") == no
142 assert "abcde".without_prefix("ab") == "cde"
143 assert "abcde".without_suffix("ab") == "abcde"
145 assert "abcde".without_prefix("de") == "abcde"
146 assert "abcde".without_suffix("de") == "abc"
148 assert ("hello" ++ " " ++ "Amélie").reversed() == "eilémA olleh"
151 say("Testing concatenation-stability:")
152 ab := Text.from_codepoint_names(["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"])!
153 assert ab.codepoint_names() == ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
154 assert ab.length == 1
156 a := Text.from_codepoint_names(["LATIN SMALL LETTER E"])!
157 b := Text.from_codepoint_names(["COMBINING VERTICAL LINE BELOW"])!
158 assert (a++b).codepoint_names() == ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
160 assert (a++b).length == 1
164 concat := "e" ++ Text.from_utf32([Int32(0x300)])!
165 assert concat.length == 1
167 concat2 := concat ++ Text.from_utf32([Int32(0x302)])!
168 assert concat2.length == 1
170 concat3 := concat2 ++ Text.from_utf32([Int32(0x303)])!
171 assert concat3.length == 1
173 final := Text.from_utf32([Int32(0x65), Int32(0x300), Int32(0x302), Int32(0x303)])!
174 assert final.length == 1
175 assert concat3 == final
177 concat4 := Text.from_utf32([Int32(0x65), Int32(0x300)])! ++ Text.from_utf32([Int32(0x302), Int32(0x303)])!
178 assert concat4.length == 1
179 assert concat4 == final
181 assert "x".left_pad(5) == " x"
182 assert "x".right_pad(5) == "x "
183 assert "x".middle_pad(5) == " x "
184 assert "1234".left_pad(8, "XYZ") == "XYZX1234"
185 assert "1234".right_pad(8, "XYZ") == "1234XYZX"
186 assert "1234".middle_pad(9, "XYZ") == "XY1234XYZ"
188 assert amelie.width() == 6
190 assert " one, ".trim(" ,") == "one"
191 assert " one, ".trim(" ,", left=no) == " one"
192 assert " one, ".trim(" ,", right=no) == "one, "
193 assert " ".trim(" ,") == ""
194 assert " ".trim(" ,", left=no) == ""
198 assert test.utf32() == [150370]
199 assert test.utf16() == [-10158, -8350]
200 assert test.utf8() == [0xf0, 0xa4, 0xad, 0xa2]
202 assert Text.from_utf32([150370]) == test
203 assert Text.from_utf16([-10158, -8350]) == test
204 assert Text.from_utf8([0xf0, 0xa4, 0xad, 0xa2]) == test
207 assert "one two".find("one") == 1
208 assert "one two".find("two") == 5
209 assert "one two".find("three") == none
210 assert "one two".find("o", start=2) == 7
213 assert "hello".distance("hello") == 0
214 assert "hello".distance("goodbye") > 2.0
215 assert "hello".distance("hola") < "hello".distance("goodbye")
216 assert "hello".distance("Hello") <= 1.0
217 assert "hello".distance("xello") <= 1.0
218 assert "hello".distance("ehllo") <= "hello".distance("XXllo")
219 assert "shffle".distance("shuffle") <= "shffle".distance("sample")