-
Notifications
You must be signed in to change notification settings - Fork 20
/
qunit2-parameterize.js
172 lines (140 loc) · 3.81 KB
/
qunit2-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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* 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() {
'use strict';
var currentCases = null,
clone = function(testCase) {
var result = {},
p = null;
for (p in testCase) {
if (testCase.hasOwnProperty(p)) {
result[p] = testCase[p];
}
}
return result;
},
createTest = function(methodName, title, callback, parameters) {
QUnit[methodName](title, function(assert) {
return callback.call(this, parameters, assert);
});
},
iterateTestCases = function(methodName, title, callback) {
var i = 0,
parameters = null,
testCaseTitle = null;
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;
}
for (i = 0; i < currentCases.length; i += 1) {
parameters = currentCases[i];
testCaseTitle = title;
if (parameters.title) {
testCaseTitle += "[" + parameters.title + "]";
}
if (parameters._skip === true) {
methodName = 'skip';
}
createTest(methodName, testCaseTitle, callback, parameters);
}
},
getLength = function(arr) {
return arr ? arr.length : 0;
},
getItem = function(arr, idx) {
return arr ? arr[idx] : undefined;
},
mix = function(testCase, mixData) {
var result = null,
p = null;
if (testCase && mixData) {
result = clone(testCase);
for (p in mixData) {
if (mixData.hasOwnProperty(p)) {
if (p !== "title") {
if (!(result.hasOwnProperty(p))) {
result[p] = mixData[p];
}
} else {
result[p] = [result[p], mixData[p]].join("");
}
}
}
} else if (testCase) {
result = testCase;
} else if (mixData) {
result = mixData;
} else {
// return null or undefined whatever testCase is
result = testCase;
}
return result;
};
return {
init: function(testCasesList) {
currentCases = testCasesList;
return this;
},
sequential: function(addData) {
var casesLength = getLength(currentCases),
addDataLength = getLength(addData),
length = casesLength > addDataLength ? casesLength : addDataLength,
newCases = [],
i = 0,
currentCaseI = null,
dataI = null,
newCase = null;
for (i = 0; i < length; i += 1) {
currentCaseI = getItem(currentCases, i);
dataI = getItem(addData, i);
newCase = mix(currentCaseI, dataI);
if (newCase) {
newCases.push(newCase);
}
}
currentCases = newCases;
return this;
},
combinatorial: function(mixData) {
var current = (currentCases && currentCases.length > 0) ? currentCases : [null],
currentLength = current.length,
mixDataLength = 0,
newCases = [],
i = 0,
j = 0,
currentCaseI = null,
dataJ = null,
newCase = null;
mixData = (mixData && mixData.length > 0) ? mixData : [null];
mixDataLength = mixData.length;
for (i = 0; i < currentLength; i += 1) {
for (j = 0; j < mixDataLength; j += 1) {
currentCaseI = current[i];
dataJ = mixData[j];
newCase = mix(currentCaseI, dataJ);
if (newCase) {
newCases.push(newCase);
}
}
}
currentCases = newCases;
return this;
},
test: function(title, callback) {
iterateTestCases("test", title, callback);
return this;
},
getCurrentTestCases: function () {
return currentCases;
}
};
}())
});