From bf9d9561c4a6eb7552c1a1318e610845e33d3f92 Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 1 Aug 2016 11:37:23 +0200 Subject: [PATCH] Tighten "try ... catch" clauses in spec helpers They wrapped too much code. --- .../generated-parser-behavior.spec.js | 48 +++++++++---------- spec/unit/compiler/passes/helpers.js | 18 +++---- spec/unit/parser.spec.js | 42 ++++++++-------- 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/spec/behavior/generated-parser-behavior.spec.js b/spec/behavior/generated-parser-behavior.spec.js index 8c2724f..228fea4 100644 --- a/spec/behavior/generated-parser-behavior.spec.js +++ b/spec/behavior/generated-parser-behavior.spec.js @@ -41,20 +41,6 @@ describe("generated parser behavior", function() { try { result = this.actual.parse(input, options); - - if (expected !== undefined) { - this.message = function() { - return "Expected " + jasmine.pp(input) + " " - + "with options " + jasmine.pp(options) + " " - + (this.isNot ? "not " : "") - + "to parse as " + jasmine.pp(expected) + ", " - + "but it parsed as " + jasmine.pp(result) + "."; - }; - - return this.env.equals_(result, expected); - } else { - return true; - } } catch (e) { this.message = function() { return "Expected " + jasmine.pp(input) + " " @@ -66,6 +52,20 @@ describe("generated parser behavior", function() { return false; } + + if (expected !== undefined) { + this.message = function() { + return "Expected " + jasmine.pp(input) + " " + + "with options " + jasmine.pp(options) + " " + + (this.isNot ? "not " : "") + + "to parse as " + jasmine.pp(expected) + ", " + + "but it parsed as " + jasmine.pp(result) + "."; + }; + + return this.env.equals_(result, expected); + } else { + return true; + } }, toFailToParse: function(input, details, options) { @@ -75,16 +75,6 @@ describe("generated parser behavior", function() { try { result = this.actual.parse(input, options); - - this.message = function() { - return "Expected " + jasmine.pp(input) + " " - + "with options " + jasmine.pp(options) + " " - + "to fail to parse" - + (details ? " with details " + jasmine.pp(details) : "") + ", " - + "but it parsed as " + jasmine.pp(result) + "."; - }; - - return false; } catch (e) { if (this.isNot) { this.message = function() { @@ -117,6 +107,16 @@ describe("generated parser behavior", function() { return true; } + + this.message = function() { + return "Expected " + jasmine.pp(input) + " " + + "with options " + jasmine.pp(options) + " " + + "to fail to parse" + + (details ? " with details " + jasmine.pp(details) : "") + ", " + + "but it parsed as " + jasmine.pp(result) + "."; + }; + + return false; } }); }); diff --git a/spec/unit/compiler/passes/helpers.js b/spec/unit/compiler/passes/helpers.js index 16c9e89..d6228ed 100644 --- a/spec/unit/compiler/passes/helpers.js +++ b/spec/unit/compiler/passes/helpers.js @@ -66,15 +66,6 @@ beforeEach(function() { try { this.actual(ast); - - this.message = function() { - return "Expected the pass to report an error " - + (details ? "with details " + jasmine.pp(details) + " ": "") - + "for grammar " + jasmine.pp(grammar) + ", " - + "but it didn't."; - }; - - return false; } catch (e) { if (this.isNot) { this.message = function() { @@ -104,6 +95,15 @@ beforeEach(function() { return true; } + + this.message = function() { + return "Expected the pass to report an error " + + (details ? "with details " + jasmine.pp(details) + " ": "") + + "for grammar " + jasmine.pp(grammar) + ", " + + "but it didn't."; + }; + + return false; } }); }); diff --git a/spec/unit/parser.spec.js b/spec/unit/parser.spec.js index d40a9c3..d01b664 100644 --- a/spec/unit/parser.spec.js +++ b/spec/unit/parser.spec.js @@ -179,17 +179,6 @@ describe("PEG.js grammar parser", function() { try { result = peg.parser.parse(this.actual); - - stripLocation(result); - - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " " - + (this.isNot ? "not " : "") - + "to parse as " + jasmine.pp(expected) + ", " - + "but it parsed as " + jasmine.pp(result) + "."; - }; - - return this.env.equals_(result, expected); } catch (e) { this.message = function() { return "Expected " + jasmine.pp(this.actual) + " " @@ -200,6 +189,17 @@ describe("PEG.js grammar parser", function() { return false; } + + stripLocation(result); + + this.message = function() { + return "Expected " + jasmine.pp(this.actual) + " " + + (this.isNot ? "not " : "") + + "to parse as " + jasmine.pp(expected) + ", " + + "but it parsed as " + jasmine.pp(result) + "."; + }; + + return this.env.equals_(result, expected); }, toFailToParse: function(details) { @@ -207,16 +207,6 @@ describe("PEG.js grammar parser", function() { try { result = peg.parser.parse(this.actual); - - stripLocation(result); - - this.message = function() { - return "Expected " + jasmine.pp(this.actual) + " to fail to parse" - + (details ? " with details " + jasmine.pp(details) : "") + ", " - + "but it parsed as " + jasmine.pp(result) + "."; - }; - - return false; } catch (e) { if (this.isNot) { this.message = function() { @@ -245,6 +235,16 @@ describe("PEG.js grammar parser", function() { return true; } + + stripLocation(result); + + this.message = function() { + return "Expected " + jasmine.pp(this.actual) + " to fail to parse" + + (details ? " with details " + jasmine.pp(details) : "") + ", " + + "but it parsed as " + jasmine.pp(result) + "."; + }; + + return false; } }); });