Files
freeCodeCamp/client/commonFramework/add-test-to-string.js

39 lines
877 B
JavaScript
Raw Normal View History

2015-11-17 21:25:16 -08:00
window.common = (function({ common = { init: [] }}) {
var BDDregex = new RegExp(
'(expect(\\s+)?\\(.*\\;)|' +
'(assert(\\s+)?\\(.*\\;)|' +
'(assert\\.\\w.*\\;)|' +
'(.*\\.should\\..*\\;)/'
);
common.addTestsToString = function({ code, tests = [], ...rest }) {
2015-11-17 21:25:16 -08:00
const userTests = [];
2015-11-21 14:44:33 -08:00
code = tests.reduce((code, test) => code + test + '\n', code + '\n');
2015-11-17 21:25:16 -08:00
var counter = 0;
var match = BDDregex.exec(code);
while (match) {
var replacement = '//' + counter + common.salt;
code = code.substring(0, match.index) +
replacement +
code.substring(match.index + match[0].length);
userTests.push({
err: null,
text: match[0],
line: counter
});
counter++;
match = BDDregex.exec(code);
}
return { ...rest, code, userTests };
2015-11-17 21:25:16 -08:00
};
return common;
}(window));