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