|
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 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>Array Processing</title> |
|
|
7 |
|
|
|
8 |
<style type="text/css"> |
|
|
9 |
/*margin and padding on body element |
|
|
10 |
can introduce errors in determining |
|
|
11 |
element position and are not recommended; |
|
|
12 |
we turn them off as a foundation for YUI |
|
|
13 |
CSS treatments. */ |
|
|
14 |
body { |
|
|
15 |
margin:0; |
|
|
16 |
padding:0; |
|
|
17 |
} |
|
|
18 |
</style> |
|
|
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 |
|
|
|
24 |
<!--begin custom header content for this example--> |
|
|
25 |
<style type="text/css"> |
|
|
26 |
#testLogger { |
|
|
27 |
margin-bottom: 1em; |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
#testLogger .yui-console .yui-console-title { |
|
|
31 |
border: 0 none; |
|
|
32 |
color: #000; |
|
|
33 |
font-size: 13px; |
|
|
34 |
font-weight: bold; |
|
|
35 |
margin: 0; |
|
|
36 |
text-transform: none; |
|
|
37 |
} |
|
|
38 |
#testLogger .yui-console .yui-console-entry-meta { |
|
|
39 |
margin: 0; |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
.yui-skin-sam .yui-console-entry-pass .yui-console-entry-cat { |
|
|
43 |
background: #070; |
|
|
44 |
color: #fff; |
|
|
45 |
} |
|
|
46 |
</style> |
|
|
47 |
|
|
|
48 |
<!--end custom header content for this example--> |
|
|
49 |
|
|
|
50 |
</head> |
|
|
51 |
|
|
|
52 |
<body class=" yui-skin-sam"> |
|
|
53 |
|
|
|
54 |
<h1>Array Processing</h1> |
|
|
55 |
|
|
|
56 |
<div class="exampleIntro"> |
|
|
57 |
<p>This example shows how to use the <a href="/yui/test/#arrayassert"><code>ArrayAssert</code></a> object, which |
|
|
58 |
contains assertions designed to be used specifically with JavaScript Arrays and array-like objects.</p> |
|
|
59 |
</div> |
|
|
60 |
|
|
|
61 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
62 |
|
|
|
63 |
<div id="testLogger"></div> |
|
|
64 |
<script type="text/javascript"> |
|
|
65 |
YUI({base:"../../build/", timeout: 10000}).use("node", "console", "test",function (Y) { |
|
|
66 |
|
|
|
67 |
Y.namespace("example.test"); |
|
|
68 |
|
|
|
69 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
70 |
|
|
|
71 |
//the name of the test case - if not provided, one is automatically generated |
|
|
72 |
name: "Array Tests", |
|
|
73 |
|
|
|
74 |
//------------------------------------------------------------------------- |
|
|
75 |
// Setup and teardown |
|
|
76 |
//------------------------------------------------------------------------- |
|
|
77 |
|
|
|
78 |
/* |
|
|
79 |
* The setUp() method is used to setup data that necessary for a test to |
|
|
80 |
* run. This method is called immediately before each test method is run, |
|
|
81 |
* so it is run as many times as there are test methods. |
|
|
82 |
*/ |
|
|
83 |
setUp : function () { |
|
|
84 |
this.data = new Array (0,1,2,3,4,5); |
|
|
85 |
}, |
|
|
86 |
|
|
|
87 |
/* |
|
|
88 |
* The tearDown() method is used to clean up the initialization that was done |
|
|
89 |
* in the setUp() method. Ideally, it should free up all memory allocated in |
|
|
90 |
* setUp(), anticipating any possible changes to the data. This method is called |
|
|
91 |
* immediately after each test method is run. |
|
|
92 |
*/ |
|
|
93 |
tearDown : function () { |
|
|
94 |
delete this.data; |
|
|
95 |
}, |
|
|
96 |
|
|
|
97 |
//------------------------------------------------------------------------- |
|
|
98 |
// Basic tests - all method names must begin with "test" |
|
|
99 |
//------------------------------------------------------------------------- |
|
|
100 |
|
|
|
101 |
/* |
|
|
102 |
* Test the push() method. |
|
|
103 |
*/ |
|
|
104 |
testPush : function() { |
|
|
105 |
|
|
|
106 |
//shortcut variables |
|
|
107 |
var ArrayAssert = Y.ArrayAssert; |
|
|
108 |
|
|
|
109 |
//do whatever data manipulation is necessary |
|
|
110 |
this.data.push(6); |
|
|
111 |
|
|
|
112 |
//array-specific assertions |
|
|
113 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
114 |
ArrayAssert.contains(6, this.data, "Array should contain 6."); |
|
|
115 |
ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6."); |
|
|
116 |
|
|
|
117 |
//check that all the values are there |
|
|
118 |
ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal."); |
|
|
119 |
|
|
|
120 |
}, |
|
|
121 |
|
|
|
122 |
/* |
|
|
123 |
* Test the pop() method. |
|
|
124 |
*/ |
|
|
125 |
testPop : function() { |
|
|
126 |
|
|
|
127 |
//shortcut variables |
|
|
128 |
var Assert = Y.Assert; |
|
|
129 |
var ArrayAssert = Y.ArrayAssert; |
|
|
130 |
|
|
|
131 |
//do whatever data manipulation is necessary |
|
|
132 |
var value = this.data.pop(); |
|
|
133 |
|
|
|
134 |
//array shouldn't be empty |
|
|
135 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
136 |
|
|
|
137 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
138 |
Assert.areEqual(5, this.data.length, "Array should have 5 items."); |
|
|
139 |
Assert.areEqual(5, value, "Value should be 5."); |
|
|
140 |
|
|
|
141 |
ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal."); |
|
|
142 |
}, |
|
|
143 |
|
|
|
144 |
/* |
|
|
145 |
* Test the reverse() method. |
|
|
146 |
*/ |
|
|
147 |
testReverse : function() { |
|
|
148 |
|
|
|
149 |
//shortcut variables |
|
|
150 |
var ArrayAssert = Y.ArrayAssert; |
|
|
151 |
|
|
|
152 |
//do whatever data manipulation is necessary |
|
|
153 |
this.data = this.data.reverse(); |
|
|
154 |
|
|
|
155 |
ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal."); |
|
|
156 |
}, |
|
|
157 |
|
|
|
158 |
/* |
|
|
159 |
* Test the shift() method. |
|
|
160 |
*/ |
|
|
161 |
testShift : function() { |
|
|
162 |
|
|
|
163 |
//shortcut variables |
|
|
164 |
var Assert = Y.Assert; |
|
|
165 |
var ArrayAssert = Y.ArrayAssert; |
|
|
166 |
|
|
|
167 |
//do whatever data manipulation is necessary |
|
|
168 |
var value = this.data.shift(); |
|
|
169 |
|
|
|
170 |
//array shouldn't be empty |
|
|
171 |
ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); |
|
|
172 |
|
|
|
173 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
174 |
Assert.areEqual(5, this.data.length, "Array should have 6 items."); |
|
|
175 |
Assert.areEqual(0, value, "Value should be 0."); |
|
|
176 |
|
|
|
177 |
ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal."); |
|
|
178 |
}, |
|
|
179 |
|
|
|
180 |
/* |
|
|
181 |
* Test the splice() method. |
|
|
182 |
*/ |
|
|
183 |
testSplice : function() { |
|
|
184 |
|
|
|
185 |
//shortcut variables |
|
|
186 |
var Assert = Y.Assert; |
|
|
187 |
var ArrayAssert = Y.ArrayAssert; |
|
|
188 |
|
|
|
189 |
//do whatever data manipulation is necessary |
|
|
190 |
var removed = this.data.splice(1, 2, 99, 100); |
|
|
191 |
|
|
|
192 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
193 |
Assert.areEqual(6, this.data.length, "Array should have 6 items."); |
|
|
194 |
|
|
|
195 |
//the new items should be there |
|
|
196 |
ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99."); |
|
|
197 |
ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100."); |
|
|
198 |
|
|
|
199 |
ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal."); |
|
|
200 |
ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2."); |
|
|
201 |
|
|
|
202 |
}, |
|
|
203 |
|
|
|
204 |
/* |
|
|
205 |
* Test the unshift() method. |
|
|
206 |
*/ |
|
|
207 |
testUnshift : function() { |
|
|
208 |
|
|
|
209 |
//shortcut variables |
|
|
210 |
var Assert = Y.Assert; |
|
|
211 |
var ArrayAssert = Y.ArrayAssert; |
|
|
212 |
|
|
|
213 |
//do whatever data manipulation is necessary |
|
|
214 |
this.data.unshift(-1); |
|
|
215 |
|
|
|
216 |
//basic equality assertion - expected value, actual value, optional error message |
|
|
217 |
Assert.areEqual(7, this.data.length, "Array should have 7 items."); |
|
|
218 |
|
|
|
219 |
//the new item should be there |
|
|
220 |
ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1."); |
|
|
221 |
|
|
|
222 |
ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal."); |
|
|
223 |
} |
|
|
224 |
|
|
|
225 |
}); |
|
|
226 |
|
|
|
227 |
//create the console |
|
|
228 |
var r = new Y.Console({ |
|
|
229 |
newestOnTop : false, |
|
|
230 |
style: 'block' // to anchor in the example content |
|
|
231 |
}); |
|
|
232 |
|
|
|
233 |
r.render('#testLogger'); |
|
|
234 |
|
|
|
235 |
Y.Test.Runner.add(Y.example.test.ArrayTestCase); |
|
|
236 |
|
|
|
237 |
//run the tests |
|
|
238 |
Y.Test.Runner.run(); |
|
|
239 |
}); |
|
|
240 |
|
|
|
241 |
</script> |
|
|
242 |
|
|
|
243 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
244 |
|
|
|
245 |
</body> |
|
|
246 |
</html> |