summaryrefslogtreecommitdiff
path: root/test-suite/tests/match.test
blob: d1432d8af9c5a70ed986ac45bdd1bee1d57d2903 (plain)
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
;;;; match.test --- (ice-9 match)  -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; 	Copyright (C) 2010 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library 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
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-match)
  #:use-module (ice-9 match)
  #:use-module (test-suite lib))

(define exception:match-error
  (cons 'match-error "^.*$"))


(with-test-prefix "matches"

  (pass-if "wildcard"
    (match "hello" (_ #t)))

  (pass-if "symbol"
    (match 'foo ('foo #t)))

  (pass-if "string"
    (match "bar" ("bar" #t)))

  (pass-if "number"
    (match 777 (777 #t)))

  (pass-if "char"
    (match #\g (#\g #t)))

  (pass-if "sexp"
    (match '(a b c) ('(a b c) #t)))

  (pass-if "predicate"
    (match '(a 1 2)
      (('a (and (? odd?) one) (? even?))
       (= one 1))))

  (pass-if "list"
    (let ((lst '(a b c)))
      (match lst
        ((x y z)
         (equal? (list x y z) lst)))))

  (pass-if "list rest..."
    (let ((lst '(a b c)))
      (match lst
        ((x rest ...)
         (and (eq? x 'a) (equal? rest '(b c)))))))

  (pass-if "list . rest"
    (let ((lst '(a b c)))
      (match lst
        ((x . rest)
         (and (eq? x 'a) (equal? rest '(b c)))))))

  (pass-if "list ..1"
    (match '(a b c)
      ((x ..1)
       (equal? x '(a b c)))))

  (pass-if "list ..1, with predicate"
    (match '(a b c)
      (((and x (? symbol?)) ..1)
       (equal? x '(a b c)))))

  (pass-if "tree"
    (let ((tree '(one (two 2) (three 3 (and 4 (and 5))))))
      (match tree
        (('one ('two x) ('three y ('and z '(and 5))))
         (equal? (list x y z) '(2 3 4)))))))


(with-test-prefix "doesn't match"

  (pass-if-exception "tree"
    exception:match-error
    (match '(a (b c))
      ((foo (bar)) #t)))

  (pass-if-exception "list ..1"
    exception:match-error
    (match '()
      ((x ..1) #f)))

  (pass-if-exception "list ..1, with predicate"
    exception:match-error
    (match '(a 0)
      (((and x (? symbol?)) ..1)
       (equal? x '(a b c))))))