diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-09-21 17:44:08 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-09-21 17:44:08 -0400 |
| commit | c941b9a3325228eba404455afea7ccea0da45095 (patch) | |
| tree | d4ca88c6848ac2e6ceee635bb87add87ba6d2322 /test/text.tm | |
| parent | 1cec086a6034ad546977cae7aeaf4bb876d21970 (diff) | |
Fix tests
Diffstat (limited to 'test/text.tm')
| -rw-r--r-- | test/text.tm | 383 |
1 files changed, 134 insertions, 249 deletions
diff --git a/test/text.tm b/test/text.tm index 84c5a2a3..c12e7a78 100644 --- a/test/text.tm +++ b/test/text.tm @@ -2,309 +2,194 @@ func main() str := "Hello Amélie!" say("Testing strings like $str") - >> str.upper() - = "HELLO AMÉLIE!" - >> str.lower() - = "hello amélie!" - >> str.lower().title() - = "Hello Amélie!" - >> str[1] - = "H" + assert str.upper() == "HELLO AMÉLIE!" + assert str.lower() == "hello amélie!" + assert str.lower().title() == "Hello Amélie!" + assert str[1] == "H" - >> "I".lower() - = "i" - >> "I".lower(language="tr_TR") - = "ı" + assert "I".lower() == "i" + assert "I".lower(language="tr_TR") == "ı" - >> "i".upper() - = "I" - >> "i".upper(language="tr_TR") - = "İ" + assert "i".upper() == "I" + assert "i".upper(language="tr_TR") == "İ" - >> "ian".title() - = "Ian" - >> "ian".title(language="tr_TR") - = "İan" + assert "ian".title() == "Ian" + assert "ian".title(language="tr_TR") == "İan" - >> "I".caseless_equals("ı") - = no - >> "I".caseless_equals("ı", language="tr_TR") - = yes + assert "I".caseless_equals("ı") == no + assert "I".caseless_equals("ı", language="tr_TR") == yes - >> str[9] - = "é" + assert str[9] == "é" - >> str[99] - = none + assert str[99] == none - >> "\{UE9}" - = "é" + assert "\{UE9}" == "é" - >> "\{U65}\{U301}" - = "é" + assert "\{U65}\{U301}" == "é" - >> "\{Penguin}".codepoint_names() - = ["PENGUIN"] + assert "\{Penguin}".codepoint_names() == ["PENGUIN"] - >> "\[31;1]" - = "\e[31;1m" + assert "\[31;1]" == "\e[31;1m" assert "\{UE9}" == "\{U65}\{U301}" amelie := "Am\{UE9}lie" - >> amelie.split() - = ["A", "m", "é", "l", "i", "e"] - >> amelie.utf32() - = [65, 109, 233, 108, 105, 101] - >> amelie.utf8() - = [0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65] - >> Text.from_utf8([0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65])! - = "Amélie" - >> Text.from_utf8([Byte(0xFF)]) - = none + 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" - >> amelie2.split() - = ["A", "m", "é", "l", "i", "e"] - >> amelie2.utf32() - = [65, 109, 233, 108, 105, 101] - >> amelie2.utf8() - = [0x41, 0x6D, 0xC3, 0xA9, 0x6C, 0x69, 0x65] - - >> 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("x") - = no - - >> "Hello".replace("l", "") - = "Heo" - >> "xxxx".replace("x", "") - = "" - >> "xxxx".replace("y", "") - = "xxxx" - >> "One two three four five six".replace("e ", "") - = "Ontwo threfour fivsix" - - >> amelie.has(amelie2) - = yes - - >> multiline := " + 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 " - = "line one\nline two" + assert multiline == "line one\nline two" say("Interpolation tests:") - >> "A $(1+2)" - = "A 3" - >> "A \$(1+2)" - = "A \$(1+2)" - >> 'A $(1+2)' - = "A 3" - >> `A @(1+2)` - = "A 3" + 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 := "É̩" - >> c.codepoint_names() - = ["LATIN CAPITAL LETTER E WITH ACUTE", "COMBINING VERTICAL LINE BELOW"] + 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())! - >> "one\ntwo\nthree".lines() - = ["one", "two", "three"] - >> "one\ntwo\nthree\n".lines() - = ["one", "two", "three"] - >> "one\ntwo\nthree\n\n".lines() - = ["one", "two", "three", ""] - >> "one\r\ntwo\r\nthree\r\n".lines() - = ["one", "two", "three"] - >> "".lines() - = [] + 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:") - >> "one,, two,three".split(",") - = ["one", "", " two", "three"] - >> [t for t in "one,, two,three".by_split(",")] - = ["one", "", " two", "three"] - >> "one,, two,three".split_any(", ") - = ["one", "two", "three"] - >> [t for t in "one,, two,three".by_split_any(", ")] - = ["one", "two", "three"] - >> ",one,, two,three,".split(",") - = ["", "one", "", " two", "three", ""] - >> [t for t in ",one,, two,three,".by_split(",")] - = ["", "one", "", " two", "three", ""] - >> ",one,, two,three,".split_any(", ") - = ["", "one", "two", "three", ""] - >> [t for t in ",one,, two,three,".by_split_any(", ")] - = ["", "one", "two", "three", ""] - - >> "abc".split() - = ["a", "b", "c"] - - >> "one two three".split_any() - = ["one", "two", "three"] - - >> ", ".join(["one", "two", "three"]) - = "one, two, three" - - >> "".join(["one", "two", "three"]) - = "onetwothree" - - >> "+".join(["one"]) - = "one" - - >> "+".join([]) - = "" - - >> "".split() - = [] + 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:") - >> "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) - = "" - - >> house := "家" - = "家" - >> house.length - = 1 - >> house.codepoint_names() - = ["CJK Unified Ideographs-5BB6"] - >> house.utf32() - = [23478] - - >> "🐧".codepoint_names() - = ["PENGUIN"] - - >> Text.from_codepoint_names(["not a valid name here buddy"]) - = none - - >> "Hello".replace("ello", "i") - = "Hi" - - >> "<tag>".translate({"<"="<", ">"=">"}) - = "<tag>" - - >> "Abc".repeat(3) - = "AbcAbcAbc" - - >> "abcde".starts_with("ab") - = yes - >> "abcde".starts_with("bc") - = no - - >> "abcde".ends_with("de") - = yes - >> "abcde".starts_with("cd") - = no - - >> "abcde".without_prefix("ab") - = "cde" - >> "abcde".without_suffix("ab") - = "abcde" - - >> "abcde".without_prefix("de") - = "abcde" - >> "abcde".without_suffix("de") - = "abc" - - >> ("hello" ++ " " ++ "Amélie").reversed() - = "eilémA olleh" + 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({"<"="<", ">"=">"}) == "<tag>" + + 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"])! - >> ab.codepoint_names() - = ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"] - >> ab.length - = 1 + 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"])! - >> (a++b).codepoint_names() - = ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"] + assert (a++b).codepoint_names() == ["LATIN SMALL LETTER E", "COMBINING VERTICAL LINE BELOW"] assert (a++b) == ab - >> (a++b).length - = 1 + assert (a++b).length == 1 do concat := "e" ++ Text.from_utf32([Int32(0x300)])! - >> concat.length - = 1 + assert concat.length == 1 concat2 := concat ++ Text.from_utf32([Int32(0x302)])! - >> concat2.length - = 1 + assert concat2.length == 1 concat3 := concat2 ++ Text.from_utf32([Int32(0x303)])! - >> concat3.length - = 1 + assert concat3.length == 1 final := Text.from_utf32([Int32(0x65), Int32(0x300), Int32(0x302), Int32(0x303)])! - >> final.length - = 1 + assert final.length == 1 assert concat3 == final concat4 := Text.from_utf32([Int32(0x65), Int32(0x300)])! ++ Text.from_utf32([Int32(0x302), Int32(0x303)])! - >> concat4.length - = 1 + assert concat4.length == 1 assert concat4 == final - >> "x".left_pad(5) - = " x" - >> "x".right_pad(5) - = "x " - >> "x".middle_pad(5) - = " x " - >> "1234".left_pad(8, "XYZ") - = "XYZX1234" - >> "1234".right_pad(8, "XYZ") - = "1234XYZX" - >> "1234".middle_pad(9, "XYZ") - = "XY1234XYZ" - - >> amelie.width() - = 6 - - # Unicode character width is somewhat platform dependent: - # cowboy := "🤠" - # >> cowboy.width() - # = 2 - # >> cowboy.left_pad(4) - # = " 🤠" - # >> cowboy.right_pad(4) - # = "🤠 " - # >> cowboy.middle_pad(4) - # = " 🤠 " - - >> " one, ".trim(" ,") - = "one" - >> " one, ".trim(" ,", left=no) - = " one" - >> " one, ".trim(" ,", right=no) - = "one, " - >> " ".trim(" ,") - = "" - >> " ".trim(" ,", left=no) - = "" + 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 := "𤭢" |
