monads-co
Version:
An implementation of Haskell's type classes in TS
199 lines • 8.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mocha_1 = require("mocha");
const Result_1 = require("./Result");
const chai_1 = require("chai");
(0, mocha_1.describe)('Result', () => {
(0, mocha_1.describe)('#map', () => {
it('Should replace transform the result\'s value if it exists', () => {
const a = (0, Result_1.Ok)(4);
const b = a.map((value) => value + 1);
(0, chai_1.expect)(b._value).to.eq(5);
});
it('Should propagate the error otherwise', () => {
const a = (0, Result_1.Err)('failure occurred');
const b = a.map((value) => value + 1);
(0, chai_1.expect)(b._value).to.eq('failure occurred');
});
});
(0, mocha_1.describe)('#amap', () => {
it('Should perform mapping if all Ok', () => {
const a = (0, Result_1.Ok)(4);
const fn = (0, Result_1.Ok)((value) => value + 1);
const b = a.amap(fn);
(0, chai_1.expect)(b._value).to.eq(5);
});
it('Should return value error if Err', () => {
const a = (0, Result_1.Err)('a');
const fn = (0, Result_1.Ok)((value) => value + 1);
const b = a.amap(fn);
(0, chai_1.expect)(b._value).to.eq('a');
});
it('Should return fn error if Err', () => {
const a = (0, Result_1.Ok)(4);
const fn = (0, Result_1.Err)('b');
const b = a.amap(fn);
(0, chai_1.expect)(b._value).to.eq('b');
});
it('Should return mapping error if both are Err', () => {
const a = (0, Result_1.Err)('a');
const fn = (0, Result_1.Err)('b');
const b = a.amap(fn);
(0, chai_1.expect)(b._value).to.eq('b');
});
});
(0, mocha_1.describe)('#pure', () => {
it('Should create a new result with new value', () => {
const a = (0, Result_1.Ok)(3);
const b = a.pure(4);
(0, chai_1.expect)(b._value).to.eq(4);
});
it('Should replace an error', () => {
const a = (0, Result_1.Err)('a');
const b = a.pure(4);
(0, chai_1.expect)(b._value).to.eq(4);
});
});
(0, mocha_1.describe)('#then', () => {
it('Should transform the result\'s value if it exists', () => {
const a = (0, Result_1.Ok)(4);
const b = a.then((value) => (0, Result_1.Ok)(value + 1));
(0, chai_1.expect)(b._value).to.eq(5);
});
it('Should propagate error if value Err', () => {
const a = (0, Result_1.Err)('a');
const b = a.then((value) => (0, Result_1.Ok)(value + 1));
(0, chai_1.expect)(b._value).to.eq('a');
});
it('Should return mapping error if its an Err', () => {
const a = (0, Result_1.Ok)(4);
const b = a.then(() => (0, Result_1.Err)('b'));
(0, chai_1.expect)(b._value).to.eq('b');
});
it('Should prefer original Err', () => {
const a = (0, Result_1.Err)('a');
const b = a.then(() => (0, Result_1.Err)('b'));
(0, chai_1.expect)(b._value).to.eq('a');
});
});
(0, mocha_1.describe)('#and', () => {
it('Should form Ok([a, b]) from Ok(a) and Ok(b)', () => {
const a = (0, Result_1.Ok)(2);
const b = (0, Result_1.Ok)(3);
const c = a.and(b);
(0, chai_1.expect)(c._value).to.deep.eq([2, 3]);
});
it('Should Err if Left is Err', () => {
const a = (0, Result_1.Err)('a');
const b = (0, Result_1.Ok)(3);
const c = a.and(b);
(0, chai_1.expect)(c._value).to.eq('a');
});
it('Should Err if Right is Err', () => {
const a = (0, Result_1.Ok)(2);
const b = (0, Result_1.Err)('b');
const c = a.and(b);
(0, chai_1.expect)(c._value).to.eq('b');
});
it('Should Err if Left if both are Err', () => {
const a = (0, Result_1.Err)('a');
const b = (0, Result_1.Err)('b');
const c = a.and(b);
(0, chai_1.expect)(c._value).to.eq('a');
});
});
(0, mocha_1.describe)('#andIgnoreRight', () => {
it('Should form Ok(a) from Ok(a) and Ok(b)', () => {
const a = (0, Result_1.Ok)(2);
const b = (0, Result_1.Ok)(3);
const c = a.andIgnoreRight(b);
(0, chai_1.expect)(c._value).to.eq(2);
});
it('Should Err if Left is Err', () => {
const a = (0, Result_1.Err)('a');
const b = (0, Result_1.Ok)(3);
const c = a.andIgnoreRight(b);
(0, chai_1.expect)(c._value).to.eq('a');
});
it('Should Err if Right is Err', () => {
const a = (0, Result_1.Ok)(2);
const b = (0, Result_1.Err)('b');
const c = a.andIgnoreRight(b);
(0, chai_1.expect)(c._value).to.eq('b');
});
it('Should Err if Left if both are Err', () => {
const a = (0, Result_1.Err)('a');
const b = (0, Result_1.Err)('b');
const c = a.andIgnoreRight(b);
(0, chai_1.expect)(c._value).to.eq('a');
});
});
(0, mocha_1.describe)('#andIgnoreLeft', () => {
it('Should form Ok(b) from Ok(a) and Ok(b)', () => {
const a = (0, Result_1.Ok)(2);
const b = (0, Result_1.Ok)(3);
const c = a.andIgnoreLeft(b);
(0, chai_1.expect)(c._value).to.eq(3);
});
it('Should Err if Left is Err', () => {
const a = (0, Result_1.Err)('a');
const b = (0, Result_1.Ok)(3);
const c = a.andIgnoreLeft(b);
(0, chai_1.expect)(c._value).to.eq('a');
});
it('Should Err if Right is Err', () => {
const a = (0, Result_1.Ok)(2);
const b = (0, Result_1.Err)('b');
const c = a.andIgnoreLeft(b);
(0, chai_1.expect)(c._value).to.eq('b');
});
it('Should Err if Left if both are Err', () => {
const a = (0, Result_1.Err)('a');
const b = (0, Result_1.Err)('b');
const c = a.andIgnoreLeft(b);
(0, chai_1.expect)(c._value).to.eq('a');
});
});
(0, mocha_1.describe)('#catch', () => {
it('Should do nothing if LHS Ok', () => {
const a = (0, Result_1.Ok)(2);
const b = a.catch(() => 3);
(0, chai_1.expect)(b._value).to.eq(2);
});
it('Should map error if LHS Err', () => {
const a = (0, Result_1.Err)('a');
const b = a.catch((error) => error === 'a' ? 1 : 2);
(0, chai_1.expect)(b._value).to.eq(1);
});
});
(0, mocha_1.describe)('#catchThen', () => {
it('Should do nothing if LHS Ok', () => {
const a = (0, Result_1.Ok)(2);
const b = a.catchThen(() => (0, Result_1.Ok)(3));
(0, chai_1.expect)(b._value).to.eq(2);
});
it('Should return Ok if mapping Ok if LHS Err', () => {
const a = (0, Result_1.Err)('a');
const b = a.catchThen((error) => (0, Result_1.Ok)(error === 'a' ? 1 : 2));
(0, chai_1.expect)(b._value).to.eq(1);
});
it('Should return Err if mapping Err if LHS Err', () => {
const a = (0, Result_1.Err)('a');
const b = a.catchThen((error) => (0, Result_1.Err)('b'));
(0, chai_1.expect)(b._value).to.eq('b');
});
});
(0, mocha_1.describe)('#toOptional', () => {
it('Should return Some(T) for Ok(T)', () => {
const a = (0, Result_1.Ok)(2);
const b = a.toOptional();
(0, chai_1.expect)(b._value).to.eq(2);
});
it('Should return None for Err', () => {
const a = (0, Result_1.Err)('b');
const b = a.toOptional();
(0, chai_1.expect)(b._value).to.eq(null);
});
});
});
//# sourceMappingURL=Result.spec.js.map