| 1 |
;;;; srfi-197.sr64 --- SRFI 197: Pipeline Operators test suite |
| 2 |
;;;; |
| 3 |
;;;; SPDX-FileCopyrightText: 2020 Adam R. Nelson <[email protected]> |
| 4 |
;;;; |
| 5 |
;;;; SPDX-License-Identifier: MIT |
| 6 |
;;;; |
| 7 |
;;;; MIT License |
| 8 |
;;;; |
| 9 |
;;;; Copyright (c) 2020 Adam R. Nelson <[email protected]> |
| 10 |
;;;; |
| 11 |
;;;; Permission is hereby granted, free of charge, to any person |
| 12 |
;;;; obtaining a copy of this software and associated documentation |
| 13 |
;;;; files (the "Software"), to deal in the Software without |
| 14 |
;;;; restriction, including without limitation the rights to use, copy, |
| 15 |
;;;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 16 |
;;;; of the Software, and to permit persons to whom the Software is |
| 17 |
;;;; furnished to do so, subject to the following conditions: |
| 18 |
;;;; |
| 19 |
;;;; The above copyright notice and this permission notice shall be |
| 20 |
;;;; included in all copies or substantial portions of the Software. |
| 21 |
;;;; |
| 22 |
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 23 |
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 24 |
;;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 25 |
;;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 26 |
;;;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 27 |
;;;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 28 |
;;;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 |
;;;; SOFTWARE. |
| 30 |
|
| 31 |
;;; Commentary: |
| 32 |
|
| 33 |
;;; The Copyright statement above was taken from the srfi-197.html file |
| 34 |
;;; in the original source. |
| 35 |
|
| 36 |
;;; Code: |
| 37 |
|
| 38 |
(define-module (srfi-197-test) |
| 39 |
#:use-module (srfi srfi-64) |
| 40 |
#:use-module (srfi srfi-197)) |
| 41 |
|
| 42 |
(define (exclamation x) (string-append x "!")) |
| 43 |
|
| 44 |
(define (foo+bar x) (values (string-append x "foo") (string-append x "bar"))) |
| 45 |
|
| 46 |
(test-begin "Pipeline Operators") |
| 47 |
|
| 48 |
(test-equal "chain" "bazbarfoo!" |
| 49 |
(chain "" |
| 50 |
(string-append "foo" _) |
| 51 |
(string-append "bar" _) |
| 52 |
(string-append "baz" _) |
| 53 |
(exclamation _))) |
| 54 |
|
| 55 |
(test-equal "chain with mixed _ position" "barfoobaz" |
| 56 |
(chain "" |
| 57 |
(string-append _ "foo") |
| 58 |
(string-append "bar" _) |
| 59 |
(string-append _ "baz"))) |
| 60 |
|
| 61 |
(test-equal "chain with _ in operator position" 3 |
| 62 |
(chain + |
| 63 |
(_ 1 2))) |
| 64 |
|
| 65 |
(test-equal "chain without _" "barbazqux" |
| 66 |
(chain "" |
| 67 |
(string-append _ "foo") |
| 68 |
(string-append "bar" "baz") |
| 69 |
(string-append _ "qux"))) |
| 70 |
|
| 71 |
(test-equal "chain multiple _" "quxfoo/quxbar" |
| 72 |
(chain "qux" |
| 73 |
(foo+bar _) |
| 74 |
(string-append _ "/" _))) |
| 75 |
|
| 76 |
(test-equal "chain _ ..." "bazquxfooquxbar" |
| 77 |
(chain "qux" |
| 78 |
(foo+bar _) |
| 79 |
(string-append "baz" _ ...))) |
| 80 |
|
| 81 |
(test-equal "chain _ _ ..." "quxfoobazquxbar" |
| 82 |
(chain "qux" |
| 83 |
(foo+bar _) |
| 84 |
(string-append _ "baz" _ ...))) |
| 85 |
|
| 86 |
(test-equal "chain with custom _" "bazbarfoo!" |
| 87 |
(chain "" <> |
| 88 |
(string-append "foo" <>) |
| 89 |
(string-append "bar" <>) |
| 90 |
(string-append "baz" <>) |
| 91 |
(exclamation <>))) |
| 92 |
|
| 93 |
(test-equal "chain with custom ..." "bazquxfooquxbar" |
| 94 |
(chain "qux" - --- |
| 95 |
(foo+bar -) |
| 96 |
(string-append "baz" - ---))) |
| 97 |
|
| 98 |
(test-equal "chain-and" "bazbarfoo!" |
| 99 |
(chain-and "" |
| 100 |
(string-append "foo" _) |
| 101 |
(string-append "bar" _) |
| 102 |
(string-append "baz" _) |
| 103 |
(exclamation _))) |
| 104 |
|
| 105 |
(test-equal "chain-and with mixed _ position" "barfoobaz" |
| 106 |
(chain-and "" |
| 107 |
(string-append _ "foo") |
| 108 |
(string-append "bar" _) |
| 109 |
(string-append _ "baz"))) |
| 110 |
|
| 111 |
(test-equal "chain-and without _" "barbazqux" |
| 112 |
(chain-and "" |
| 113 |
(string-append "foo" _) |
| 114 |
(string-append "bar" "baz") |
| 115 |
(string-append _ "qux"))) |
| 116 |
|
| 117 |
(test-equal "chain-and short-circuit" #f |
| 118 |
(chain-and "" |
| 119 |
(string-append "foo" _) |
| 120 |
(equal? _ "bar") |
| 121 |
(string-append "baz" _) |
| 122 |
(exclamation _))) |
| 123 |
|
| 124 |
(test-equal "chain-and short-circuit first" #f |
| 125 |
(chain-and #f |
| 126 |
(not _))) |
| 127 |
|
| 128 |
(test-equal "chain-and with custom _" "bazbarfoo!" |
| 129 |
(chain-and "" <> |
| 130 |
(string-append "foo" <>) |
| 131 |
(string-append "bar" <>) |
| 132 |
(string-append "baz" <>) |
| 133 |
(exclamation <>))) |
| 134 |
|
| 135 |
(test-equal "chain-when" "bazfoo" |
| 136 |
(chain-when "" |
| 137 |
((= (+ 2 2) 4) (string-append "foo" _)) |
| 138 |
((= (+ 2 2) 5) (string-append "bar" _)) |
| 139 |
(#t (string-append "baz" _)))) |
| 140 |
|
| 141 |
(test-equal "chain-when with mixed _ position" "barfooqux" |
| 142 |
(chain-when "" |
| 143 |
(#t (string-append _ "foo")) |
| 144 |
(#t (string-append "bar" _)) |
| 145 |
(#f (string-append _ "baz")) |
| 146 |
(#t (string-append _ "qux")))) |
| 147 |
|
| 148 |
(test-equal "chain-when without _" "barqux" |
| 149 |
(chain-when "" |
| 150 |
(#t (string-append _ "foo")) |
| 151 |
(#t (string-append "bar")) |
| 152 |
(#f (string-append _ "baz")) |
| 153 |
(#t (string-append _ "qux")))) |
| 154 |
|
| 155 |
(test-equal "chain-when with custom _" "bazfoo" |
| 156 |
(chain-when "" <> |
| 157 |
((= (+ 2 2) 4) (string-append "foo" <>)) |
| 158 |
((= (+ 2 2) 5) (string-append "bar" <>)) |
| 159 |
(#t (string-append "baz" <>)))) |
| 160 |
|
| 161 |
(test-equal "chain-lambda" "bazbarfoo!" |
| 162 |
((chain-lambda (string-append "foo" _) |
| 163 |
(string-append "bar" _) |
| 164 |
(string-append "baz" _) |
| 165 |
(exclamation _)) |
| 166 |
"")) |
| 167 |
|
| 168 |
(test-equal "chain-lambda one step" "foobar" |
| 169 |
((chain-lambda (string-append "foo" _)) "bar")) |
| 170 |
|
| 171 |
(test-equal "chain-lambda with mixed _ position" "barfoobaz" |
| 172 |
((chain-lambda (string-append _ "foo") |
| 173 |
(string-append "bar" _) |
| 174 |
(string-append _ "baz")) |
| 175 |
"")) |
| 176 |
|
| 177 |
(test-equal "chain-lambda multiple _" "foobarbazqux" |
| 178 |
((chain-lambda (string-append _ "bar" _) |
| 179 |
(string-append _ "qux")) |
| 180 |
"foo" |
| 181 |
"baz")) |
| 182 |
|
| 183 |
(test-equal "chain-lambda without _" "barqux" |
| 184 |
((chain-lambda (string-append "bar") |
| 185 |
(string-append _ "qux")))) |
| 186 |
|
| 187 |
(test-equal "chain-lambda _ ..." "foobarbazqux" |
| 188 |
((chain-lambda (string-append "foo" _ ...) |
| 189 |
(string-append _ "qux")) |
| 190 |
"bar" |
| 191 |
"baz")) |
| 192 |
|
| 193 |
(test-equal "chain-lambda _ _ ..." "foobarbazquxquux" |
| 194 |
((chain-lambda (string-append _ "bar" _ ...) |
| 195 |
(string-append _ "quux")) |
| 196 |
"foo" |
| 197 |
"baz" |
| 198 |
"qux")) |
| 199 |
|
| 200 |
(test-equal "chain-lambda with custom _" "bazbarfoo!" |
| 201 |
((chain-lambda <> |
| 202 |
(string-append "foo" <>) |
| 203 |
(string-append "bar" <>) |
| 204 |
(string-append "baz" <>) |
| 205 |
(exclamation <>)) |
| 206 |
"")) |
| 207 |
|
| 208 |
(test-equal "chain-lambda with custom ..." "foobarbazqux" |
| 209 |
((chain-lambda - --- |
| 210 |
(string-append "foo" - ---) |
| 211 |
(string-append - "qux")) |
| 212 |
"bar" |
| 213 |
"baz")) |
| 214 |
|
| 215 |
(test-equal "nest" '(1 2 (3 (4) 5)) |
| 216 |
(nest (quote _) |
| 217 |
(1 2 _) |
| 218 |
(3 _ 5) |
| 219 |
(_) |
| 220 |
4)) |
| 221 |
|
| 222 |
(test-equal "nest with custom _" '(1 2 (3 (4) 5)) |
| 223 |
(nest <> |
| 224 |
(quote <>) |
| 225 |
(1 2 <>) |
| 226 |
(3 <> 5) |
| 227 |
(<>) |
| 228 |
4)) |
| 229 |
|
| 230 |
(test-equal "nested nest" '(1 2 3 (4 5 6)) |
| 231 |
(nest (nest _2 (quote _2) (1 2 3 _2) _ 6) |
| 232 |
(_ 5 _2) |
| 233 |
4)) |
| 234 |
|
| 235 |
(test-equal "nest-reverse" '(1 2 (3 (4) 5)) |
| 236 |
(nest-reverse 4 |
| 237 |
(_) |
| 238 |
(3 _ 5) |
| 239 |
(1 2 _) |
| 240 |
(quote _))) |
| 241 |
|
| 242 |
(test-equal "nest-reverse with custom _" '(1 2 (3 (4) 5)) |
| 243 |
(nest-reverse 4 <> |
| 244 |
(<>) |
| 245 |
(3 <> 5) |
| 246 |
(1 2 <>) |
| 247 |
(quote <>))) |
| 248 |
|
| 249 |
(test-end "Pipeline Operators") |
| 250 |
|
| 251 |
;; Local Variables: |
| 252 |
;; mode: scheme |
| 253 |
;; End: |