1 | const chai = require('chai');
|
2 | const expect = chai.expect;
|
3 | const expressionEvaluator = require('../lib/expressionEvaluator');
|
4 |
|
5 | describe('ExpressionEvaluator', () => {
|
6 |
|
7 | describe('evaluate', () => {
|
8 | it('should return true on null', () => {
|
9 | expect(expressionEvaluator.evaluate(null)).to.be.true;
|
10 | });
|
11 | it('should return true on undefined', () => {
|
12 | expect(expressionEvaluator.evaluate(undefined)).to.be.true;
|
13 | });
|
14 | it('should return true on empty object', () => {
|
15 | expect(expressionEvaluator.evaluate({})).to.be.true;
|
16 | });
|
17 | });
|
18 |
|
19 | describe('comparison operator', () => {
|
20 | it('eq - should return true on 1 <> 1', () => {
|
21 | expect(expressionEvaluator.eq({
|
22 | key: 'test',
|
23 | value: 1
|
24 | }, { 'test': 1 })).to.be.true;
|
25 | });
|
26 | it('neq - should return true on 1 <> 2', () => {
|
27 | expect(expressionEvaluator.neq({
|
28 | key: 'test',
|
29 | value: 1
|
30 | }, { 'test': 2 })).to.be.true;
|
31 | });
|
32 | });
|
33 | describe('logical operator', () => {
|
34 | it('and - should return true on eq left <> 1 and right <> 1', () => {
|
35 | expect(expressionEvaluator.and({
|
36 | fields: [{
|
37 | type: 'eq',
|
38 | key: 'left',
|
39 | value: 1
|
40 | }, {
|
41 | type: 'eq',
|
42 | key: 'right',
|
43 | value: 1
|
44 | }]
|
45 | }, { left: 1, right: 1 })).to.be.true;
|
46 | });
|
47 | it('or - should return true on eq left <> 1 and right <> 2', () => {
|
48 | expect(expressionEvaluator.or({
|
49 | fields: [{
|
50 | type: 'eq',
|
51 | key: 'left',
|
52 | value: 1
|
53 | }, {
|
54 | type: 'eq',
|
55 | key: 'right',
|
56 | value: 2
|
57 | }]
|
58 | }, { left: 1, right: 1 })).to.be.true;
|
59 | });
|
60 | });
|
61 |
|
62 | describe('evaluate', () => {
|
63 | it('should evaluate logical operator', () => {
|
64 | expect(expressionEvaluator.evaluate({
|
65 | type: 'and',
|
66 | fields: [{
|
67 | type: 'eq',
|
68 | key: 'left',
|
69 | value: 1
|
70 | }, {
|
71 | type: 'eq',
|
72 | key: 'right',
|
73 | value: 1
|
74 | }]
|
75 | }, { left: 1, right: 1 })).to.be.true;
|
76 | });
|
77 |
|
78 | it('should evaluate comparison operator', () => {
|
79 | expect(expressionEvaluator.evaluate({
|
80 | type: 'eq',
|
81 | key: 'test',
|
82 | value: 1
|
83 | }, { 'test': 1 })).to.be.true;
|
84 | });
|
85 |
|
86 | });
|
87 | });
|