-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const functionsDir = path.join(__dirname, '/functions'); | ||
const userFunc_1 = require("./functions/userFunc"); | ||
const counterFunc_1 = require("./functions/counterFunc"); | ||
const functionsTable = {}; | ||
exports.functionsTable = functionsTable; | ||
fs.readdirSync(functionsDir).forEach((file) => { | ||
if (file.endsWith('.js')) { | ||
const data = require(path.join(functionsDir, file)).factory; | ||
const description = data(); | ||
if (functionsTable[description.name]) { | ||
throw new Error(`Function with name ${description.name} already exists`); | ||
} | ||
functionsTable[description.name] = description.evaluate; | ||
const _addFunction = (description) => { | ||
if (functionsTable[description.name]) { | ||
throw new Error(`Function with name ${description.name} already exists`); | ||
} | ||
}); | ||
functionsTable[description.name] = description.evaluate; | ||
}; | ||
_addFunction(userFunc_1.factory()); | ||
_addFunction(counterFunc_1.factory()); |
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 |
---|---|---|
@@ -1,161 +1,223 @@ | ||
'use strict'; | ||
|
||
import { expect } from 'chai'; | ||
import { evaluate } from '../dist/index'; | ||
import {expect} from 'chai'; | ||
import {evaluate} from '../dist'; | ||
|
||
describe('evaluator', function() { | ||
interface Context { | ||
userId: string; | ||
timesCounter: number; | ||
} | ||
|
||
describe('evaluator', function () { | ||
|
||
const functionsTable = { | ||
user: (user: string, context: {userId: string}): boolean => { | ||
user: (user: string, context: { userId: string }): boolean => { | ||
return context.userId === user; | ||
} | ||
}; | ||
|
||
it('should evaluate short eq compare op to true', function() { | ||
expect(evaluate({ timesCounter: 5 }, { timesCounter: 5 }, functionsTable)).to.eql(true); | ||
it('should evaluate short eq compare op to true', function () { | ||
expect(evaluate<Context>({timesCounter: 5}, {timesCounter: 5, userId: 'a'}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate short eq compare op to false', function() { | ||
expect(evaluate({ timesCounter: 5 }, { timesCounter: 7 }, functionsTable)).to.eql(false); | ||
it('should evaluate short eq compare op to false', function () { | ||
expect(evaluate<Context>({timesCounter: 5}, {timesCounter: 7, userId: 'a'}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate eq compare op to true', function() { | ||
expect(evaluate({ timesCounter: {eq: 5} }, { timesCounter: 5 }, functionsTable)).to.eql(true); | ||
it('should evaluate eq compare op to true', function () { | ||
expect(evaluate<Context>({timesCounter: {eq: 5}}, {timesCounter: 5, userId: 'a'}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate eq compare op to false', function() { | ||
expect(evaluate({ timesCounter: {eq: 5} }, { timesCounter: 7 }, functionsTable)).to.eql(false); | ||
it('should evaluate eq compare op to false', function () { | ||
expect(evaluate<Context>({timesCounter: {eq: 5}}, { | ||
timesCounter: 7, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate neq compare op to true', function() { | ||
expect(evaluate({ timesCounter: {neq: 5} }, { timesCounter: 8 }, functionsTable)).to.eql(true); | ||
it('should evaluate neq compare op to true', function () { | ||
expect(evaluate<Context>({timesCounter: {neq: 5}}, { | ||
timesCounter: 8, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate eq compare op to false', function() { | ||
expect(evaluate({ timesCounter: {neq: 5} }, { timesCounter: 5 }, functionsTable)).to.eql(false); | ||
it('should evaluate eq compare op to false', function () { | ||
expect(evaluate<Context>({timesCounter: {neq: 5}}, { | ||
timesCounter: 5, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate gt compare op to true', function() { | ||
expect(evaluate({ timesCounter: {gt: 5} }, { timesCounter: 8 }, functionsTable)).to.eql(true); | ||
it('should evaluate gt compare op to true', function () { | ||
expect(evaluate<Context>({timesCounter: {gt: 5}}, {timesCounter: 8, userId: 'a'}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate gt compare op to false', function() { | ||
expect(evaluate({ timesCounter: {gt: 5} }, { timesCounter: 3 }, functionsTable)).to.eql(false); | ||
it('should evaluate gt compare op to false', function () { | ||
expect(evaluate<Context>({timesCounter: {gt: 5}}, { | ||
timesCounter: 3, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate gt compare op to false 2', function() { | ||
expect(evaluate({ timesCounter: {gt: 5} }, { timesCounter: 5 }, functionsTable)).to.eql(false); | ||
it('should evaluate gt compare op to false 2', function () { | ||
expect(evaluate<Context>({timesCounter: {gt: 5}}, { | ||
timesCounter: 5, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate gte compare op to true', function() { | ||
expect(evaluate({ timesCounter: {gte: 5} }, { timesCounter: 8 }, functionsTable)).to.eql(true); | ||
it('should evaluate gte compare op to true', function () { | ||
expect(evaluate<Context>({timesCounter: {gte: 5}}, { | ||
timesCounter: 8, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate gte compare op to true 2', function() { | ||
expect(evaluate({ timesCounter: {gte: 5} }, { timesCounter: 5 }, functionsTable)).to.eql(true); | ||
it('should evaluate gte compare op to true 2', function () { | ||
expect(evaluate<Context>({timesCounter: {gte: 5}}, { | ||
timesCounter: 5, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate gte compare op to false', function() { | ||
expect(evaluate({ timesCounter: {gte: 5} }, { timesCounter: 3 }, functionsTable)).to.eql(false); | ||
it('should evaluate gte compare op to false', function () { | ||
expect(evaluate<Context>({timesCounter: {gte: 5}}, { | ||
timesCounter: 3, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate lt compare op to true', function() { | ||
expect(evaluate({ timesCounter: {lt: 5} }, { timesCounter: 3 }, functionsTable)).to.eql(true); | ||
it('should evaluate lt compare op to true', function () { | ||
expect(evaluate<Context>({timesCounter: {lt: 5}}, {timesCounter: 3, userId: 'a'}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate gt compare lt to false', function() { | ||
expect(evaluate({ timesCounter: {lt: 5} }, { timesCounter: 8 }, functionsTable)).to.eql(false); | ||
it('should evaluate gt compare lt to false', function () { | ||
expect(evaluate<Context>({timesCounter: {lt: 5}}, { | ||
timesCounter: 8, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate lt compare op to false 2', function() { | ||
expect(evaluate({ timesCounter: {lt: 5} }, { timesCounter: 5 }, functionsTable)).to.eql(false); | ||
it('should evaluate lt compare op to false 2', function () { | ||
expect(evaluate<Context>({timesCounter: {lt: 5}}, { | ||
timesCounter: 5, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate lte compare op to true', function() { | ||
expect(evaluate({ timesCounter: {lte: 5} }, { timesCounter: 3 }, functionsTable)).to.eql(true); | ||
it('should evaluate lte compare op to true', function () { | ||
expect(evaluate<Context>({timesCounter: {lte: 5}}, { | ||
timesCounter: 3, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate lte compare op to true 2', function() { | ||
expect(evaluate({ timesCounter: {lte: 5} }, { timesCounter: 5 }, functionsTable)).to.eql(true); | ||
it('should evaluate lte compare op to true 2', function () { | ||
expect(evaluate<Context>({timesCounter: {lte: 5}}, { | ||
timesCounter: 5, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate lte compare op to false', function() { | ||
expect(evaluate({ timesCounter: {lte: 5} }, { timesCounter: 8 }, functionsTable)).to.eql(false); | ||
it('should evaluate lte compare op to false', function () { | ||
expect(evaluate<Context>({timesCounter: {lte: 5}}, { | ||
timesCounter: 8, | ||
userId: 'a' | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate a single function', function() { | ||
expect(evaluate({ user: 'r@a.com' }, { userId: 'r@a.com' }, functionsTable)).to.eql(true); | ||
it('should evaluate a single function', function () { | ||
expect(evaluate<Context>({user: 'r@a.com'}, {userId: 'r@a.com', timesCounter: 8}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate a single not function to false', function() { | ||
expect(evaluate({ not: { user: 'r@a.com' } }, { userId: 'r@a.com' }, functionsTable)).to.eql(false); | ||
it('should evaluate a single not function to false', function () { | ||
expect(evaluate<Context>({not: {user: 'r@a.com'}}, { | ||
userId: 'r@a.com', | ||
timesCounter: 8 | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate a single not function to true', function() { | ||
expect(evaluate({ not: { user: 'a@a.com' } }, { userId: 'r@a.com' }, functionsTable)).to.eql(true); | ||
it('should evaluate a single not function to true', function () { | ||
expect(evaluate<Context>({not: {user: 'a@a.com'}}, { | ||
userId: 'r@a.com', | ||
timesCounter: 8 | ||
}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate a single and function to true', function() { | ||
expect(evaluate({ and: [{ user: 'r@a.com' }] }, { userId: 'r@a.com' }, functionsTable)).to.eql(true); | ||
it('should evaluate a single and function to true', function () { | ||
expect(evaluate<Context>({and: [{user: 'r@a.com'}]}, { | ||
userId: 'r@a.com', | ||
timesCounter: 8 | ||
}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate a single or function to true', function() { | ||
expect(evaluate({ or: [{ user: 'r@a.com' }] }, { userId: 'r@a.com' }, functionsTable)).to.eql(true); | ||
it('should evaluate a single or function to true', function () { | ||
expect(evaluate<Context>({or: [{user: 'r@a.com'}]}, { | ||
userId: 'r@a.com', | ||
timesCounter: 8 | ||
}, functionsTable)).to.eql(true); | ||
}); | ||
|
||
it('should evaluate a single and function to false', function() { | ||
expect(evaluate({ and: [{ user: 't@a.com' }] }, { userId: 'r@a.com' }, functionsTable)).to.eql(false); | ||
it('should evaluate a single and function to false', function () { | ||
expect(evaluate<Context>({and: [{user: 't@a.com'}]}, { | ||
userId: 'r@a.com', | ||
timesCounter: 8 | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should evaluate a single or function to false', function() { | ||
expect(evaluate({ or: [{ user: 't@a.com' }] }, { userId: 'r@a.com' }, functionsTable)).to.eql(false); | ||
it('should evaluate a single or function to false', function () { | ||
expect(evaluate<Context>({or: [{user: 't@a.com'}]}, { | ||
userId: 'r@a.com', | ||
timesCounter: 8 | ||
}, functionsTable)).to.eql(false); | ||
}); | ||
|
||
it('should fail on empty or op', function() { | ||
expect(function() { | ||
evaluate({ or: [] }, { userId: 'r@a.com' }, functionsTable); | ||
it('should fail on empty or op', function () { | ||
expect(function () { | ||
evaluate<Context>({or: []}, {userId: 'r@a.com', timesCounter: 8}, functionsTable); | ||
}).to.throw('Invalid expression - or operator must have at least one expression'); | ||
}); | ||
|
||
it('should fail on empty and op', function() { | ||
expect(function() { | ||
evaluate({ and: [] }, { userId: 'r@a.com' }, functionsTable); | ||
it('should fail on empty and op', function () { | ||
expect(function () { | ||
evaluate<Context>({and: []}, {userId: 'r@a.com', timesCounter: 8}, functionsTable); | ||
}).to.throw('Invalid expression - and operator must have at least one expression'); | ||
}); | ||
|
||
it('should fail on non existing function', function() { | ||
expect(function() { | ||
evaluate({ and: [{ dummy: 1 }] }, { userId: 'r@a.com' }, functionsTable); | ||
it('should fail on non existing function', function () { | ||
expect(function () { | ||
evaluate<Context>({and: [{dummy: 1}]}, {userId: 'r@a.com', timesCounter: 8}, functionsTable); | ||
}).to.throw('Invalid expression - unknown function dummy'); | ||
}); | ||
|
||
it('should fail on non existing function 2', function() { | ||
expect(function() { | ||
evaluate({ dummy: 1 }, { userId: 'r@a.com' }, functionsTable); | ||
it('should fail on non existing function 2', function () { | ||
expect(function () { | ||
evaluate<Context>({dummy: 1}, {userId: 'r@a.com', timesCounter: 8}, functionsTable); | ||
}).to.throw('Invalid expression - unknown function dummy'); | ||
}); | ||
|
||
it('should fail on non existing op', function() { | ||
expect(function() { | ||
evaluate({ userId: {bk : 1} }, { userId: 'r@a.com' }, functionsTable); | ||
it('should fail on non existing op', function () { | ||
expect(function () { | ||
evaluate<Context>({userId: {bk: 1}}, {userId: 'r@a.com', timesCounter: 8}, functionsTable); | ||
}).to.throw('Invalid expression - unknown op bk'); | ||
}); | ||
|
||
it('should fail too many keys to op', function() { | ||
expect(function() { | ||
evaluate({ userId: {eq : 1, nq: 2} }, { userId: 'r@a.com' }, functionsTable); | ||
it('should fail too many keys to op', function () { | ||
expect(function () { | ||
evaluate<Context>({userId: {eq: 1, nq: 2}}, {userId: 'r@a.com', timesCounter: 8}, functionsTable); | ||
}).to.throw('Invalid expression - too may keys'); | ||
}); | ||
|
||
it('should fail too many keys', function() { | ||
expect(function() { | ||
evaluate({ dummy: 1, user: 'a' }, { userId: 'r@a.com' }, functionsTable); | ||
it('should fail too many keys', function () { | ||
expect(function () { | ||
evaluate<Context>({dummy: 1, user: 'a'}, {userId: 'r@a.com', timesCounter: 8}, functionsTable); | ||
}).to.throw('Invalid expression - too may keys'); | ||
}); | ||
|
||
it('should fail too many keys 2', function() { | ||
expect(function() { | ||
evaluate({ or: [{ dummy: 1, user: 'a' }] }, { userId: 'r@a.com' }, functionsTable); | ||
it('should fail too many keys 2', function () { | ||
expect(function () { | ||
evaluate<Context>({or: [{dummy: 1, user: 'a'}]}, {userId: 'r@a.com', timesCounter: 8}, functionsTable); | ||
}).to.throw('Invalid expression - too may keys'); | ||
}); | ||
}); |