Carp/test/pattern.carp

94 lines
3.1 KiB
Plaintext

(load "Test.carp")
(use-all Pattern Test)
(deftest 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
0
(find #"\.\*" ".*")
"find works as expected with special characters")
(assert-equal test
&[3 6]
&(find-all #"\d\d" " 12 67")
"find-all works as expected")
(assert-equal test
&[]
&(find-all #"\d\d" " ")
"find-all works as expected if not found")
(assert-equal test
&[@"12"]
&(match-groups #"(\d+)" " 12")
"match works as expected")
(assert-equal test
&[]
&(match-groups #"(\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? #"\r" "\r"))
"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.unsafe-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
&[@"" @"1" @"2" @"3" @""]
&(split #"\-\-" "--1--2--3--")
"split 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"))