From 932b601cc96e02b0606a05e338440452c937fdd1 Mon Sep 17 00:00:00 2001 From: fkleuver Date: Thu, 21 Jun 2018 13:46:43 +0200 Subject: [PATCH] test(parser): increase test coverage for binary associativity --- test/parser.spec.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/test/parser.spec.js b/test/parser.spec.js index 29144ca6..f22656cd 100644 --- a/test/parser.spec.js +++ b/test/parser.spec.js @@ -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', () => {