1 ;;;; regexp.test --- test Guile's regular expression functions -*- scheme -*-
2 ;;;; Jim Blandy <jimb@red-bean.com> --- September 1999
4 ;;;; Copyright (C) 1999, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;;;; This program is free software; you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation; either version 2, or (at your option)
9 ;;;; any later version.
11 ;;;; This program is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with this software; see the file COPYING. If not, write to
18 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 ;;;; Boston, MA 02110-1301 USA
21 (define-module (test-suite test-regexp)
22 #:use-module (test-suite lib)
23 #:use-module (ice-9 regex))
26 ;;; Run a regexp-substitute or regexp-substitute/global test, once
27 ;;; providing a real port and once providing #f, requesting direct
29 (define (vary-port func expected . args)
30 (pass-if "port is string port"
32 (call-with-output-string
34 (apply func port args)))))
37 (apply func #f args))))
39 (define (object->string obj)
40 (call-with-output-string
48 (with-test-prefix "make-regexp"
50 (pass-if-exception "no args" exception:wrong-num-args
53 (pass-if-exception "bad pat arg" exception:wrong-type-arg
56 ;; in guile prior to 1.6.5 make-regex didn't validate its flags args
57 (pass-if-exception "bad arg 2" exception:wrong-type-arg
58 (make-regexp "xyz" 'abc))
60 (pass-if-exception "bad arg 3" exception:wrong-type-arg
61 (make-regexp "xyz" regexp/icase 'abc)))
67 (with-test-prefix "match:string"
70 (string=? "foo" (match:string (string-match ".*" "foo"))))
72 (pass-if "foo offset 1"
73 (string=? "foo" (match:string (string-match ".*" "foo" 1)))))
79 (with-test-prefix "regexp-exec"
81 (pass-if-exception "non-integer offset" exception:wrong-type-arg
82 (let ((re (make-regexp "ab+")))
83 (regexp-exec re "aaaabbbb" 1.5 'bogus-flags-arg)))
85 (pass-if-exception "non-string input" exception:wrong-type-arg
86 (let ((re (make-regexp "ab+")))
87 (regexp-exec re 'not-a-string)))
89 (pass-if-exception "non-string input, with offset" exception:wrong-type-arg
90 (let ((re (make-regexp "ab+")))
91 (regexp-exec re 'not-a-string 5)))
93 ;; in guile 1.8.1 and earlier, a #\nul character in the input string was
94 ;; only detected in a critical section, and the resulting error throw
95 ;; abort()ed the program
96 (pass-if-exception "nul in input" exception:string-contains-nul
97 (let ((re (make-regexp "ab+")))
98 (regexp-exec re (string #\a #\b (integer->char 0)))))
100 ;; in guile 1.8.1 and earlier, a bogus flags argument was only detected
101 ;; inside a critical section, and the resulting error throw abort()ed the
103 (pass-if-exception "non-integer flags" exception:wrong-type-arg
104 (let ((re (make-regexp "ab+")))
105 (regexp-exec re "aaaabbbb" 0 'bogus-flags-arg))))
111 (with-test-prefix "fold-matches"
113 (pass-if "without flags"
115 (fold-matches "^[a-z]+$" "hello" '()
116 (lambda (match result)
117 (cons (match:substring match)
120 (pass-if "with flags"
121 ;; Prior to 1.8.6, passing an additional flag would not work.
123 (fold-matches "^[a-z]+$" "hello" '()
124 (lambda (match result)
125 (cons (match:substring match)
127 (logior regexp/notbol regexp/noteol)))))
134 (with-test-prefix "regexp-quote"
136 (pass-if-exception "no args" exception:wrong-num-args
139 (pass-if-exception "bad string arg" exception:wrong-type-arg
140 (regexp-quote 'blah))
142 (let ((lst `((regexp/basic ,regexp/basic)
143 (regexp/extended ,regexp/extended)))
144 ;; string of all characters, except #\nul which doesn't work because
145 ;; it's the usual end-of-string for the underlying C regexec()
146 (allchars (list->string (map integer->char
147 (cdr (iota char-code-limit))))))
150 (let ((name (car elem))
153 (with-test-prefix name
155 ;; try on each individual character, except #\nul
157 ((>= i char-code-limit))
158 (let* ((c (integer->char i))
160 (q (regexp-quote s)))
161 (pass-if (list "char" i c s q)
162 (let ((m (regexp-exec (make-regexp q flag) s)))
163 (and (= 0 (match:start m))
164 (= 1 (match:end m)))))))
166 ;; try on pattern "aX" where X is each character, except #\nul
167 ;; this exposes things like "?" which are special only when they
168 ;; follow a pattern to repeat or whatever ("a" in this case)
170 ((>= i char-code-limit))
171 (let* ((c (integer->char i))
173 (q (regexp-quote s)))
174 (pass-if (list "string \"aX\"" i c s q)
175 (let ((m (regexp-exec (make-regexp q flag) s)))
176 (and (= 0 (match:start m))
177 (= 2 (match:end m)))))))
179 (pass-if "string of all chars"
180 (let ((m (regexp-exec (make-regexp (regexp-quote allchars)
182 (and (= 0 (match:start m))
183 (= (string-length allchars) (match:end m))))))))
187 ;;; regexp-substitute
190 (with-test-prefix "regexp-substitute"
192 (string-match "patleft(sub1)patmid(sub2)patright"
193 "contleftpatleftsub1patmidsub2patrightcontright")))
194 (define (try expected . args)
195 (with-test-prefix (object->string args)
196 (apply vary-port regexp-substitute expected match args)))
199 (try "string1" "string1")
200 (try "string1string2" "string1" "string2")
201 (try "patleftsub1patmidsub2patright" 0)
202 (try "hi-patleftsub1patmidsub2patright-bye" "hi-" 0 "-bye")
204 (try "hi-sub1-bye" "hi-" 1 "-bye")
205 (try "hi-sub2-bye" "hi-" 2 "-bye")
206 (try "contleft" 'pre)
207 (try "contright" 'post)
208 (try "contrightcontleft" 'post 'pre)
209 (try "contrightcontleftcontrightcontleft" 'post 'pre 'post 'pre)
210 (try "contrightsub2sub1contleft" 'post 2 1 'pre)
211 (try "foosub1sub1sub1sub1bar" "foo" 1 1 1 1 "bar")))
213 (with-test-prefix "regexp-substitute/global"
215 (define (try expected . args)
216 (with-test-prefix (object->string args)
217 (apply vary-port regexp-substitute/global expected args)))
219 (try "hi" "a(x*)b" "ab" "hi")
220 (try "" "a(x*)b" "ab" 1)
221 (try "xx" "a(x*)b" "axxb" 1)
222 (try "xx" "a(x*)b" "_axxb_" 1)
223 (try "pre" "a(x*)b" "preaxxbpost" 'pre)
224 (try "post" "a(x*)b" "preaxxbpost" 'post)
225 (try "string" "x" "string" 'pre "y" 'post)
226 (try "4" "a(x*)b" "_axxb_" (lambda (m)
227 (number->string (match:end m 1))))
229 (try "_aybycyd_" "x+" "_axbxxcxxxd_" 'pre "y" 'post)
231 ;; This should not go into an infinite loop, just because the regexp
232 ;; can match the empty string. This test also kind of beats on our
233 ;; definition of where a null string can match.
234 (try "y_yaybycydy_y" "x*" "_axbxxcxxxd_" 'pre "y" 'post)
236 ;; These kind of bother me. The extension from regexp-substitute to
237 ;; regexp-substitute/global is only natural if your item list
238 ;; includes both pre and post. If those are required, why bother
239 ;; to include them at all?
240 (try "4:7:12:_" "a(x*)b" "_axxbaxbaxxxb_"
241 (lambda (m) (number->string (match:end m 1))) ":"
243 (try "4:10:19:_:19:10:4" "a(x*)b" "_axxbaxxxxbaxxxxxxxb_"
244 (lambda (m) (number->string (match:end m 1))) ":"
246 ":" (lambda (m) (number->string (match:end m 1))))
248 ;; Jan Nieuwenhuizen's bug, 2 Sep 1999
249 (try "" "_" (make-string 500 #\_)