Skip to content

Commit df0eda4

Browse files
committed
Added tests for Runtime.evaluateBinaryExpression
1 parent bcd2557 commit df0eda4

3 files changed

Lines changed: 203 additions & 3 deletions

File tree

src/Runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Runner {
163163
}
164164

165165
if (expression.operator === OPERATOR_LESS_THAN) {
166-
return leftValue > rightValue;
166+
return leftValue < rightValue;
167167
}
168168

169169
if (expression.operator === OPERATOR_AND) {
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const Runtime = require('../../src/Runtime');
5+
const nb = require('../nodeBuilder');
6+
7+
const {
8+
OPERATOR_EQUALS,
9+
OPERATOR_NOT_EQUALS,
10+
OPERATOR_STRICT_EQUALS,
11+
OPERATOR_STRICT_NOT_EQUALS,
12+
OPERATOR_AND,
13+
OPERATOR_OR,
14+
OPERATOR_GREATER_THAN,
15+
OPERATOR_LESS_THAN,
16+
OPERATOR_GREATER_EQUAL_THAN,
17+
OPERATOR_LESS_EQUAL_THAN
18+
} = require('../../src/constants');
19+
20+
const runtime = new Runtime();
21+
22+
const constantValue = nb.value('foo');
23+
const lowNumericValue = nb.value(10);
24+
const highNumericValue = nb.value(20);
25+
const trueValue = nb.value(true);
26+
const falseValue = nb.value(false);
27+
28+
describe('Runtime.evaluateBinaryExpression', function() {
29+
it('should compare same values with equals operator', function() {
30+
expect(runtime.evaluateBinaryExpression(
31+
nb.binaryExpression(constantValue, constantValue, OPERATOR_EQUALS))
32+
).to.be.true;
33+
});
34+
35+
it('should compare different values with equals operator', function() {
36+
expect(runtime.evaluateBinaryExpression(
37+
nb.binaryExpression(constantValue, falseValue, OPERATOR_EQUALS))
38+
).to.be.false;
39+
});
40+
41+
it('should compare same values with not equals operator', function() {
42+
expect(runtime.evaluateBinaryExpression(
43+
nb.binaryExpression(constantValue, constantValue, OPERATOR_NOT_EQUALS))
44+
).to.be.false;
45+
});
46+
47+
it('should compare different values with equals operator', function() {
48+
expect(runtime.evaluateBinaryExpression(
49+
nb.binaryExpression(constantValue, falseValue, OPERATOR_NOT_EQUALS))
50+
).to.be.true;
51+
});
52+
53+
it('should compare same values with strict equals operator', function() {
54+
expect(runtime.evaluateBinaryExpression(
55+
nb.binaryExpression(constantValue, constantValue, OPERATOR_STRICT_EQUALS))
56+
).to.be.true;
57+
});
58+
59+
it('should compare different types values with strict equals operator', function() {
60+
expect(runtime.evaluateBinaryExpression(
61+
nb.binaryExpression(constantValue, trueValue, OPERATOR_STRICT_EQUALS))
62+
).to.be.false;
63+
});
64+
65+
it('should compare different values, same type with strict equals operator', function() {
66+
expect(runtime.evaluateBinaryExpression(
67+
nb.binaryExpression(falseValue, trueValue, OPERATOR_STRICT_EQUALS))
68+
).to.be.false;
69+
});
70+
71+
it('should compare same values with strict not equals operator', function() {
72+
expect(runtime.evaluateBinaryExpression(
73+
nb.binaryExpression(constantValue, constantValue, OPERATOR_STRICT_NOT_EQUALS))
74+
).to.be.false;
75+
});
76+
77+
it('should compare different types values with strict not equals operator', function() {
78+
expect(runtime.evaluateBinaryExpression(
79+
nb.binaryExpression(constantValue, trueValue, OPERATOR_STRICT_NOT_EQUALS))
80+
).to.be.true;
81+
});
82+
83+
it('should compare different values, same type with strict not equals operator', function() {
84+
expect(runtime.evaluateBinaryExpression(
85+
nb.binaryExpression(falseValue, trueValue, OPERATOR_STRICT_NOT_EQUALS))
86+
).to.be.true;
87+
});
88+
89+
it('should compare lesser value against greater value with greater equal than', function() {
90+
expect(runtime.evaluateBinaryExpression(
91+
nb.binaryExpression(lowNumericValue, highNumericValue, OPERATOR_GREATER_EQUAL_THAN))
92+
).to.be.false;
93+
});
94+
95+
it('should compare greater value against lesser value with greater equal than', function() {
96+
expect(runtime.evaluateBinaryExpression(
97+
nb.binaryExpression(highNumericValue, lowNumericValue, OPERATOR_GREATER_EQUAL_THAN))
98+
).to.be.true;
99+
});
100+
101+
it('should compare equal values with greater equal than', function() {
102+
expect(runtime.evaluateBinaryExpression(
103+
nb.binaryExpression(lowNumericValue, lowNumericValue, OPERATOR_GREATER_EQUAL_THAN))
104+
).to.be.true;
105+
});
106+
107+
it('should compare lesser value against greater value with lesser equal than', function() {
108+
expect(runtime.evaluateBinaryExpression(
109+
nb.binaryExpression(lowNumericValue, highNumericValue, OPERATOR_LESS_EQUAL_THAN))
110+
).to.be.true;
111+
});
112+
113+
it('should compare greater value against lesser value with lesser equal than', function() {
114+
expect(runtime.evaluateBinaryExpression(
115+
nb.binaryExpression(highNumericValue, lowNumericValue, OPERATOR_LESS_EQUAL_THAN))
116+
).to.be.false;
117+
});
118+
119+
it('should compare equal values with greater lesser than', function() {
120+
expect(runtime.evaluateBinaryExpression(
121+
nb.binaryExpression(lowNumericValue, lowNumericValue, OPERATOR_LESS_EQUAL_THAN))
122+
).to.be.true;
123+
});
124+
125+
it('should compare lesser value against greater value with greater than', function() {
126+
expect(runtime.evaluateBinaryExpression(
127+
nb.binaryExpression(lowNumericValue, highNumericValue, OPERATOR_GREATER_THAN))
128+
).to.be.false;
129+
});
130+
131+
it('should compare greater value against lesser value with greater than', function() {
132+
expect(runtime.evaluateBinaryExpression(
133+
nb.binaryExpression(highNumericValue, lowNumericValue, OPERATOR_GREATER_THAN))
134+
).to.be.true;
135+
});
136+
137+
it('should compare equal values with greater than', function() {
138+
expect(runtime.evaluateBinaryExpression(
139+
nb.binaryExpression(lowNumericValue, lowNumericValue, OPERATOR_GREATER_THAN))
140+
).to.be.false;
141+
});
142+
143+
it('should compare lesser value against greater value with lesser than', function() {
144+
expect(runtime.evaluateBinaryExpression(
145+
nb.binaryExpression(lowNumericValue, highNumericValue, OPERATOR_LESS_THAN))
146+
).to.be.true;
147+
});
148+
149+
it('should compare greater value against lesser value with lesser than', function() {
150+
expect(runtime.evaluateBinaryExpression(
151+
nb.binaryExpression(highNumericValue, lowNumericValue, OPERATOR_LESS_THAN))
152+
).to.be.false;
153+
});
154+
155+
it('should compare equal values with greater than', function() {
156+
expect(runtime.evaluateBinaryExpression(
157+
nb.binaryExpression(lowNumericValue, lowNumericValue, OPERATOR_LESS_THAN))
158+
).to.be.false;
159+
});
160+
161+
it('should compare same true values with and operator', function() {
162+
expect(runtime.evaluateBinaryExpression(
163+
nb.binaryExpression(trueValue, trueValue, OPERATOR_AND))
164+
).to.be.true;
165+
});
166+
167+
it('should compare different values with and operator', function() {
168+
expect(runtime.evaluateBinaryExpression(
169+
nb.binaryExpression(trueValue, falseValue, OPERATOR_AND))
170+
).to.be.false;
171+
});
172+
173+
it('should compare same false values with and operator', function() {
174+
expect(runtime.evaluateBinaryExpression(
175+
nb.binaryExpression(falseValue, falseValue, OPERATOR_AND))
176+
).to.be.false;
177+
});
178+
179+
it('should compare same true values with or operator', function() {
180+
expect(runtime.evaluateBinaryExpression(
181+
nb.binaryExpression(trueValue, trueValue, OPERATOR_OR))
182+
).to.be.true;
183+
});
184+
185+
it('should compare different values with or operator', function() {
186+
expect(runtime.evaluateBinaryExpression(
187+
nb.binaryExpression(trueValue, falseValue, OPERATOR_OR))
188+
).to.be.true;
189+
});
190+
191+
it('should compare same false values with or operator', function() {
192+
expect(runtime.evaluateBinaryExpression(
193+
nb.binaryExpression(falseValue, falseValue, OPERATOR_OR))
194+
).to.be.false;
195+
});
196+
197+
it('should throw error on unknown operator', function() {
198+
expect(() => runtime.evaluateBinaryExpression(nb.invalidExpression())).to.throw(/Unknown operator:/);
199+
});
200+
});

test/nodeBuilder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ module.exports = {
8686
invalidExpression() {
8787
return {
8888
type: 'INVALID_EXPRESSION',
89-
left: 'left',
90-
right: 'right',
89+
left: module.exports.value('left'),
90+
right: module.exports.value('right'),
9191
operator: 'INVALID'
9292
};
9393
}

0 commit comments

Comments
 (0)