-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimization: merge identical premises
- Loading branch information
1 parent
06111ee
commit e9fe9e4
Showing
3 changed files
with
188 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
const Rools = require('../src'); | ||
require('./setup'); | ||
|
||
describe('Rules.register() / optimization of premises', () => { | ||
it('should not merge premises if not identical', () => { | ||
const rule1 = { | ||
name: 'rule1', | ||
when: facts => facts.user.name === 'frank', | ||
then: () => {}, | ||
}; | ||
const rule2 = { | ||
name: 'rule2', | ||
when: facts => facts.user.name === 'michael', | ||
then: () => {}, | ||
}; | ||
const rools = new Rools(); | ||
rools.register(rule1, rule2); | ||
expect(rools.premises.length).to.be.equal(2); | ||
}); | ||
|
||
it('should merge premises if identical / reference / arrow function', () => { | ||
const isFrank = facts => facts.user.name === 'frank'; | ||
const rule1 = { | ||
name: 'rule1', | ||
when: isFrank, | ||
then: () => {}, | ||
}; | ||
const rule2 = { | ||
name: 'rule2', | ||
when: isFrank, | ||
then: () => {}, | ||
}; | ||
const rools = new Rools(); | ||
rools.register(rule1, rule2); | ||
expect(rools.premises.length).to.be.equal(1); | ||
}); | ||
|
||
it('should merge premises if identical / reference / classic function', () => { | ||
function isFrank(facts) { | ||
return facts.user.name === 'frank'; | ||
} | ||
const rule1 = { | ||
name: 'rule1', | ||
when: isFrank, | ||
then: () => {}, | ||
}; | ||
const rule2 = { | ||
name: 'rule2', | ||
when: isFrank, | ||
then: () => {}, | ||
}; | ||
const rools = new Rools(); | ||
rools.register(rule1, rule2); | ||
expect(rools.premises.length).to.be.equal(1); | ||
}); | ||
|
||
it('should merge premises if identical / hash / arrow function', () => { | ||
const rule1 = { | ||
name: 'rule1', | ||
when: facts => facts.user.name === 'frank', | ||
then: () => {}, | ||
}; | ||
const rule2 = { | ||
name: 'rule2', | ||
when: facts => facts.user.name === 'frank', | ||
then: () => {}, | ||
}; | ||
const rools = new Rools(); | ||
rools.register(rule1, rule2); | ||
expect(rools.premises.length).to.be.equal(1); | ||
}); | ||
|
||
it('should merge premises if identical / hash / classic function()', () => { | ||
const rule1 = { | ||
name: 'rule1', | ||
when: function p(facts) { | ||
return facts.user.name === 'frank'; | ||
}, | ||
then: () => {}, | ||
}; | ||
const rule2 = { | ||
name: 'rule2', | ||
when: function p(facts) { | ||
return facts.user.name === 'frank'; | ||
}, | ||
then: () => {}, | ||
}; | ||
const rools = new Rools(); | ||
rools.register(rule1, rule2); | ||
expect(rools.premises.length).to.be.equal(1); | ||
}); | ||
|
||
it('should merge premises if identical / hash / slightly different', () => { | ||
const rule1 = { | ||
name: 'rule1', | ||
when: facts => facts.user.name === 'frank', | ||
then: () => {}, | ||
}; | ||
const rule2 = { | ||
name: 'rule2', | ||
when: facts => facts.user.name === "frank", // eslint-disable-line quotes | ||
then: () => {}, | ||
}; | ||
const rools = new Rools(); | ||
rools.register(rule1, rule2); | ||
expect(rools.premises.length).to.be.equal(1); | ||
}); | ||
|
||
it('should merge premises if identical / with Date object', () => { | ||
const date = new Date('2000-01-01'); | ||
const rule1 = { | ||
name: 'rule1', | ||
when: facts => facts.user.birthdate > date, | ||
then: () => {}, | ||
}; | ||
const rule2 = { | ||
name: 'rule2', | ||
when: facts => facts.user.birthdate > date, | ||
then: () => {}, | ||
}; | ||
const rools = new Rools(); | ||
rools.register(rule1, rule2); | ||
expect(rools.premises.length).to.be.equal(1); | ||
}); | ||
}); |