Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit b97ce4d

Browse files
committed
Provides tests for futures.
1 parent c7e6cbe commit b97ce4d

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
"bugs": {
2626
"url": "https://github.com/folktale/data.future/issues"
2727
},
28-
"dependencies": {},
28+
"dependencies": {
29+
"claire": "~0.4.1"
30+
},
2931
"devDependencies": {
3032
"browserify": "git://github.com/robotlolita/node-browserify",
3133
"groc": "git://github.com/robotlolita/groc#patch-livescript",

test/specs/future.ls

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+

test/specs/index.ls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ module.exports = [
2727
# The specification objects go here
2828
# See: http://hifivejs.github.io/hifive/getting-started.html
2929
require './monad-laws'
30+
require './future'
3031
]

0 commit comments

Comments
 (0)