1 |
|
2 | const assert = require('assert');
|
3 | const { either, not, identity } = require('../logic');
|
4 |
|
5 | describe('Logic', function() {
|
6 | describe('#either()', function() {
|
7 | it('should return the result of function if there is no exception', function() {
|
8 | let fn = either(JSON.parse, 0);
|
9 | assert.deepStrictEqual(fn('{"a":3}'), { a: 3 });
|
10 | });
|
11 | it('should return the default value if there is exception', function() {
|
12 | let fn = either(JSON.parse, 0);
|
13 | assert.deepStrictEqual(fn({}), 0);
|
14 | });
|
15 | it('should return the default value from a function if there is exception', function() {
|
16 | let fn = either(JSON.parse, x => 0);
|
17 | assert.deepStrictEqual(fn({}), 0);
|
18 | });
|
19 | });
|
20 | describe('not()', function() {
|
21 | it('should negate the value passed if identity provided', function() {
|
22 | assert.strictEqual(not(identity)(true), false);
|
23 | });
|
24 | it('should return the opposite value given by the function', function() {
|
25 | assert.strictEqual(not(x => x.length > 2)(''), true);
|
26 | assert.strictEqual(not(x => x.length > 2)('xxx'), false);
|
27 | });
|
28 | });
|
29 | describe('identity()', function() {
|
30 | it('should return the exact input passed', function() {
|
31 | assert.strictEqual(identity(3), 3);
|
32 | assert.strictEqual(identity(''), '');
|
33 | assert.strictEqual(identity(null), null);
|
34 | assert.strictEqual(identity(), undefined);
|
35 | });
|
36 | });
|
37 | });
|