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
This commit is contained in:
Emily Eisenberg 2014-09-12 13:30:30 -07:00
parent 5cca3a299e
commit 403dca64ab
8 changed files with 62 additions and 16 deletions

View File

@ -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

View File

@ -708,7 +708,7 @@ var buildTree = function(tree) {
makeSpan(["katex-inner"], [topStrut, bottomStrut, span])
]);
return katexNode.toDOM();
return katexNode;
};
module.exports = buildTree;

View File

@ -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);
};

View File

@ -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"
}
}

View File

@ -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({});

View File

@ -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");
});
});

View File

@ -5,7 +5,7 @@
<script src="jasmine/jasmine-html.js"></script>
<script src="jasmine/boot.js"></script>
<link rel="stylesheet" href="jasmine/jasmine.css">
<script src="katex-tests.js"></script>
<script src="katex-spec.js"></script>
</head>
<body>
</body>

View File

@ -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) {