-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added toc.html test, pulled out some code to share between prince/js-…
…test.html and toc.html
- Loading branch information
1 parent
305395c
commit e88609f
Showing
10 changed files
with
218 additions
and
1,926 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
For each .test element, compare DOM trees in | ||
.actual and .expected for "visual" equality (html elements and text content). | ||
Publish results to #overall. | ||
Note: Keep this code prince-safe! | ||
*/ | ||
(function(){ | ||
function normalize(value){ | ||
if( value ){ | ||
value.replace(/\s+/, ''); | ||
} | ||
return value; | ||
}; | ||
|
||
function normalizeChildren(children){ | ||
var normalized = []; | ||
for(var i = 0; i < children.length; i++) { | ||
var node = children[i]; | ||
if( !node ){ | ||
continue; | ||
} | ||
if( node.nodeType == 3 && node.nodeValue.match(/^\s*$/) ){ | ||
continue; | ||
} | ||
normalized.push(node); | ||
} | ||
return normalized; | ||
} | ||
|
||
function looksLike(actual, expected){ | ||
if( !actual && !expected ) { | ||
return true; | ||
} | ||
|
||
if( expected.hasChildNodes() ){ | ||
var expectedKids = normalizeChildren(expected.childNodes); | ||
var actualKids = normalizeChildren(actual.childNodes); | ||
for(var i = 0; i < expectedKids.length; i++){ | ||
if( !looksLike(actualKids[i], expectedKids[i]) ){ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
if( expected.nodeType !== actual.nodeType ){ | ||
return false; | ||
} | ||
|
||
if( expected.nodeValue && actual.nodeValue && expected.nodeValue !== actual.nodeValue ){ | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
console.log("\nColoring the tests..."); | ||
var tests = getElementsByClassName("test", "div", document); | ||
var failCount = 0; | ||
tests.forEach(function(test, i, arr){ | ||
var expected = getElementsByClassName("expected", null, test)[0]; | ||
var actual = getElementsByClassName("actual", null, test)[0]; | ||
if( looksLike(actual, expected) ){ | ||
test.className += " pass"; | ||
} | ||
else { | ||
failCount++; | ||
test.className += " fail"; | ||
} | ||
}); | ||
var overall = document.getElementById("overall"); | ||
var msg; | ||
if( failCount == 0 ){ | ||
msg = tests.length + " tests pass!"; | ||
overall.className = "pass"; | ||
} | ||
else { | ||
msg = failCount + " out of " + tests.length + " tests failed."; | ||
overall.className = "fail"; | ||
} | ||
var msgNode = document.createTextNode(msg); | ||
overall.appendChild(msgNode); | ||
console.log(msg); | ||
console.log("done."); | ||
})(); | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.