|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<title>YUI Library Examples: Test: Array Processing</title> |
|
|
6 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
7 |
<link rel="stylesheet" type="text/css" href="../../assets/yui.css" > |
|
|
8 |
|
|
|
9 |
<style> |
|
|
10 |
/*Supplemental CSS for the YUI distribution*/ |
|
|
11 |
#custom-doc { width: 95%; min-width: 950px; } |
|
|
12 |
#pagetitle {background-image: url(../../assets/bg_hd.gif);} |
|
|
13 |
/* #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}*/ |
|
|
14 |
</style> |
|
|
15 |
|
|
|
16 |
<link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css"> |
|
|
17 |
|
|
|
18 |
<!--there is no custom header content for this example--> |
|
|
19 |
|
|
|
20 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
21 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
22 |
|
|
|
23 |
</head> |
|
|
24 |
<body id="yahoo-com" class=" yui-skin-sam"> |
|
|
25 |
<div id="custom-doc" class="yui-t2"> |
|
|
26 |
<div id="hd"> |
|
|
27 |
<div id="ygunav"> |
|
|
28 |
<p> |
|
|
29 |
<em> |
|
|
30 |
<a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> |
|
|
31 |
</em> |
|
|
32 |
</p> |
|
|
33 |
<form action="http://search.yahoo.com/search" id="sitesearchform"> |
|
|
34 |
<input name="vs" type="hidden" value="developer.yahoo.com"> |
|
|
35 |
<input name="vs" type="hidden" value="yuiblog.com"> |
|
|
36 |
<div id="sitesearch"> |
|
|
37 |
<label for="searchinput">Site Search (YDN & YUIBlog): </label> |
|
|
38 |
<input type="text" id="searchinput" name="p"> |
|
|
39 |
<input type="submit" value="Search" id="searchsubmit" class="ygbt"> |
|
|
40 |
</div> |
|
|
41 |
</form> |
|
|
42 |
</div> |
|
|
43 |
<div id="ygma"><a href="../../"><img src="../../assets/logo.gif" border="0" width="200" height="93"></a></div> |
|
|
44 |
<div id="pagetitle"><h1>YUI Library Examples: Test: Array Processing</h1></div> |
|
|
45 |
</div> |
|
|
46 |
<div id="bd"> |
|
|
47 |
|
|
|
48 |
<div id="bar-note"><p><strong>Note:</strong> This is YUI 3.x. Looking for <a href="http://developer.yahoo.com/yui/">YUI 2.x</a>?</p></div> |
|
|
49 |
|
|
|
50 |
<div id="yui-main"> |
|
|
51 |
<div class="yui-b"> |
|
|
52 |
<div class="yui-ge"> |
|
|
53 |
<div class="yui-u first example" id="main"> |
|
|
54 |
|
|
|
55 |
<h2>Test: Array Processing</h2> |
|
|
56 |
|
|
|
57 |
<div id="example" class="promo"> |
|
|
58 |
<p> |
|
|
59 |
<p>This example shows how to use the <a href="/yui/test/#arrayassert"><code>ArrayAssert</code></a> object, which |
|
|
60 |
contains assertions designed to be used specifically with JavaScript Arrays and array-like objects.</p> </p> |
|
|
61 |
|
|
|
62 |
<div class="module example-container "> |
|
|
63 |
<div class="hd exampleHd"> |
|
|
64 |
<p class="newWindowButton yui-skin-sam"> |
|
|
65 |
<a href="test-array-tests_clean.html" target="_blank">View example in new window.</a> |
|
|
66 |
</p> |
|
|
67 |
</div> <div id="example-canvas" class="bd"> |
|
|
68 |
|
|
|
69 |
|
|
|
70 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
71 |
|
|
|
72 |
<div id="testLogger"></div> |
|
|
73 |
<script type="text/javascript"> |
|
|
74 |
YUI({base:"../../build/", timeout: 10000}).use("node", "console", "test",function (Y) { |
|
|
75 |
|
|
|
76 |
Y.namespace("example.test"); |
|
|
77 |
|
|
|
78 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
79 |
|
|
|
80 |
//the name of the test case - if not provided, one is automatically generated |
|
|
81 |
name: "Array Tests", |
|
|
82 |
|
|
|
83 |
//------------------------------------------------------------------------- |
|
|
84 |
// Setup and teardown |
|
|
85 |
//------------------------------------------------------------------------- |
|
|
86 |
|
|
|
87 |
/* |
|
|
88 |
* The setUp() method is used to setup data that necessary for a test to |
|
|
89 |
* run. This method is called immediately before each test method is run, |
|
|
90 |
* so it is run as many times as there are test methods. |
|
|
91 |
*/ |
|
|
92 |
setUp : function () { |
|
|
93 |
this.data = new Array (0,1,2,3,4,5); |
|
|
94 |
}, |
|
|
95 |
|
|
|
96 |
/* |
|
|
97 |
* The tearDown() method is used to clean up the initialization that was done |
|
|
98 |
* in the setUp() method. Ideally, it should free up all memory allocated in |
|
|
99 |
* setUp(), anticipating any possible changes to the data. This method is called |
|
|
100 |
* immediately after each test method is run. |
|
|
101 |
*/ |
|
|
102 |
tearDown : function () { |
|
|
103 |
delete this.data; |
|
|
104 |
}, |
|
|
105 |
|
|
|
106 |
//------------------------------------------------------------------------- |
|
|
107 |
// Basic tests - all method names must begin with "test" |
|
|
108 |
//------------------------------------------------------------------------- |
|
|
109 |
|
|
|
110 |
/* |
|
|
111 |
* Test the push() method. |
|
|
112 |
*/ |
|
|
113 |
testPush : function() { |
|
|
114 |
|
|
|
115 |
//shortcut variables |
|
|
116 |
var ArrayAssert = Y.ArrayAssert; |
|
|
117 |
|
|
|
118 |
//do whatever data manipulation is necessary |
|
|
119 |
this.data.push(6); |
|
|
120 |
|
|
|
121 |
//array-specific assertions |
|
|
122 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
123 |
ArrayAssert.contains(6, this.data, "Array should contain 6."); |
|
|
124 |
ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6."); |
|
|
125 |
|
|
|
126 |
//check that all the values are there |
|
|
127 |
ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal."); |
|
|
128 |
|
|
|
129 |
}, |
|
|
130 |
|
|
|
131 |
/* |
|
|
132 |
* Test the pop() method. |
|
|
133 |
*/ |
|
|
134 |
testPop : function() { |
|
|
135 |
|
|
|
136 |
//shortcut variables |
|
|
137 |
var Assert = Y.Assert; |
|
|
138 |
var ArrayAssert = Y.ArrayAssert; |
|
|
139 |
|
|
|
140 |
//do whatever data manipulation is necessary |
|
|
141 |
var value = this.data.pop(); |
|
|
142 |
|
|
|
143 |
//array shouldn't be empty |
|
|
144 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
145 |
|
|
|
146 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
147 |
Assert.areEqual(5, this.data.length, "Array should have 5 items."); |
|
|
148 |
Assert.areEqual(5, value, "Value should be 5."); |
|
|
149 |
|
|
|
150 |
ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal."); |
|
|
151 |
}, |
|
|
152 |
|
|
|
153 |
/* |
|
|
154 |
* Test the reverse() method. |
|
|
155 |
*/ |
|
|
156 |
testReverse : function() { |
|
|
157 |
|
|
|
158 |
//shortcut variables |
|
|
159 |
var ArrayAssert = Y.ArrayAssert; |
|
|
160 |
|
|
|
161 |
//do whatever data manipulation is necessary |
|
|
162 |
this.data = this.data.reverse(); |
|
|
163 |
|
|
|
164 |
ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal."); |
|
|
165 |
}, |
|
|
166 |
|
|
|
167 |
/* |
|
|
168 |
* Test the shift() method. |
|
|
169 |
*/ |
|
|
170 |
testShift : function() { |
|
|
171 |
|
|
|
172 |
//shortcut variables |
|
|
173 |
var Assert = Y.Assert; |
|
|
174 |
var ArrayAssert = Y.ArrayAssert; |
|
|
175 |
|
|
|
176 |
//do whatever data manipulation is necessary |
|
|
177 |
var value = this.data.shift(); |
|
|
178 |
|
|
|
179 |
//array shouldn't be empty |
|
|
180 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
181 |
|
|
|
182 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
183 |
Assert.areEqual(5, this.data.length, "Array should have 6 items."); |
|
|
184 |
Assert.areEqual(0, value, "Value should be 0."); |
|
|
185 |
|
|
|
186 |
ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal."); |
|
|
187 |
}, |
|
|
188 |
|
|
|
189 |
/* |
|
|
190 |
* Test the splice() method. |
|
|
191 |
*/ |
|
|
192 |
testSplice : function() { |
|
|
193 |
|
|
|
194 |
//shortcut variables |
|
|
195 |
var Assert = Y.Assert; |
|
|
196 |
var ArrayAssert = Y.ArrayAssert; |
|
|
197 |
|
|
|
198 |
//do whatever data manipulation is necessary |
|
|
199 |
var removed = this.data.splice(1, 2, 99, 100); |
|
|
200 |
|
|
|
201 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
202 |
Assert.areEqual(6, this.data.length, "Array should have 6 items."); |
|
|
203 |
|
|
|
204 |
//the new items should be there |
|
|
205 |
ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99."); |
|
|
206 |
ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100."); |
|
|
207 |
|
|
|
208 |
ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal."); |
|
|
209 |
ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2."); |
|
|
210 |
|
|
|
211 |
}, |
|
|
212 |
|
|
|
213 |
/* |
|
|
214 |
* Test the unshift() method. |
|
|
215 |
*/ |
|
|
216 |
testUnshift : function() { |
|
|
217 |
|
|
|
218 |
//shortcut variables |
|
|
219 |
var Assert = Y.Assert; |
|
|
220 |
var ArrayAssert = Y.ArrayAssert; |
|
|
221 |
|
|
|
222 |
//do whatever data manipulation is necessary |
|
|
223 |
this.data.unshift(-1); |
|
|
224 |
|
|
|
225 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
226 |
Assert.areEqual(7, this.data.length, "Array should have 7 items."); |
|
|
227 |
|
|
|
228 |
//the new item should be there |
|
|
229 |
ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1."); |
|
|
230 |
|
|
|
231 |
ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal."); |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
}); |
|
|
235 |
|
|
|
236 |
//create the console |
|
|
237 |
var r = new Y.Console({ |
|
|
238 |
verbose : true, |
|
|
239 |
newestOnTop : false |
|
|
240 |
}); |
|
|
241 |
|
|
|
242 |
r.render('#testLogger'); |
|
|
243 |
|
|
|
244 |
Y.Test.Runner.add(Y.example.test.ArrayTestCase); |
|
|
245 |
|
|
|
246 |
//run the tests |
|
|
247 |
Y.Test.Runner.run(); |
|
|
248 |
}); |
|
|
249 |
|
|
|
250 |
</script> |
|
|
251 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
252 |
|
|
|
253 |
|
|
|
254 |
</div> |
|
|
255 |
</div> |
|
|
256 |
</div> |
|
|
257 |
|
|
|
258 |
<h2 class="first">Array Assertions</h2> |
|
|
259 |
|
|
|
260 |
<p>This example uses the <code>Y.ArrayAssert</code> object to test methods on JavaScript's |
|
|
261 |
built-in <code>Array</code> object. The intent of this example is to introduce <code>Y.ArrayAssert</code> and its methods |
|
|
262 |
as an alternative to the generic methods available on <code>Y.Assert</code>.</p> |
|
|
263 |
<p>The example begins by creating an example namespace and <code>Y.Test.Case</code>:</p> |
|
|
264 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
265 |
Y.namespace("example.test"); |
|
|
266 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
267 |
|
|
|
268 |
name: "Array Tests", |
|
|
269 |
|
|
|
270 |
//------------------------------------------------------------------------- |
|
|
271 |
// Setup and teardown |
|
|
272 |
//------------------------------------------------------------------------- |
|
|
273 |
|
|
|
274 |
/* |
|
|
275 |
* The setUp() method is used to setup data that necessary for a test to |
|
|
276 |
* run. This method is called immediately before each test method is run, |
|
|
277 |
* so it is run as many times as there are test methods. |
|
|
278 |
*/ |
|
|
279 |
setUp : function () { |
|
|
280 |
this.data = new Array (0,1,2,3,4,5); |
|
|
281 |
}, |
|
|
282 |
|
|
|
283 |
/* |
|
|
284 |
* The tearDown() method is used to clean up the initialization that was done |
|
|
285 |
* in the setUp() method. Ideally, it should free up all memory allocated in |
|
|
286 |
* setUp(), anticipating any possible changes to the data. This method is called |
|
|
287 |
* immediately after each test method is run. |
|
|
288 |
*/ |
|
|
289 |
tearDown : function () { |
|
|
290 |
delete this.data; |
|
|
291 |
}, |
|
|
292 |
|
|
|
293 |
... |
|
|
294 |
}); |
|
|
295 |
</textarea> |
|
|
296 |
<p>This <code>TestCase</code> has a <code>setUp()</code> method that creates an array for all the tests to use, as well as |
|
|
297 |
a <code>tearDown()</code> method that deletes the array after each test has been executed. This array is used throughout |
|
|
298 |
the tests as a base for array manipulations.</p> |
|
|
299 |
|
|
|
300 |
<h3>Testing <code>push()</code></h3> |
|
|
301 |
<p>The first test is <code>testPush()</code>, which tests the functionality of the <code>Array</code> object's <code>push()</code> method |
|
|
302 |
(other methods hidden for simpicity):</p> |
|
|
303 |
|
|
|
304 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
305 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
306 |
|
|
|
307 |
... |
|
|
308 |
|
|
|
309 |
testPush : function() { |
|
|
310 |
|
|
|
311 |
//shortcut variables |
|
|
312 |
var ArrayAssert = Y.ArrayAssert; |
|
|
313 |
|
|
|
314 |
//do whatever data manipulation is necessary |
|
|
315 |
this.data.push(6); |
|
|
316 |
|
|
|
317 |
//array-specific assertions |
|
|
318 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
319 |
ArrayAssert.contains(6, this.data, "Array should contain 6."); |
|
|
320 |
ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6."); |
|
|
321 |
|
|
|
322 |
//check that all the values are there |
|
|
323 |
ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal."); |
|
|
324 |
|
|
|
325 |
}, |
|
|
326 |
|
|
|
327 |
... |
|
|
328 |
}); |
|
|
329 |
</textarea> |
|
|
330 |
<p>The test begins by setting up a shortcut variables for <code>Y.ArrayAssert</code>, then pushes the value 6 onto |
|
|
331 |
the <code>data</code> array (which was created by <code>setUp()</code>). Next, <code>Y.ArrayAssert.isNotEmpty()</code> determines if the |
|
|
332 |
array has at least one item; this should definitely pass because the <code>push()</code> operation only adds values to the array. To determine |
|
|
333 |
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 |
|
|
334 |
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> |
|
|
335 |
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 |
|
|
336 |
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 |
|
|
337 |
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 |
|
|
338 |
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 |
|
|
339 |
passes only when the values in both arrays match up (the values are equal and the positions are the same).</p> |
|
|
340 |
|
|
|
341 |
<h3>Testing <code>pop()</code></h3> |
|
|
342 |
<p>The next test is <code>testPop()</code>, which tests the functionality of the <code>Array</code> object's <code>pop()</code> method:</p> |
|
|
343 |
|
|
|
344 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
345 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
346 |
|
|
|
347 |
... |
|
|
348 |
|
|
|
349 |
testPop : function() { |
|
|
350 |
|
|
|
351 |
//shortcut variables |
|
|
352 |
var Assert = Y.Assert; |
|
|
353 |
var ArrayAssert = Y.ArrayAssert; |
|
|
354 |
|
|
|
355 |
//do whatever data manipulation is necessary |
|
|
356 |
var value = this.data.pop(); |
|
|
357 |
|
|
|
358 |
//array shouldn't be empty |
|
|
359 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
360 |
|
|
|
361 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
362 |
Assert.areEqual(5, this.data.length, "Array should have 5 items."); |
|
|
363 |
Assert.areEqual(5, value, "Value should be 5."); |
|
|
364 |
|
|
|
365 |
ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal."); |
|
|
366 |
|
|
|
367 |
}, |
|
|
368 |
|
|
|
369 |
... |
|
|
370 |
}); |
|
|
371 |
</textarea> |
|
|
372 |
<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> |
|
|
373 |
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> |
|
|
374 |
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 |
|
|
375 |
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 |
|
|
376 |
<code>Y.ArrayAssert.itemsAreSame()</code>, which is similar to <code>Y.ArrayAssert.itemsAreEqual()</code> in that it compares values between two |
|
|
377 |
arrays. The difference is that <code>Y.ArrayAssert.itemsAreSame()</code> uses strict equality (<code>===</code>) to compare values, ensuring that |
|
|
378 |
no behind-the-scenes type conversions will occur (this makes <code>Y.ArrayAssert.itemsAreSame()</code> more useful for working with arrays of |
|
|
379 |
objects).</p> |
|
|
380 |
|
|
|
381 |
<h3>Testing <code>reverse()</code></h3> |
|
|
382 |
<p>The next test is <code>testReverse()</code>, which tests the functionality of the <code>Array</code> object's <code>reverse()</code> method:</p> |
|
|
383 |
|
|
|
384 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
385 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
386 |
|
|
|
387 |
... |
|
|
388 |
|
|
|
389 |
testReverse : function() { |
|
|
390 |
|
|
|
391 |
//shortcut variables |
|
|
392 |
var ArrayAssert = Y.ArrayAssert; |
|
|
393 |
|
|
|
394 |
//do whatever data manipulation is necessary |
|
|
395 |
this.data = this.data.reverse(); |
|
|
396 |
|
|
|
397 |
ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal."); |
|
|
398 |
|
|
|
399 |
}, |
|
|
400 |
|
|
|
401 |
... |
|
|
402 |
}); |
|
|
403 |
</textarea> |
|
|
404 |
<p>The <code>testRemove()</code> method is very simple, calling <code>reverse()</code> on the array and then testing the result. Since |
|
|
405 |
every item in the array has changed, the changes can be tested by calling <code>Y.ArrayAssert.itemsAreEqual()</code> once (instead of |
|
|
406 |
calling <code>Y.ArrayAssert.indexOf()</code> multiple times). The first argument is an array with all the values in the reverse order |
|
|
407 |
of the array that was created in <code>setUp()</code>. When compared with the second argument, the newly reversed array, the values in |
|
|
408 |
each position should be equal.</p> |
|
|
409 |
|
|
|
410 |
<h3>Testing <code>shift()</code></h3> |
|
|
411 |
<p>The next test is <code>testShift()</code>, which tests the functionality of the <code>Array</code> object's <code>shift()</code> method:</p> |
|
|
412 |
|
|
|
413 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
414 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
415 |
|
|
|
416 |
... |
|
|
417 |
|
|
|
418 |
testShift : function() { |
|
|
419 |
|
|
|
420 |
//shortcut variables |
|
|
421 |
var Assert = Y.Assert; |
|
|
422 |
var ArrayAssert = Y.ArrayAssert; |
|
|
423 |
|
|
|
424 |
//do whatever data manipulation is necessary |
|
|
425 |
var value = this.data.shift(); |
|
|
426 |
|
|
|
427 |
//array shouldn't be empty |
|
|
428 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
429 |
|
|
|
430 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
431 |
Assert.areEqual(5, this.data.length, "Array should have 6 items."); |
|
|
432 |
Assert.areEqual(0, value, "Value should be 0."); |
|
|
433 |
|
|
|
434 |
ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal."); |
|
|
435 |
|
|
|
436 |
}, |
|
|
437 |
|
|
|
438 |
... |
|
|
439 |
}); |
|
|
440 |
</textarea> |
|
|
441 |
<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 |
|
|
442 |
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 |
|
|
443 |
that the rest of the array is still there, <code>Y.ArrayAssert.isNotEmpty()</code> is called. After that, <code>Array.areEqual()</code> is |
|
|
444 |
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 |
|
|
445 |
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 |
|
|
446 |
into the appropriate positions in the array.</p> |
|
|
447 |
|
|
|
448 |
<h3>Testing <code>splice()</code></h3> |
|
|
449 |
<p>The next test is <code>testSplice()</code>, which tests the functionality of the <code>Array</code> object's <code>splice()</code> method:</p> |
|
|
450 |
|
|
|
451 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
452 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
453 |
|
|
|
454 |
... |
|
|
455 |
|
|
|
456 |
testSplice : function() { |
|
|
457 |
|
|
|
458 |
//shortcut variables |
|
|
459 |
var Assert = Y.Assert; |
|
|
460 |
var ArrayAssert = Y.ArrayAssert; |
|
|
461 |
|
|
|
462 |
//do whatever data manipulation is necessary |
|
|
463 |
var removed = this.data.splice(1, 2, 99, 100); |
|
|
464 |
|
|
|
465 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
466 |
Assert.areEqual(6, this.data.length, "Array should have 6 items."); |
|
|
467 |
|
|
|
468 |
//the new items should be there |
|
|
469 |
ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99."); |
|
|
470 |
ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100."); |
|
|
471 |
|
|
|
472 |
ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal."); |
|
|
473 |
ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2."); |
|
|
474 |
|
|
|
475 |
}, |
|
|
476 |
|
|
|
477 |
... |
|
|
478 |
}); |
|
|
479 |
</textarea> |
|
|
480 |
<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 |
|
|
481 |
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 |
|
|
482 |
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 |
|
|
483 |
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 |
|
|
484 |
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 |
|
|
485 |
the array.</p> |
|
|
486 |
<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 |
|
|
487 |
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 |
|
|
488 |
1 and 2, respectively. To ensure the integrity of the entire array, <code>Y.ArrayAssert.itemsAreEqual()</code> is called on the array, comparing |
|
|
489 |
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 |
|
|
490 |
the removed values, 1 and 2. <code>Y.ArrayAssert.itemsAreEqual()</code> is appropriate for this task as well.</p> |
|
|
491 |
|
|
|
492 |
<h3>Testing <code>unshift()</code></h3> |
|
|
493 |
<p>The next test is <code>testUnshift()</code>, which tests the functionality of the <code>Array</code> object's <code>unshift()</code> method:</p> |
|
|
494 |
|
|
|
495 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
496 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
497 |
|
|
|
498 |
... |
|
|
499 |
|
|
|
500 |
testUnshift : function() { |
|
|
501 |
|
|
|
502 |
//shortcut variables |
|
|
503 |
var Assert = Y.Assert; |
|
|
504 |
var ArrayAssert = Y.ArrayAssert; |
|
|
505 |
|
|
|
506 |
//do whatever data manipulation is necessary |
|
|
507 |
this.data.unshift(-1); |
|
|
508 |
|
|
|
509 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
510 |
Assert.areEqual(7, this.data.length, "Array should have 7 items."); |
|
|
511 |
|
|
|
512 |
//the new item should be there |
|
|
513 |
ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1."); |
|
|
514 |
|
|
|
515 |
ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal."); |
|
|
516 |
|
|
|
517 |
}, |
|
|
518 |
|
|
|
519 |
... |
|
|
520 |
}); |
|
|
521 |
</textarea> |
|
|
522 |
<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 |
|
|
523 |
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 |
|
|
524 |
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 |
|
|
525 |
correct location. The final assertions tests that the entire array is expected by using <code>Y.ArrayAssert.itemsAreEqual()</code>.</p> |
|
|
526 |
|
|
|
527 |
<h3>Running the tests</h3> |
|
|
528 |
|
|
|
529 |
<p>With all of the tests defined, the last step is to run them:</p> |
|
|
530 |
|
|
|
531 |
<textarea name="code" class="JScript" cols="60" rows="1"> |
|
|
532 |
//create the console |
|
|
533 |
var r = new Y.Console({ |
|
|
534 |
verbose : true, |
|
|
535 |
newestOnTop : false |
|
|
536 |
}); |
|
|
537 |
|
|
|
538 |
r.render('#testLogger'); |
|
|
539 |
|
|
|
540 |
Y.Test.Runner.add(Y.example.test.ArrayTestCase); |
|
|
541 |
|
|
|
542 |
//run the tests |
|
|
543 |
Y.Test.Runner.run(); |
|
|
544 |
</textarea> |
|
|
545 |
|
|
|
546 |
<p>Before running the tests, it's necessary to create a <code>Y.Console</code> object to display the results (otherwise the tests would run |
|
|
547 |
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 |
|
|
548 |
<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>, |
|
|
549 |
this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its |
|
|
550 |
queue and displays the results in the <code>Y.Console</code>.</p> </div> |
|
|
551 |
<div class="yui-u sidebar"> |
|
|
552 |
|
|
|
553 |
|
|
|
554 |
<div id="examples" class="mod box4"> |
|
|
555 |
<div class="hd"> |
|
|
556 |
<h4> |
|
|
557 |
Test Examples:</h4> |
|
|
558 |
</div> |
|
|
559 |
<div class="bd"> |
|
|
560 |
<ul> |
|
|
561 |
<li><a href='../test/test-simple-example.html'>Simple Testing Example</a></li><li><a href='../test/test-advanced-test-options.html'>Advanced Test Options</a></li><li class='selected'><a href='../test/test-array-tests.html'>Array Processing</a></li><li><a href='../test/test-async-test.html'>Asynchronous Testing</a></li><li><a href='../test/test-async-event-tests.html'>Asynchronous Event Testing</a></li> </ul> |
|
|
562 |
</div> |
|
|
563 |
</div> |
|
|
564 |
|
|
|
565 |
<div class="mod box4"> |
|
|
566 |
<div class="hd"> |
|
|
567 |
<h4>More Test Resources:</h4> |
|
|
568 |
</div> |
|
|
569 |
<div class="bd"> |
|
|
570 |
<ul> |
|
|
571 |
<!-- <li><a href="http://developer.yahoo.com/yui/test/">User's Guide</a> (external)</li> --> |
|
|
572 |
<li><a href="../../api/module_test.html">API Documentation</a></li> |
|
|
573 |
</ul> |
|
|
574 |
</div> |
|
|
575 |
</div> |
|
|
576 |
</div> |
|
|
577 |
</div> |
|
|
578 |
|
|
|
579 |
</div> |
|
|
580 |
</div> |
|
|
581 |
|
|
|
582 |
|
|
|
583 |
<div class="yui-b toc3" id="tocWrapper"> |
|
|
584 |
<!-- TABLE OF CONTENTS --> |
|
|
585 |
<div id="toc"> |
|
|
586 |
|
|
|
587 |
<ul> |
|
|
588 |
<li class="sect first">YUI 3.x Project</li><li class="item"><a title="The Yahoo! User Interface (YUI) Library, 3.x Branch, " href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site (external)</a></li><li class="item"><a title="Examples of every YUI utility and control in action" href="../../examples/">YUI 3 Examples</a></li><li class="item"><a title="Instantly searchable API documentation for the entire YUI library." href="../../api/">YUI 3 API Docs</a></li><li class="item"><a title="The YUI Library can be downloaded from SourceForge" href="http://sourceforge.net/projects/yui/">YUI 3 on Sourceforge (external)</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/3/license.html">YUI License (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="item"><a title="YUI (Global Prerequisite) - Functional Examples" href="../../examples/yui/index.html">YUI (Global Prerequisite)</a></li><li class="item"><a title="Event - Functional Examples" href="../../examples/event/index.html">Event</a></li><li class="item"><a title="Node - Functional Examples" href="../../examples/node/index.html">Node</a></li><li class="sect">YUI 3 Component Infrastructure - Examples</li><li class="item"><a title="Attribute - Functional Examples" href="../../examples/attribute/index.html">Attribute</a></li><li class="item"><a title="Plugin - Functional Examples" href="../../examples/plugin/index.html">Plugin</a></li><li class="item"><a title="Widget - Functional Examples" href="../../examples/widget/index.html">Widget</a></li><li class="sect">YUI 3 Utilities - Examples</li><li class="item"><a title="Animation - Functional Examples" href="../../examples/anim/index.html">Animation</a></li><li class="item"><a title="Cache - Functional Examples" href="../../examples/cache/index.html">Cache</a></li><li class="item"><a title="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="DataSchema - Functional Examples" href="../../examples/dataschema/index.html">DataSchema</a></li><li class="item"><a title="DataSource - Functional Examples" href="../../examples/datasource/index.html">DataSource</a></li><li class="item"><a title="DataType - Functional Examples" href="../../examples/datatype/index.html">DataType</a></li><li class="item"><a title="Drag & Drop - Functional Examples" href="../../examples/dd/index.html">Drag & Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</a></li><li class="item"><a title="History - Functional Examples" href="../../examples/history/index.html">History</a></li><li class="item"><a title="ImageLoader - Functional Examples" href="../../examples/imageloader/index.html">ImageLoader</a></li><li class="item"><a title="IO - Functional Examples" href="../../examples/io/index.html">IO</a></li><li class="item"><a title="JSON (JavaScript Object Notation) - Functional Examples" href="../../examples/json/index.html">JSON</a></li><li class="item"><a title="Queue - Functional Examples" href="../../examples/queue/index.html">Queue</a></li><li class="item"><a title="Stylesheet - Functional Examples" href="../../examples/stylesheet/index.html">Stylesheet</a></li><li class="sect">YUI 3 Widgets - Examples</li><li class="item"><a title="Overlay - Functional Examples" href="../../examples/overlay/index.html">Overlay</a></li><li class="item"><a title="Slider - Functional Examples" href="../../examples/slider/index.html">Slider</a></li><li class="sect">YUI 3 Node Plugins - Examples</li><li class="item"><a title="FocusManager Node Plugin - Functional Examples" href="../../examples/node-focusmanager/index.html">FocusManager Node Plugin</a></li><li class="item"><a title="MenuNav Node Plugin - Functional Examples" href="../../examples/node-menunav/index.html">MenuNav Node Plugin</a></li><li class="sect">YUI 3 CSS - Examples</li><li class="item"><a title="YUI CSS Reset - Functional Examples" href="../../examples/cssreset/index.html">CSS Reset</a></li><li class="item"><a title="YUI Fonts - Functional Examples" href="../../examples/cssfonts/index.html">CSS Fonts</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</a></li><li class="sect">YUI 3 Developer Tools - Examples</li><li class="item"><a title="Console - Functional Examples" href="../../examples/console/index.html">Console</a></li><li class="item"><a title="Profiler - Functional Examples" href="../../examples/profiler/index.html">Profiler</a></li><li class="selected "><a title="Test - Functional Examples" href="../../examples/test/index.html">Test</a></li><li class="sect">The YUI Community</li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="item"><a title="The Yahoo! Group YDN-JavaScript hosts the YUI community forum" href="http://tech.groups.yahoo.com/group/ydn-javascript/">YUI Forum (external)</a></li><li class="item"><a title="The Yahoo! Group yui3 is dedicated to the 3.x branch of the Yahoo! User Interface (YUI) Library." href="http://tech.groups.yahoo.com/group/yui3/">YUI 3 Forum (external)</a></li><li class="item"><a title="YUI is used by Yahoo! and by hundreds of other sites, including many you know and love." href="/yui/poweredby/">YUI Sightings (external)</a></li><li class="item"><a title="Videos and podcasts from the YUI Team and from the Yahoo! frontend engineering community." href="http://developer.yahoo.com/yui/theater/">YUI Theater (external)</a></li><li class="sect">YUI Articles on the YUI Website</li><li class="item"><a title="Answers to Frequently Asked Questions about the YUI Library" href="http://developer.yahoo.com/yui/articles/faq/">YUI FAQ (external)</a></li><li class="item"><a title="Yahoo!'s philosophy of Graded Browser Support" href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support (external)</a></li><li class="item"><a title="Reporting Bugs and Making Feature Requests for YUI Components" href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests (external)</a></li><li class="item"><a title="Serve YUI source files from Yahoo! -- free, fast, and simple" href="http://developer.yahoo.com/yui/3/articles/hosting/">Serving YUI Files from Yahoo! (external)</a></li></ul> |
|
|
589 |
</div> |
|
|
590 |
</div> |
|
|
591 |
</div><!--closes bd--> |
|
|
592 |
|
|
|
593 |
<div id="ft"> |
|
|
594 |
<p class="first">Copyright © 2009 Yahoo! Inc. All rights reserved.</p> |
|
|
595 |
<p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> - |
|
|
596 |
<a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> - |
|
|
597 |
<a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> - |
|
|
598 |
<a href="http://careers.yahoo.com/">Job Openings</a></p> |
|
|
599 |
</div> |
|
|
600 |
</div> |
|
|
601 |
<script src="../../assets/dpSyntaxHighlighter.js"></script> |
|
|
602 |
<script language="javascript"> |
|
|
603 |
dp.SyntaxHighlighter.HighlightAll('code'); |
|
|
604 |
</script> |
|
|
605 |
</body> |
|
|
606 |
</html> |