Last active
December 20, 2015 16:49
-
-
Save mokkabonna/6163981 to your computer and use it in GitHub Desktop.
Client side html doc for jstd-karma adapter
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
/** | |
* A wrapper around the testcase and setup/teardown methods that parses the function first and inserts HTML into the page or the scope | |
* This is according to the jstestdriver HTML doc feature https://code.google.com/p/js-test-driver/wiki/HtmlDoc | |
*/ | |
function createFunc(test) { | |
var commentMatches = test.toString().match(/\/\*[^*]*\*+(?:[^*\/][^*]*\*+)*\//g); //finds all comment blocks in the function | |
return function() { | |
var self = this; | |
if (commentMatches !== null) { //if we have any comment blocks loop each | |
commentMatches.forEach(function(match) { | |
var internalMatches = match.match(/(\/\*\:\s*DOC\s*)([^\s\+]*)\s*\=\s+(([^*\/][^*]*\*+)*).*/); //find all instances of comment blocks that uses the format: element = HTML syntax | |
var appendMatches = match.match(/(\/\*\:\s*DOC\s*)\s*\+\=\s+([^\*]+)\*\//); //find all instances of comment blocks that uses the format: += HTML for appending to the document.body | |
if (internalMatches !== null) { | |
var element = document.createElement('div'); | |
element.innerHTML = internalMatches[3]; //first create a wrapper div and insert the HTML into that one | |
self[internalMatches[2]] = element.children[0]; //then assign the first child to the self context with the name given (like elementName = HTML) | |
} else if (appendMatches !== null) { | |
document.body.innerHTML = appendMatches[2]; //append the HTML to the body if it uses the syntax += HTML | |
} | |
}); | |
} | |
//finally apply the original function with the original context as well as the potentially added elements | |
test.apply(this, arguments); | |
}; | |
} | |
//this bottom part is probably not needed as verbose as here, here also other things are done like prepending the name with test and creating a sinon sandbox. Should work fine with a simpler process. The createFunc above is the important part. | |
window.testCase = function(name, tests) { | |
var jstdCase = {}, | |
jstdTestCase = window.TestCase, prop; | |
for (prop in tests) { | |
if (tests.hasOwnProperty(prop)) { | |
if (!/(^(setUp|tearDown)$)|^test/.test(prop) && typeof tests[prop] === "function") { | |
jstdCase["test " + prop] = createFunc(tests[prop]); | |
} else { | |
jstdCase[prop] = createFunc(tests[prop]); | |
} | |
} | |
} | |
jstdTestCase(name, sinon.testCase(jstdCase)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work! I am not sure how to implement it thought. Could you help me out?