-
Notifications
You must be signed in to change notification settings - Fork 20
/
qunit-parameterize.js
138 lines (117 loc) · 3.52 KB
/
qunit-parameterize.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Parameterize v 0.4
* A QUnit Addon For Running Parameterized Tests
* https://github.com/AStepaniuk/qunit-parameterize
* Released under the MIT license.
*/
QUnit.extend(QUnit, {
cases : function(testCases) {
var currentCases = testCases;
var createTest = function(methodName, title, expected, callback, parameters) {
QUnit[methodName](
title,
expected,
function(assert) { return callback.call(this, parameters, assert); }
);
};
var iterateTestCases = function(methodName, title, expected, callback) {
if (!currentCases || currentCases.length == 0) {
// setup test which will always fail
QUnit.test(title, function(assert) {
assert.ok(false, "No test cases are provided");
});
return;
}
if (!callback) {
callback = expected;
expected = null;
}
for (var i = 0; i < currentCases.length; ++i) {
var methodNameString = methodName;
var parameters = currentCases[i];
var testCaseTitle = title;
if (parameters.title) {
testCaseTitle += "[" + parameters.title + "]";
}
if (parameters._skip === true) {
methodNameString = 'skip';
}
createTest(methodNameString, testCaseTitle, expected, callback, parameters);
}
}
var getLength = function(arr) {
return arr ? arr.length : 0;
}
var getItem = function(arr, idx) {
return arr ? arr[idx] : undefined;
}
var mix = function(testCase, mixData) {
if (testCase && mixData) {
var result = clone(testCase);
for(var p in mixData) {
if (p !== "title") {
if (!(p in result)) result[p] = mixData[p];
} else {
result[p] = [result[p], mixData[p]].join("");
}
}
return result;
} else if (testCase) {
return testCase;
} else if (mixData) {
return mixData;
} else {
// return null or undefined whatever testCase is
return testCase;
}
}
var clone = function(testCase) {
var result = {};
for (var p in testCase) {
result[p] = testCase[p];
}
return result;
}
return {
sequential : function(addData) {
var casesLength = getLength(currentCases);
var addDataLength = getLength(addData);
var length = casesLength > addDataLength ? casesLength : addDataLength;
var newCases = [];
for (var i = 0; i < length; ++i) {
var currentCaseI = getItem(currentCases, i);
var dataI = getItem(addData, i);
var newCase = mix(currentCaseI, dataI);
if (newCase) newCases.push(newCase);
}
currentCases = newCases;
return this;
},
combinatorial : function(mixData) {
var current = (currentCases && currentCases.length > 0) ? currentCases : [ null ];
mixData = (mixData && mixData.length > 0) ? mixData : [ null ];
var currentLength = current.length;
var mixDataLength = mixData.length;
var newCases = [];
for (var i = 0; i < currentLength; ++i) {
for(var j = 0; j < mixDataLength; ++j) {
var currentCaseI = current[i];
var dataJ = mixData[j];
var newCase = mix(currentCaseI, dataJ);
if (newCase) newCases.push(newCase);
}
}
currentCases = newCases;
return this;
},
test : function(title, expected, callback) {
iterateTestCases("test", title, expected, callback);
return this;
},
asyncTest : function(title, expected, callback) {
iterateTestCases("asyncTest", title, expected, callback);
return this;
}
}
}
});