Carp/test/pattern.carp
2018-05-23 18:40:54 +10:00

81 lines
2.8 KiB
Plaintext

(load "Test.carp")
(use-all Pattern Test)
(defn main []
(with-test test
(assert-equal test
#"1234"
&(init "1234")
"init works as expected")
(assert-equal test
"1234"
&(str #"1234")
"str works as expected")
(assert-equal test
"#\"1234\""
&(prn #"1234")
"prn works as expected")
(assert-equal test
true
(= #"1234" #"1234")
"= works as expected on equality")
(assert-equal test
false
(= #"1234" #"235")
"= works as expected on non-equality")
(assert-equal test
3
(find #"\d" " 12")
"find works as expected")
(assert-equal test
-1
(find #"\d" " ")
"find works as expected if not found")
(assert-equal test
&[@"12"]
&(match #"(\d+)" " 12")
"match works as expected")
(assert-equal test
&[]
&(match #"(\d+)" " ")
"match works as expected if not found")
(assert-equal test
true
(matches? #"ana" "banana")
"matches? works as exptected")
(assert-equal test
false
(matches? #"ano" "banana")
"matches? works as exptected II")
(assert-equal test
true
(and* (matches? #"\n" "\n") (matches? #"\n" "\r")
(matches? #"\n" "\r\n"))
"matches? works as exptected on newlines special case")
(assert-equal test
true
(matches? #"\t" "\t")
"matches? works as exptected on tabs special case")
(assert-equal test
&[@"3" @"4"]
(Array.nth &(global-match #"(\d)-(\d)" "1-2 2-3 3-4 4-5") 2)
"global-match works as expected")
(assert-equal test
"1-2"
&(match-str #"(\d)-(\d)" "1-2 2-3 3-4 4-5")
"match-str works as expected")
(assert-equal test
""
&(match-str #"erik" "1-2 2-3 3-4 4-5")
"match-str works as expected II")
(assert-equal test
"sub 2-3 3-4"
&(substitute #"(\d)-(\d)" "1-2 2-3 3-4" "sub" 1)
"substitute works as expected")
(assert-equal test
"sub sub sub"
&(substitute #"(\d)-(\d)" "1-2 2-3 3-4" "sub" -1)
"substitute works as expected if all should be replaces")
(print-test-results test)))