1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
;;; Gash -- Guile As SHell
;;; Copyright © 2018, 2019 Timothy Sample <samplet@ngyro.com>
;;;
;;; This file is part of Gash.
;;;
;;; Gash is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Gash is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Gash. If not, see <http://www.gnu.org/licenses/>.
(define-module (test-pattern)
#:use-module (gash pattern)
#:use-module (srfi srfi-64)
#:use-module (tests unit automake))
;;; Commentary:
;;;
;;; Tests for the pattern module.
;;;
;;; Code:
(test-begin "pattern")
;;; Basic matching
(test-assert "Matches single characters"
(pattern-match? (parse-pattern "a") "a"))
(test-assert "Matches a sequence of characters"
(pattern-match? (parse-pattern "abc") "abc"))
(test-assert "Matches an empty string"
(pattern-match? (parse-pattern "") ""))
(test-assert "Fails on pattern too long"
(not (pattern-match? (parse-pattern "ab") "a")))
(test-assert "Fails on pattern too short"
(not (pattern-match? (parse-pattern "a") "ab")))
;;; Question marks
(test-assert "Matches a question mark with anything"
(pattern-match? (parse-pattern "???") "abc"))
(test-assert "Does not match question mark with empty string"
(not (pattern-match? (parse-pattern "?") "")))
;;; Bracket expressions
(test-assert "Matches with bracket expressions"
(pattern-match? (parse-pattern "[abc][def][ghi]") "aei"))
(test-assert "Fails on non-matching bracket expressions"
(not (pattern-match? (parse-pattern "[abc][def][ghi]") "aex")))
(test-assert "Matches unterminated bracket expression normally"
(pattern-match? (parse-pattern "a[bc") "a[bc"))
(test-assert "Matches with bracket expressions with left brackets"
(pattern-match? (parse-pattern "a[[]c") "a[c"))
(test-assert "Matches with bracket expressions with right brackets"
(pattern-match? (parse-pattern "a[]]c") "a]c"))
(test-assert "Matches with bracket expressions starting with hyphen"
(pattern-match? (parse-pattern "a[-b]c") "a-c"))
(test-assert "Matches with bracket expressions ending with hyphen"
(pattern-match? (parse-pattern "a[b-]c") "a-c"))
(test-assert "Matches with bracket expressions containing bang"
(pattern-match? (parse-pattern "a[b!]c") "a!c"))
(test-assert "Matches with negated bracket expressions"
(pattern-match? (parse-pattern "a[!x]c") "abc"))
(test-assert "Fails on non-matching negated bracket expressions"
(not (pattern-match? (parse-pattern "a[!x]c") "axc")))
;; We do not fully support the following features. However, rather
;; than do something strange, we raise explicit errors when they are
;; encountered.
(test-error "Does not allow collating symbols"
(parse-pattern "[[.ch.]]"))
(test-assert "Allows unterminated collating symbols"
(parse-pattern "[[.ch]"))
(test-error "Does not allow equivalence classes"
(parse-pattern "[[=a=]]"))
(test-assert "Allows unterminated equivalence classes"
(parse-pattern "[[=a]"))
(test-error "Does not allow character classes"
(parse-pattern "[[:space:]]"))
(test-assert "Allows unterminated character classes"
(parse-pattern "[[:space]"))
(test-error "Does not allow general character ranges"
(parse-pattern "[<->]"))
(test-assert "Allows [a-z]"
(parse-pattern "[a-z]"))
(test-assert "Allows [A-Z]"
(parse-pattern "[A-Z]"))
(test-assert "Allows [0-9]"
(parse-pattern "[0-9]"))
(test-assert "Matches with allowed character ranges"
(pattern-match? (parse-pattern "[a-z][A-Z][0-9]") "mJ2"))
(test-assert "Allows bad ranges in not-quite bracket expressions"
(pattern-plain? (parse-pattern "[]-foo")))
;;; Asterisks
(test-assert "Matches with asterisk"
(pattern-match? (parse-pattern "*") "abc"))
(test-assert "Matches empty string with asterisk"
(pattern-match? (parse-pattern "*") ""))
(test-assert "Matches with trailing asterisk"
(pattern-match? (parse-pattern "foo*") "foobar"))
(test-assert "Fails on non-matching trailing asterisk"
(not (pattern-match? (parse-pattern "foo*") "goobar")))
(test-assert "Matches with leading asterisk"
(pattern-match? (parse-pattern "*bar") "foobar"))
(test-assert "Fails on non-matching leading asterisk"
(not (pattern-match? (parse-pattern "*bar") "foobaz")))
(test-assert "Fails on non-matching leading asterisk (internal match)"
(not (pattern-match? (parse-pattern "*bar") "foobarbaz")))
(test-assert "Matches with internal asterisk"
(pattern-match? (parse-pattern "foo*bar") "foo boo! bar"))
(test-assert "Fails on non-matching internal asterisk (start)"
(not (pattern-match? (parse-pattern "foo*bar") "goo boo! bar")))
(test-assert "Fails on non-matching internal asterisk (end)"
(not (pattern-match? (parse-pattern "foo*bar") "foo boo! baz")))
;;; Quoting
;; TODO: Test quoting.
;;; Plain patterns
(test-assert "Recognizes plain patterns"
(pattern-plain? (parse-pattern "foo")))
(test-assert "Asterisks at the start are not plain"
(not (pattern-plain? (parse-pattern "*foo"))))
(test-assert "Asterisks at the end are not plain"
(not (pattern-plain? (parse-pattern "foo*"))))
(test-assert "Asterisks in the middle are not plain"
(not (pattern-plain? (parse-pattern "f*o"))))
(test-assert "Question marks are not plain"
(not (pattern-plain? (parse-pattern "f?o"))))
(test-assert "Character classes are not plain"
(not (pattern-plain? (parse-pattern "[fo]o"))))
;;; Dropping
;; Dropping on the left.
(test-equal "Drops by a pattern"
"obar"
(pattern-drop (parse-pattern "*o") "foobar"))
(test-equal "Drops by a pattern (greedy)"
"bar"
(pattern-drop (parse-pattern "*o") "foobar" #:greedy? #t))
(test-equal "Does not drop by an empty pattern"
"foobar"
(pattern-drop (parse-pattern "") "foobar"))
(test-equal "Does not drop by a non-matching pattern"
"foobar"
(pattern-drop (parse-pattern "*x*") "foobar"))
(test-equal "Drops by a pattern ending with a wildcard"
"oobar"
(pattern-drop (parse-pattern "f*") "foobar"))
(test-equal "Drops by a pattern ending with a wildcard (greedy)"
""
(pattern-drop (parse-pattern "f*") "foobar" #:greedy? #t))
;; Dropping on the right.
(test-equal "Drops-right by a pattern"
"buz"
(pattern-drop-right (parse-pattern "z*") "buzzy"))
(test-equal "Drops-right by a pattern (greedy)"
"bu"
(pattern-drop-right (parse-pattern "z*") "buzzy" #:greedy? #t))
(test-equal "Does not drop-right by an empty pattern"
"buzzy"
(pattern-drop-right (parse-pattern "") "buzzy"))
(test-equal "Does not drop-right by a non-matching pattern"
"buzzy"
(pattern-drop-right (parse-pattern "*x*") "buzzy"))
(test-equal "Drops-right by a pattern starting with a wildcard"
"buzz"
(pattern-drop-right (parse-pattern "*y") "buzzy"))
(test-equal "Drops-right by a pattern starting with a wildcard (greedy)"
""
(pattern-drop-right (parse-pattern "*y") "buzzy" #:greedy? #t))
(test-end)
;; Local Variables:
;; eval: (put 'test-error 'scheme-indent-function 1)
;; End:
|