Download
94 lines 3.4 KiBTomoModified 2026-08-11 raw
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
use ./patterns.tm

func main()
	assert $Pat"e".replace("Hello", "X") == "HXllo"

	assert $Pat"l".is_in("Hello") == yes
	assert $Pat"l{end}".is_in("Hello") == no
	assert $Pat"{start}l".is_in("Hello") == no

	assert $Pat"o".is_in("Hello") == yes
	assert $Pat"o{end}".is_in("Hello") == yes
	assert $Pat"{start}o".is_in("Hello") == no

	assert $Pat"H".is_in("Hello") == yes
	assert $Pat"H{end}".is_in("Hello") == no
	assert $Pat"{start}H".is_in("Hello") == yes

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

	assert $Pat"{start}{space}".replace(" one ", "") == "one "
	assert $Pat"{space}{end}".replace(" one ", "") == " one"

	assert $Pat"{alpha}".replace("one two three", "") == "  "
	assert $Pat"{alpha}".replace("one two three", "word") == "word word word"

	assert $Pat" ".split("one two three") == ["one", "two", "three"]

	assert $Pat",".split("one,two,three,") == ["one", "two", "three", ""]

	assert $Pat"{space}".split("one    two three") == ["one", "two", "three"]

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

	assert $Pat"#{alpha}".find_in(" #one  #two #three   ") == [
		PatternMatch(text="#one", index=2, captures=["one"]),
		PatternMatch(text="#two", index=8, captures=["two"]),
		PatternMatch(text="#three", index=13, captures=["three"]),
	]

	assert $Pat"#{!space}".find_in(" #one  #two #three   ") == [
		PatternMatch(text="#one", index=2, captures=["one"]),
		PatternMatch(text="#two", index=8, captures=["two"]),
		PatternMatch(text="#three", index=13, captures=["three"]),
	]

	assert $Pat"{alpha}".find_in("    ") == []

	assert $Pat"{id}(?)".find_in(" foo(baz(), 1)  doop() ") == [
		PatternMatch(text="foo(baz(), 1)", index=2, captures=["foo", "baz(), 1"]),
		PatternMatch(text="doop()", index=17, captures=["doop", ""]),
	]

	assert $Pat"".find_in("") == []

	assert $Pat"".find_in("Hello") == []

	assert $Pat"; {..}".find_in("one two; three four") == [
		PatternMatch(text="; three four", index=8, captures=["three four"]),
	]

	malicious := "{xxx}"
	assert $Pat"$malicious" == $Pat"{1{}xxx}"

	assert $Pat"{lower}".replace("Hello", "(@0)") == "H(ello)"

	assert $Pat"foo(?)".replace(" foo(xyz) foo(yyy) foo(z()) ", "baz(@1)") == " baz(xyz) baz(yyy) baz(z()) "

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

	assert $Pat"BAD(?)".replace(" BAD(x, fn(y), BAD(z), w) ", "good(@1)", recursive=yes) == " good(x, fn(y), good(z), w) "

	assert $Pat"BAD(?)".replace(" BAD(x, fn(y), BAD(z), w) ", "good(@1)", recursive=no) == " good(x, fn(y), BAD(z), w) "

	assert $Pat"{id}".matches("Hello") == yes
	assert $Pat"{lower}".matches("Hello") == no
	assert $Pat"{upper}".matches("Hello") == no
	assert $Pat"{id}".matches("Hello...") == no

	assert $Pat"world".map("hello world", func(m:PatternMatch) m.text.upper()) == "hello WORLD"

	assert $Pat"{space}".trim("   abc def    ") == "abc def"
	assert $Pat"{!digit}".trim(" abc123def ") == "123"
	assert $Pat"{!digit}".trim(" abc123def ", left=no) == " abc123"
	assert $Pat"{!digit}".trim(" abc123def ", right=no) == "123def "
	# Only trim single whole matches that bookend the text:
	assert $Pat"Abc".trim("AbcAbcxxxxxxxxAbcAbc") == "AbcxxxxxxxxAbc"

	assert $Pat"{..}={..}".replace("A=B=C=D", "1:(@1) 2:(@2)") == "1:(A) 2:(B=C=D)"

	say("All tests passed!")