forked from cBioPortal/cbioportal-frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestWriter.js
97 lines (81 loc) · 2.36 KB
/
testWriter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import $ from 'jquery';
window._trCountMap = {};
function testObj(me) {
Object.assign(this, me);
}
testObj.prototype.writeTest = function() {
console.log(_formatTest(this));
};
window._handleTestReports = function(args, func, params, context, funcName) {
window._tr = window._tr || {};
var name = funcName || func.name;
var paramArr = /\(/.test(params)
? params.replace(/[()]/g, '').split(',')
: null;
var argArr = Array.from(args);
var argMap = argArr.map((a, i) => JSON.stringify(a));
window._trCountMap[name] =
name in window._trCountMap ? window._trCountMap[name]++ : 0;
let ret = func.apply(context, args);
// if there are no constraints or we asre within constraints
window._tr[name] = window._tr[name] || [];
if (
paramArr === null ||
(window._trCountMap[name] >= paramArr[0] &&
window._trCountMap[name] <= paramArr[1])
) {
window._tr[name].push(
new testObj({
argMap: argMap,
args: args,
ret: ret,
name: funcName || func.name,
fn: func,
argNames: func
.toString()
.match(/\((.*)\)/)[1]
.split(', '),
})
);
}
return ret;
};
window._writeTest = function(name, argJSON, retJSON) {
showTest(formatTest(name, argJSON, retJSON));
};
window._formatTest = function(report) {
let argDeclarations = '';
if (report.argMap.length > 0) {
argDeclarations = `
${report.argNames.reduce(
(s, a, i) => (s += 'let ' + a + ' = ' + report.argMap[i] + ';\n\n'),
''
)}
`;
}
return `
describe('${report.name}', ()=>{
it('###should do something###',()=>{
${argDeclarations}
const ret = ${report.name}(${report.argNames
.map((n, i) => n)
.join(', ')});
const expectedResult = ${JSON.stringify(report.ret)};
assert.equal(ret, expectedResult);
})
});
`;
};
window._showTest = function showTest(txt) {
$('<textarea/>')
.css({
position: 'absolute',
left: 200,
right: 200,
top: 100,
bottom: 100,
zIndex: 100,
})
.val(txt)
.appendTo('body');
};