|
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>Simple Testing Example</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>Simple Testing Example</h1> |
|
|
55 |
|
|
|
56 |
<div class="exampleIntro"> |
|
|
57 |
<p>This example shows basic usage of the YUI Test framework for testing browser-based JavaScript code. |
|
|
58 |
Two different <a href="/yui/test/#testcase"><code>TestCase</code></a> objects are created and added to a |
|
|
59 |
<a href="/yui/test/#testsuite"><code>TestSuite</code></a> object. The <a href="/yui/test/#testrunner"><code>TestRunner</code></a> |
|
|
60 |
is then used to run the tests once the page has loaded.</p> |
|
|
61 |
</div> |
|
|
62 |
|
|
|
63 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
64 |
|
|
|
65 |
<div id="testLogger"></div> |
|
|
66 |
<script type="text/javascript"> |
|
|
67 |
YUI({base:"../../build/", timeout: 10000}).use("node", "console", "test",function (Y) { |
|
|
68 |
|
|
|
69 |
Y.namespace("example.test"); |
|
|
70 |
|
|
|
71 |
Y.example.test.DataTestCase = new Y.Test.Case({ |
|
|
72 |
|
|
|
73 |
//name of the test case - if not provided, one is auto-generated |
|
|
74 |
name : "Data Tests", |
|
|
75 |
|
|
|
76 |
//--------------------------------------------------------------------- |
|
|
77 |
// setUp and tearDown methods - optional |
|
|
78 |
//--------------------------------------------------------------------- |
|
|
79 |
|
|
|
80 |
/* |
|
|
81 |
* Sets up data that is needed by each test. |
|
|
82 |
*/ |
|
|
83 |
setUp : function () { |
|
|
84 |
this.data = { |
|
|
85 |
name: "test", |
|
|
86 |
year: 2007, |
|
|
87 |
beta: true |
|
|
88 |
}; |
|
|
89 |
}, |
|
|
90 |
|
|
|
91 |
/* |
|
|
92 |
* Cleans up everything that was created by setUp(). |
|
|
93 |
*/ |
|
|
94 |
tearDown : function () { |
|
|
95 |
delete this.data; |
|
|
96 |
}, |
|
|
97 |
|
|
|
98 |
//--------------------------------------------------------------------- |
|
|
99 |
// Test methods - names must begin with "test" |
|
|
100 |
//--------------------------------------------------------------------- |
|
|
101 |
|
|
|
102 |
testName : function () { |
|
|
103 |
var Assert = Y.Assert; |
|
|
104 |
|
|
|
105 |
Assert.isObject(this.data); |
|
|
106 |
Assert.isString(this.data.name); |
|
|
107 |
Assert.areEqual("test", this.data.name); |
|
|
108 |
}, |
|
|
109 |
|
|
|
110 |
testYear : function () { |
|
|
111 |
var Assert = Y.Assert; |
|
|
112 |
|
|
|
113 |
Assert.isObject(this.data); |
|
|
114 |
Assert.isNumber(this.data.year); |
|
|
115 |
Assert.areEqual(2007, this.data.year); |
|
|
116 |
}, |
|
|
117 |
|
|
|
118 |
testBeta : function () { |
|
|
119 |
var Assert = Y.Assert; |
|
|
120 |
|
|
|
121 |
Assert.isObject(this.data); |
|
|
122 |
Assert.isBoolean(this.data.beta); |
|
|
123 |
Assert.isTrue(this.data.beta); |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
}); |
|
|
127 |
|
|
|
128 |
Y.example.test.ArrayTestCase = new Y.Test.Case({ |
|
|
129 |
|
|
|
130 |
//name of the test case - if not provided, one is auto-generated |
|
|
131 |
name : "Array Tests", |
|
|
132 |
|
|
|
133 |
//--------------------------------------------------------------------- |
|
|
134 |
// setUp and tearDown methods - optional |
|
|
135 |
//--------------------------------------------------------------------- |
|
|
136 |
|
|
|
137 |
/* |
|
|
138 |
* Sets up data that is needed by each test. |
|
|
139 |
*/ |
|
|
140 |
setUp : function () { |
|
|
141 |
this.data = [0,1,2,3,4] |
|
|
142 |
}, |
|
|
143 |
|
|
|
144 |
/* |
|
|
145 |
* Cleans up everything that was created by setUp(). |
|
|
146 |
*/ |
|
|
147 |
tearDown : function () { |
|
|
148 |
delete this.data; |
|
|
149 |
}, |
|
|
150 |
|
|
|
151 |
//--------------------------------------------------------------------- |
|
|
152 |
// Test methods - names must begin with "test" |
|
|
153 |
//--------------------------------------------------------------------- |
|
|
154 |
|
|
|
155 |
testPop : function () { |
|
|
156 |
var Assert = Y.Assert; |
|
|
157 |
|
|
|
158 |
var value = this.data.pop(); |
|
|
159 |
|
|
|
160 |
Assert.areEqual(4, this.data.length); |
|
|
161 |
Assert.areEqual(4, value); |
|
|
162 |
}, |
|
|
163 |
|
|
|
164 |
testPush : function () { |
|
|
165 |
var Assert = Y.Assert; |
|
|
166 |
|
|
|
167 |
this.data.push(5); |
|
|
168 |
|
|
|
169 |
Assert.areEqual(6, this.data.length); |
|
|
170 |
Assert.areEqual(5, this.data[5]); |
|
|
171 |
}, |
|
|
172 |
|
|
|
173 |
testSplice : function () { |
|
|
174 |
var Assert = Y.Assert; |
|
|
175 |
|
|
|
176 |
this.data.splice(2, 1, 6, 7); |
|
|
177 |
|
|
|
178 |
Assert.areEqual(6, this.data.length); |
|
|
179 |
Assert.areEqual(6, this.data[2]); |
|
|
180 |
Assert.areEqual(7, this.data[3]); |
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
}); |
|
|
184 |
|
|
|
185 |
Y.example.test.ExampleSuite = new Y.Test.Suite("Example Suite"); |
|
|
186 |
Y.example.test.ExampleSuite.add(Y.example.test.DataTestCase); |
|
|
187 |
Y.example.test.ExampleSuite.add(Y.example.test.ArrayTestCase); |
|
|
188 |
|
|
|
189 |
//create the console |
|
|
190 |
var r = new Y.Console({ |
|
|
191 |
newestOnTop : false, |
|
|
192 |
style: 'block' // to anchor in the example content |
|
|
193 |
}); |
|
|
194 |
|
|
|
195 |
r.render('#testLogger'); |
|
|
196 |
|
|
|
197 |
Y.Test.Runner.add(Y.example.test.ExampleSuite); |
|
|
198 |
|
|
|
199 |
//run the tests |
|
|
200 |
Y.Test.Runner.run(); |
|
|
201 |
|
|
|
202 |
}); |
|
|
203 |
</script> |
|
|
204 |
|
|
|
205 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
206 |
|
|
|
207 |
</body> |
|
|
208 |
</html> |