(fn, transformFn, rowNum)
| 720 | } |
| 721 | |
| 722 | function testScript(fn, transformFn, rowNum) { |
| 723 | |
| 724 | function deindentFunc(fn) { |
| 725 | fn = (fn+''); |
| 726 | var indent = /(?:^|\n)([\t ]+)[^\n]+/.exec(fn); |
| 727 | if (indent) { |
| 728 | fn = fn.replace(new RegExp('\n' + indent[1], 'g'), '\n'); |
| 729 | } |
| 730 | return fn; |
| 731 | } |
| 732 | |
| 733 | if (!fn) { |
| 734 | return ''; |
| 735 | } |
| 736 | if (typeof fn === 'function') { |
| 737 | // see if the code is encoded in a comment |
| 738 | var expr = (fn+"").match(/[^]*\/\*([^]*)\*\/\}$/); |
| 739 | // if there wasn't an expression, make the function statement into one |
| 740 | if (!expr) { |
| 741 | if (transformFn) { |
| 742 | try { |
| 743 | expr = transformFn("("+fn+")"); |
| 744 | } catch(e) { |
| 745 | expr = "false"; |
| 746 | } |
| 747 | } else { |
| 748 | expr = deindentFunc(fn); |
| 749 | } |
| 750 | return cheerio.load('')('<script>test(\n' + expr + '())</script>').attr('data-source', expr); |
| 751 | } else { |
| 752 | expr = deindentFunc(expr[1]); |
| 753 | var transformed = false; |
| 754 | if (transformFn) { |
| 755 | try { |
| 756 | if (expr.search(/Function\s*\(|eval\s*\(/) > -1) { |
| 757 | throw new Error("This test's code invokes eval() and cannot be compiled."); |
| 758 | } |
| 759 | expr = transformFn("(function (){" + expr + "})"); |
| 760 | transformed = true; |
| 761 | } catch(e) { |
| 762 | expr = "/* Error during compilation: " + e.message + "*/"; |
| 763 | } |
| 764 | } |
| 765 | /* jshint unused: true */ // this appears unused, but removing it might break things. |
| 766 | var async = !!/asyncTestPassed/.exec(fn); |
| 767 | /* jshint unused: false */ |
| 768 | var codeString = JSON.stringify(expr).replace(/\\r/g,''); |
| 769 | var asyncFn = 'global.__asyncPassedFn && __asyncPassedFn("' + rowNum + '")'; |
| 770 | var strictAsyncFn = 'global.__strictAsyncPassedFn && __strictAsyncPassedFn("' + rowNum + '")'; |
| 771 | var funcString = |
| 772 | transformed ? '' + asyncFn + ' && eval(' + codeString + ')()' |
| 773 | : 'Function("asyncTestPassed",' + codeString + ')(asyncTestPassed)'; |
| 774 | var strictFuncString = |
| 775 | transformed ? '' + strictAsyncFn + ' && function (){"use strict";' + codeString + '}() && "Strict"' |
| 776 | : 'Function("asyncTestPassed","\'use strict\';"+' + codeString + ')(asyncTestPassed)'; |
| 777 | |
| 778 | return cheerio.load('')('<script>' + |
| 779 | 'test(function (){' |
no test coverage detected