match.test 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #! /bin/sh
  2. # -*-scheme-*-
  3. exec ${MES-src/mes} --no-auto-compile -L ${0%/*} -L module -C module -e '(tests match)' -s "$0" "$@"
  4. !#
  5. ;;; -*-scheme-*-
  6. ;;; GNU Mes --- Maxwell Equations of Software
  7. ;;; Copyright © 2016,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  8. ;;;
  9. ;;; This file is part of GNU Mes.
  10. ;;;
  11. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Mes is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (tests match)
  24. #:use-module (mes mes-0)
  25. #:use-module (mes test))
  26. (mes-use-module (mes match))
  27. (mes-use-module (mes test))
  28. (cond-expand
  29. (guile
  30. (use-modules (ice-9 match)))
  31. (mes))
  32. (pass-if "first dummy" #t)
  33. (pass-if-not "second dummy" #f)
  34. (pass-if "match symbol"
  35. (seq?
  36. (match 'bla
  37. ('bla 'bla))
  38. 'bla))
  39. (pass-if "match no symbol"
  40. (sequal?
  41. (match 'foo
  42. ('bla 'bla)
  43. (_ "no match: foo"))
  44. "no match: foo"))
  45. (pass-if "match symbol?"
  46. (seq?
  47. (match 'foo
  48. ((? symbol?) 'symbol)
  49. (_ "no match: symbol")
  50. )
  51. 'symbol))
  52. (pass-if "match list"
  53. (sequal?
  54. (match '(0)
  55. ((0) '(0))
  56. (_ "no match: (0)"))
  57. '(0)))
  58. (pass-if "match list 2"
  59. (sequal?
  60. (match (list 1 2 3) ((1 b c) (list b c)))
  61. '(2 3)))
  62. (pass-if "match unquote"
  63. (sequal?
  64. (match (list 1 2 3) (`(1 ,b ,c) (list b c)))
  65. '(2 3)))
  66. (pass-if "match x-hygiene"
  67. (seq?
  68. (match '(0 1 2)
  69. ((0 x y) (+ x y))
  70. (_ "no match: 0 1 2"))
  71. 3))
  72. (pass-if "match ellipsis"
  73. (sequal?
  74. (match '(1 2)
  75. ((t ...) t)
  76. (_ "no match: (1 2)"))
  77. '(1 2)))
  78. (pass-if-equal "match nyacc 0"
  79. '(rest)
  80. (match '(first rest)
  81. ((first . rest)
  82. rest)))
  83. (pass-if-equal "match nyacc 1"
  84. '(#\. rest)
  85. (match '(first #\. rest)
  86. (('first . rest)
  87. rest)))
  88. (let ((tkl0-simple '((ident . type) rest)))
  89. (pass-if-equal "match nyacc simple"
  90. (cons (cdar tkl0-simple) (cdr tkl0-simple))
  91. (match tkl0-simple
  92. ((('ident . val) . rest)
  93. (cons val rest)))))
  94. (let ((tkl0 '((ident . "type") #\. #\] (arg . "0") #\[ (ident . "g_cells"))))
  95. (pass-if-equal "match nyacc tkl0"
  96. (cdr tkl0)
  97. (match tkl0
  98. ((('ident . val) . rest)
  99. rest))))
  100. (result 'report (if mes? 2 0))