From 9cc35d2cab3456fead374b00eb2d983736af5d5f Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 16 Dec 2016 14:14:05 +0100 Subject: [PATCH] Tests: DRY up tracing tests --- test/api/generated-parser-api.spec.js | 124 ++++++++++++++------------ 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/test/api/generated-parser-api.spec.js b/test/api/generated-parser-api.spec.js index 6df3f5a..a22008a 100644 --- a/test/api/generated-parser-api.spec.js +++ b/test/api/generated-parser-api.spec.js @@ -58,6 +58,15 @@ describe("generated parser API", function() { describe("default tracer", function() { it("traces using console.log (if console is defined)", function() { + let messages = [ + "1:1-1:1 rule.enter start", + "1:1-1:1 rule.enter a", + "1:1-1:1 rule.fail a", + "1:1-1:1 rule.enter b", + "1:1-1:2 rule.match b", + "1:1-1:2 rule.match start" + ]; + if (typeof console === "object") { sinon.stub(console, "log"); } @@ -66,12 +75,9 @@ describe("generated parser API", function() { parser.parse("b"); if (typeof console === "object") { - expect(console.log.calledWithExactly("1:1-1:1 rule.enter start")).to.equal(true); - expect(console.log.calledWithExactly("1:1-1:1 rule.enter a")).to.equal(true); - expect(console.log.calledWithExactly("1:1-1:1 rule.fail a")).to.equal(true); - expect(console.log.calledWithExactly("1:1-1:1 rule.enter b")).to.equal(true); - expect(console.log.calledWithExactly("1:1-1:2 rule.match b")).to.equal(true); - expect(console.log.calledWithExactly("1:1-1:2 rule.match start")).to.equal(true); + messages.forEach(message => { + expect(console.log.calledWithExactly(message)).to.equal(true); + }); } } finally { if (typeof console === "object") { @@ -84,60 +90,66 @@ describe("generated parser API", function() { describe("custom tracers", function() { describe("trace", function() { it("receives tracing events", function() { + let events = [ + { + type: "rule.enter", + rule: "start", + location: { + start: { offset: 0, line: 1, column: 1 }, + end: { offset: 0, line: 1, column: 1 } + } + }, + { + type: "rule.enter", + rule: "a", + location: { + start: { offset: 0, line: 1, column: 1 }, + end: { offset: 0, line: 1, column: 1 } + } + }, + { + type: "rule.fail", + rule: "a", + location: { + start: { offset: 0, line: 1, column: 1 }, + end: { offset: 0, line: 1, column: 1 } + } + }, + { + type: "rule.enter", + rule: "b", + location: { + start: { offset: 0, line: 1, column: 1 }, + end: { offset: 0, line: 1, column: 1 } + } + }, + { + type: "rule.match", + rule: "b", + result: "b", + location: { + start: { offset: 0, line: 1, column: 1 }, + end: { offset: 1, line: 1, column: 2 } + } + }, + { + type: "rule.match", + rule: "start", + result: "b", + location: { + start: { offset: 0, line: 1, column: 1 }, + end: { offset: 1, line: 1, column: 2 } + } + } + ]; + let tracer = { trace: sinon.spy() }; parser.parse("b", { tracer: tracer }); - expect(tracer.trace.calledWithExactly({ - type: "rule.enter", - rule: "start", - location: { - start: { offset: 0, line: 1, column: 1 }, - end: { offset: 0, line: 1, column: 1 } - } - })).to.equal(true); - expect(tracer.trace.calledWithExactly({ - type: "rule.enter", - rule: "a", - location: { - start: { offset: 0, line: 1, column: 1 }, - end: { offset: 0, line: 1, column: 1 } - } - })).to.equal(true); - expect(tracer.trace.calledWithExactly({ - type: "rule.fail", - rule: "a", - location: { - start: { offset: 0, line: 1, column: 1 }, - end: { offset: 0, line: 1, column: 1 } - } - })).to.equal(true); - expect(tracer.trace.calledWithExactly({ - type: "rule.enter", - rule: "b", - location: { - start: { offset: 0, line: 1, column: 1 }, - end: { offset: 0, line: 1, column: 1 } - } - })).to.equal(true); - expect(tracer.trace.calledWithExactly({ - type: "rule.match", - rule: "b", - result: "b", - location: { - start: { offset: 0, line: 1, column: 1 }, - end: { offset: 1, line: 1, column: 2 } - } - })).to.equal(true); - expect(tracer.trace.calledWithExactly({ - type: "rule.match", - rule: "start", - result: "b", - location: { - start: { offset: 0, line: 1, column: 1 }, - end: { offset: 1, line: 1, column: 2 } - } - })).to.equal(true); + events.forEach(event => { + expect(tracer.trace.calledWithExactly(event)).to.equal(true); + }); }); }); });