glob.scm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; 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.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-glob)
  19. #:use-module (guix glob)
  20. #:use-module (srfi srfi-64))
  21. (test-begin "glob")
  22. (define-syntax test-string->sglob
  23. (syntax-rules (=>)
  24. ((_ pattern => result rest ...)
  25. (begin
  26. (test-equal (format #f "string->sglob, ~s" pattern)
  27. result
  28. (string->sglob pattern))
  29. (test-string->sglob rest ...)))
  30. ((_)
  31. #t)))
  32. (define-syntax test-glob-match
  33. (syntax-rules (matches and not)
  34. ((_ (pattern-string matches strings ... (and not others ...)) rest ...)
  35. (begin
  36. (test-assert (format #f "glob-match? ~s" pattern-string)
  37. (let ((pattern (string->compiled-sglob pattern-string)))
  38. (and (glob-match? pattern strings) ...
  39. (not (glob-match? pattern others)) ...)))
  40. (test-glob-match rest ...)))
  41. ((_)
  42. #t)))
  43. (test-string->sglob
  44. "foo" => "foo"
  45. "?foo*" => '(? "foo" *)
  46. "foo[1-5]" => '("foo" (range #\1 #\5))
  47. "foo[abc]bar" => '("foo" (set #\a #\b #\c) "bar")
  48. "foo[a[b]c]bar" => '("foo" (set #\a #\[ #\b #\] #\c) "bar")
  49. "[123]x" => '((set #\1 #\2 #\3) "x")
  50. "[a-z]" => '((range #\a #\z)))
  51. (test-glob-match
  52. ("foo" matches "foo" (and not "foobar" "barfoo"))
  53. ("foo*" matches "foo" "foobar" (and not "xfoo"))
  54. ("foo??bar" matches "fooxxbar" "fooZZbar"
  55. (and not "foobar" "fooxxxbar" "fooxxbarzz"))
  56. ("foo?" matches "foox" (and not "fooxx"))
  57. ("ab[0-9]c" matches "ab0c" "ab7c" "ab9c"
  58. (and not "ab-c" "ab00c" "ab3"))
  59. ("ab[cdefg]" matches "abc" "abd" "abg"
  60. (and not "abh" "abcd" "ab[")))
  61. (test-end "glob")