www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

pat-expander-tests.rkt (6113B)


      1 #lang turnstile
      2 
      3 (require turnstile/rackunit-typechecking
      4          "pat-expander-tests-def.rkt")
      5 
      6 ;; The for/list macro defined in "pat-expander-tests-def.rkt" uses the
      7 ;; ~typecheck pattern-expander to typecheck the for clauses within a
      8 ;; syntax class.
      9 
     10 ;; These tests make sure that #:when conditions can refer to
     11 ;; identifiers defined in previous clauses.
     12 
     13 (check-type (for/list () 1) : (Listof Int) -> (list 1))
     14 (check-type (for/list () #t) : (Listof Bool) -> (list #t))
     15 (check-type (for/list () #f) : (Listof Bool) -> (list #f))
     16 
     17 (check-type (for/list (#:when #t) 1) : (Listof Int) -> (list 1))
     18 (check-type (for/list (#:when #f) 1) : (Listof Int) -> (list))
     19 (check-type (for/list ([x (in-range 5)]) x)
     20             : (Listof Int)
     21             -> (list 0 1 2 3 4))
     22 
     23 (check-type (for/list ([(s i) (in-indexed (in-list (list "a" "b" "c")))])
     24               (tuple s i))
     25             : (Listof (Tuple String Int))
     26             -> (list (tuple "a" 0) (tuple "b" 1) (tuple "c" 2)))
     27 
     28 (check-type (for/list ([(s i) (in-indexed (in-list (list "a" "b" "c")))]
     29                        #:when (even? i))
     30               (tuple s i))
     31             : (Listof (Tuple String Int))
     32             -> (list (tuple "a" 0) (tuple "c" 2)))
     33 
     34 (check-type (for/list ([(s i) (in-indexed (in-list (list "a" "b" "c" "d" "e")))]
     35                        #:when (even? i)
     36                        [j (in-range i)])
     37               (tuple s i j))
     38             : (Listof (Tuple String Int Int))
     39             -> (list (tuple "c" 2 0)
     40                      (tuple "c" 2 1)
     41                      (tuple "e" 4 0)
     42                      (tuple "e" 4 1)
     43                      (tuple "e" 4 2)
     44                      (tuple "e" 4 3)))
     45 
     46 ;; ------------------------------------------------------------------------
     47 
     48 ;; Test based on section 11 of the racket guide
     49 
     50 (check-type (for/list ([book (in-list (list "Guide" "Reference" "Notes"))]
     51                        #:when (not (string=? book "Notes"))
     52                        [i (in-naturals 1)]
     53                        [chapter (in-list (list "Intro" "Details" "Conclusion" "Index"))]
     54                        #:when (not (string=? chapter "Index")))
     55               (tuple book i chapter))
     56             : (Listof (Tuple String Int String))
     57             -> (list (tuple "Guide" 1 "Intro")
     58                      (tuple "Guide" 2 "Details")
     59                      (tuple "Guide" 3 "Conclusion")
     60                      (tuple "Reference" 1 "Intro")
     61                      (tuple "Reference" 2 "Details")
     62                      (tuple "Reference" 3 "Conclusion")))
     63 
     64 (check-type (for/list ([book (in-list (list "Guide" "Story" "Reference"))]
     65                        #:break (string=? book "Story")
     66                        [chapter (in-list (list "Intro" "Details" "Conclusion"))])
     67               (tuple book chapter))
     68             : (Listof (Tuple String String))
     69             -> (list (tuple "Guide" "Intro")
     70                      (tuple "Guide" "Details")
     71                      (tuple "Guide" "Conclusion")))
     72 
     73 (check-type (for/list ([book (in-list (list "Guide" "Story" "Reference"))]
     74                        #:when #true
     75                        [chapter (in-list (list "Intro" "Details" "Conclusion"))]
     76                        #:break (and (string=? book "Story")
     77                                     (string=? chapter "Conclusion")))
     78               (tuple book chapter))
     79             : (Listof (Tuple String String))
     80             -> (list (tuple "Guide" "Intro")
     81                      (tuple "Guide" "Details")
     82                      (tuple "Guide" "Conclusion")
     83                      (tuple "Story" "Intro")
     84                      (tuple "Story" "Details")))
     85 
     86 (check-type (for/list ([book (in-list (list "Guide" "Story" "Reference"))]
     87                        #:when #true
     88                        [chapter (in-list (list "Intro" "Details" "Conclusion"))]
     89                        #:final (and (string=? book "Story")
     90                                     (string=? chapter "Conclusion")))
     91               (tuple book chapter))
     92             : (Listof (Tuple String String))
     93             -> (list (tuple "Guide" "Intro")
     94                      (tuple "Guide" "Details")
     95                      (tuple "Guide" "Conclusion")
     96                      (tuple "Story" "Intro")
     97                      (tuple "Story" "Details")
     98                      (tuple "Story" "Conclusion")))
     99 
    100 (check-type (for/list ([book (in-list (list "Guide" "Story" "Reference"))]
    101                        #:final (string=? book "Story")
    102                        [chapter (in-list (list "Intro" "Details" "Conclusion"))])
    103               (tuple book chapter))
    104             : (Listof (Tuple String String))
    105             -> (list (tuple "Guide" "Intro")
    106                      (tuple "Guide" "Details")
    107                      (tuple "Guide" "Conclusion")
    108                      (tuple "Story" "Intro")))
    109 
    110 ;; ------------------------------------------------------------------------
    111 
    112 ;; Tests based on section 3.18 of the racket reference
    113 
    114 (check-type (for/list ([i (in-list (list 1 2 3))]
    115                        [j (in-list (list "a" "b" "c"))]
    116                        #:when (odd? i)
    117                        [k (in-list (list #t #f))])
    118               (tuple i j k))
    119             : (Listof (Tuple Int String Bool))
    120             -> (list (tuple 1 "a" #t)
    121                      (tuple 1 "a" #f)
    122                      (tuple 3 "c" #t)
    123                      (tuple 3 "c" #f)))
    124 
    125 (check-type (for/list ([i (in-list (list 1 2 3))]
    126                        [j (in-list (list "a" "b" "c"))]
    127                        #:break (not (odd? i))
    128                        [k (in-list (list #t #f))])
    129               (tuple i j k))
    130             : (Listof (Tuple Int String Bool))
    131             -> (list (tuple 1 "a" #true)
    132                      (tuple 1 "a" #false)))
    133 
    134 (check-type (for/list ([i (in-list (list 1 2 3))]
    135                        [j (in-list (list "a" "b" "c"))]
    136                        #:final (not (odd? i))
    137                        [k (in-list (list #t #f))])
    138               (tuple i j k))
    139             : (Listof (Tuple Int String Bool))
    140             -> (list (tuple 1 "a" #true)
    141                      (tuple 1 "a" #false)
    142                      (tuple 2 "b" #true)))
    143