tomo/test/text.tm

311 lines
6.6 KiB
Plaintext
Raw Normal View History

2024-04-28 11:58:55 -07:00
func main():
2024-04-12 10:09:31 -07:00
>> str := "Hello Amélie!"
!! Testing strings like $str
2024-07-04 13:23:05 -07:00
2024-04-12 10:09:31 -07:00
>> str:upper()
= "HELLO AMÉLIE!"
>> str:lower()
= "hello amélie!"
>> str:lower():title()
= "Hello Amélie!"
2024-03-03 14:49:40 -08:00
2024-07-04 13:23:05 -07:00
2024-04-12 10:09:31 -07:00
>> \UE9
= "é"
2024-04-12 10:09:31 -07:00
>> \U65\U301
= "é"
>> \{Penguin}:codepoint_names()
2024-09-06 22:43:36 -07:00
= ["PENGUIN"]
>> \[31;1]
= "$\e[31;1m"
2024-04-12 10:09:31 -07:00
>> \UE9 == \U65\U301
= yes
>> amelie := "Am$(\UE9)lie"
>> amelie:split()
2024-04-12 10:09:31 -07:00
= ["A", "m", "é", "l", "i", "e"] : [Text]
2024-09-02 17:22:13 -07:00
>> amelie:utf32_codepoints()
2024-09-15 12:33:47 -07:00
= [65[32], 109[32], 233[32], 108[32], 105[32], 101[32]] : [Int32]
2024-09-02 17:22:13 -07:00
>> amelie:utf8_bytes()
2024-09-15 12:33:47 -07:00
= [65[B], 109[B], 195[B], 169[B], 108[B], 105[B], 101[B]] : [Byte]
>> Text.from_bytes([65[B], 109[B], 195[B], 169[B], 108[B], 105[B], 101[B]])!
2024-09-15 12:33:47 -07:00
= "Amélie"
>> Text.from_bytes([255[B]])
= !Text
>> amelie2 := "Am$(\U65\U301)lie"
>> amelie2:split()
2024-04-12 10:09:31 -07:00
= ["A", "m", "é", "l", "i", "e"] : [Text]
2024-09-02 17:22:13 -07:00
>> amelie2:utf32_codepoints()
2024-09-15 12:33:47 -07:00
= [65[32], 109[32], 233[32], 108[32], 105[32], 101[32]] : [Int32]
2024-09-02 17:22:13 -07:00
>> amelie2:utf8_bytes()
2024-09-15 12:33:47 -07:00
= [65[B], 109[B], 195[B], 169[B], 108[B], 105[B], 101[B]] : [Byte]
2024-09-02 17:22:13 -07:00
>> amelie:codepoint_names()
2024-09-02 18:18:15 -07:00
= ["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"]
2024-09-02 17:22:13 -07:00
>> amelie2:codepoint_names()
2024-09-02 18:18:15 -07:00
= ["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"]
2024-03-09 15:22:12 -08:00
>> "Hello":replace($/e/, "X")
2024-04-12 10:09:31 -07:00
= "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}/, "")
2024-09-02 17:22:13 -07:00
= "one "
>> " one ":replace($/{space}{end}/, "")
2024-09-02 17:22:13 -07:00
= " one"
>> amelie:has($/$amelie2/)
2024-07-01 10:09:26 -07:00
>> multiline := "
line one
line two
"
= "line one$\nline two"
2024-07-01 10:09:26 -07:00
!! 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"
2024-09-06 09:17:43 -07:00
>> ${one {nested} two $(1+2)}
= "one {nested} two 3"
>> "one two three":replace($/{alpha}/, "")
2024-09-02 19:42:02 -07:00
= " "
>> "one two three":replace($/{alpha}/, "word")
2024-09-02 19:42:02 -07:00
= "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
2024-09-02 19:57:49 -07:00
>> "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"]
2024-09-02 21:54:48 -07:00
>> "":lines()
= []
!! 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"]
2024-09-02 20:13:02 -07:00
>> ", ":join(["one", "two", "three"])
= "one, two, three"
>> "":join(["one", "two", "three"])
= "onetwothree"
>> "+":join(["one"])
= "one"
>> "+":join([:Text])
= ""
2024-09-02 20:24:16 -07:00
>> "":split()
= []
2024-09-02 20:26:55 -07:00
!! Test text:find_all()
>> " one two three ":find_all($/{alpha}/)
2024-09-02 20:26:55 -07:00
= ["one", "two", "three"]
>> " one two three ":find_all($/{!space}/)
2024-09-02 20:26:55 -07:00
= ["one", "two", "three"]
>> " ":find_all($/{alpha}/)
2024-09-02 20:26:55 -07:00
= []
>> " foo(baz(), 1) doop() ":find_all($/{id}(?)/)
2024-09-02 20:26:55 -07:00
= ["foo(baz(), 1)", "doop()"]
>> "":find_all($Pattern'')
2024-09-02 20:26:55 -07:00
= []
>> "Hello":find_all($Pattern'')
2024-09-02 20:26:55 -07:00
= []
!! Test text:find()
>> " one two three ":find($/{id}/, start=-999)
= !Int
>> " one two three ":find($/{id}/, start=999)
= !Int
>> " one two three ":find($/{id}/)
= 2?
>> " one two three ":find($/{id}/, start=5)
= 8?
!! Test text slicing:
2024-09-02 20:56:08 -07:00
>> "abcdef":slice()
= "abcdef"
>> "abcdef":slice(from=3)
= "cdef"
>> "abcdef":slice(to=-2)
= "abcde"
>> "abcdef":slice(from=2, to=4)
= "bcd"
>> "abcdef":slice(from=5, to=1)
= ""
2024-09-02 22:20:36 -07:00
>> house := "家"
= "家"
>> house.length
= 1
>> house:codepoint_names()
= ["CJK Unified Ideographs-5BB6"]
2024-09-02 22:20:36 -07:00
>> house:utf32_codepoints()
2024-09-15 12:33:47 -07:00
= [23478[32]]
>> "🐧":codepoint_names()
= ["PENGUIN"]
>> Text.from_codepoint_names(["not a valid name here buddy"])
= !Text
>> "one two; three four":find_all($/; {..}/)
= ["; three four"]
>> malicious := "{xxx}"
>> $/$malicious/
= $/{1{}xxx}/
>> "Hello":replace($/{lower}/, "(\0)")
= "H(ello)"
>> " foo(xyz) foo(yyy) foo(z()) ":replace($/foo(?)/, "baz(\1)")
= " baz(xyz) baz(yyy) baz(z()) "
>> "<tag>":replace_all({$/</:"&lt;", $/>/:"&gt;"})
= "&lt;tag&gt;"
>> " BAD(x, fn(y), BAD(z), w) ":replace($/BAD(?)/, "good(\1)", recursive=yes)
= " good(x, fn(y), good(z), w) "
>> " BAD(x, fn(y), BAD(z), w) ":replace($/BAD(?)/, "good(\1)", recursive=no)
= " good(x, fn(y), BAD(z), w) "
>> "Hello":matches($/{id}/)
= ["Hello"]?
>> "Hello":matches($/{lower}/)
= ![Text]
>> "Hello":matches($/{upper}/)
= ![Text]
>> "Hello...":matches($/{id}/)
= ![Text]
if matches := "hello world":matches($/{id} {id}/):
>> matches
= ["hello", "world"]
else:
fail("Failed to match")
2024-09-04 18:02:37 -07:00
>> "hello world":map($/world/, Text.upper)
= "hello WORLD"
2024-09-04 18:22:03 -07:00
>> "Abc":repeat(3)
= "AbcAbcAbc"
2024-09-05 00:47:15 -07:00
2024-09-05 21:03:28 -07:00
>> " abc def ":trim()
= "abc def"
>> " abc123def ":trim($/{!digit}/)
= "123"
>> " abc123def ":trim($/{!digit}/, trim_left=no)
= " abc123"
>> " abc123def ":trim($/{!digit}/, trim_right=no)
= "123def "
# Only trim single whole matches that bookend the text:
>> "AbcAbcxxxxxxxxAbcAbc":trim($/Abc/)
= "AbcxxxxxxxxAbc"
2024-09-06 00:29:07 -07:00
>> "A=B=C=D":replace($/{..}={..}/, "1:(\1) 2:(\2)")
= "1:(A) 2:(B=C=D)"
>> "abcde":starts_with("ab")
= yes
>> "abcde":starts_with("bc")
= no
>> "abcde":ends_with("de")
= yes
>> "abcde":starts_with("cd")
= no
2024-09-05 00:47:15 -07:00
do:
!! Testing concatenation-stability:
>> ab := Text.from_codepoint_names(["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"])!
2024-09-05 00:47:15 -07:00
>> ab:codepoint_names()
= ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
>> ab.length
= 1
>> a := Text.from_codepoint_names(["LATIN SMALL LETTER E"])!
>> b := Text.from_codepoint_names(["COMBINING VERTICAL LINE BELOW"])!
2024-09-05 00:47:15 -07:00
>> (a++b):codepoint_names()
= ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"]
>> (a++b) == ab
= yes
>> (a++b).length
= 1