Skip to content

Instantly share code, notes, and snippets.

@toolness
Created April 9, 2012 16:01
Show Gist options
  • Save toolness/2344431 to your computer and use it in GitHub Desktop.
Save toolness/2344431 to your computer and use it in GitHub Desktop.
slowparse testing spec, take 1
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test Suite</title>
<link rel="stylesheet" href="qunit.css">
<h1 id="qunit-header">QUnit example</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"></div>
<script src="qunit.js"></script>
<script src="../src/slowparse.js"></script>
<script>
module("General");
test("parsing of valid HTML", function() {
var html = '<p class="foo">hello there</p>';
var doc = Slowparse.HTML(document, html).document;
var p = doc.childNodes[0];
var textNode = p.childNodes[0];
var attr = p.attributes[0];
equal(p.nodeName, "P", "first child of generated DOM is <p>");
ok('parseInfo' in p, "<p> has 'parseInfo' expando property");
equal(p.parseInfo.start, 0, "<p> starts at beginning of HTML string");
equal(p.parseInfo.end, html.length, "<p> ends at end of HTML string");
equal(textNode.nodeType, textNode.TEXT_NODE, "<p>'s child is a text node.");
ok('parseInfo' in textNode, "text node has 'parseInfo' expando property");
equal(html.slice(textNode.parseInfo.start, textNode.parseInfo.end),
"hello there",
"parseInfo.start/end positions are correct");
equal('parseInfo' in attr, "attr node has 'parseInfo' expando property");
equal(html.slice(attr.parseInfo.name.start, attr.parseInfo.name.end),
"class",
"parseInfo.name.start/end positions are correct");
equal(html.slice(attr.parseInfo.value.start, attr.parseInfo.value.end),
'"foo"',
"parseInfo.value.start/end positions are correct");
equal(outerHTML(doc), html,
"serialization of generated DOM matches original HTML");
});
test("parsing of invalid HTML", function() {
var html = '<p class="foo">hello there';
var result = Slowparse.HTML(document, html);
var error = result.error;
var p = result.document.childNodes[0];
equal(p.nodeName, "P", "first child of generated DOM is <p>");
equal(error.type, "UNCLOSED_TAG", "parser dies b/c of unclosed tag");
equal(error.position, html.length, "parser dies at end of string");
equal(error.node, p, "affiliated node of error is <p>");
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment