aboutsummaryrefslogtreecommitdiff
path: root/test/text.tm
blob: c12e7a78a459fce1250e27dc464e969063259d11 (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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
func main()
	str := "Hello Amélie!"
	say("Testing strings like $str")

	assert str.upper() == "HELLO AMÉLIE!"
	assert str.lower() == "hello amélie!"
	assert str.lower().title() == "Hello Amélie!"
	assert str[1] == "H"

	assert "I".lower() == "i"
	assert "I".lower(language="tr_TR") == "ı"

	assert "i".upper() == "I"
	assert "i".upper(language="tr_TR") == "İ"

	assert "ian".title() == "Ian"
	assert "ian".title(language="tr_TR") == "İan"

	assert "I".caseless_equals("ı") == no
	assert "I".caseless_equals("ı", language="tr_TR") == yes

	assert str[9] == "é"

	assert str[99] == none

	assert "\{UE9}" == "é"

	assert "\{U65}\{U301}" == "é"

	assert "\{Penguin}".codepoint_names() == ["PENGUIN"]

	assert "\[31;1]" == "\e[31;1m"

	assert "\{UE9}" == "\{U65}\{U301}"

	amelie := "Am\{UE9}lie"
	assert amelie.split() == ["A", "m", "é", "l", "i", "e"]
	assert amelie.utf32() == [65, 109, 233, 108, 105, 101]
	assert amelie.utf8() == [0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65]
	assert Text.from_utf8([0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65])! == "Amélie"
	assert Text.from_utf8([Byte(0xFF)]) == none

	amelie2 := "Am\{U65}\{U301}lie"
	assert amelie2.split() == ["A", "m", "é", "l", "i", "e"]
	assert amelie2.utf32() == [65, 109, 233, 108, 105, 101]
	assert amelie2.utf8() == [0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65]

	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"]
	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"]

	assert "Hello".replace("e", "X") == "HXllo"

	assert "Hello".has("l") == yes
	assert "Hello".has("x") == no

	assert "Hello".replace("l", "") == "Heo"
	assert "xxxx".replace("x", "") == ""
	assert "xxxx".replace("y", "") == "xxxx"
	assert "One two three four five six".replace("e ", "") == "Ontwo threfour fivsix"

	assert amelie.has(amelie2) == yes

	multiline := "
		line one
		line two
	"
	assert multiline == "line one\nline two"

	say("Interpolation tests:")
	assert "A $(1+2)" == "A 3"
	assert "A \$(1+2)" == "A \$(1+2)"
	assert 'A $(1+2)' == "A 3"
	assert `A @(1+2)` == "A 3"

	c := "É̩"
	assert c.codepoint_names() == ["LATIN CAPITAL LETTER E WITH ACUTE", "COMBINING VERTICAL LINE BELOW"]
	assert c == Text.from_codepoint_names(c.codepoint_names())!
	assert c == Text.from_utf32(c.utf32())!
	assert c == Text.from_utf8(c.utf8())!

	assert "one\ntwo\nthree".lines() == ["one", "two", "three"]
	assert "one\ntwo\nthree\n".lines() == ["one", "two", "three"]
	assert "one\ntwo\nthree\n\n".lines() == ["one", "two", "three", ""]
	assert "one\r\ntwo\r\nthree\r\n".lines() == ["one", "two", "three"]
	assert "".lines() == []

	say("Test splitting and joining text:")
	assert "one,, two,three".split(",") == ["one", "", " two", "three"]
	assert [t for t in "one,, two,three".by_split(",")] == ["one", "", " two", "three"]
	assert "one,, two,three".split_any(", ") == ["one", "two", "three"]
	assert [t for t in "one,, two,three".by_split_any(", ")] == ["one", "two", "three"]
	assert ",one,, two,three,".split(",") == ["", "one", "", " two", "three", ""]
	assert [t for t in ",one,, two,three,".by_split(",")] == ["", "one", "", " two", "three", ""]
	assert ",one,, two,three,".split_any(", ") == ["", "one", "two", "three", ""]
	assert [t for t in ",one,, two,three,".by_split_any(", ")] == ["", "one", "two", "three", ""]

	assert "abc".split() == ["a", "b", "c"]

	assert "one two three".split_any() == ["one", "two", "three"]

	assert ", ".join(["one", "two", "three"]) == "one, two, three"

	assert "".join(["one", "two", "three"]) == "onetwothree"

	assert "+".join(["one"]) == "one"

	assert "+".join([]) == ""

	assert "".split() == []

	say("Test text slicing:")
	assert "abcdef".slice() == "abcdef"
	assert "abcdef".slice(from=3) == "cdef"
	assert "abcdef".slice(to=-2) == "abcde"
	assert "abcdef".slice(from=2, to=4) == "bcd"
	assert "abcdef".slice(from=5, to=1) == ""

	house := "家"
	assert house == "家"
	assert house.length == 1
	assert house.codepoint_names() == ["CJK Unified Ideographs-5BB6"]
	assert house.utf32() == [23478]

	assert "🐧".codepoint_names() == ["PENGUIN"]

	assert Text.from_codepoint_names(["not a valid name here buddy"]) == none

	assert "Hello".replace("ello", "i") == "Hi"

	assert "<tag>".translate({"<"="&lt;", ">"="&gt;"}) == "&lt;tag&gt;"

	assert "Abc".repeat(3) == "AbcAbcAbc"

	assert "abcde".starts_with("ab") == yes
	assert "abcde".starts_with("bc") == no

	assert "abcde".ends_with("de") == yes
	assert "abcde".starts_with("cd") == no

	assert "abcde".without_prefix("ab") == "cde"
	assert "abcde".without_suffix("ab") == "abcde"

	assert "abcde".without_prefix("de") == "abcde"
	assert "abcde".without_suffix("de") == "abc"

	assert ("hello" ++ " " ++ "Amélie").reversed() == "eilémA olleh"

	do
		say("Testing concatenation-stability:")
		ab := Text.from_codepoint_names(["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"])!
		assert ab.codepoint_names() == ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
		assert ab.length == 1

		a := Text.from_codepoint_names(["LATIN SMALL LETTER E"])!
		b := Text.from_codepoint_names(["COMBINING VERTICAL LINE BELOW"])!
		assert (a++b).codepoint_names() == ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
		assert (a++b) == ab
		assert (a++b).length == 1


	do
		concat := "e" ++ Text.from_utf32([Int32(0x300)])!
		assert concat.length == 1

		concat2 := concat ++ Text.from_utf32([Int32(0x302)])!
		assert concat2.length == 1

		concat3 := concat2 ++ Text.from_utf32([Int32(0x303)])!
		assert concat3.length == 1

		final := Text.from_utf32([Int32(0x65), Int32(0x300), Int32(0x302), Int32(0x303)])!
		assert final.length == 1
		assert concat3 == final

		concat4 := Text.from_utf32([Int32(0x65), Int32(0x300)])! ++ Text.from_utf32([Int32(0x302), Int32(0x303)])!
		assert concat4.length == 1
		assert concat4 == final

	assert "x".left_pad(5) == "    x"
	assert "x".right_pad(5) == "x    "
	assert "x".middle_pad(5) == "  x  "
	assert "1234".left_pad(8, "XYZ") == "XYZX1234"
	assert "1234".right_pad(8, "XYZ") == "1234XYZX"
	assert "1234".middle_pad(9, "XYZ") == "XY1234XYZ"

	assert amelie.width() == 6

	assert "   one,  ".trim(" ,") == "one"
	assert "   one,  ".trim(" ,", left=no) == "   one"
	assert "   one,  ".trim(" ,", right=no) == "one,  "
	assert "  ".trim(" ,") == ""
	assert "  ".trim(" ,", left=no) == ""

	do
		test := "𤭢"
		assert test.utf32() == [150370]
		assert test.utf16() == [-10158, -8350]
		assert test.utf8() == [0xf0, 0xa4, 0xad, 0xa2]

		assert Text.from_utf32([150370]) == test
		assert Text.from_utf16([-10158, -8350]) == test
		assert Text.from_utf8([0xf0, 0xa4, 0xad, 0xa2]) == test