/
array-tests-example.htm
299 lines (228 loc) · 10.9 KB
/
array-tests-example.htm
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Array Tests Example</title>
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script>
<script type="text/javascript" src="../javascript/build/yuitest/yuitest-min.js"></script>
<style type="text/css">
.yui3-skin-sam .yui3-console-entry-pass .yui3-console-entry-cat {
background-color: green;
color: #fff;
}
.yui3-skin-sam .yui3-console-entry-fail .yui3-console-entry-cat {
background-color: red;
color: #fff;
}
.yui3-skin-sam .yui3-console-entry-ignore .yui3-console-entry-cat {
background-color: #666;
}
</style>
<body class="yui3-skin-sam yui-skin-sam">
<h1>Array Tests Example</h1>
<p>This example shows how to use the <code>ArrayAssert</code> object, which contains assertions designed to be used specifically with JavaScript Arrays and array-like objects.</p>
<div id="testLogger"></div>
<script type="text/javascript">
YUI().use("console", function (Y) {
var arrayTestCase = new YUITest.TestCase({
//the name of the test case - if not provided, one is automatically generated
name: "Array Tests",
//-------------------------------------------------------------------------
// Setup and teardown
//-------------------------------------------------------------------------
/*
* The setUp() method is used to setup data that necessary for a test to
* run. This method is called immediately before each test method is run,
* so it is run as many times as there are test methods.
*/
setUp : function () {
this.data = new Array (0,1,2,3,4,5);
},
/*
* The tearDown() method is used to clean up the initialization that was done
* in the setUp() method. Ideally, it should free up all memory allocated in
* setUp(), anticipating any possible changes to the data. This method is called
* immediately after each test method is run.
*/
tearDown : function () {
delete this.data;
},
//-------------------------------------------------------------------------
// Basic tests - all method names must begin with "test"
//-------------------------------------------------------------------------
/*
* Test the push() method.
*/
testPush : function() {
//shortcut variables
var ArrayAssert = YUITest.ArrayAssert;
//do whatever data manipulation is necessary
this.data.push(6);
//array-specific assertions
ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");
ArrayAssert.contains(6, this.data, "Array should contain 6.");
ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6.");
//check that all the values are there
ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal.");
},
/*
* Test the pop() method.
*/
testPop : function() {
//shortcut variables
var Assert = YUITest.Assert;
var ArrayAssert = YUITest.ArrayAssert;
//do whatever data manipulation is necessary
var value = this.data.pop();
//array shouldn't be empty
ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");
//basic equality assertion - expected value, actual value, optional error message
Assert.areEqual(5, this.data.length, "Array should have 5 items.");
Assert.areEqual(5, value, "Value should be 5.");
ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal.");
},
/*
* Test the reverse() method.
*/
testReverse : function() {
//shortcut variables
var ArrayAssert = YUITest.ArrayAssert;
//do whatever data manipulation is necessary
this.data = this.data.reverse();
ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal.");
},
/*
* Test the shift() method.
*/
testShift : function() {
//shortcut variables
var Assert = YUITest.Assert;
var ArrayAssert = YUITest.ArrayAssert;
//do whatever data manipulation is necessary
var value = this.data.shift();
//array shouldn't be empty
ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");
//basic equality assertion - expected value, actual value, optional error message
Assert.areEqual(5, this.data.length, "Array should have 6 items.");
Assert.areEqual(0, value, "Value should be 0.");
ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal.");
},
/*
* Test the splice() method.
*/
testSplice : function() {
//shortcut variables
var Assert = YUITest.Assert;
var ArrayAssert = YUITest.ArrayAssert;
//do whatever data manipulation is necessary
var removed = this.data.splice(1, 2, 99, 100);
//basic equality assertion - expected value, actual value, optional error message
Assert.areEqual(6, this.data.length, "Array should have 6 items.");
//the new items should be there
ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99.");
ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100.");
ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal.");
ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2.");
},
/*
* Test the unshift() method.
*/
testUnshift : function() {
//shortcut variables
var Assert = YUITest.Assert;
var ArrayAssert = YUITest.ArrayAssert;
//do whatever data manipulation is necessary
this.data.unshift(-1);
//basic equality assertion - expected value, actual value, optional error message
Assert.areEqual(7, this.data.length, "Array should have 7 items.");
//the new item should be there
ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1.");
ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal.");
}
});
//create the console
var r = new Y.Console({
newestOnTop : false,
style: 'block' // to anchor in the example content
});
r.render('#testLogger');
var TestRunner = YUITest.TestRunner;
TestRunner.add(arrayTestCase);
//function to handle events generated by the testrunner
function logEvent(event){
//data variables
var message = "",
messageType = "";
switch(event.type){
case TestRunner.BEGIN_EVENT:
message = "Testing began at " + (new Date()).toString() + ".";
messageType = "info";
break;
case TestRunner.COMPLETE_EVENT:
message = Y.substitute("Testing completed at " +
(new Date()).toString() + ".\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
case TestRunner.TEST_FAIL_EVENT:
message = event.testName + ": failed.\n" + event.error.getMessage();
messageType = "fail";
break;
case TestRunner.TEST_IGNORE_EVENT:
message = event.testName + ": ignored.";
messageType = "ignore";
break;
case TestRunner.TEST_PASS_EVENT:
message = event.testName + ": passed.";
messageType = "pass";
break;
case TestRunner.TEST_SUITE_BEGIN_EVENT:
message = "Test suite \"" + event.testSuite.name + "\" started.";
messageType = "info";
break;
case TestRunner.TEST_SUITE_COMPLETE_EVENT:
message = Y.substitute("Test suite \"" +
event.testSuite.name + "\" completed" + ".\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
case TestRunner.TEST_CASE_BEGIN_EVENT:
message = "Test case \"" + event.testCase.name + "\" started.";
messageType = "info";
break;
case TestRunner.TEST_CASE_COMPLETE_EVENT:
message = Y.substitute("Test case \"" +
event.testCase.name + "\" completed.\n" +
"Passed:{passed} Failed:{failed} " +
"Total:{total} ({ignored} ignored)",
event.results);
messageType = "info";
break;
default:
message = "Unexpected event " + event.type;
message = "info";
}
//only log if required
Y.log(message, messageType, "TestRunner");
}
//listen for events to publish to the logger
TestRunner.attach(TestRunner.BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_CASE_BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_CASE_COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_SUITE_BEGIN_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_SUITE_COMPLETE_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_PASS_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_FAIL_EVENT, logEvent);
TestRunner.attach(TestRunner.TEST_IGNORE_EVENT, logEvent);
//run the tests
TestRunner.run();
});
</script>
</body>
</html>