1 | const assert = require('assert');
|
2 | const { setPrecision, parseOr, jsonOr, intOr, floatOr } = require('../conversions');
|
3 | const { identity } = require('../logic');
|
4 |
|
5 | describe('Conversions', function() {
|
6 | describe('#jsonOr()', function() {
|
7 | it('should return default value if empty', function() {
|
8 | assert.equal(jsonOr(0)(null), 0);
|
9 | assert.equal(jsonOr(0)(''), 0);
|
10 | assert.equal(jsonOr(0)(), 0);
|
11 | assert.equal(jsonOr(1)(0), 0);
|
12 | assert.equal(jsonOr(0)('asdffsda'), 0);
|
13 | assert.deepEqual(jsonOr(0)('{a:b}'), 0);
|
14 | });
|
15 | it('should return empty object if empty object passed', function() {
|
16 | assert.deepEqual(jsonOr(0)('{}'), {});
|
17 | assert.deepEqual(jsonOr(0)('{"a":{"b":1}}'), { a: { b: 1 } });
|
18 | });
|
19 | it('should return function returned value if passed a function', function() {
|
20 | assert.equal(jsonOr(x => 3)(null), 3);
|
21 | });
|
22 | it('should return same value if identity is passed as default value', function() {
|
23 | assert.equal(jsonOr(identity)(null), null);
|
24 | });
|
25 | });
|
26 | describe('#floatOr()', function() {
|
27 | it('should return default value if empty', function() {
|
28 | assert.equal(floatOr(0)(null), 0);
|
29 | assert.equal(floatOr(0)(), 0);
|
30 | assert.equal(floatOr(0)('xxxx'), 0);
|
31 | assert.equal(floatOr(0)('x3x'), 0);
|
32 | assert.equal(floatOr(1)(0), 0);
|
33 | });
|
34 | it('should return value if is a float', function() {
|
35 | assert.equal(floatOr(0)(1.1), 1.1);
|
36 | assert.equal(floatOr(0)('1.1'), 1.1);
|
37 | assert.equal(floatOr(0)('1.123456789'), 1.123456789);
|
38 | });
|
39 | it('should return function returned value if passed a function', function() {
|
40 | assert.equal(floatOr(x => 3)(null), 3);
|
41 | });
|
42 | it('should return same value if identity is passed as default value', function() {
|
43 | assert.equal(floatOr(identity)(null), null);
|
44 | });
|
45 | });
|
46 | describe('#intOr()', function() {
|
47 | it('should return default value if empty', function() {
|
48 | assert.equal(intOr(0)(null), 0);
|
49 | assert.equal(intOr(0)(), 0);
|
50 | assert.equal(intOr(0)('xxxx'), 0);
|
51 | assert.equal(intOr(0)('x3x'), 0);
|
52 | assert.equal(intOr(1)(0), 0);
|
53 | });
|
54 | it('should return value if is a int', function() {
|
55 | assert.equal(intOr(0)(1), 1);
|
56 | assert.equal(intOr(0)('1'), 1);
|
57 | });
|
58 | it('should return an int if passed a float', function() {
|
59 | assert.equal(intOr(0)(1.1), 1);
|
60 | assert.equal(intOr(0)('1.9'), 1);
|
61 | });
|
62 | it('should return function returned value if passed a function', function() {
|
63 | assert.equal(intOr(x => 3)(null), 3);
|
64 | });
|
65 | it('should return same value if identity is passed as default value', function() {
|
66 | assert.equal(intOr(identity)(null), null);
|
67 | });
|
68 | });
|
69 | describe('#setPrecision()', function() {
|
70 | it('should return with no decimals if 0 passed', function() {
|
71 | assert.equal(setPrecision(0, 1.3), 1);
|
72 | });
|
73 | it('should return with no decimals if wrong precision passed', function() {
|
74 | assert.equal(setPrecision(null, 1.3), 1);
|
75 | assert.equal(setPrecision(undefined, 1.3), 1);
|
76 | });
|
77 | it('should return the exact decimals passed', function() {
|
78 | assert.equal(setPrecision(2, 1.12345), 1.12);
|
79 | });
|
80 | it('should return decimals rounded', function() {
|
81 | assert.equal(setPrecision(3, 1.12345), 1.123);
|
82 | assert.equal(setPrecision(4, 1.12345), 1.1235);
|
83 | });
|
84 | });
|
85 | describe('#parseOr()', function() {
|
86 | it('should return default value if parser throws an exception', function() {
|
87 | assert.equal(
|
88 | parseOr(x => {
|
89 | throw Error('whatever');
|
90 | })(33)(1),
|
91 | 33
|
92 | );
|
93 | });
|
94 | it('should return value if identity passes as a parser', function() {
|
95 | assert.equal(parseOr(identity)(33)(1), 1);
|
96 | assert.equal(parseOr(identity)(33)(0), 0);
|
97 | });
|
98 | });
|
99 | });
|