Skip to content

Commit 92194bb

Browse files
author
Cache Hamm
committed
Add test for undefined fact values with equals operator
1 parent e6560f7 commit 92194bb

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Diff for: src/fact.js

-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ class Fact {
2626
}
2727

2828
if (!this.id) throw new Error('factId required')
29-
// if (typeof this.value === 'undefined' && typeof this.calculationMethod === 'undefined') {
30-
// throw new Error('facts must have a value or method')
31-
// }
3229

3330
this.priority = parseInt(options.priority || 1, 10)
3431
this.options = Object.assign({}, defaultOptions, options)

Diff for: test/condition.test.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ describe('Condition', () => {
6666
let condition
6767
let almanac
6868
function setup (options, factValue) {
69-
if (typeof factValue === 'undefined') factValue = 1
7069
const properties = Object.assign({}, conditionBase, options)
7170
condition = new Condition(properties)
7271
const fact = new Fact(conditionBase.fact, factValue)
7372
almanac = new Almanac(new Map([[fact.id, fact]]))
7473
}
7574

7675
context('validations', () => {
77-
beforeEach(() => setup())
76+
beforeEach(() => setup({}, 1))
7877
it('throws when missing an almanac', () => {
7978
return expect(condition.evaluate(undefined, operators)).to.be.rejectedWith('almanac required')
8079
})
@@ -94,6 +93,18 @@ describe('Condition', () => {
9493
expect((await condition.evaluate(almanac, operators, 5)).result).to.equal(false)
9594
})
9695

96+
it('evaluates "equal" to check for undefined', async () => {
97+
condition = new Condition({ fact: 'age', operator: 'equal', value: undefined })
98+
let fact = new Fact('age', undefined)
99+
almanac = new Almanac(new Map([[fact.id, fact]]))
100+
101+
expect((await condition.evaluate(almanac, operators)).result).to.equal(true)
102+
103+
fact = new Fact('age', 1)
104+
almanac = new Almanac(new Map([[fact.id, fact]]))
105+
expect((await condition.evaluate(almanac, operators)).result).to.equal(false)
106+
})
107+
97108
it('evaluates "notEqual"', async () => {
98109
setup({ operator: 'notEqual' }, 50)
99110
expect((await condition.evaluate(almanac, operators)).result).to.equal(false)

Diff for: test/engine.test.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import sinon from 'sinon'
44
import engineFactory, { Fact, Rule, Operator } from '../src/index'
5+
import defaultOperators from '../src/engine-default-operators'
56

67
describe('Engine', () => {
8+
const operatorCount = defaultOperators.length
9+
710
let engine
811
let sandbox
912
before(() => {
@@ -31,7 +34,7 @@ describe('Engine', () => {
3134
it('initializes with the default state', () => {
3235
expect(engine.status).to.equal('READY')
3336
expect(engine.rules.length).to.equal(0)
34-
expect(engine.operators.size).to.equal(10)
37+
expect(engine.operators.size).to.equal(operatorCount)
3538
})
3639

3740
it('can be initialized with rules', () => {
@@ -119,37 +122,37 @@ describe('Engine', () => {
119122

120123
describe('addOperator()', () => {
121124
it('adds the operator', () => {
122-
expect(engine.operators.size).to.equal(10)
125+
expect(engine.operators.size).to.equal(operatorCount)
123126
engine.addOperator('startsWithLetter', (factValue, jsonValue) => {
124127
return factValue[0] === jsonValue
125128
})
126-
expect(engine.operators.size).to.equal(11)
129+
expect(engine.operators.size).to.equal(operatorCount + 1)
127130
expect(engine.operators.get('startsWithLetter')).to.exist()
128131
expect(engine.operators.get('startsWithLetter')).to.be.an.instanceof(Operator)
129132
})
130133

131134
it('accepts an operator instance', () => {
132-
expect(engine.operators.size).to.equal(10)
135+
expect(engine.operators.size).to.equal(operatorCount)
133136
const op = new Operator('my-operator', _ => true)
134137
engine.addOperator(op)
135-
expect(engine.operators.size).to.equal(11)
138+
expect(engine.operators.size).to.equal(operatorCount + 1)
136139
expect(engine.operators.get('my-operator')).to.equal(op)
137140
})
138141
})
139142

140143
describe('removeOperator()', () => {
141144
it('removes the operator', () => {
142-
expect(engine.operators.size).to.equal(10)
145+
expect(engine.operators.size).to.equal(operatorCount)
143146
engine.addOperator('startsWithLetter', (factValue, jsonValue) => {
144147
return factValue[0] === jsonValue
145148
})
146-
expect(engine.operators.size).to.equal(11)
149+
expect(engine.operators.size).to.equal(operatorCount + 1)
147150
engine.removeOperator('startsWithLetter')
148-
expect(engine.operators.size).to.equal(10)
151+
expect(engine.operators.size).to.equal(operatorCount)
149152
})
150153

151154
it('can only remove added operators', () => {
152-
expect(engine.operators.size).to.equal(10)
155+
expect(engine.operators.size).to.equal(operatorCount)
153156
const isRemoved = engine.removeOperator('nonExisting')
154157
expect(isRemoved).to.equal(false)
155158
})

0 commit comments

Comments
 (0)