From 1c932051677e4dba2ef5268f26ae0e00712f0387 Mon Sep 17 00:00:00 2001 From: "Quildreen \"Sorella\" Motta" Date: Wed, 25 Dec 2013 12:44:16 -0200 Subject: [PATCH] Adds specific tests. --- package.json | 4 +- test/specs/either.ls | 172 +++++++++++++++++++++++++++++++++++++++++++ test/specs/index.ls | 1 + 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 test/specs/either.ls diff --git a/package.json b/package.json index 38424fc..c10fc22 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,9 @@ "bugs": { "url": "https://github.com/folktale/data.either/issues" }, - "dependencies": {}, + "dependencies": { + "claire": "~0.4.1" + }, "devDependencies": { "browserify": "git://github.com/robotlolita/node-browserify", "groc": "git://github.com/robotlolita/groc#typography", diff --git a/test/specs/either.ls b/test/specs/either.ls new file mode 100644 index 0000000..e679814 --- /dev/null +++ b/test/specs/either.ls @@ -0,0 +1,172 @@ +# # Specification for Either + +/** ^ + * Copyright (c) 2013 Quildreen Motta + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +spec = (require 'hifive')! +Either = require '../../src/' +{for-all, data: {Any:BigAny, Int}, sized} = require 'claire' +{ok, throws} = require 'assert' + +{Left, Right} = Either + +Any = sized (-> 10), BigAny +k = (a, b) --> a + + +module.exports = spec 'Either' (o, spec) -> + + spec 'Constructors' (o) -> + o 'Left' do + for-all(Any).satisfy (a) -> + Left(a).is-left and not Left(a).is-right + .as-test! + o 'Right' do + for-all(Any).satisfy (a) -> + Right(a).is-right and not Right(a).is-left + .as-test! + o 'from-nullable' do + for-all(Any).satisfy (a) -> + | a? => Either.from-nullable(a).is-right + | _ => Either.from-nullable(a).is-left + .classify (a) -> + | a? => 'Not null' + | _ => 'Null' + .as-test! + + o 'of should always return a Right' do + for-all(Any).satisfy (a) -> + Either.of(a).is-equal Right(a) + .as-test! + + o 'ap(b) should keep b for Lefts' do + for-all(Int).satisfy (a) -> + Left((a) -> a + 1).ap(Right(a)).is-equal Right(a) + .as-test! + + o 'map(f) should keep Lefts unchanged' do + for-all(Any).satisfy (a) -> + Left(a).map((_) -> [a,a]).is-equal Left(a) + .as-test! + + o 'chain(f) should keep Lefts unchanged' do + for-all(Any).satisfy (a) -> + Left(a).chain((_) -> Right(a)).is-equal Left(a) + .as-test! + + spec 'to-string()' (o) -> + o 'Right' do + for-all(Int).satisfy (a) -> + Right(a).to-string! is "Either.Right(#a)" + .as-test! + o 'Left' do + for-all(Int).satisfy (a) -> + Left(a).to-string! is "Either.Left(#a)" + .as-test! + + spec 'is-equal(b)' (o) -> + o 'Rights are always equivalent to Rights, but not Lefts' do + for-all(Any).satisfy (a) -> + Right(a).is-equal(Right(a)) and not Right(a).is-equal(Left(a)) + .as-test! + o 'Rights are never equal Rights with different values' do + for-all(Any, Any).given (!==) .satisfy (a, b) -> + not Right(a).is-equal(Right(b)) + .as-test! + + spec 'get()' (o) -> + o 'For rights should return the value.' do + for-all(Any).satisfy (a) -> + Right(a).get! is a + .as-test! + o 'For lefts should throw a type error' do + for-all(Any).satisfy (a) -> + throws (-> Left(a).get!), TypeError + true + .as-test! + + spec 'get-or-else(a)' (o) -> + o 'For rights should return the value.' do + for-all(Any, Any).satisfy (a, b) -> + Right(a).get-or-else(b) is a + .as-test! + o 'For lefts should return the alternative.' do + for-all(Any, Any).satisfy (a, b) -> + Left(a).get-or-else(b) is b + .as-test! + + spec 'or-else(f)' (o) -> + o 'For rights should return itself' do + for-all(Any, Any).satisfy (a, b) -> + Right(a).or-else((-> Left(b))).is-equal Right(a) + .as-test! + o 'For lefts should return the Either f returns' do + for-all(Any, Any).given (!==) .satisfy (a, b) -> + Left(a).or-else((-> Right(b))).is-equal Right(b) + .as-test! + + o 'merge() should return any value' do + for-all(Any).satisfy (a) -> + Right(a).merge! is Left(a).merge! + .as-test! + + spec 'fold(f, g)' (o) -> + o 'For lefts, should call f' do + for-all(Any, Any, Any).given (!==) .satisfy (a, b, c) -> + Left(a).fold(k Right(b); k Left(c)).is-equal Right(b) + .as-test! + o 'For rights, should call g' do + for-all(Any, Any, Any).given (!==) .satisfy (a, b, c) -> + Right(a).fold(k Right(b); k Left(c)).is-equal Left(c) + .as-test! + + o 'swap()' do + for-all(Any).satisfy (a) -> + Right(a).swap!is-equal(Left(a)) and Left(a).swap!is-equal(Right(a)) + .as-test! + + spec 'bimap(f, g)' (o) -> + o 'For lefts should return a new left mapped by f' do + for-all(Any, Any, Any) + .given ((a, b, c) -> a !== b and b !== c) + .satisfy (a, b, c) -> + Left(a).bimap(k b; k c).is-equal Left(b) + .as-test! + o 'For rights should return a new right mapped by f' do + for-all(Any, Any, Any) + .given ((a, b, c) -> a !== c and b !== c) + .satisfy (a, b, c) -> + Right(a).bimap(k b; k c).is-equal Right(c) + .as-test! + + spec 'left-map(f)' (o) -> + o 'For lefts should return a new left mapped by f' do + for-all(Any, Any).given (!==) .satisfy (a, b) -> + Left(a).left-map(k b).is-equal Left(b) + .as-test! + o 'For rights should return itself' do + for-all(Any, Any).given (!==) .satisfy (a, b) -> + Right(a).left-map(k b).is-equal Right(a) + .as-test! + + diff --git a/test/specs/index.ls b/test/specs/index.ls index 10a2fd5..c456015 100644 --- a/test/specs/index.ls +++ b/test/specs/index.ls @@ -27,4 +27,5 @@ module.exports = [ # The specification objects go here # See: http://hifivejs.github.io/hifive/getting-started.html require './monad-laws' + require './either' ]