edward

An extensible POSIX-compatible implementation of the ed(1) text editor

git clone https://git.8pit.net/edward.git

 1(import posix-regex (edward replace))
 2
 3(define (test-re str pattern replacement . o)
 4  (let-values (((result modified)
 5                (regex-replace
 6                  (make-regex pattern)
 7                  (parse (parse-replace #\/) replacement)
 8                  str
 9                  (if (null? o) 0 (car o)))))
10    (cons result modified)))
11
12;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
14(test-group "backreferences"
15  (test "single backreference"
16        '("faz" . #t)
17        (test-re "foo" "\\([a-z]\\)oo" "\\1az"))
18  (test "multiple backreferences"
19        '("bar foo" . #t)
20        (test-re "foo bar" "\\([a-z]*\\) \\([a-z]*\\)" "\\2 \\1"))
21  (test "unused backreferences"
22        '("2" . #t)
23        (test-re "123" "\\([0-9]\\)\\([0-9]\\)\\([0-9]\\)" "\\2"))
24  (test "escape subexpression"
25        '("bar" . #t)
26        (test-re "\\(foo\\)" "\\\\(foo\\\\)" "bar")))
27
28(test-group "ampersand"
29  (test "append text"
30        '("foobar" . #t)
31        (test-re "foo" "foo" "&bar"))
32  (test "append in middle"
33        '("foo123bar" . #t)
34        (test-re "foo1bar" "1" "&23"))
35  (test "escape ampersand"
36        '("&bar" . #t)
37        (test-re "foo" "foo" "\\&bar")))
38
39(test-group "replacement amount"
40  (test "replace first match only"
41        '("testbarfoo" . #t)
42        (test-re "foobarfoo" "foo" "test" 1))
43  (test "replace all matches"
44        '("testbartest" . #t)
45        (test-re "foobarfoo" "foo" "test" 0))
46  (test "replace second match only"
47        '("foobartest" . #t)
48        (test-re "foobarfoo" "foo" "test" 2)))
49
50(test-group "miscellaneous"
51  (test "input string with multibyte character"
52        '("foo|bar" . #t)
53        (test-re "fooλbar" "λ" "|"))
54  (test "replacement with multibyte character"
55        '("fooλbar" . #t)
56        (test-re "foo|bar" "|" "λ"))
57  (test "replace with newline"
58        '("foo\nbar" . #t)
59        (test-re "foo|bar" "|" "\\\n"))
60  (test "non-participating submatch"
61        '("baz" . #t)
62        (test-re "foo  baz" "foo \\(..*\\)* \\(..*\\)" "\\2\\1"))
63  (test "empty submatch"
64        '("matched: " . #t)
65        (test-re "foo <> baz"
66                 "foo <\\(.*\\)> baz"
67                 "matched: \\1")))
68
69(test-group "modified"
70  (test "no match"
71        '("foo" . #f)
72        (test-re "foo" "bar" "test"))
73
74  (test "replace second match"
75        '(" foo foo bar " . #t)
76        (test-re " foo foo foo " " foo " " bar " 2)))