Carp/test/pattern.carp
guberathome e412559380
reorganize Pattern library (#1257)
* feat!: implemented Pattern.match which returns start and end indizes of the first match

* fix: Pattern.match now returns correct end index

* feat: moved Pattern.match-str and Pattern.find from C to Carp code

* chore: fix build after merges

* chore: fix build after merges

* feat: moved Pattern.find-all from C to Carp code

* feat: Pattern.global-match-str no longer relies on Pattern.global-match

* docs: updated for Pattern.global-match

* fix: moved str/prn functions into sub module

* fix: removed unused functions from carp_pattern.h (using cflow)

* feat!: renamed (Pattern.global-match) to (Pattern.match-all-groups)

* fix: unit test

* fix: some functions renamed to match Carp style

Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
2021-06-23 22:07:56 +02:00

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 &(match-all-groups #"(\d)-(\d)" "1-2 2-3 3-4 4-5") 2)
"match-all-groups 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"))