This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmatch.js
62 lines (55 loc) · 2.06 KB
/
match.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
/* globals describe, it */
var assert = require('assert');
var t = require('../index');
var throwsWithMessage = require('./util').throwsWithMessage;
describe('match', function () {
it('should match on type constructors', function () {
assert.deepEqual(t.match(1,
t.String, function () { return 'a string'; },
t.Number, function (n) { return 2 * n; }
), 2);
});
it('should handle an optional guard', function () {
assert.deepEqual(t.match(1,
t.String, function () { return 'a string'; },
t.Number, function () { return false; }, function (n) { return 2 * n; },
t.Number, function (n) { return 3 * n; }
), 3);
});
it('should throw if no match is found', function () {
throwsWithMessage(function () {
t.match(true,
t.String, function () { return 'a string'; },
t.Number, function (n) { return 2 * n; }
);
}, '[tcomb] Match error');
});
it('should throw if cases are misplaced', function () {
throwsWithMessage(function () {
t.match(true,
t.String, function () { return 'a string'; },
t.Number
);
}, '[tcomb] Invalid block in clause #2');
});
it('should handle unions of unions', function () {
var A = t.subtype(t.String, function (s) { return s === 'A'; });
var B = t.subtype(t.String, function (s) { return s === 'B'; });
var C = t.subtype(t.String, function (s) { return s === 'C'; });
var D = t.subtype(t.String, function (s) { return s === 'D'; });
var E = t.subtype(t.String, function (s) { return s === 'E'; });
var F = t.subtype(t.String, function (s) { return s === 'F'; });
var G = t.subtype(t.String, function (s) { return s === 'G'; });
var H = t.subtype(t.String, function (s) { return s === 'H'; });
var U1 = t.union([A, B]);
var U2 = t.union([C, D]);
var U3 = t.union([E, F]);
var U4 = t.union([G, H]);
var UU1 = t.union([U1, U2]);
var UU2 = t.union([U3, U4]);
assert.deepEqual(t.match('F',
UU1, function () { return '1'; },
UU2, function () { return '2'; }
), '2');
});
});