posix-regex

CHICKEN Scheme wrapper for POSIX regular expression matching

git clone https://git.8pit.net/posix-regex.git

 1(cond-expand
 2      (chicken-5
 3        (import (r7rs) (test) (posix-regex)))
 4      (chicken-6
 5        (import (scheme base) test posix-regex)))
 6
 7(define (test-match pattern string . options)
 8  (regex-match?
 9    (apply make-regex (append (list pattern) options))
10    string))
11
12(define (test-exec pattern string)
13  (regex-exec
14    (make-regex pattern)
15    (string->utf8 string)))
16
17;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18
19(test-group "make-regex"
20  (test-assert "literal string" (regex? (make-regex "foo")))
21  (test-assert "ignorecase" (regex? (make-regex "foobar" #t)))
22  (test-assert "extended regular expression" (regex? (make-regex "[0-9]+" #f #t)))
23  (test-assert "newline option" (regex? (make-regex "foo" #f #f #t)))
24
25  (test-error "invalid interval expression" (make-regex "\\{foo,foo\\}")) ;; REG_BADAR
26  (test-error "parentheses imbalance" (make-regex "\\(foo"))              ;; REG_EBRACE
27  (test-error "bracket imbalance" (make-regex "["))                       ;; REG_EBRACK
28  (test-error "trailing backslash" (make-regex "\\")))                    ;; REG_EESCAPE
29
30(test-group "regex-match?"
31  (test "match literal string" #t (test-match "foo" "foo"))
32  (test "don't match literal string" #f (test-match "foo" "bar"))
33  (test "partially match literal string" #t (test-match "foo" "foobar"))
34  (test "match bracket expression" #t (test-match "f[a-z][a-z]b" "foobar"))
35  (test "match repeated expression" #t (test-match "a*b" "aaaaab"))
36  (test "match start and end" #f (test-match "^foo$" "|foo|"))
37  (test "multiline mode" #t (test-match "^foo" "bar\nfoo\n" #f #f #t))
38  (test "non-multiline mode" #f (test-match "^foo" "bar\nfoo\n")))
39
40(test-group "regex-exec"
41  (test "match literal string"
42        #((0 . 3))
43        (test-exec "foo" "foo"))
44
45  (test "not matching"
46        #f
47        (test-exec "foo" "bar"))
48
49  (test "match single submatch"
50        #((0 . 13) (5 . 8))
51        (test-exec "foo |\\(..*\\)| baz" "foo |bar| baz"))
52
53  (test "match zero-length string"
54        #((0 . 10) (5 . 5))
55        (test-exec "foo '\\(.*\\)' baz" "foo '' baz"))
56
57  (test "non-participating submatch"
58        #((0 . 8) #f (5 . 8))
59        (test-exec "foo \\(..*\\)* \\(..*\\)" "foo  baz")))
60
61;; Exit with non-zero exit status if some test failed.
62(test-exit)