|
| 1 | +(ns wisp.test.escodegen |
| 2 | + (:require [wisp.test.util :refer [is thrown?]] |
| 3 | + [wisp.src.sequence :refer [concat cons vec take first rest |
| 4 | + second third list list? count drop |
| 5 | + lazy-seq? seq nth map]] |
| 6 | + [wisp.src.runtime :refer [subs = dec identity keys nil? vector? |
| 7 | + string? dec re-find satisfies?]] |
| 8 | + [wisp.src.compiler :refer [compile]] |
| 9 | + [wisp.src.reader :refer [read* read-from-string] |
| 10 | + :rename {read-from-string read-string}] |
| 11 | + [wisp.src.ast :refer [meta name pr-str symbol]])) |
| 12 | + |
| 13 | +(defprotocol INope |
| 14 | + (nope? [self])) |
| 15 | + |
| 16 | +(is (thrown? (nope? 1) #"method") |
| 17 | + "protocol isn't implemented") |
| 18 | + |
| 19 | + |
| 20 | +(is (not (satisfies? INope js/Number)) |
| 21 | + "number doesn't satisfies INope") |
| 22 | + |
| 23 | +(deftype Nope [x] |
| 24 | + INope |
| 25 | + (nope? [_] true)) |
| 26 | + |
| 27 | +(is (Nope. 1) |
| 28 | + "Can be instantiated") |
| 29 | + |
| 30 | +(is (satisfies? INope (Nope.)) |
| 31 | + "satisfies protocol") |
| 32 | + |
| 33 | +(is (nope? (Nope.)) |
| 34 | + "implements protcol method") |
| 35 | + |
| 36 | +(extend-type number |
| 37 | + INope |
| 38 | + (nope? [x] true)) |
| 39 | + |
| 40 | +(is (satisfies? INope 4) "numbers implement protocol") |
| 41 | +(is (nope? 3) "numbers implement protocol") |
| 42 | + |
| 43 | +(is (not (satisfies? INope "foo")) |
| 44 | + "strings do not satisfy") |
| 45 | + |
| 46 | +(extend-type default |
| 47 | + INope |
| 48 | + (nope? [_] false)) |
| 49 | + |
| 50 | +(is (satisfies? INope "foo") |
| 51 | + "everything satisfies protocol now") |
| 52 | + |
| 53 | +(is (= (nope? "foo") false) |
| 54 | + "default implementation") |
| 55 | + |
| 56 | +(is (= (nope? 3) true) |
| 57 | + "overriden implementation") |
| 58 | + |
| 59 | +(is (= (nope? true) false) |
| 60 | + "default implementation") |
| 61 | + |
| 62 | +(defprotocol IType |
| 63 | + (-type [x])) |
| 64 | + |
| 65 | +(defn satisfaction |
| 66 | + [protocol] |
| 67 | + {:nil (satisfies? protocol nil) |
| 68 | + :boolean (satisfies? protocol true) |
| 69 | + :number (satisfies? protocol 1) |
| 70 | + :string (satisfies? protocol "foo") |
| 71 | + :pattern (satisfies? protocol #"foo") |
| 72 | + :fn (satisfies? protocol (fn [x] x)) |
| 73 | + :vector (satisfies? protocol [1 2 3]) |
| 74 | + :object (satisfies? protocol {})}) |
| 75 | + |
| 76 | +(is (= (satisfaction IType) |
| 77 | + {:nil false |
| 78 | + :boolean false |
| 79 | + :number false |
| 80 | + :string false |
| 81 | + :pattern false |
| 82 | + :fn false |
| 83 | + :vector false |
| 84 | + :object false}) |
| 85 | + "no types satisfy protocol") |
| 86 | + |
| 87 | +(extend-type nil |
| 88 | + IType |
| 89 | + (-type [_] :nil)) |
| 90 | + |
| 91 | +(is (= (satisfaction IType) |
| 92 | + {:nil true |
| 93 | + :boolean false |
| 94 | + :number false |
| 95 | + :string false |
| 96 | + :pattern false |
| 97 | + :fn false |
| 98 | + :vector false |
| 99 | + :object false}) |
| 100 | + "only nil satisfyies protocol") |
| 101 | + |
| 102 | +(extend-type boolean |
| 103 | + IType |
| 104 | + (-type [_] :boolean)) |
| 105 | + |
| 106 | +(is (= (satisfaction IType) |
| 107 | + {:nil true |
| 108 | + :boolean true |
| 109 | + :number false |
| 110 | + :string false |
| 111 | + :pattern false |
| 112 | + :fn false |
| 113 | + :vector false |
| 114 | + :object false}) |
| 115 | + "nil & booleans satisfyies protocol") |
| 116 | + |
| 117 | +(extend-type number |
| 118 | + IType |
| 119 | + (-type [_] :number)) |
| 120 | + |
| 121 | +(is (= (satisfaction IType) |
| 122 | + {:nil true |
| 123 | + :boolean true |
| 124 | + :number true |
| 125 | + :string false |
| 126 | + :pattern false |
| 127 | + :fn false |
| 128 | + :vector false |
| 129 | + :object false}) |
| 130 | + "nil, booleans & numbers satisfyies protocol") |
| 131 | + |
| 132 | +(extend-type string |
| 133 | + IType |
| 134 | + (-type [_] :string)) |
| 135 | + |
| 136 | +(is (= (satisfaction IType) |
| 137 | + {:nil true |
| 138 | + :boolean true |
| 139 | + :number true |
| 140 | + :string true |
| 141 | + :pattern false |
| 142 | + :fn false |
| 143 | + :vector false |
| 144 | + :object false}) |
| 145 | + "nil, booleans, numbers & strings satisfyies protocol") |
| 146 | + |
| 147 | +(extend-type re-pattern |
| 148 | + IType |
| 149 | + (-type [_] :pattern)) |
| 150 | + |
| 151 | +(is (= (satisfaction IType) |
| 152 | + {:nil true |
| 153 | + :boolean true |
| 154 | + :number true |
| 155 | + :string true |
| 156 | + :pattern true |
| 157 | + :fn false |
| 158 | + :vector false |
| 159 | + :object false}) |
| 160 | + "nil, booleans, numbers, strings & patterns satisfyies protocol") |
| 161 | + |
| 162 | +(extend-type function |
| 163 | + IType |
| 164 | + (-type [_] :function)) |
| 165 | + |
| 166 | +(is (= (satisfaction IType) |
| 167 | + {:nil true |
| 168 | + :boolean true |
| 169 | + :number true |
| 170 | + :string true |
| 171 | + :pattern true |
| 172 | + :fn true |
| 173 | + :vector false |
| 174 | + :object false}) |
| 175 | + "nil, booleans, numbers, strings, patterns & functions satisfyies protocol") |
| 176 | + |
| 177 | +(extend-type vector |
| 178 | + IType |
| 179 | + (-type [_] :vector)) |
| 180 | + |
| 181 | +(is (= (satisfaction IType) |
| 182 | + {:nil true |
| 183 | + :boolean true |
| 184 | + :number true |
| 185 | + :string true |
| 186 | + :pattern true |
| 187 | + :fn true |
| 188 | + :vector true |
| 189 | + :object false}) |
| 190 | + "nil, booleans, numbers, strings, patterns, functions & vectors satisfyies protocol") |
| 191 | + |
| 192 | +(extend-type default |
| 193 | + IType |
| 194 | + (-type [_] :default)) |
| 195 | + |
| 196 | +(is (= (satisfaction IType) |
| 197 | + {:nil true |
| 198 | + :boolean true |
| 199 | + :number true |
| 200 | + :string true |
| 201 | + :pattern true |
| 202 | + :fn true |
| 203 | + :vector true |
| 204 | + :object true}) |
| 205 | + "all types satisfyies protocol") |
| 206 | + |
| 207 | +(is (= (-type nil) :nil)) |
| 208 | +(is (= (-type true) :boolean)) |
| 209 | +(is (= (-type false) :boolean)) |
| 210 | +(is (= (-type 1) :number)) |
| 211 | +(is (= (-type 0) :number)) |
| 212 | +(is (= (-type 17) :number)) |
| 213 | +(is (= (-type "hello") :string)) |
| 214 | +(is (= (-type "") :string)) |
| 215 | +(is (= (-type #"foo") :pattern)) |
| 216 | +(is (= (-type (fn [x] x)) :function)) |
| 217 | +(is (= (-type #(inc %)) :function)) |
| 218 | +(is (= (-type []) :vector)) |
| 219 | +(is (= (-type [1]) :vector)) |
| 220 | +(is (= (-type [1 2 3]) :vector)) |
| 221 | +(is (= (-type {}) :default)) |
| 222 | +(is (= (-type {:a 1}) :default)) |
| 223 | + |
| 224 | +(defprotocol IFoo |
| 225 | + (foo? [x])) |
| 226 | + |
| 227 | +(is (= (satisfaction IFoo) |
| 228 | + {:nil false |
| 229 | + :boolean false |
| 230 | + :number false |
| 231 | + :string false |
| 232 | + :pattern false |
| 233 | + :fn false |
| 234 | + :vector false |
| 235 | + :object false}) |
| 236 | + "no types satisfyies protocol") |
| 237 | + |
| 238 | +(extend-type default |
| 239 | + IFoo |
| 240 | + (foo? [_] false)) |
| 241 | + |
| 242 | +(is (= (satisfaction IFoo) |
| 243 | + {:nil true |
| 244 | + :boolean true |
| 245 | + :number true |
| 246 | + :string true |
| 247 | + :pattern true |
| 248 | + :fn true |
| 249 | + :vector true |
| 250 | + :object true}) |
| 251 | + "all types satisfy protocol") |
| 252 | + |
| 253 | +(defprotocol IBar |
| 254 | + (bar? [x])) |
| 255 | + |
| 256 | +(extend-type js/Object |
| 257 | + IBar |
| 258 | + (bar? [_] true)) |
| 259 | + |
| 260 | +(is (= (satisfaction IBar) |
| 261 | + {:nil false |
| 262 | + :boolean false |
| 263 | + :number false |
| 264 | + :string false |
| 265 | + :pattern false |
| 266 | + :fn false |
| 267 | + :vector false |
| 268 | + :object true}) |
| 269 | + "only objects satisfy protocol") |
| 270 | + |
| 271 | +(extend-type js/Number |
| 272 | + IBar |
| 273 | + (bar? [_] true)) |
| 274 | + |
| 275 | +(is (= (satisfaction IBar) |
| 276 | + {:nil false |
| 277 | + :boolean false |
| 278 | + :number true |
| 279 | + :string false |
| 280 | + :pattern false |
| 281 | + :fn false |
| 282 | + :vector false |
| 283 | + :object true}) |
| 284 | + "only objects & numbers satisfy protocol") |
| 285 | + |
| 286 | + |
| 287 | +(extend-type js/String |
| 288 | + IBar |
| 289 | + (bar? [_] true)) |
| 290 | + |
| 291 | +(is (= (satisfaction IBar) |
| 292 | + {:nil false |
| 293 | + :boolean false |
| 294 | + :number true |
| 295 | + :string true |
| 296 | + :pattern false |
| 297 | + :fn false |
| 298 | + :vector false |
| 299 | + :object true}) |
| 300 | + "only objects, numbers & strings satisfy protocol") |
| 301 | + |
| 302 | + |
| 303 | +(extend-type js/Boolean |
| 304 | + IBar |
| 305 | + (bar? [_] true)) |
| 306 | + |
| 307 | +(is (= (satisfaction IBar) |
| 308 | + {:nil false |
| 309 | + :boolean true |
| 310 | + :number true |
| 311 | + :string true |
| 312 | + :pattern false |
| 313 | + :fn false |
| 314 | + :vector false |
| 315 | + :object true}) |
| 316 | + "only objects, numbers, strings & booleans satisfy protocol") |
| 317 | + |
| 318 | +(extend-type js/Function |
| 319 | + IBar |
| 320 | + (bar? [_] true)) |
| 321 | + |
| 322 | +(is (= (satisfaction IBar) |
| 323 | + {:nil false |
| 324 | + :boolean true |
| 325 | + :number true |
| 326 | + :string true |
| 327 | + :pattern false |
| 328 | + :fn true |
| 329 | + :vector false |
| 330 | + :object true}) |
| 331 | + "only objects, numbers, strings, booleans & functions satisfy protocol") |
| 332 | + |
| 333 | +(extend-type js/Array |
| 334 | + IBar |
| 335 | + (bar? [_] true)) |
| 336 | + |
| 337 | +(is (= (satisfaction IBar) |
| 338 | + {:nil false |
| 339 | + :boolean true |
| 340 | + :number true |
| 341 | + :string true |
| 342 | + :pattern false |
| 343 | + :fn true |
| 344 | + :vector true |
| 345 | + :object true}) |
| 346 | + "only objects, numbers, strings, booleans, functions & array satisfy protocol") |
| 347 | + |
| 348 | +(extend-type js/RegExp |
| 349 | + IBar |
| 350 | + (bar? [_] true)) |
| 351 | + |
| 352 | +(is (= (satisfaction IBar) |
| 353 | + {:nil false |
| 354 | + :boolean true |
| 355 | + :number true |
| 356 | + :string true |
| 357 | + :pattern true |
| 358 | + :fn true |
| 359 | + :vector true |
| 360 | + :object true}) |
| 361 | + "only objects, numbers, strings, booleans, functions & patterns satisfy protocol") |
0 commit comments