-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.spec.js
131 lines (127 loc) · 4.33 KB
/
examples.spec.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Binliner from "../dist";
import equal from 'fast-deep-equal';
describe("Binliner", () => {
it('handles verbose control flows transparently', () => {
const messages = {
'verbose': [],
'binliner': [],
}
const doThing = (ns, thing) => messages[ns].push(String(thing));
const verboseFlow = (first, second, third, fourth) => {
let valid;
let ns = 'verbose';
if (!first) {
valid = false; // first is required
return valid;
}
if (!second && third) {
valid = false; // third depends on second
return valid;
}
if (second && !third) {
valid = false; // third depends on second
return valid;
}
if (second && third) {
valid = true;
doThing(ns, 'second and third are both true, continue');
}
if (!second && !third) {
valid = true;
doThing(ns, 'second and third are both false, continue');
}
if (fourth) {
doThing(ns, 'fourth is true, return foo = bar');
return {
valid,
foo: 'bar'
};
} else {
doThing(ns, 'fourth is false, return foo = baz');
return {
valid,
foo: 'baz'
};
}
};
const binLinerFlow = (first, second, third, fourth) => {
let valid;
let ns = 'binliner';
const bin = new Binliner({ // Represent conditions as binary stream: 1000, 1001, etc.
validation: [8, 9, 14, 15] // Arbitrary validation rules
}, first, second, third, fourth);
valid = bin.isValid();
if (!valid) { // capture all invalid cases
return valid;
}
if (Number(bin) > 10) { // 1110: 14, 1111: 15
doThing(ns, 'second and third are both true, continue');
} else { // 1000: 8, 1001: 9
doThing(ns, 'second and third are both false, continue');
}
if (bin.get(3) === 0) { // 1000: 8, 1110: 14
doThing(ns, 'fourth is false, return foo = baz');
return {
valid,
foo: 'baz'
};
} else { // 1001: 9, 1111: 15
doThing(ns, 'fourth is true, return foo = bar');
return {
valid,
foo: 'bar'
};
}
}
/**
* Truth table:
* first | second | third | fourth | valid | foo
* 1 | 0 | 0 | 0 | true | baz
* 1 | 0 | 0 | 1 | true | bar
* 1 | 1 | 1 | 0 | true | baz
* 1 | 1 | 1 | 1 | true | bar
*
* Any other conditions are invalid.
*/
let verbose = verboseFlow(false, true, true, true);
let binliner = binLinerFlow(false, true, true, true);
expect(verbose).toBeFalsy();
expect(binliner).toBeFalsy();
expect(equal(verbose, binliner)).toBeTruthy();
expect(equal(messages['verbose'], messages['binliner'])).toBeTruthy();
messages['verbose'] = [];
messages['binliner'] = [];
verbose = verboseFlow(true, true, false, true);
binliner = binLinerFlow(true, true, false, true);
expect(verbose).toBeFalsy();
expect(binliner).toBeFalsy();
expect(equal(verbose, binliner)).toBeTruthy();
expect(equal(messages['verbose'], messages['binliner'])).toBeTruthy();
messages['verbose'] = [];
messages['binliner'] = [];
verbose = verboseFlow(true, false, true, true);
binliner = binLinerFlow(true, false, true, true);
expect(verbose).toBeFalsy();
expect(binliner).toBeFalsy();
expect(equal(verbose, binliner)).toBeTruthy();
expect(equal(messages['verbose'], messages['binliner'])).toBeTruthy();
messages['verbose'] = [];
messages['binliner'] = [];
verbose = verboseFlow(true, true, true, true);
binliner = binLinerFlow(true, true, true, true);
expect(verbose.valid).toBeTruthy();
expect(binliner.valid).toBeTruthy();
expect(equal(verbose, binliner)).toBeTruthy();
expect(equal(messages['verbose'], messages['binliner'])).toBeTruthy();
messages['verbose'] = [];
messages['binliner'] = [];
verbose = verboseFlow(true, false, false, false);
binliner = binLinerFlow(true, false, false, false);
expect(verbose.valid).toBeTruthy();
expect(binliner.valid).toBeTruthy();
expect(equal(verbose, binliner)).toBeTruthy();
expect(equal(messages['verbose'], messages['binliner'])).toBeTruthy();
messages['verbose'] = [];
messages['binliner'] = [];
});
});