Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
126 lines (109 sloc) 3.78 KB
(use r7rs)
(use test)
(include "parser-lib.scm")
(import parser-lib)
(test-group "nop"
(test '(#t . 0) (nop "abc" 0)))
(test-group "nop"
(test '#f (fail "abc" 0)))
(test-group "any-char"
(test '("a" . 1) (any-char "abc" 0))
(test '("b" . 2) (any-char "abc" 1))
(test '("c" . 3) (any-char "abc" 2))
(test #f (any-char "abc" 3))
)
(test-group "char"
(test '("a" . 1) ((char #\a) "abc" 0))
(test #f ((char #\b) "abc" 0))
(test #f ((char #\b) "abc" 3))
)
(test-group "if-char"
(test '("a" . 1) ((if-char char-alphabetic?) "abc" 0))
(test #f ((if-char char-numeric?) "abc" 0))
(test #f ((if-char char-alphabetic?) "abc" 3))
)
(test-group "seq"
(test '(("a" "b") . 2) ((seq any-char any-char) "abc" 0))
(test #f ((seq any-char any-char) "abc" 2))
(test '(("a" "b") . 2) ((seq (char #\a)
(char #\b)) "abc" 0))
(test #f ((seq (char #\b)
(char #\b)) "abc" 0))
(test #f ((seq (char #\a)
(char #\c)) "abc" 0))
)
(test-group "str-seq"
(test '("ab" . 2) ((str-seq any-char any-char) "abc" 0))
(test #f ((str-seq any-char any-char) "abc" 2))
(test '("ab" . 2) ((str-seq (char #\a) (char #\b)) "abc" 0))
(test #f ((str-seq (char #\b) (char #\b)) "abc" 0))
(test #f ((str-seq (char #\c) (char #\c)) "abc" 0))
)
(test-group "matches"
(test '("123" . 3) ((matches "123") "123abc" 0))
(test #f ((matches "123") "abc123" 0))
(test #f ((matches "1234") "123" 0))
)
(test-group "while"
(test '(("1" "2" "3") . 3) ((while (if-char char-numeric?)) "123abc" 0))
(test '(("1" "2" "3") . 3) ((while (if-char char-numeric?)) "123" 0))
(test '(("12" "12") . 4) ((while (matches "12")) "12123" 0))
(test '(() . 0) ((while (if-char char-numeric?)) "abc123abc" 0))
)
(test-group "while1"
(test '(("1" "2" "3") . 3) ((while1 (if-char char-numeric?)) "123abc" 0))
(test #f ((while1 (if-char char-numeric?)) "abc123abc" 0))
)
(test-group "while-char"
(test '("123" . 3) ((while-char char-numeric?) "123abc" 0))
(test '("123" . 3) ((while-char char-numeric?) "123" 0))
(test '("" . 0) ((while-char char-numeric?) "abc" 0))
)
(test-group "while1-char"
(test '("123" . 3) ((while1-char char-numeric?) "123abc" 0))
(test '("123" . 3) ((while1-char char-numeric?) "123" 0))
(test #f ((while1-char char-numeric?) "abc" 0))
)
(test-group "choice"
(test '("123" . 3) ((choice (while1-char char-numeric?)
(char #\a)) "123abc" 0))
(test '("a" . 1) ((choice (while1-char char-numeric?)
(char #\a)) "abc123" 0))
(test #f ((choice (while1-char char-numeric?)
(char #\a)) "xabc123" 0))
)
(test-group "parser-macro"
(test '(#t . 1) (let ((p (parser c1 <- any-char)))
(p "abc" 0)))
(test '(#t . 2) (let ((p (parser c1 <- any-char
c2 <- any-char)))
(p "abc" 0)))
(test '(#t . 2) (let ((p (parser c1 <- (char #\a)
c2 <- any-char)))
(p "abc" 0)))
(test #f (let ((p (parser c1 <- (char #\a)
c2 <- any-char)))
(p "dabc" 0)))
(test '("ab" . 2) (let ((p (parser c1 <- any-char
c2 <- any-char
return (string-append c1 c2))))
(p "abc" 0)))
(test '("b" . 2) (let ((p (parser any-char
c <- any-char
return c)))
(p "abc" 0)))
(test '("c" . 3) (let ((p (parser (matches "ab")
c <- any-char
return c)))
(p "abc" 0)))
(test #f (let ((p (parser c1 <- any-char
c2 <- (char #\a)
return (string-append c1 c2))))
(p "abc" 0)))
(test #f (let ((p (parser c1 <- any-char
c2 <- (char #\b)
c3 <- (char #\a)
return (string-append c1 c2))))
(p "abc" 0)))
)
(test-exit)