From 403dca64ab2f3379286032e7c7d44dd6160756e1 Mon Sep 17 00:00:00 2001 From: Emily Eisenberg Date: Fri, 12 Sep 2014 13:30:30 -0700 Subject: [PATCH] Improve testing Summary: Move dom creation into katex.js so our tests can test non-dom things, and add some buildTree tests. Add some checks make utils.js work in node. Add support for jasmine-node, to allow for command line unit testing. Test Plan: - Make sure tests work, in both the browser and with `make test` - Make sure huxley screenshots didn't change Reviewers: alpert Reviewed By: alpert Differential Revision: http://phabricator.khanacademy.org/D13125 --- Makefile | 5 +++- buildTree.js | 2 +- katex.js | 2 +- package.json | 6 ++++- server.js | 4 +-- test/{katex-tests.js => katex-spec.js} | 37 ++++++++++++++++++++++++++ test/test.html | 2 +- utils.js | 20 +++++++------- 8 files changed, 62 insertions(+), 16 deletions(-) rename test/{katex-tests.js => katex-spec.js} (95%) diff --git a/Makefile b/Makefile index f47544fc8..a56414f65 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build setup copy serve clean metrics +.PHONY: build setup copy serve clean metrics test build: setup build/katex.min.js build/katex.min.css compress setup: @@ -27,6 +27,9 @@ compress: build/katex.min.js build/katex.min.css serve: node server.js +test: + ./node_modules/.bin/jasmine-node test/katex-spec.js + metrics: cd metrics && ./mapping.pl | ./extract_tfms.py | ./replace_line.py diff --git a/buildTree.js b/buildTree.js index 3a5f5d2db..589ee298b 100644 --- a/buildTree.js +++ b/buildTree.js @@ -708,7 +708,7 @@ var buildTree = function(tree) { makeSpan(["katex-inner"], [topStrut, bottomStrut, span]) ]); - return katexNode.toDOM(); + return katexNode; }; module.exports = buildTree; diff --git a/katex.js b/katex.js index 8df0100ea..5f0a660f5 100644 --- a/katex.js +++ b/katex.js @@ -8,7 +8,7 @@ var process = function(toParse, baseNode) { utils.clearNode(baseNode); var tree = parseTree(toParse); - var node = buildTree(tree); + var node = buildTree(tree).toDOM(); baseNode.appendChild(node); }; diff --git a/package.json b/package.json index c8c798458..c815c7353 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,10 @@ "less": "~1.4.2", "uglify-js": "~2.4.15", "clean-css": "~2.2.15", - "huxley": "~0.7.4" + "huxley": "~0.7.4", + "jasmine-node": "git://github.com/mhevery/jasmine-node.git#Jasmine2.0" + }, + "scripts": { + "test": "make test" } } diff --git a/server.js b/server.js index 2a98c6f0a..13bae7c20 100644 --- a/server.js +++ b/server.js @@ -41,9 +41,9 @@ app.get("/katex.css", function(req, res, next) { }); }); -app.get("/test/katex-tests.js", function(req, res, next) { +app.get("/test/katex-spec.js", function(req, res, next) { var b = browserify(); - b.add("./test/katex-tests"); + b.add("./test/katex-spec"); var stream = b.bundle({}); diff --git a/test/katex-tests.js b/test/katex-spec.js similarity index 95% rename from test/katex-tests.js rename to test/katex-spec.js index 4c0d4fc9e..4caae2fe8 100644 --- a/test/katex-tests.js +++ b/test/katex-spec.js @@ -2,6 +2,15 @@ var buildTree = require("../buildTree"); var parseTree = require("../parseTree"); var ParseError = require("../ParseError"); +var getBuilt = function(expr) { + expect(expr).toBuild(); + + var built = buildTree(parseTree(expr)); + + // Remove the outer .katex and .katex-inner layers + return built.children[0].children[2].children; +}; + beforeEach(function() { jasmine.addMatchers({ toParse: function() { @@ -949,3 +958,31 @@ describe("A style change parser", function() { expect(displayBody[0].value).toMatch("e"); }); }); + +describe("A bin builder", function() { + it("should create mbins normally", function() { + var built = getBuilt("x + y"); + + expect(built[1].classes).toContain("mbin"); + }); + + it("should create ords when at the beginning of lists", function() { + var built = getBuilt("+ x"); + + expect(built[0].classes).toContain("mord"); + expect(built[0].classes).not.toContain("mbin"); + }); + + it("should create ords after some other objects", function() { + expect(getBuilt("x + + 2")[2].classes).toContain("mord"); + expect(getBuilt("( + 2")[1].classes).toContain("mord"); + expect(getBuilt("= + 2")[1].classes).toContain("mord"); + expect(getBuilt("\\sin + 2")[1].classes).toContain("mord"); + expect(getBuilt(", + 2")[1].classes).toContain("mord"); + }); + + it("should correctly interact with color objects", function() { + expect(getBuilt("\\blue{x}+y")[1].classes).toContain("mbin"); + expect(getBuilt("\\blue{x+}+y")[1].classes).toContain("mord"); + }); +}); diff --git a/test/test.html b/test/test.html index 4087fd5fe..713c56727 100644 --- a/test/test.html +++ b/test/test.html @@ -5,7 +5,7 @@ - + diff --git a/utils.js b/utils.js index cc9325114..0aa1cc6bf 100644 --- a/utils.js +++ b/utils.js @@ -21,15 +21,17 @@ var contains = function(list, elem) { var setTextContent; -var testNode = document.createElement("span"); -if ("textContent" in testNode) { - setTextContent = function(node, text) { - node.textContent = text; - }; -} else { - setTextContent = function(node, text) { - node.innerText = text; - }; +if (typeof document !== "undefined") { + var testNode = document.createElement("span"); + if ("textContent" in testNode) { + setTextContent = function(node, text) { + node.textContent = text; + }; + } else { + setTextContent = function(node, text) { + node.innerText = text; + }; + } } function clearNode(node) {