1 ;;;; regexp.test --- test Guile's regexps -*- coding: utf-8; mode: scheme -*-
2 ;;;; Jim Blandy <jimb@red-bean.com> --- September 1999
4 ;;;; Copyright (C) 1999, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library 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 GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (define-module (test-suite test-regexp)
21 #:use-module (test-suite lib)
22 #:use-module (srfi srfi-1)
23 #:use-module (ice-9 regex))
25 (if (defined? 'setlocale)
26 (setlocale LC_ALL "C"))
28 ;; Don't fail if we can't display a test name to stdout/stderr.
29 (set-port-conversion-strategy! (current-output-port) 'escape)
30 (set-port-conversion-strategy! (current-error-port) 'escape)
33 ;;; Run a regexp-substitute or regexp-substitute/global test, once
34 ;;; providing a real port and once providing #f, requesting direct
36 (define (vary-port func expected . args)
37 (pass-if "port is string port"
39 (call-with-output-string
41 (apply func port args)))))
44 (apply func #f args))))
46 (define (object->string obj)
47 (call-with-output-string
55 (with-test-prefix "make-regexp"
57 (pass-if-exception "no args" exception:wrong-num-args
60 (pass-if-exception "bad pat arg" exception:wrong-type-arg
63 ;; in guile prior to 1.6.5 make-regex didn't validate its flags args
64 (pass-if-exception "bad arg 2" exception:wrong-type-arg
65 (make-regexp "xyz" 'abc))
67 (pass-if-exception "bad arg 3" exception:wrong-type-arg
68 (make-regexp "xyz" regexp/icase 'abc)))
74 (with-test-prefix "match:string"
77 (string=? "foo" (match:string (string-match ".*" "foo"))))
79 (pass-if "foo offset 1"
80 (string=? "foo" (match:string (string-match ".*" "foo" 1)))))
86 (with-test-prefix "regexp-exec"
88 (pass-if-exception "non-integer offset" exception:wrong-type-arg
89 (let ((re (make-regexp "ab+")))
90 (regexp-exec re "aaaabbbb" 1.5 'bogus-flags-arg)))
92 (pass-if-exception "non-string input" exception:wrong-type-arg
93 (let ((re (make-regexp "ab+")))
94 (regexp-exec re 'not-a-string)))
96 (pass-if-exception "non-string input, with offset" exception:wrong-type-arg
97 (let ((re (make-regexp "ab+")))
98 (regexp-exec re 'not-a-string 5)))
100 ;; in guile 1.8.1 and earlier, a #\nul character in the input string was
101 ;; only detected in a critical section, and the resulting error throw
102 ;; abort()ed the program
103 (pass-if-exception "nul in input" exception:string-contains-nul
104 (let ((re (make-regexp "ab+")))
105 (regexp-exec re (string #\a #\b (integer->char 0)))))
107 ;; in guile 1.8.1 and earlier, a bogus flags argument was only detected
108 ;; inside a critical section, and the resulting error throw abort()ed the
110 (pass-if-exception "non-integer flags" exception:wrong-type-arg
111 (let ((re (make-regexp "ab+")))
112 (regexp-exec re "aaaabbbb" 0 'bogus-flags-arg))))
118 (with-test-prefix "fold-matches"
120 (pass-if "without flags"
122 (fold-matches "^[a-z]+$" "hello" '()
123 (lambda (match result)
124 (cons (match:substring match)
127 (pass-if "with flags"
128 ;; Prior to 1.8.6, passing an additional flag would not work.
130 (fold-matches "^[a-z]+$" "hello" '()
131 (lambda (match result)
132 (cons (match:substring match)
134 (logior regexp/notbol regexp/noteol)))))
141 (define-syntax with-ascii-or-latin1-locale
145 (with-latin1-locale body ...)
148 (with-test-prefix "regexp-quote"
150 (pass-if-exception "no args" exception:wrong-num-args
153 (pass-if-exception "bad string arg" exception:wrong-type-arg
154 (regexp-quote 'blah))
156 (let ((lst `((regexp/basic ,regexp/basic)
157 (regexp/extended ,regexp/extended)))
158 ;; string of all characters, except #\nul which doesn't work because
159 ;; it's the usual end-of-string for the underlying C regexec()
160 (allchars (list->string (map integer->char
161 (cdr (iota char-code-limit))))))
164 (let ((name (car elem))
167 (with-test-prefix name
169 ;; try on each individual character, except #\nul
171 ((>= i char-code-limit))
172 (let* ((c (integer->char i))
174 (pass-if (list "char" i (format #f "~s ~s" c s))
175 (with-ascii-or-latin1-locale i
176 (let* ((q (regexp-quote s))
177 (m (regexp-exec (make-regexp q flag) s)))
178 (and (= 0 (match:start m))
179 (= 1 (match:end m))))))))
181 ;; try on pattern "aX" where X is each character, except #\nul
182 ;; this exposes things like "?" which are special only when they
183 ;; follow a pattern to repeat or whatever ("a" in this case)
185 ((>= i char-code-limit))
186 (let* ((c (integer->char i))
188 (q (regexp-quote s)))
189 (pass-if (list "string \"aX\"" i (format #f "~s ~s ~s" c s q))
190 (with-ascii-or-latin1-locale i
191 (let* ((m (regexp-exec (make-regexp q flag) s)))
192 (and (= 0 (match:start m))
193 (= 2 (match:end m))))))))
195 (pass-if "string of all chars"
197 (let ((m (regexp-exec (make-regexp (regexp-quote allchars)
199 (and (= 0 (match:start m))
200 (= (string-length allchars) (match:end m)))))))))
204 ;;; regexp-substitute
207 (with-test-prefix "regexp-substitute"
209 (string-match "patleft(sub1)patmid(sub2)patright"
210 "contleftpatleftsub1patmidsub2patrightcontright")))
211 (define (try expected . args)
212 (with-test-prefix (object->string args)
213 (apply vary-port regexp-substitute expected match args)))
216 (try "string1" "string1")
217 (try "string1string2" "string1" "string2")
218 (try "patleftsub1patmidsub2patright" 0)
219 (try "hi-patleftsub1patmidsub2patright-bye" "hi-" 0 "-bye")
221 (try "hi-sub1-bye" "hi-" 1 "-bye")
222 (try "hi-sub2-bye" "hi-" 2 "-bye")
223 (try "contleft" 'pre)
224 (try "contright" 'post)
225 (try "contrightcontleft" 'post 'pre)
226 (try "contrightcontleftcontrightcontleft" 'post 'pre 'post 'pre)
227 (try "contrightsub2sub1contleft" 'post 2 1 'pre)
228 (try "foosub1sub1sub1sub1bar" "foo" 1 1 1 1 "bar")))
230 (with-test-prefix "regexp-substitute/global"
232 (define (try expected . args)
233 (with-test-prefix (object->string args)
234 (apply vary-port regexp-substitute/global expected args)))
236 (try "hi" "a(x*)b" "ab" "hi")
237 (try "" "a(x*)b" "ab" 1)
238 (try "xx" "a(x*)b" "axxb" 1)
239 (try "xx" "a(x*)b" "_axxb_" 1)
240 (try "pre" "a(x*)b" "preaxxbpost" 'pre)
241 (try "post" "a(x*)b" "preaxxbpost" 'post)
242 (try "string" "x" "string" 'pre "y" 'post)
243 (try "4" "a(x*)b" "_axxb_" (lambda (m)
244 (number->string (match:end m 1))))
246 (try "_aybycyd_" "x+" "_axbxxcxxxd_" 'pre "y" 'post)
248 ;; This should not go into an infinite loop, just because the regexp
249 ;; can match the empty string. This test also kind of beats on our
250 ;; definition of where a null string can match.
251 (try "y_yaybycydy_y" "x*" "_axbxxcxxxd_" 'pre "y" 'post)
253 ;; These kind of bother me. The extension from regexp-substitute to
254 ;; regexp-substitute/global is only natural if your item list
255 ;; includes both pre and post. If those are required, why bother
256 ;; to include them at all?
257 (try "4:7:12:_" "a(x*)b" "_axxbaxbaxxxb_"
258 (lambda (m) (number->string (match:end m 1))) ":"
260 (try "4:10:19:_:19:10:4" "a(x*)b" "_axxbaxxxxbaxxxxxxxb_"
261 (lambda (m) (number->string (match:end m 1))) ":"
263 ":" (lambda (m) (number->string (match:end m 1))))
265 ;; Jan Nieuwenhuizen's bug, 2 Sep 1999
266 (try "" "_" (make-string 500 #\_)
269 (with-test-prefix "nonascii locales"
270 (pass-if "match structures refer to char offsets"
271 (with-locale "en_US.utf8"
273 (equal? (match:substring (string-match ".*" "calçot") 0)