glob.scm 2.5 KB

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