~lutostag/ubuntu/utopic/maas/1.5.2

« back to all changes in this revision

Viewing changes to src/maasserver/static/jslibs/yui/3.4.1/docs/test/test-array-tests.html

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-03-15 18:14:08 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120315181408-zgl94hzo0x4n99an
Tags: 0.1+bzr295+dfsg-0ubuntu2
* debian/patches:
  - 01-fix-database-settings.patch: Update to set PSERV_URL.
  - 02-pserv-config.patch: Set port to 8001.
* debian/maas.postinst: Run maas-import-isos on install.
* debian/control: Depends on rabbitmq-server.

Show diffs side-by-side

added added

removed removed

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