|
1 <!DOCTYPE html> |
|
2 <html lang="en"> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Example: Array Processing</title> |
|
6 <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
7 <link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
8 <link rel="stylesheet" href="../assets/css/main.css"> |
|
9 <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
10 <link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
11 <script src="../../build/yui/yui-min.js"></script> |
|
12 |
|
13 </head> |
|
14 <body> |
|
15 <!-- |
|
16 <a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
17 --> |
|
18 <div id="doc"> |
|
19 <div id="hd"> |
|
20 <h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
21 </div> |
|
22 |
|
23 <a href="#toc" class="jump">Jump to Table of Contents</a> |
|
24 |
|
25 |
|
26 <h1>Example: Array Processing</h1> |
|
27 <div class="yui3-g"> |
|
28 <div class="yui3-u-3-4"> |
|
29 <div id="main"> |
|
30 <div class="content"><div class="intro"> |
|
31 <p>This example shows how to use the <a href="index.html#arrayassert"><code>ArrayAssert</code></a> object, which |
|
32 contains assertions designed to be used specifically with JavaScript Arrays and array-like objects.</p> |
|
33 </div> |
|
34 |
|
35 <div class="example yui3-skin-sam"> |
|
36 <div id="testLogger"></div> |
|
37 |
|
38 <script> |
|
39 YUI().use('node', 'test-console', 'test', function (Y) { |
|
40 |
|
41 Y.namespace("example.test"); |
|
42 |
|
43 Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
44 |
|
45 //the name of the test case - if not provided, one is automatically generated |
|
46 name: "Array Tests", |
|
47 |
|
48 //------------------------------------------------------------------------- |
|
49 // Setup and teardown |
|
50 //------------------------------------------------------------------------- |
|
51 |
|
52 /* |
|
53 * The setUp() method is used to setup data that necessary for a test to |
|
54 * run. This method is called immediately before each test method is run, |
|
55 * so it is run as many times as there are test methods. |
|
56 */ |
|
57 setUp : function () { |
|
58 this.data = new Array (0,1,2,3,4,5); |
|
59 }, |
|
60 |
|
61 /* |
|
62 * The tearDown() method is used to clean up the initialization that was done |
|
63 * in the setUp() method. Ideally, it should free up all memory allocated in |
|
64 * setUp(), anticipating any possible changes to the data. This method is called |
|
65 * immediately after each test method is run. |
|
66 */ |
|
67 tearDown : function () { |
|
68 delete this.data; |
|
69 }, |
|
70 |
|
71 //------------------------------------------------------------------------- |
|
72 // Basic tests - all method names must begin with "test" |
|
73 //------------------------------------------------------------------------- |
|
74 |
|
75 /* |
|
76 * Test the push() method. |
|
77 */ |
|
78 testPush : function() { |
|
79 |
|
80 //shortcut variables |
|
81 var ArrayAssert = Y.ArrayAssert; |
|
82 |
|
83 //do whatever data manipulation is necessary |
|
84 this.data.push(6); |
|
85 |
|
86 //array-specific assertions |
|
87 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
88 ArrayAssert.contains(6, this.data, "Array should contain 6."); |
|
89 ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6."); |
|
90 |
|
91 //check that all the values are there |
|
92 ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal."); |
|
93 |
|
94 }, |
|
95 |
|
96 /* |
|
97 * Test the pop() method. |
|
98 */ |
|
99 testPop : function() { |
|
100 |
|
101 //shortcut variables |
|
102 var Assert = Y.Assert; |
|
103 var ArrayAssert = Y.ArrayAssert; |
|
104 |
|
105 //do whatever data manipulation is necessary |
|
106 var value = this.data.pop(); |
|
107 |
|
108 //array shouldn't be empty |
|
109 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
110 |
|
111 //basic equality assertion - expected value, actual value, optional error message |
|
112 Assert.areEqual(5, this.data.length, "Array should have 5 items."); |
|
113 Assert.areEqual(5, value, "Value should be 5."); |
|
114 |
|
115 ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal."); |
|
116 }, |
|
117 |
|
118 /* |
|
119 * Test the reverse() method. |
|
120 */ |
|
121 testReverse : function() { |
|
122 |
|
123 //shortcut variables |
|
124 var ArrayAssert = Y.ArrayAssert; |
|
125 |
|
126 //do whatever data manipulation is necessary |
|
127 this.data = this.data.reverse(); |
|
128 |
|
129 ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal."); |
|
130 }, |
|
131 |
|
132 /* |
|
133 * Test the shift() method. |
|
134 */ |
|
135 testShift : function() { |
|
136 |
|
137 //shortcut variables |
|
138 var Assert = Y.Assert; |
|
139 var ArrayAssert = Y.ArrayAssert; |
|
140 |
|
141 //do whatever data manipulation is necessary |
|
142 var value = this.data.shift(); |
|
143 |
|
144 //array shouldn't be empty |
|
145 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
146 |
|
147 //basic equality assertion - expected value, actual value, optional error message |
|
148 Assert.areEqual(5, this.data.length, "Array should have 6 items."); |
|
149 Assert.areEqual(0, value, "Value should be 0."); |
|
150 |
|
151 ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal."); |
|
152 }, |
|
153 |
|
154 /* |
|
155 * Test the splice() method. |
|
156 */ |
|
157 testSplice : function() { |
|
158 |
|
159 //shortcut variables |
|
160 var Assert = Y.Assert; |
|
161 var ArrayAssert = Y.ArrayAssert; |
|
162 |
|
163 //do whatever data manipulation is necessary |
|
164 var removed = this.data.splice(1, 2, 99, 100); |
|
165 |
|
166 //basic equality assertion - expected value, actual value, optional error message |
|
167 Assert.areEqual(6, this.data.length, "Array should have 6 items."); |
|
168 |
|
169 //the new items should be there |
|
170 ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99."); |
|
171 ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100."); |
|
172 |
|
173 ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal."); |
|
174 ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2."); |
|
175 |
|
176 }, |
|
177 |
|
178 /* |
|
179 * Test the unshift() method. |
|
180 */ |
|
181 testUnshift : function() { |
|
182 |
|
183 //shortcut variables |
|
184 var Assert = Y.Assert; |
|
185 var ArrayAssert = Y.ArrayAssert; |
|
186 |
|
187 //do whatever data manipulation is necessary |
|
188 this.data.unshift(-1); |
|
189 |
|
190 //basic equality assertion - expected value, actual value, optional error message |
|
191 Assert.areEqual(7, this.data.length, "Array should have 7 items."); |
|
192 |
|
193 //the new item should be there |
|
194 ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1."); |
|
195 |
|
196 ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal."); |
|
197 } |
|
198 |
|
199 }); |
|
200 |
|
201 //create the console |
|
202 (new Y.Test.Console({ |
|
203 newestOnTop : false, |
|
204 filters: { |
|
205 pass: true, |
|
206 fail: true |
|
207 } |
|
208 })).render('#testLogger'); |
|
209 |
|
210 Y.Test.Runner.add(Y.example.test.ArrayTestCase); |
|
211 |
|
212 //run the tests |
|
213 Y.Test.Runner.run(); |
|
214 }); |
|
215 |
|
216 </script> |
|
217 |
|
218 </div> |
|
219 |
|
220 <h2 class="first" id="array-assertions">Array Assertions</h2> |
|
221 |
|
222 <p>This example uses the <code>Y.ArrayAssert</code> object to test methods on JavaScript's |
|
223 built-in <code>Array</code> object. The intent of this example is to introduce <code>Y.ArrayAssert</code> and its methods |
|
224 as an alternative to the generic methods available on <code>Y.Assert</code>.</p> |
|
225 <p>The example begins by creating an example namespace and <code>Y.Test.Case</code>:</p> |
|
226 <pre class="code prettyprint">Y.namespace("example.test"); |
|
227 Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
228 |
|
229 name: "Array Tests", |
|
230 |
|
231 //------------------------------------------------------------------------- |
|
232 // Setup and teardown |
|
233 //------------------------------------------------------------------------- |
|
234 |
|
235 /* |
|
236 * The setUp() method is used to setup data that necessary for a test to |
|
237 * run. This method is called immediately before each test method is run, |
|
238 * so it is run as many times as there are test methods. |
|
239 */ |
|
240 setUp : function () { |
|
241 this.data = new Array (0,1,2,3,4,5); |
|
242 }, |
|
243 |
|
244 /* |
|
245 * The tearDown() method is used to clean up the initialization that was done |
|
246 * in the setUp() method. Ideally, it should free up all memory allocated in |
|
247 * setUp(), anticipating any possible changes to the data. This method is called |
|
248 * immediately after each test method is run. |
|
249 */ |
|
250 tearDown : function () { |
|
251 delete this.data; |
|
252 }, |
|
253 |
|
254 ... |
|
255 });</pre> |
|
256 |
|
257 <p>This <code>TestCase</code> has a <code>setUp()</code> method that creates an array for all the tests to use, as well as |
|
258 a <code>tearDown()</code> method that deletes the array after each test has been executed. This array is used throughout |
|
259 the tests as a base for array manipulations.</p> |
|
260 |
|
261 <h3 id="testing-push">Testing <code>push()</code></h3> |
|
262 <p>The first test is <code>testPush()</code>, which tests the functionality of the <code>Array</code> object's <code>push()</code> method |
|
263 (other methods hidden for simpicity):</p> |
|
264 |
|
265 <pre class="code prettyprint">Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
266 |
|
267 ... |
|
268 |
|
269 testPush : function() { |
|
270 |
|
271 //shortcut variables |
|
272 var ArrayAssert = Y.ArrayAssert; |
|
273 |
|
274 //do whatever data manipulation is necessary |
|
275 this.data.push(6); |
|
276 |
|
277 //array-specific assertions |
|
278 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
279 ArrayAssert.contains(6, this.data, "Array should contain 6."); |
|
280 ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6."); |
|
281 |
|
282 //check that all the values are there |
|
283 ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal."); |
|
284 |
|
285 }, |
|
286 |
|
287 ... |
|
288 });</pre> |
|
289 |
|
290 <p>The test begins by setting up a shortcut variables for <code>Y.ArrayAssert</code>, then pushes the value 6 onto |
|
291 the <code>data</code> array (which was created by <code>setUp()</code>). Next, <code>Y.ArrayAssert.isNotEmpty()</code> determines if the |
|
292 array has at least one item; this should definitely pass because the <code>push()</code> operation only adds values to the array. To determine |
|
293 that the new value, 6, is in the array, <code>Y.ArrayAssert.contains()</code> is used. The first argument is the value to look for and the second |
|
294 is the array to look in. To find out if the new value ended up where it should have (the last position, index 6), <code>Y.ArrayAssert.indexOf()</code> |
|
295 is used, passing in the value to search for as the first argument, the array to search in as the second, and the index at which the value should |
|
296 occur as the final argument. Since 6 was pushed onto the end of an array that already had 6 items, it should end up at index 6 (the length of the |
|
297 array minus one). As a final test, <code>Y.ArrayAssert.itemsAreEqual()</code> is used to determine that all of the items in the array are in the |
|
298 correct place. The first argument of this method is an array that has all of the values that should be in the array you're testing. This assertion |
|
299 passes only when the values in both arrays match up (the values are equal and the positions are the same).</p> |
|
300 |
|
301 <h3 id="testing-pop">Testing <code>pop()</code></h3> |
|
302 <p>The next test is <code>testPop()</code>, which tests the functionality of the <code>Array</code> object's <code>pop()</code> method:</p> |
|
303 |
|
304 <pre class="code prettyprint">Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
305 |
|
306 ... |
|
307 |
|
308 testPop : function() { |
|
309 |
|
310 //shortcut variables |
|
311 var Assert = Y.Assert; |
|
312 var ArrayAssert = Y.ArrayAssert; |
|
313 |
|
314 //do whatever data manipulation is necessary |
|
315 var value = this.data.pop(); |
|
316 |
|
317 //array shouldn't be empty |
|
318 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
319 |
|
320 //basic equality assertion - expected value, actual value, optional error message |
|
321 Assert.areEqual(5, this.data.length, "Array should have 5 items."); |
|
322 Assert.areEqual(5, value, "Value should be 5."); |
|
323 |
|
324 ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal."); |
|
325 |
|
326 }, |
|
327 |
|
328 ... |
|
329 });</pre> |
|
330 |
|
331 <p>This test also starts out by creating some shortcut variables, for <code>Y.Assert</code> and <code>Y.ArrayAssert</code>. Next, the <code>pop()</code> |
|
332 method is called, storing the returned item in <code>value</code>. Since <code>pop()</code> should only remove a single item, <code>Y.ArrayAssert.isNotEmpty()</code> |
|
333 is called to ensure that only one item has been removed. After that, <code>Y.Assert.areEqual()</code> is called twice: once to check the |
|
334 length of the array and once to confirm the value of the item that was removed from the array (which should be 5). The last assertion uses |
|
335 <code>Y.ArrayAssert.itemsAreSame()</code>, which is similar to <code>Y.ArrayAssert.itemsAreEqual()</code> in that it compares values between two |
|
336 arrays. The difference is that <code>Y.ArrayAssert.itemsAreSame()</code> uses strict equality (<code>===</code>) to compare values, ensuring that |
|
337 no behind-the-scenes type conversions will occur (this makes <code>Y.ArrayAssert.itemsAreSame()</code> more useful for working with arrays of |
|
338 objects).</p> |
|
339 |
|
340 <h3 id="testing-reverse">Testing <code>reverse()</code></h3> |
|
341 <p>The next test is <code>testReverse()</code>, which tests the functionality of the <code>Array</code> object's <code>reverse()</code> method:</p> |
|
342 |
|
343 <pre class="code prettyprint">Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
344 |
|
345 ... |
|
346 |
|
347 testReverse : function() { |
|
348 |
|
349 //shortcut variables |
|
350 var ArrayAssert = Y.ArrayAssert; |
|
351 |
|
352 //do whatever data manipulation is necessary |
|
353 this.data = this.data.reverse(); |
|
354 |
|
355 ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal."); |
|
356 |
|
357 }, |
|
358 |
|
359 ... |
|
360 });</pre> |
|
361 |
|
362 <p>The <code>testRemove()</code> method is very simple, calling <code>reverse()</code> on the array and then testing the result. Since |
|
363 every item in the array has changed, the changes can be tested by calling <code>Y.ArrayAssert.itemsAreEqual()</code> once (instead of |
|
364 calling <code>Y.ArrayAssert.indexOf()</code> multiple times). The first argument is an array with all the values in the reverse order |
|
365 of the array that was created in <code>setUp()</code>. When compared with the second argument, the newly reversed array, the values in |
|
366 each position should be equal.</p> |
|
367 |
|
368 <h3 id="testing-shift">Testing <code>shift()</code></h3> |
|
369 <p>The next test is <code>testShift()</code>, which tests the functionality of the <code>Array</code> object's <code>shift()</code> method:</p> |
|
370 |
|
371 <pre class="code prettyprint">Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
372 |
|
373 ... |
|
374 |
|
375 testShift : function() { |
|
376 |
|
377 //shortcut variables |
|
378 var Assert = Y.Assert; |
|
379 var ArrayAssert = Y.ArrayAssert; |
|
380 |
|
381 //do whatever data manipulation is necessary |
|
382 var value = this.data.shift(); |
|
383 |
|
384 //array shouldn't be empty |
|
385 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
386 |
|
387 //basic equality assertion - expected value, actual value, optional error message |
|
388 Assert.areEqual(5, this.data.length, "Array should have 6 items."); |
|
389 Assert.areEqual(0, value, "Value should be 0."); |
|
390 |
|
391 ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal."); |
|
392 |
|
393 }, |
|
394 |
|
395 ... |
|
396 });</pre> |
|
397 |
|
398 <p>The <code>shift()</code> method removes the first item in the array and returns it (similar to <code>pop()</code>, which removes the item |
|
399 from the end). In the <code>testShift()</code> method, <code>shift()</code> is called and the item is stored in <code>value</code>. To ensure |
|
400 that the rest of the array is still there, <code>Y.ArrayAssert.isNotEmpty()</code> is called. After that, <code>Array.areEqual()</code> is |
|
401 called twice, once to test the length of the array and once to test the value that was returned from <code>shift()</code> (which should be |
|
402 0). As a last test, the entire array is tested using <code>Y.ArrayAssert.itemsAreEqual()</code> to ensure that all of the items have shifted |
|
403 into the appropriate positions in the array.</p> |
|
404 |
|
405 <h3 id="testing-splice">Testing <code>splice()</code></h3> |
|
406 <p>The next test is <code>testSplice()</code>, which tests the functionality of the <code>Array</code> object's <code>splice()</code> method:</p> |
|
407 |
|
408 <pre class="code prettyprint">Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
409 |
|
410 ... |
|
411 |
|
412 testSplice : function() { |
|
413 |
|
414 //shortcut variables |
|
415 var Assert = Y.Assert; |
|
416 var ArrayAssert = Y.ArrayAssert; |
|
417 |
|
418 //do whatever data manipulation is necessary |
|
419 var removed = this.data.splice(1, 2, 99, 100); |
|
420 |
|
421 //basic equality assertion - expected value, actual value, optional error message |
|
422 Assert.areEqual(6, this.data.length, "Array should have 6 items."); |
|
423 |
|
424 //the new items should be there |
|
425 ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99."); |
|
426 ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100."); |
|
427 |
|
428 ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal."); |
|
429 ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2."); |
|
430 |
|
431 }, |
|
432 |
|
433 ... |
|
434 });</pre> |
|
435 |
|
436 <p>The <code>splice()</code> method is one of the most powerful <code>Array</code> manipulations. It can both remove and add any number of items |
|
437 from an array at the same time. This test begins by splicing some values into the array. When calling <code>splice()</code>, the first argument |
|
438 is 1, indicating that values should be inserted at index 1 of the array; the second argument is 2, indicating that two values should be |
|
439 removed from the array (the value in index 1 and the value in index 2); the third and fourth arguments are values that should be inserted |
|
440 into the array at the position given by the first argument. Essentially, values 1 and 2 should end up being replaced by values 99 and 100 in |
|
441 the array.</p> |
|
442 <p>The first test is to determine that the length of the array is still 6 (since the previous step removed two items and then inserted two, the |
|
443 length should still be 6). After that, <code>Y.Assert.indexOf()</code> is called to determine that the values of 99 and 100 are in positions |
|
444 1 and 2, respectively. To ensure the integrity of the entire array, <code>Y.ArrayAssert.itemsAreEqual()</code> is called on the array, comparing |
|
445 it to an array with the same values. The very last step is to test the value returned from <code>splice()</code>, which is an array containing |
|
446 the removed values, 1 and 2. <code>Y.ArrayAssert.itemsAreEqual()</code> is appropriate for this task as well.</p> |
|
447 |
|
448 <h3 id="testing-unshift">Testing <code>unshift()</code></h3> |
|
449 <p>The next test is <code>testUnshift()</code>, which tests the functionality of the <code>Array</code> object's <code>unshift()</code> method:</p> |
|
450 |
|
451 <pre class="code prettyprint">Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
452 |
|
453 ... |
|
454 |
|
455 testUnshift : function() { |
|
456 |
|
457 //shortcut variables |
|
458 var Assert = Y.Assert; |
|
459 var ArrayAssert = Y.ArrayAssert; |
|
460 |
|
461 //do whatever data manipulation is necessary |
|
462 this.data.unshift(-1); |
|
463 |
|
464 //basic equality assertion - expected value, actual value, optional error message |
|
465 Assert.areEqual(7, this.data.length, "Array should have 7 items."); |
|
466 |
|
467 //the new item should be there |
|
468 ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1."); |
|
469 |
|
470 ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal."); |
|
471 |
|
472 }, |
|
473 |
|
474 ... |
|
475 });</pre> |
|
476 |
|
477 <p>Working similar to <code>push()</code>, <code>unshift()</code> adds a value to the array, but the item is added to the front (index 0) instead of |
|
478 the back. This test begins by adding the value -1 to the array. The first assertion determines if the length of the array has been incremented |
|
479 to 7 to account for the new value. After that, <code>Y.ArrayAssert.indexOf()</code> is used to determine if the value has been placed in the |
|
480 correct location. The final assertions tests that the entire array is expected by using <code>Y.ArrayAssert.itemsAreEqual()</code>.</p> |
|
481 |
|
482 <h3 id="running-the-tests">Running the tests</h3> |
|
483 |
|
484 <p>With all of the tests defined, the last step is to run them:</p> |
|
485 |
|
486 <pre class="code prettyprint">//create the console |
|
487 (new Y.Test.Console({ |
|
488 verbose : true, |
|
489 newestOnTop : false, |
|
490 filters: { |
|
491 pass: true, |
|
492 fail: true |
|
493 } |
|
494 })).render('#testLogger'); |
|
495 |
|
496 Y.Test.Runner.add(Y.example.test.ArrayTestCase); |
|
497 |
|
498 //run the tests |
|
499 Y.Test.Runner.run();</pre> |
|
500 |
|
501 |
|
502 <p>Before running the tests, it's necessary to create a <code>Y.Test.Console</code> object to display the results (otherwise the tests would run |
|
503 but you wouldn't see the results). After that, the <code>Y.Test.Runner</code> is loaded with the <code>Y.Test.Case</code> object by calling |
|
504 <code>add()</code> (any number of <code>Y.Test.Case</code> and <code>TestSuite</code> objects can be added to a <code>Y.Test.Runner</code>, |
|
505 this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its |
|
506 queue and displays the results in the <code>Y.Test.Console</code>.</p> |
|
507 |
|
508 <h2 id="complete-example-source">Complete Example Source</h2> |
|
509 |
|
510 <pre class="code prettyprint"><div id="testLogger"></div> |
|
511 |
|
512 <script> |
|
513 YUI().use('node', 'test-console', 'test', function (Y) { |
|
514 |
|
515 Y.namespace("example.test"); |
|
516 |
|
517 Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
518 |
|
519 //the name of the test case - if not provided, one is automatically generated |
|
520 name: "Array Tests", |
|
521 |
|
522 //------------------------------------------------------------------------- |
|
523 // Setup and teardown |
|
524 //------------------------------------------------------------------------- |
|
525 |
|
526 /* |
|
527 * The setUp() method is used to setup data that necessary for a test to |
|
528 * run. This method is called immediately before each test method is run, |
|
529 * so it is run as many times as there are test methods. |
|
530 */ |
|
531 setUp : function () { |
|
532 this.data = new Array (0,1,2,3,4,5); |
|
533 }, |
|
534 |
|
535 /* |
|
536 * The tearDown() method is used to clean up the initialization that was done |
|
537 * in the setUp() method. Ideally, it should free up all memory allocated in |
|
538 * setUp(), anticipating any possible changes to the data. This method is called |
|
539 * immediately after each test method is run. |
|
540 */ |
|
541 tearDown : function () { |
|
542 delete this.data; |
|
543 }, |
|
544 |
|
545 //------------------------------------------------------------------------- |
|
546 // Basic tests - all method names must begin with "test" |
|
547 //------------------------------------------------------------------------- |
|
548 |
|
549 /* |
|
550 * Test the push() method. |
|
551 */ |
|
552 testPush : function() { |
|
553 |
|
554 //shortcut variables |
|
555 var ArrayAssert = Y.ArrayAssert; |
|
556 |
|
557 //do whatever data manipulation is necessary |
|
558 this.data.push(6); |
|
559 |
|
560 //array-specific assertions |
|
561 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
562 ArrayAssert.contains(6, this.data, "Array should contain 6."); |
|
563 ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6."); |
|
564 |
|
565 //check that all the values are there |
|
566 ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal."); |
|
567 |
|
568 }, |
|
569 |
|
570 /* |
|
571 * Test the pop() method. |
|
572 */ |
|
573 testPop : function() { |
|
574 |
|
575 //shortcut variables |
|
576 var Assert = Y.Assert; |
|
577 var ArrayAssert = Y.ArrayAssert; |
|
578 |
|
579 //do whatever data manipulation is necessary |
|
580 var value = this.data.pop(); |
|
581 |
|
582 //array shouldn't be empty |
|
583 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
584 |
|
585 //basic equality assertion - expected value, actual value, optional error message |
|
586 Assert.areEqual(5, this.data.length, "Array should have 5 items."); |
|
587 Assert.areEqual(5, value, "Value should be 5."); |
|
588 |
|
589 ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal."); |
|
590 }, |
|
591 |
|
592 /* |
|
593 * Test the reverse() method. |
|
594 */ |
|
595 testReverse : function() { |
|
596 |
|
597 //shortcut variables |
|
598 var ArrayAssert = Y.ArrayAssert; |
|
599 |
|
600 //do whatever data manipulation is necessary |
|
601 this.data = this.data.reverse(); |
|
602 |
|
603 ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal."); |
|
604 }, |
|
605 |
|
606 /* |
|
607 * Test the shift() method. |
|
608 */ |
|
609 testShift : function() { |
|
610 |
|
611 //shortcut variables |
|
612 var Assert = Y.Assert; |
|
613 var ArrayAssert = Y.ArrayAssert; |
|
614 |
|
615 //do whatever data manipulation is necessary |
|
616 var value = this.data.shift(); |
|
617 |
|
618 //array shouldn't be empty |
|
619 ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
620 |
|
621 //basic equality assertion - expected value, actual value, optional error message |
|
622 Assert.areEqual(5, this.data.length, "Array should have 6 items."); |
|
623 Assert.areEqual(0, value, "Value should be 0."); |
|
624 |
|
625 ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal."); |
|
626 }, |
|
627 |
|
628 /* |
|
629 * Test the splice() method. |
|
630 */ |
|
631 testSplice : function() { |
|
632 |
|
633 //shortcut variables |
|
634 var Assert = Y.Assert; |
|
635 var ArrayAssert = Y.ArrayAssert; |
|
636 |
|
637 //do whatever data manipulation is necessary |
|
638 var removed = this.data.splice(1, 2, 99, 100); |
|
639 |
|
640 //basic equality assertion - expected value, actual value, optional error message |
|
641 Assert.areEqual(6, this.data.length, "Array should have 6 items."); |
|
642 |
|
643 //the new items should be there |
|
644 ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99."); |
|
645 ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100."); |
|
646 |
|
647 ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal."); |
|
648 ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2."); |
|
649 |
|
650 }, |
|
651 |
|
652 /* |
|
653 * Test the unshift() method. |
|
654 */ |
|
655 testUnshift : function() { |
|
656 |
|
657 //shortcut variables |
|
658 var Assert = Y.Assert; |
|
659 var ArrayAssert = Y.ArrayAssert; |
|
660 |
|
661 //do whatever data manipulation is necessary |
|
662 this.data.unshift(-1); |
|
663 |
|
664 //basic equality assertion - expected value, actual value, optional error message |
|
665 Assert.areEqual(7, this.data.length, "Array should have 7 items."); |
|
666 |
|
667 //the new item should be there |
|
668 ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1."); |
|
669 |
|
670 ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal."); |
|
671 } |
|
672 |
|
673 }); |
|
674 |
|
675 //create the console |
|
676 (new Y.Test.Console({ |
|
677 newestOnTop : false, |
|
678 filters: { |
|
679 pass: true, |
|
680 fail: true |
|
681 } |
|
682 })).render('#testLogger'); |
|
683 |
|
684 Y.Test.Runner.add(Y.example.test.ArrayTestCase); |
|
685 |
|
686 //run the tests |
|
687 Y.Test.Runner.run(); |
|
688 }); |
|
689 |
|
690 </script></pre> |
|
691 |
|
692 </div> |
|
693 </div> |
|
694 </div> |
|
695 |
|
696 <div class="yui3-u-1-4"> |
|
697 <div class="sidebar"> |
|
698 |
|
699 <div id="toc" class="sidebox"> |
|
700 <div class="hd"> |
|
701 <h2 class="no-toc">Table of Contents</h2> |
|
702 </div> |
|
703 |
|
704 <div class="bd"> |
|
705 <ul class="toc"> |
|
706 <li> |
|
707 <a href="#array-assertions">Array Assertions</a> |
|
708 <ul class="toc"> |
|
709 <li> |
|
710 <a href="#testing-push">Testing <code>push()</code></a> |
|
711 </li> |
|
712 <li> |
|
713 <a href="#testing-pop">Testing <code>pop()</code></a> |
|
714 </li> |
|
715 <li> |
|
716 <a href="#testing-reverse">Testing <code>reverse()</code></a> |
|
717 </li> |
|
718 <li> |
|
719 <a href="#testing-shift">Testing <code>shift()</code></a> |
|
720 </li> |
|
721 <li> |
|
722 <a href="#testing-splice">Testing <code>splice()</code></a> |
|
723 </li> |
|
724 <li> |
|
725 <a href="#testing-unshift">Testing <code>unshift()</code></a> |
|
726 </li> |
|
727 <li> |
|
728 <a href="#running-the-tests">Running the tests</a> |
|
729 </li> |
|
730 </ul> |
|
731 </li> |
|
732 <li> |
|
733 <a href="#complete-example-source">Complete Example Source</a> |
|
734 </li> |
|
735 </ul> |
|
736 </div> |
|
737 </div> |
|
738 |
|
739 |
|
740 |
|
741 <div class="sidebox"> |
|
742 <div class="hd"> |
|
743 <h2 class="no-toc">Examples</h2> |
|
744 </div> |
|
745 |
|
746 <div class="bd"> |
|
747 <ul class="examples"> |
|
748 |
|
749 |
|
750 <li data-description="Demonstrates basic usage of YUI Test for setting up and running tests."> |
|
751 <a href="test-simple-example.html">Simple Testing Example</a> |
|
752 </li> |
|
753 |
|
754 |
|
755 |
|
756 <li data-description="Demonstrates how to use advanced testing features such as defining tests that should fail, tests that should be ignored, and tests that should throw an error."> |
|
757 <a href="test-advanced-test-options.html">Advanced Test Options</a> |
|
758 </li> |
|
759 |
|
760 |
|
761 |
|
762 <li data-description="Demonstrates how to use the ArrayAssert object to test array data."> |
|
763 <a href="test-array-tests.html">Array Processing</a> |
|
764 </li> |
|
765 |
|
766 |
|
767 |
|
768 <li data-description="Demonstrates basic asynchronous tests."> |
|
769 <a href="test-async-test.html">Asynchronous Testing</a> |
|
770 </li> |
|
771 |
|
772 |
|
773 |
|
774 <li data-description="Demonstrates using events with asynchronous tests."> |
|
775 <a href="test-async-event-tests.html">Asynchronous Event Testing</a> |
|
776 </li> |
|
777 |
|
778 |
|
779 </ul> |
|
780 </div> |
|
781 </div> |
|
782 |
|
783 |
|
784 |
|
785 </div> |
|
786 </div> |
|
787 </div> |
|
788 </div> |
|
789 |
|
790 <script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
791 <script>prettyPrint();</script> |
|
792 |
|
793 <script> |
|
794 YUI.Env.Tests = { |
|
795 examples: [], |
|
796 project: '../assets', |
|
797 assets: '../assets/test', |
|
798 name: 'test-array-tests', |
|
799 title: 'Array Processing', |
|
800 newWindow: '', |
|
801 auto: false |
|
802 }; |
|
803 YUI.Env.Tests.examples.push('test-simple-example'); |
|
804 YUI.Env.Tests.examples.push('test-advanced-test-options'); |
|
805 YUI.Env.Tests.examples.push('test-array-tests'); |
|
806 YUI.Env.Tests.examples.push('test-async-test'); |
|
807 YUI.Env.Tests.examples.push('test-async-event-tests'); |
|
808 |
|
809 </script> |
|
810 <script src="../assets/yui/test-runner.js"></script> |
|
811 |
|
812 |
|
813 |
|
814 </body> |
|
815 </html> |