diff --git a/Jakefile b/Jakefile index 2243d83..36d814f 100644 --- a/Jakefile +++ b/Jakefile @@ -429,3 +429,27 @@ task("clean", [], function () { } }); }); + +desc("Test"); +task("test", [], function () { + var Mocha = require("mocha"); + + var mocha = new Mocha({ + ui: "tdd" + }); + fs.readdirSync("./tests/").filter(function (filename) { + if (filename.indexOf("test-") === 0) { + var extensionIndex = filename.lastIndexOf(".js"); + if (extensionIndex !== -1 && extensionIndex === filename.length - ".js".length) { + return true; + } + } + + return false; + }).forEach(function (filename) { + mocha.addFile("./tests/" + filename); + }); + + mocha.run(); + // .\node_modules\.bin\mocha -u tdd tests/test*.js +}); diff --git a/package.json b/package.json index 6039026..33cc4d6 100644 --- a/package.json +++ b/package.json @@ -20,10 +20,12 @@ "url": "https://github.com/Arnavion/libjass" }, "scripts": { - "postinstall": "jake clean && jake --trace" + "postinstall": "jake clean && jake --trace", + "test": "jake test" }, "dependencies": { "jake": "latest", + "mocha": "~1.12", "pegjs": "0.7.0", "typescript": "0.9.1-1", "uglify-js": "~2" diff --git a/test/index.js b/tests/index.js similarity index 91% rename from test/index.js rename to tests/index.js index 5f0ea6b..bef6702 100644 --- a/test/index.js +++ b/tests/index.js @@ -29,6 +29,21 @@ var suite = null; var setup = null; var test = null; +var require = function (name) { + if (name === "../libjass.js") { + return libjass; + } + else if (name === "assert") { + return assert; + } + else if (name === "./parser-test.js") { + return parserTest; + } + else { + throw new Error("Cannot require unmocked module: " + name); + } +}; + (function () { var Suite = function (name, parent) { this.name = name; @@ -206,40 +221,6 @@ var assert = new function () { }; }; -var parserTest = function (description, input, rule, expected) { - var result = test(description, function () { - var actual = null; - - try { - actual = libjass.parser.parse(input, rule); - } - catch (parseException) { - if (expected === null) { - passed = true; - return; - } - else { - throw new Error("Expected parse to succeed but it threw an exception: " + parseException.message); - } - } - - if (expected === null) { - throw new Error("Expected parse to fail."); - } - else if (actual === null) { - throw new Error("Parse failed without throwing an exception."); - } - - assert.deepEqual(actual, expected); - }); - - result.customProperties = Object.create(null); - result.customProperties.input = input; - result.customProperties.rule = rule; - - return result; -}; - var Logger = function (outputDiv) { var testDiv = document.createElement("div"); testDiv.className = "test"; diff --git a/test/index.xhtml b/tests/index.xhtml similarity index 88% rename from test/index.xhtml rename to tests/index.xhtml index 076548c..f483db6 100644 --- a/test/index.xhtml +++ b/tests/index.xhtml @@ -34,10 +34,11 @@ + - - - + + +
diff --git a/tests/parser-test.js b/tests/parser-test.js new file mode 100644 index 0000000..a9b236e --- /dev/null +++ b/tests/parser-test.js @@ -0,0 +1,60 @@ +/** + * libjass + * + * https://github.com/Arnavion/libjass + * + * Copyright 2013 Arnav Singh + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var libjass = require("../libjass.js"); +var assert = require("assert"); + +var parserTest = function (description, input, rule, expected) { + var result = test(description, function () { + var actual = null; + + try { + actual = libjass.parser.parse(input, rule); + } + catch (parseException) { + if (expected === null) { + passed = true; + return; + } + else { + throw new Error("Expected parse to succeed but it threw an exception: " + parseException.message); + } + } + + if (expected === null) { + throw new Error("Expected parse to fail."); + } + else if (actual === null) { + throw new Error("Parse failed without throwing an exception."); + } + + assert.deepEqual(actual, expected); + }); + + result.customProperties = Object.create(null); + result.customProperties.input = input; + result.customProperties.rule = rule; + + return result; +}; + +if (typeof module !== "undefined") { + module.exports = parserTest; +} diff --git a/test/miscellaneous.js b/tests/test-miscellaneous.js similarity index 91% rename from test/miscellaneous.js rename to tests/test-miscellaneous.js index de49d4d..61f6b37 100644 --- a/test/miscellaneous.js +++ b/tests/test-miscellaneous.js @@ -18,8 +18,11 @@ * limitations under the License. */ +var libjass = require("../libjass.js"); +var parserTest = require("./parser-test.js"); + suite("Miscellaneous", function () { - parserTest("herkz", "{\\pos(311,4)\\blur0.8\\fs40\\bord0\\c&H3F171F&\\t(3820,3820,\\blur6}Chi{\\c&H422CB1&}tose {\\c&H3F171F&}Furu", "dialogue", [ + parserTest("herkz", "{\\pos(311,4)\\blur0.8\\fs40\\bord0\\c&H3F171F&\\t(3820,3820,\\blur6}Chi{\\c&H422CB1&}tose {\\c&H3F171F&}Furu", "dialogueParts", [ new libjass.tags.Comment("\\pos(311,4)\\blur0.8\\fs40\\bord0\\c&H3F171F&\\t(3820,3820,\\blur6"), new libjass.tags.Text("Chi"), new libjass.tags.PrimaryColor(new libjass.tags.Color(177, 44, 66, 1)), diff --git a/test/primitives.js b/tests/test-primitives.js similarity index 92% rename from test/primitives.js rename to tests/test-primitives.js index e3232c9..70c3e25 100644 --- a/test/primitives.js +++ b/tests/test-primitives.js @@ -18,6 +18,9 @@ * limitations under the License. */ +var libjass = require("../libjass.js"); +var parserTest = require("./parser-test.js"); + suite("Primitives", function () { suite("Color", function () { parserTest("BBGGRR", "&H3F171F&", "color", new libjass.tags.Color(31, 23, 63, 1)); diff --git a/test/tags.js b/tests/test-tags.js similarity index 92% rename from test/tags.js rename to tests/test-tags.js index 857668a..8b46aa3 100644 --- a/test/tags.js +++ b/tests/test-tags.js @@ -18,6 +18,9 @@ * limitations under the License. */ +var libjass = require("../libjass.js"); +var parserTest = require("./parser-test.js"); + suite("Tags", function () { suite("Bold tag - \\b", function () { parserTest("True", "b1", "tag_b", new libjass.tags.Bold(true)); @@ -60,7 +63,7 @@ suite("Tags", function () { parserTest("Enclosed tag", "{\\c&H3F171F&}", "enclosedTags", [new libjass.tags.PrimaryColor(new libjass.tags.Color(31, 23, 63, 1))]); - parserTest("Dialogue", "{\\c&H3F171F&}", "dialogue", [new libjass.tags.PrimaryColor(new libjass.tags.Color(31, 23, 63, 1))]); + parserTest("Dialogue", "{\\c&H3F171F&}", "dialogueParts", [new libjass.tags.PrimaryColor(new libjass.tags.Color(31, 23, 63, 1))]); }); suite("Alpha tag - \\alpha", function () {