aboutsummaryrefslogtreecommitdiff
path: root/test/text.tm
blob: 39e8a6e19626eb32a8ff417a69e3f0854ed2116a (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
func main():
	>> str := "Hello Amélie!"
	//! Testing strings like $str

	>> str:upper()
	= "HELLO AMÉLIE!"
	>> str:lower()
	= "hello amélie!"
	>> str:lower():title()
	= "Hello Amélie!"


	>> \UE9
	= "é"

	>> \U65\U301
	= "é"

	>> \UE9 == \U65\U301
	= yes

	>> amelie := "Am$(\UE9)lie"
	>> amelie:split()
	= ["A", "m", "é", "l", "i", "e"] : [Text]
	>> amelie:utf32_codepoints()
	= [65_i32, 109_i32, 233_i32, 108_i32, 105_i32, 101_i32] : [Int32]
	>> amelie:utf8_bytes()
	= [65_i8, 109_i8, -61_i8, -87_i8, 108_i8, 105_i8, 101_i8] : [Int8]

	>> amelie2 := "Am$(\U65\U301)lie"
	>> amelie2:split()
	= ["A", "m", "é", "l", "i", "e"] : [Text]
	>> amelie2:utf32_codepoints()
	= [65_i32, 109_i32, 233_i32, 108_i32, 105_i32, 101_i32] : [Int32]
	>> amelie2:utf8_bytes()
	= [65_i8, 109_i8, -61_i8, -87_i8, 108_i8, 105_i8, 101_i8] : [Int8]

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

	>> "Hello":replace("e", "X")
	= "HXllo"

	>> "Hello":has("l")
	= yes
	>> "Hello":has("l[..end]")
	= no
	>> "Hello":has("[..start]l")
	= no

	>> "Hello":has("o")
	= yes
	>> "Hello":has("o[..end]")
	= yes
	>> "Hello":has("[..start]o")
	= no

	>> "Hello":has("H")
	= yes
	>> "Hello":has("H[..end]")
	= no
	>> "Hello":has("[..start]H")
	= yes

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

	>> " one ":replace("[..start][..space]", "")
	= "one "
	>> " one ":replace("[..space][..end]", "")
	= " one"

	>> amelie:has(amelie2)


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

	//! Interpolation tests:
	>> "A $(1+2)"
	= "A 3"
	>> 'A $(1+2)'
	= "A $(1+2)"
	>> `A $(1+2)`
	= "A 3"

	>> $"A $(1+2)"
	= "A 3"
	>> $$"A $(1+2)"
	= "A $(1+2)"
	>> $="A =(1+2)"
	= "A 3"
	>> $(one (nested) two $(1+2))
	= "one (nested) two 3"

	>> "one two three":replace("[..alpha]", "")
	= "  "
	>> "one two three":replace("[..alpha]", "word")
	= "word word word"

	>> c := "É̩"
	>> c:codepoint_names()
	= ["LATIN CAPITAL LETTER E WITH ACUTE", "COMBINING VERTICAL LINE BELOW"]
	>> c == Text.from_codepoint_names(c:codepoint_names())
	= yes
	>> c == Text.from_codepoints(c:utf32_codepoints())
	= yes
	>> c == Text.from_bytes(c:utf8_bytes())
	= yes


	>> "one$(\n)two$(\n)three":lines()
	= ["one", "two", "three"]
	>> "one$(\n)two$(\n)three$(\n)":lines()
	= ["one", "two", "three"]
	>> "one$(\n)two$(\n)three$(\n\n)":lines()
	= ["one", "two", "three", ""]
	>> "one$(\r\n)two$(\r\n)three$(\r\n)":lines()
	= ["one", "two", "three"]

	//! Test splitting and joining text:
	>> "one two three":split(" ")
	= ["one", "two", "three"]

	>> "one,two,three,":split(",")
	= ["one", "two", "three", ""]

	>> "one    two three":split("[..space]")
	= ["one", "two", "three"]

	>> "abc":split("")
	= ["a", "b", "c"]

	>> ", ":join(["one", "two", "three"])
	= "one, two, three"

	>> "":join(["one", "two", "three"])
	= "onetwothree"

	>> "+":join(["one"])
	= "one"

	>> "+":join([:Text])
	= ""

	>> "":split()
	= []

	//! Test text:find_all()
	>> " one  two three   ":find_all("[..alpha]")
	= ["one", "two", "three"]

	>> " one  two three   ":find_all("[..!space]")
	= ["one", "two", "three"]

	>> "    ":find_all("[..alpha]")
	= []

	>> " foo(baz(), 1)  doop() ":find_all("[..id](?)")
	= ["foo(baz(), 1)", "doop()"]

	>> "":find_all("")
	= []

	>> "Hello":find_all("")
	= []

	//! Test text:find()
	>> " one   two  three   ":find("[..id]", start=-999)
	= 0
	>> " one   two  three   ":find("[..id]", start=999)
	= 0
	>> " one   two  three   ":find("[..id]")
	= 2
	>> " one   two  three   ":find("[..id]", start=5)
	= 8

	>> len := 0_i64
	>> "   one  ":find("[..id]", length=&len)
	= 4
	>> len
	= 3_i64