metric-imperial-converter/assertion-analyser.js
Rex Schrader 87ebc9c36a
Fix (Boilerplate): Add Event Parameter to test, Remove Infosec Requriements, Add Lowercase Requirement (#12)
* Fix missing event, correct example conversion

* Fix unit case

* Remove Infosec items, add lowercase requirement

* Convert to ES6, Update Packages, Fix display

* Applying @nhcarrigan's changes from site tests

* Removed unneeded mongodb ref

* Remove User Stories, Reformat README

* Apply suggestions from code review - Remove "Quality Assurance Project" Prefix

Co-authored-by: Shaun Hamilton <51722130+Sky020@users.noreply.github.com>

* Re-add the example text, clean up formatting

* Update Favicon

* Fix repo link, remove zombie.js

* Remove unused files, add sample.env

Co-authored-by: Shaun Hamilton <51722130+Sky020@users.noreply.github.com>
Co-authored-by: Rex Schrader <rex.schader@gmail.com>
2020-11-25 09:09:04 -06:00

140 lines
3.1 KiB
JavaScript

/*
*
*
*
*
*
*
*
*
*
*
*
* DO NOT EDIT THIS FILE
* For FCC testing purposes!
*
*
*
*
*
*
*
*
*
*
*
*/
function objParser(str, init) {
// finds objects, arrays, strings, and function arguments
// between parens, because they may contain ','
let openSym = ['[', '{', '"', "'", '('];
let closeSym = [']', '}', '"', "'", ')'];
let type;
let i;
for(i = (init || 0); i < str.length; i++ ) {
type = openSym.indexOf(str[i]);
if( type !== -1) break;
}
if (type === -1) return null;
let open = openSym[type];
let close = closeSym[type];
let count = 1;
let k;
for(k = i+1; k < str.length; k++) {
if(open === '"' || open === "'") {
if(str[k] === close) count--;
if(str[k] === '\\') k++;
} else {
if(str[k] === open) count++;
if(str[k] === close) count--;
}
if(count === 0) break;
}
if(count !== 0) return null;
let obj = str.slice(i, k+1);
return {
start : i,
end: k,
obj: obj
};
}
function replacer(str) {
// replace objects with a symbol ( __#n)
let obj;
let cnt = 0;
let data = [];
while(obj = objParser(str)) {
data[cnt] = obj.obj;
str = str.substring(0, obj.start) + '__#' + cnt++ + str.substring(obj.end+1)
}
return {
str : str,
dictionary : data
}
}
function splitter(str) {
// split on commas, then restore the objects
let strObj = replacer(str);
let args = strObj.str.split(',');
args = args.map(function(a){
let m = a.match(/__#(\d+)/);
while (m) {
a = a.replace(/__#(\d+)/, strObj.dictionary[m[1]]);
m = a.match(/__#(\d+)/);
}
return a.trim();
})
return args;
}
function assertionAnalyser(body) {
// already filtered in the test runner
// // remove comments
// body = body.replace(/\/\/.*\n|\/\*.*\*\//g, '');
// // get test function body
// body = body.match(/\{\s*([\s\S]*)\}\s*$/)[1];
if(!body) return "invalid assertion";
// replace assertions bodies, so that they cannot
// contain the word 'assertion'
let cleanedBody = body.match(/(?:browser\s*\.\s*)?assert\s*\.\s*\w*\([\s\S]*\)/)
if(cleanedBody && Array.isArray(cleanedBody)) {
body = cleanedBody[0];
} else {
// No assertions present
return [];
}
let s = replacer(body);
// split on 'assertion'
let splittedAssertions = s.str.split('assert');
let assertions = splittedAssertions.slice(1);
// match the METHODS
let assertionBodies = [];
let methods = assertions.map(function(a, i){
let m = a.match(/^\s*\.\s*(\w+)__#(\d+)/);
assertionBodies.push(parseInt(m[2]));
let pre = splittedAssertions[i].match(/browser\s*\.\s*/) ? 'browser.' : '';
return pre + m[1];
});
if(methods.some(function(m){ return !m })) return "invalid assertion";
// remove parens from the assertions bodies
let bodies = assertionBodies.map(function(b){
return s.dictionary[b].slice(1,-1).trim();
});
assertions = methods.map(function(m, i) {
return {
method: m,
args: splitter(bodies[i]) //replace objects, split on ',' ,then restore objects
}
})
return assertions;
}
module.exports = assertionAnalyser;