Skip to content

Commit

Permalink
Finished up test harnesses for node and browser
Browse files Browse the repository at this point in the history
  • Loading branch information
tautologistics committed Apr 21, 2012
1 parent 3d23611 commit 6b775db
Show file tree
Hide file tree
Showing 9 changed files with 623 additions and 543 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2010, 2011, Chris Winberry <chris@winberry.net>. All rights reserved. Copyright 2010 - 2012, Chris Winberry <chris@winberry.net>. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the deal in the Software without restriction, including without limitation the
Expand Down
28 changes: 9 additions & 19 deletions lib/htmlparser.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,28 +22,18 @@ IN THE SOFTWARE.


(function () { (function () {


function runningInNode () { var exports;
return( if (typeof(module) !== 'undefined' && typeof(module.exports) !== 'undefined') {
(typeof require) == "function" exports = module.exports;
&& } else {
(typeof exports) == "object" exports = {};
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!runningInNode()) {
if (!this.Tautologistics) { if (!this.Tautologistics) {
this.Tautologistics = {}; this.Tautologistics = {};
} else if (this.Tautologistics.NodeHtmlParser) {
return; //NodeHtmlParser already defined!
} }
this.Tautologistics.NodeHtmlParser = {}; if (this.Tautologistics.NodeHtmlParser) {
exports = this.Tautologistics.NodeHtmlParser; return;
}
this.Tautologistics.NodeHtmlParser = exports;
} }


function inherits (ctor, superCtor) { function inherits (ctor, superCtor) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
{ {
"name": "htmlparser" "name": "htmlparser"
, "description": "Forgiving HTML/XML/RSS Parser in JS for *both* Node and Browsers" , "description": "Forgiving HTML/XML/RSS Parser in JS for *both* Node and Browsers"
, "version": "1.7.6" , "version": "2.0.0"
, "author": "Chris Winberry <chris@winberry.net>" , "author": "Chris Winberry <chris@winberry.net>"
, "contributors": [] , "contributors": []
, "repository": { , "repository": {
Expand All @@ -25,7 +25,7 @@
} }
, "directories": { "lib": "./lib/" } , "directories": { "lib": "./lib/" }
, "main": "./lib/htmlparser" , "main": "./lib/htmlparser"
, "engines": { "node": ">=0.1.33" } , "engines": { "node": ">=0.5.0" }
, "licenses": [{ , "licenses": [{
"type": "MIT" "type": "MIT"
, "url": "http://github.com/tautologistics/node-htmlparser/raw/master/LICENSE" , "url": "http://github.com/tautologistics/node-htmlparser/raw/master/LICENSE"
Expand Down
285 changes: 208 additions & 77 deletions runtests.html
Original file line number Original file line Diff line number Diff line change
@@ -1,13 +1,37 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright 2010 - 2012, Chris Winberry <chris@winberry.net>. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-->

<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Node.js HTML Parser</title> <title>Node.js HTML Parser</title>
<style type="text/css"> <style type="text/css">
.good { .testName {
font-weight: bold;
}
.pass {
color: #363; color: #363;
} }
.bad { .fail {
color: #633; color: #633;
font-style: italic; font-style: italic;
} }
Expand All @@ -22,87 +46,194 @@
} }
</script> </script>
<script language="JavaScript" src="lib/htmlparser.js"></script> <script language="JavaScript" src="lib/htmlparser.js"></script>
<script language="JavaScript" src="tests/01-basic.js"></script> <script language="JavaScript" src="tests/testutils.js"></script>
<script language="JavaScript" src="tests/02-single_tag_1.js"></script> <script language="JavaScript" src="tests/html.js"></script>
<script language="JavaScript" src="tests/03-single_tag_2.js"></script> <script language="JavaScript" src="tests/parser.js"></script>
<script language="JavaScript" src="tests/04-unescaped_in_script.js"></script> <script language="JavaScript" src="tests/rss.js"></script>
<script language="JavaScript" src="tests/05-tags_in_comment.js"></script>
<script language="JavaScript" src="tests/06-comment_in_script.js"></script>
<script language="JavaScript" src="tests/07-unescaped_in_style.js"></script>
<script language="JavaScript" src="tests/08-extra_spaces_in_tag.js"></script>
<script language="JavaScript" src="tests/09-unquoted_attrib.js"></script>
<script language="JavaScript" src="tests/10-singular_attribute.js"></script>
<script language="JavaScript" src="tests/11-text_outside_tags.js"></script>
<script language="JavaScript" src="tests/12-text_only.js"></script>
<script language="JavaScript" src="tests/13-comment_in_text.js"></script>
<script language="JavaScript" src="tests/14-comment_in_text_in_script.js"></script>
<script language="JavaScript" src="tests/15-non-verbose.js"></script>
<script language="JavaScript" src="tests/16-ignore_whitespace.js"></script>
<script language="JavaScript" src="tests/17-xml_namespace.js"></script>
<script language="JavaScript" src="tests/18-enforce_empty_tags.js"></script>
<script language="JavaScript" src="tests/19-ignore_empty_tags.js"></script>
<script language="JavaScript" src="tests/20-rss.js"></script>
<script language="JavaScript" src="tests/21-atom.js"></script>
<script language="JavaScript" src="tests/22-position_data.js"></script>
<!-- //TODO: dynamic loading of test files --> <!-- //TODO: dynamic loading of test files -->
</head> </head>
<body style="font-size: small; font-family:Arial, Helvetica, sans-serif;"> <body style="font-size: small; font-family:Arial, Helvetica, sans-serif;">


<script language="JavaScript"> <script language="JavaScript">
var chunkSize = 5; (function () {
var testCount = 0;
var failedCount = 0; function writeTest (name, result) {
while (Tautologistics.NodeHtmlParser.Tests.length) { document.write(
testCount++; "<div>" +
var test = Tautologistics.NodeHtmlParser.Tests.shift(); "<span class='testName'>" + name + "</span>: " +
try { "<span class='" + (result ? 'pass' : 'fail') + "'>" + (result ? 'passed' : 'FAILED') + "</span>" +
var handlerCallback = function handlerCallback (error) { "</div>"
if (error) );
document.write("<hr>Handler error: " + error + "<hr>");
}
var handler = (test.type == "rss") ?
new Tautologistics.NodeHtmlParser.RssHandler(handlerCallback, test.options.handler)
:
new Tautologistics.NodeHtmlParser.DefaultHandler(handlerCallback, test.options.handler)
;
var parser = new Tautologistics.NodeHtmlParser.Parser(handler, test.options.parser);
document.write("<b>" + test.name + "</b>: ");
parser.parseComplete(test.html);
var resultComplete = handler.dom;
var chunkPos = 0;
parser.reset();
while (chunkPos < test.html.length) {
parser.parseChunk(test.html.substring(chunkPos, chunkPos + chunkSize));
chunkPos += chunkSize;
}
parser.done();
var resultChunk = handler.dom;
var testResult =
JSON.stringify(resultComplete).toString() === JSON.stringify(test.expected).toString()
&&
JSON.stringify(resultChunk).toString() === JSON.stringify(test.expected).toString()
;
document.write(testResult ? "<font class='good'>passed</font>" : "<font class='bad'>FAILED</font>");
if (!testResult) {
failedCount++;
document.write("<pre>");
document.write("<b>Complete</b>\n");
document.write(JSON.stringify(resultComplete, null, 2));
document.write("<b>Chunked</b>\n");
document.write(JSON.stringify(resultChunk, null, 2));
document.write("<h2>Expected</h2>\n");
document.write(JSON.stringify(test.expected, null, 2));
document.write("</pre>");
}
} catch (ex) {
document.write("<h1>Exception occured during test: " + ex + "</h1>")
}
document.write("<br>");
} }
document.write("<hr>");
document.write("Total tests: " + testCount + "<br>"); var testResults = {};
document.write("Failed tests: " + failedCount + "<br>");
Tautologistics.NodeHtmlParser.Tests.Utils.runBuilderTests(
Tautologistics.NodeHtmlParser.Tests.Html
, Tautologistics.NodeHtmlParser.Parser
, Tautologistics.NodeHtmlParser.HtmlBuilder
, null
, function (testName, testResult, actual, expected) {
// console.log("[" + testName + "]: " + (testResult ? "passed" : "FAILED"));
writeTest(testName, testResult);
}, function (elapsed, passed, failed) {
testResults['HTML builder'] = {
elapsed: elapsed
, passed: passed
, failed: failed
};
});
Tautologistics.NodeHtmlParser.Tests.Utils.runBuilderTests(
Tautologistics.NodeHtmlParser.Tests.Html
, Tautologistics.NodeHtmlParser.Parser
, Tautologistics.NodeHtmlParser.HtmlBuilder
, function (test) {
var newTest = {};
for (var key in test) {
if (!test.hasOwnProperty(key)) {
continue;
}
newTest[key] = (key === 'data') ?
test.data.join('').split('')
:
test[key]
;
}
return newTest;
}
, function (testName, testResult, actual, expected) {
// console.log("[" + testName + "]: " + (testResult ? "passed" : "FAILED"));
writeTest(testName, testResult);
}, function (elapsed, passed, failed) {
testResults['HTML builder (streamed)'] = {
elapsed: elapsed
, passed: passed
, failed: failed
};
});

Tautologistics.NodeHtmlParser.Tests.Utils.runBuilderTests(
Tautologistics.NodeHtmlParser.Tests.Rss
, Tautologistics.NodeHtmlParser.Parser
, Tautologistics.NodeHtmlParser.RssBuilder
, null
, function (testName, testResult, actual, expected) {
// console.log("[" + testName + "]: " + (testResult ? "passed" : "FAILED"));
writeTest(testName, testResult);
}, function (elapsed, passed, failed) {
testResults['RSS builder'] = {
elapsed: elapsed
, passed: passed
, failed: failed
};
});
Tautologistics.NodeHtmlParser.Tests.Utils.runBuilderTests(
Tautologistics.NodeHtmlParser.Tests.Rss
, Tautologistics.NodeHtmlParser.Parser
, Tautologistics.NodeHtmlParser.RssBuilder
, function (test) {
var newTest = {};
for (var key in test) {
if (!test.hasOwnProperty(key)) {
continue;
}
newTest[key] = (key === 'data') ?
test.data.join('').split('')
:
test[key]
;
}
return newTest;
}
, function (testName, testResult, actual, expected) {
// console.log("[" + testName + "]: " + (testResult ? "passed" : "FAILED"));
writeTest(testName, testResult);
}, function (elapsed, passed, failed) {
testResults['RSS builder (streamed)'] = {
elapsed: elapsed
, passed: passed
, failed: failed
};
});

Tautologistics.NodeHtmlParser.Tests.Utils.runParserTests(
Tautologistics.NodeHtmlParser.Tests.Parser
, Tautologistics.NodeHtmlParser.Parser
, null
, function (testName, testResult, actual, expected) {
// console.log("[" + testName + "]: " + (testResult ? "passed" : "FAILED"));
writeTest(testName, testResult);
}, function (elapsed, passed, failed) {
testResults['Parser'] = {
elapsed: elapsed
, passed: passed
, failed: failed
};
});
Tautologistics.NodeHtmlParser.Tests.Utils.runParserTests(
Tautologistics.NodeHtmlParser.Tests.Parser
, Tautologistics.NodeHtmlParser.Parser
, function (test) {
var newTest = {};
for (var key in test) {
if (!test.hasOwnProperty(key)) {
continue;
}
newTest[key] = (key === 'data') ?
test.data.join('').split('')
:
test[key]
;
}
return newTest;
}
, function (testName, testResult, actual, expected) {
// console.log("[" + testName + "]: " + (testResult ? "passed" : "FAILED"));
writeTest(testName, testResult);
}, function (elapsed, passed, failed) {
testResults['Parser (streamed)'] = {
elapsed: elapsed
, passed: passed
, failed: failed
};
});

document.write(
"<br>" +
'<h1>Test Results</h1>' +
'<hr>'
);
var passedTotal = 0;
var failedTotal = 0;
var elapsedTotal = 0;
for (var testName in testResults) {
if (!testResults.hasOwnProperty(testName)) {
continue;
}
var test = testResults[testName];
passedTotal += test.passed;
failedTotal += test.failed;
elapsedTotal += test.elapsed;
document.write(
"<div>" +
"<span class='testName'>" + testName + '</span>: ' +
"<span class='" + (test.failed === 0 ? 'pass' : 'fail') + "'>" +
test.passed + '/' + (test.passed + test.failed) +
' (' + Math.round(test.passed / (test.passed + test.failed) * 100) + '%) ' +
'[' + test.elapsed + 'ms]' +
"</span>" +
"</div>"
);
}
document.write(
'<hr>' +
'Total: ' + passedTotal + '/' + (passedTotal + failedTotal) +
' (' + Math.round(passedTotal / (passedTotal + failedTotal) * 100) + '%) ' +
'[' + elapsedTotal + 'ms]'
);

})();
</script> </script>

</body> </body>
</html> </html>
Loading

0 comments on commit 6b775db

Please sign in to comment.