Skip to content

Commit

Permalink
test(parser): increase test coverage for binary associativity
Browse files Browse the repository at this point in the history
  • Loading branch information
fkleuver committed Jun 21, 2018
1 parent d2d867e commit 932b601
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions test/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,30 @@ describe('Parser', () => {
});

describe('Binary left-to-right associativity', () => {
it('4/2*10', () => {
const expr = parser.parse('4/2*10');
const res = expr.evaluate({}, {});
expect(res).toBe(20);
});
const tests = [
{ expr: '4/2*10', expected: 4/2*10 },
{ expr: '4/2*10+1', expected: 4/2*10+1 },
{ expr: '1+4/2+1', expected: 1+4/2+1 },
{ expr: '1+4/2+1+1', expected: 1+4/2+1+1 },
{ expr: '4/2*10', expected: 4/2*10 },
{ expr: '4/2*10/2', expected: 4/2*10/2 },
{ expr: '4/2*10*2', expected: 4/2*10*2 },
{ expr: '4/2*10+2', expected: 4/2*10+2 },
{ expr: '2/4/2*10', expected: 2/4/2*10 },
{ expr: '2*4/2*10', expected: 2*4/2*10 },
{ expr: '2+4/2*10', expected: 2+4/2*10 },
{ expr: '2/4/2*10/2', expected: 2/4/2*10/2 },
{ expr: '2*4/2*10*2', expected: 2*4/2*10*2 },
{ expr: '2+4/2*10+2', expected: 2+4/2*10+2 }
];

it('4-2+10', () => {
const expr = parser.parse('4-2+10');
const res = expr.evaluate({}, {});
expect(res).toBe(12);
});
for (const { expr, expected } of tests) {
it(`${expr} evaluates to ${expected}`, () => {
const parsed = parser.parse('4/2*10');
const actual = parsed.evaluate({}, {});
expect(actual).toBe(expected);
});
}
});

describe('Binary operator precedence', () => {
Expand Down

0 comments on commit 932b601

Please sign in to comment.