|
| 1 | +# # Specification for Future |
| 2 | + |
| 3 | +/** ^ |
| 4 | + * Copyright (c) 2013 Quildreen Motta |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person |
| 7 | + * obtaining a copy of this software and associated documentation files |
| 8 | + * (the "Software"), to deal in the Software without restriction, |
| 9 | + * including without limitation the rights to use, copy, modify, merge, |
| 10 | + * publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | + * and to permit persons to whom the Software is furnished to do so, |
| 12 | + * subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be |
| 15 | + * included in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +spec = (require 'hifive')! |
| 27 | +Future = require '../../src/' |
| 28 | +{for-all, data: {Any:BigAny, Int}, sized, choice, transform} = require 'claire' |
| 29 | +{ok, throws, equal} = require 'assert' |
| 30 | + |
| 31 | + |
| 32 | +Any = sized (-> 10), BigAny |
| 33 | +AnyF = choice Any, (transform (-> Future.of it), AnyF) |
| 34 | + |
| 35 | +k = (a, b) --> a |
| 36 | +id = (a) -> a |
| 37 | +rejected = (a) -> new Future (f, _) -> f a |
| 38 | + |
| 39 | +module.exports = spec 'Future' (o, spec) -> |
| 40 | + |
| 41 | + o 'ap(b) should do nothing for rejected futures.' do |
| 42 | + for-all(AnyF, AnyF).satisfy (a, b) -> |
| 43 | + f = -> a |
| 44 | + (rejected f).ap(Future.of(b)).is-equal (rejected f) |
| 45 | + .as-test! |
| 46 | + |
| 47 | + o 'map(f) should do nothing for rejected futures.' do |
| 48 | + for-all(AnyF, AnyF).satisfy (a, b) -> |
| 49 | + (rejected a).map(-> b).is-equal (rejected a) |
| 50 | + .as-test! |
| 51 | + |
| 52 | + o 'chain(f) should do nothing for rejected futures.' do |
| 53 | + for-all(AnyF, AnyF).satisfy (a, b) -> |
| 54 | + (rejected a).chain(-> Future.of b).is-equal (rejected a) |
| 55 | + .as-test! |
| 56 | + |
| 57 | + spec 'orElse(f)' (o) -> |
| 58 | + o 'Should do nothing for resolved futures.' do |
| 59 | + for-all(AnyF, AnyF).satisfy (a, b) -> |
| 60 | + Future.of(a).or-else(-> rejected b).is-equal Future.of(a) |
| 61 | + .as-test! |
| 62 | + o 'Should propagate rejected futures.' do |
| 63 | + for-all(AnyF, AnyF).satisfy (a, b) -> |
| 64 | + (rejected a).or-else(-> Future.of b).is-equal Future.of(b) |
| 65 | + .as-test! |
| 66 | + |
| 67 | + spec 'fold(f,g)' (o) -> |
| 68 | + o 'Should return a resolved future mapped by f if rejected.' do |
| 69 | + for-all(AnyF, AnyF, AnyF).satisfy (a, b, c) -> |
| 70 | + (rejected a).fold((-> b), (-> c)).is-equal Future.of(b) |
| 71 | + .as-test! |
| 72 | + o 'Should return a resolved future mapped by g if resolved.' do |
| 73 | + for-all(AnyF, AnyF, AnyF).satisfy (a, b, c) -> |
| 74 | + Future.of(a).fold((-> b), (-> c)).is-equal Future.of(c) |
| 75 | + .as-test! |
| 76 | + |
| 77 | + o 'Should swap the disjunction values.' do |
| 78 | + for-all(AnyF).satisfy (a) -> |
| 79 | + Future.of(a).swap!.is-equal (rejected a) and \ |
| 80 | + (rejected a).swap!.is-equal Future.of(a) |
| 81 | + .as-test! |
| 82 | + |
| 83 | + spec 'bimap(f, g)' (o) -> |
| 84 | + o 'For rejected futures should return a new rejected future mapped by f' do |
| 85 | + for-all(AnyF, AnyF, AnyF).satisfy (a, b, c) -> |
| 86 | + (rejected a).bimap(k b; k c).is-equal (rejected b) |
| 87 | + .as-test! |
| 88 | + o 'For rights should return a new right mapped by f' do |
| 89 | + for-all(AnyF, AnyF, AnyF).satisfy (a, b, c) -> |
| 90 | + Future.of(a).bimap(k b; k c).is-equal Future.of(c) |
| 91 | + .as-test! |
| 92 | + |
| 93 | + spec 'rejected-map(f)' (o) -> |
| 94 | + o 'For rejected futures should return a new rejected future mapped by f' do |
| 95 | + for-all(AnyF, AnyF).satisfy (a, b) -> |
| 96 | + (rejected a).rejected-map(k b).is-equal (rejected b) |
| 97 | + .as-test! |
| 98 | + o 'For resolved futures should return itself' do |
| 99 | + for-all(AnyF, AnyF).satisfy (a, b) -> |
| 100 | + Future.of(a).rejected-map(k b).is-equal Future.of(a) |
| 101 | + .as-test! |
| 102 | + |
| 103 | + |
0 commit comments