-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.spec.es6.js
69 lines (67 loc) · 1.71 KB
/
misc.spec.es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import chai from 'chai';
import {match, _, otherwise, id, CASE} from '../lib/match-js';
chai.expect();
const expect = chai.expect;
describe('Miscellaneous useages', function () {
it('execute statements should be allowed', () => {
const m = match(10)(
[1, 'ONE'],
[10, ()=>{
return 'TEN';
}, null]
)
expect(m).to.be.equal('TEN');
});
it('statements value could be undefined', () => {
const m = match('undefined')(
[1, 'ONE'],
['undefined', undefined],
[2, 'TWO']
)
expect(m).to.be.equal(undefined)
});
it('pass additional parameters to matched patial function', () => {
const m = match(1)(
[1, (x,y,z)=>{
return 'Match '+x+' '+y+' '+z;
}, 2,3]
)
expect(m).to.be.equal('Match 1 2 3');
});
it('default pattern (_) that matches everything', () => {
const i = 3;
const m = match(i)(
[1, 'ONE'],
[2, 'TWO'],
[_, ()=>'_ matches '+i]
)
expect(m).to.be.equal('_ matches 3');
});
it('default pattern (otherwise) that matches everything', () => {
const i = 3;
const m = match(i)(
[1, 'ONE'],
[2, 'TWO'],
[otherwise, ()=>'otherwise matches '+i+'']
)
expect(m).to.be.equal('otherwise matches 3');
});
it('id functions returns the same input value', () => {
const x = 10;
const m = match(x)(
[1, 'ONE'],
[2, 'TWO'],
[10, id]
)
expect(m).to.be.equal(x);
});
it('CASE statements test', function() {
const i = 10;
const m = match(i)(
CASE(1, (x,y,z)=>{ return 'Match '+x+' '+y+' '+z; }, 2,3),
CASE(2, 'TWO'),
CASE(_, ()=>'_ matches '+i)
);
expect(m).to.be.equal('_ matches 10');
});
});