test-regexps.scm 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ;;; Copyright (C) 2024 David Thompson <dave@spritely.institute>
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Regular expression tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-regexps")
  22. (with-additional-imports ((hoot regexps))
  23. (test-call "#t"
  24. (lambda (pattern str)
  25. (regexp-match? (regexp-exec (make-regexp pattern) str)))
  26. "[fo]+"
  27. "foo")
  28. (test-call "#f"
  29. (lambda (pattern str)
  30. (regexp-match? (regexp-exec (make-regexp pattern) str)))
  31. "[fo]+"
  32. "bar")
  33. (test-call "\"xfoox\""
  34. (lambda (pattern str)
  35. (regexp-match-string (regexp-exec (make-regexp pattern) str)))
  36. "[fo]+"
  37. "xfoox")
  38. (test-call "1"
  39. (lambda (pattern str)
  40. (regexp-match-start (regexp-exec (make-regexp pattern) str)))
  41. "[fo]+"
  42. "xfoox")
  43. (test-call "4"
  44. (lambda (pattern str)
  45. (regexp-match-end (regexp-exec (make-regexp pattern) str)))
  46. "[fo]+"
  47. "xfoox")
  48. (test-call "\"foo\""
  49. (lambda (pattern str)
  50. (regexp-match-substring (regexp-exec (make-regexp pattern) str)))
  51. "[fo]+"
  52. "xfoox")
  53. (test-call "(\"feed\" #f \"feed\" \"\" nope)"
  54. (lambda (pattern str)
  55. (let* ((re (make-regexp pattern))
  56. (m (regexp-exec re str)))
  57. (list (regexp-match-substring m 0)
  58. ;; doesn't match
  59. (regexp-match-substring m 1)
  60. ;; matches
  61. (regexp-match-substring m 2)
  62. ;: matches but with 0 chars
  63. (regexp-match-substring m 3)
  64. ;; index out of bounds
  65. (with-exception-handler (lambda (exn) 'nope)
  66. (lambda () (regexp-match-substring m 4))
  67. #:unwind? #t))))
  68. "([abc]+)|([def]+)([ghi]*)"
  69. "feed")
  70. (test-call "\"aaa\""
  71. (lambda (pattern str)
  72. (regexp-match-prefix (regexp-exec (make-regexp pattern) str)))
  73. "b+"
  74. "aaabbbccc")
  75. (test-call "\"ccc\""
  76. (lambda (pattern str)
  77. (regexp-match-suffix (regexp-exec (make-regexp pattern) str)))
  78. "b+"
  79. "aaabbbccc"))
  80. (test-end* "test-regexps")