-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpremises.spec.js
173 lines (163 loc) · 5.13 KB
/
premises.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const md5 = require('md5');
const { Rools, Rule } = require('..');
require('./setup');
describe('Rools.register() / optimization of premises', () => {
it('should not merge premises if not identical', async () => {
const rule1 = new Rule({
name: 'rule1',
when: (facts) => facts.user.name === 'frank',
then: () => {},
});
const rule2 = new Rule({
name: 'rule2',
when: (facts) => facts.user.name === 'michael',
then: () => {},
});
const rools = new Rools();
await rools.register([rule1, rule2]);
expect(rools.rules.premises.length).to.be.equal(2);
});
it('should merge premises if identical / reference / arrow function', async () => {
const isFrank = (facts) => facts.user.name === 'frank';
const rule1 = new Rule({
name: 'rule1',
when: isFrank,
then: () => {},
});
const rule2 = new Rule({
name: 'rule2',
when: isFrank,
then: () => {},
});
const rools = new Rools();
await rools.register([rule1, rule2]);
expect(rools.rules.premises.length).to.be.equal(1);
});
it('should merge premises if identical / reference / classic function', async () => {
function isFrank(facts) {
return facts.user.name === 'frank';
}
const rule1 = new Rule({
name: 'rule1',
when: isFrank,
then: () => {},
});
const rule2 = new Rule({
name: 'rule2',
when: isFrank,
then: () => {},
});
const rools = new Rools();
await rools.register([rule1, rule2]);
expect(rools.rules.premises.length).to.be.equal(1);
});
it('should merge premises if identical / hash / arrow function', async () => {
const rule1 = new Rule({
name: 'rule1',
when: (facts) => facts.user.name === 'frank',
then: () => {},
});
const rule2 = new Rule({
name: 'rule2',
when: (facts) => facts.user.name === 'frank',
then: () => {},
});
const rools = new Rools();
await rools.register([rule1, rule2]);
expect(rools.rules.premises.length).to.be.equal(1);
});
it('should merge premises if identical / hash / classic function()', async () => {
const rule1 = new Rule({
name: 'rule1',
when: function p(facts) {
return facts.user.name === 'frank';
},
then: () => {},
});
const rule2 = new Rule({
name: 'rule2',
when: function p(facts) {
return facts.user.name === 'frank';
},
then: () => {},
});
const rools = new Rools();
await rools.register([rule1, rule2]);
expect(rools.rules.premises.length).to.be.equal(1);
});
it('should not merge premises if identical / hash / slightly different (unfortunately)', async () => {
const rule1 = new Rule({
name: 'rule1',
when: (facts) => facts.user.name === 'frank',
then: () => {},
});
const rule2 = new Rule({
name: 'rule2',
when: (facts) => facts.user.name === "frank", // eslint-disable-line quotes
then: () => {},
});
const rools = new Rools();
await rools.register([rule1, rule2]);
expect(rools.rules.premises.length).to.be.equal(2);
});
it('should not merge premises if not identical / with Date object', async () => {
const date1 = new Date('2000-01-01');
const date2 = new Date('1990-01-01');
const rule1 = new Rule({
name: 'rule1',
when: (facts) => facts.user.birthdate > date1,
then: () => {},
});
const rule2 = new Rule({
name: 'rule2',
when: (facts) => facts.user.birthdate > date2,
then: () => {},
});
const rools = new Rools();
await rools.register([rule1, rule2]);
expect(rools.rules.premises.length).to.be.equal(2);
});
it('should merge premises if identical / with Date object', async () => {
const date = new Date('2000-01-01');
const rule1 = new Rule({
name: 'rule1',
when: (facts) => facts.user.birthdate > date,
then: () => {},
});
const rule2 = new Rule({
name: 'rule2',
when: (facts) => facts.user.birthdate > date,
then: () => {},
});
const rools = new Rools();
await rools.register([rule1, rule2]);
expect(rools.rules.premises.length).to.be.equal(1);
});
it('should merge premises if identical / fn.toString() / 1', async () => {
const foo = ({ context }) => context.bar === 'buz0';
const fns = [
({ context }) => context.bar === 'buz0',
({ context }) => context.bar === 'buz 0',
({ context }) => context.bar === 'buz 0',
({ context }) => context.bar === 'buz10',
({ context }) => context.bar === 'buz00',
foo,
foo,
({ context }) => context.bar === 'buz0',
];
const set = new Set();
fns.forEach((f) => set.add(md5(f.toString())));
expect(set.size).to.be.equal(4);
});
it('should merge premises if identical / fn.toString() / 2', async () => {
const foo = ({ context }) => context.bar === Math.abs(3);
const fns = [
({ context }) => context.bar === Math.abs(0),
({ context }) => context.bar === Math.abs(1),
foo,
];
const set = new Set();
fns.forEach((f) => set.add(md5(f.toString())));
expect(set.size).to.be.equal(3);
});
});