From d493a4d143c58ec51c582644dff8e049fef9aa01 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 21 Aug 2010 15:34:56 +0200 Subject: [PATCH] Move test helper into its own file + reorder test file includes --- test/compiler-test.js | 57 ------------------------------------ test/helpers.js | 68 +++++++++++++++++++++++++++++++++++++++++++ test/index.html | 7 +++-- test/parser-test.js | 16 ---------- 4 files changed, 72 insertions(+), 76 deletions(-) create mode 100644 test/helpers.js diff --git a/test/compiler-test.js b/test/compiler-test.js index d84375b..3c10657 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -1,62 +1,5 @@ (function(global) { -/* ===== Helpers ===== */ - -global.throws = function(block, exceptionType, exceptionProperties) { - var exception = null; - try { - block(); - } catch (e) { - exception = e; - } - - ok( - exception !== null, - exception !== null ? "okay: thrown something" : "failed, nothing thrown" - ); - if (exception !== null) { - ok( - exception instanceof exceptionType, - exception instanceof exceptionType - ? "okay: thrown " + exceptionType.name - : "failed, thrown " + exception.name + " instead of " + exceptionType.name - ); - - for (var property in exceptionProperties) { - strictEqual(exception[property], exceptionProperties[property]); - } - } -}; - -global.parses = function(parser, input, expected) { - deepEqual(parser.parse(input), expected); -}; - -global.doesNotParse = function(parser, input) { - throws(function() { parser.parse(input); }, parser.SyntaxError); -}; - -global.doesNotParseWithMessage = function(parser, input, message) { - throws( - function() { parser.parse(input); }, - parser.SyntaxError, - { message: message } - ); -}; - -global.doesNotParseWithPos = function(parser, input, line, column) { - var exception = throws( - function() { parser.parse(input); }, - parser.SyntaxError, - { - line: line, - column: column - } - ); -}; - -/* ===== PEG.compiler ===== */ - module("PEG.compiler"); test("choices", function() { diff --git a/test/helpers.js b/test/helpers.js new file mode 100644 index 0000000..027075a --- /dev/null +++ b/test/helpers.js @@ -0,0 +1,68 @@ +(function(global) { + +global.throws = function(block, exceptionType, exceptionProperties) { + var exception = null; + try { + block(); + } catch (e) { + exception = e; + } + + ok( + exception !== null, + exception !== null ? "okay: thrown something" : "failed, nothing thrown" + ); + if (exception !== null) { + ok( + exception instanceof exceptionType, + exception instanceof exceptionType + ? "okay: thrown " + exceptionType.name + : "failed, thrown " + exception.name + " instead of " + exceptionType.name + ); + + for (var property in exceptionProperties) { + strictEqual(exception[property], exceptionProperties[property]); + } + } +}; + +global.parses = function(parser, input, expected) { + deepEqual(parser.parse(input), expected); +}; + +global.doesNotParse = function(parser, input) { + throws(function() { parser.parse(input); }, parser.SyntaxError); +}; + +global.doesNotParseWithMessage = function(parser, input, message) { + throws( + function() { parser.parse(input); }, + parser.SyntaxError, + { message: message } + ); +}; + +global.doesNotParseWithPos = function(parser, input, line, column) { + var exception = throws( + function() { parser.parse(input); }, + parser.SyntaxError, + { + line: line, + column: column + } + ); +}; + +global.parserParses = function(input, expected) { + global.parses(PEG.parser, input, expected); +}; + +global.parserDoesNotParse = function(input) { + global.doesNotParse(PEG.parser, input); +} + +global.parserDoesNotParseWithMessage = function(input, message) { + global.doesNotParseWithMessage(PEG.parser, input, message); +} + +})(this); diff --git a/test/index.html b/test/index.html index 7b4c352..89c8bf8 100644 --- a/test/index.html +++ b/test/index.html @@ -4,12 +4,13 @@ PEG.js Test Suite - - + + + - +

PEG.js Test Suite

diff --git a/test/parser-test.js b/test/parser-test.js index c82865a..2a39ba4 100644 --- a/test/parser-test.js +++ b/test/parser-test.js @@ -1,21 +1,5 @@ (function(global) { -/* ===== Helpers ===== */ - -global.parserParses = function(input, expected) { - global.parses(PEG.parser, input, expected); -}; - -global.parserDoesNotParse = function(input) { - global.doesNotParse(PEG.parser, input); -} - -global.parserDoesNotParseWithMessage = function(input, message) { - global.doesNotParseWithMessage(PEG.parser, input, message); -} - -/* ===== PEG.parser ===== */ - module("PEG.parser"); function initializer(code) {