/[suikacvs]/test/suikawebwww/www/js/sami/script/sami-test.js
Suika

Contents of /test/suikawebwww/www/js/sami/script/sami-test.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download) (as text)
Sun Jun 7 09:58:31 2009 UTC (17 years, 3 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +10 -6 lines
File MIME type: application/javascript
implemented LR(1)

1 /* Requires sami-core.js */
2
3 if (!SAMI.Test) SAMI.Test = {};
4
5 // Events: error
6 SAMI.Class.addClassMethods (SAMI.Test, {
7 parseTestData: function (fieldProps, s) {
8 var tests = new SAMI.List;
9
10 var s = s.replace (/\x0D\x0A/g, "\x0A")
11 .replace (/\x0D/g, "\x0A")
12 .replace (/^\x0A*#/, '')
13 .replace (/\x0A+$/, '')
14 .split (/\x0A\x0A#/);
15 s = new SAMI.List (s);
16 s.forEach (function (_) {
17 var test = new SAMI.Test.Item;
18
19 new SAMI.List (_.split (/\x0A#/)).forEach (function (v) {
20 var fieldName = '';
21 var fieldOpt = new SAMI.List;
22 v = v.replace (/^([A-Za-z0-9-]+)/, function (s) {
23 fieldName = s;
24 return '';
25 }).replace (/^([^\x0A]*)(?:\x0A|$)/, function (_, s) {
26 fieldOpt = SAMI.List.spaceSeparated (s);
27 return '';
28 });
29
30 var fieldProp = fieldProps[fieldName] || {};
31 if (fieldProp.isPrefixed) {
32 v = v.replace (/^\| /, '').replace (/\x0A\| /g, "\x0A");
33 }
34
35 if (fieldProp.isList) {
36 v = new SAMI.List (v.split (/\x0A/));
37 if (fieldOpt.getLast () == 'escaped') {
38 fieldOpt.pop ();
39 v = v.map (function (item) {
40 return SAMI.String.uUnescape (item);
41 });
42 }
43 } else { // Not a list filed
44 if (fieldOpt.getLast () == 'escaped') {
45 fieldOpt.pop ();
46 v = SAMI.String.uUnescape (v);
47 }
48 }
49
50 if (test.hasField (fieldName)) {
51 var e = new SAMI.Event ('error');
52 e.type = 'duplicate field';
53 e.text = fieldName;
54 e.value = v;
55 e.level = 'w';
56 this.dispatchEvent (e);
57 } else {
58 test.setField (fieldName, v, fieldOpt);
59 }
60 }); // field
61
62 tests.push (test);
63 }); // test
64
65 return tests;
66 }, // parseTestData
67
68 executeTestsByURL: function (url, fieldProps, code, ondone, onerror) {
69 new SAMI.XHR (url, function () {
70 SAMI.Test.parseTestData (fieldProps, this.getText ()).forEach (function (test) {
71 code (test);
72 });
73
74 if (ondone) ondone ();
75 }, function () {
76 var e = new SAMI.Event ('error');
77 e.type = 'cannot retrieve test file';
78 e.text = this.getSimpleErrorInfo ();
79 e.label = 'm';
80 if (onerror) onerror (e);
81 }).get ();
82 }
83 }); // SAMI.Test class methods
84
85 SAMI.Test.Item = new SAMI.Class (function () {
86 this.fields = new SAMI.Hash;
87 }, {
88 getField: function (fieldName) {
89 return this.fields.get (fieldName);
90 }, // getField
91 hasField: function (fieldName) {
92 return this.fields.has (fieldName);
93 }, // hasField
94 setField: function (fieldName, fieldValue, fieldOpt) {
95 fieldValue.options = fieldOpt;
96 this.fields.set (fieldName, fieldValue);
97 } // setField
98 });
99
100 SAMI.Test.Manager = new SAMI.Class (function (resultElement, out) {
101 this.resultElement = resultElement;
102 SAMI.Element.addClassName (resultElement, 'FAIL');
103 SAMI.Element.setTextContent (resultElement, 'FAIL (script error)');
104
105 this.out = out;
106 this.lastTestNumber = 0;
107 this.failedTestNumber = 0;
108 }, {
109 ok: function (expr, desc, diag) {
110 var n = ++this.lastTestNumber;
111 if (expr) {
112 var s = 'ok ' + n;
113 if (desc && desc.length) {
114 s += ' - ' + desc.replace (/\x0D\x0A?|\x0A/g, "\\n");
115 }
116 this.out.say (s);
117 } else {
118 var s = 'not ok ' + n;
119 if (desc && desc.length) {
120 s += ' - ' + desc.replace (/\x0D\x0A?|\x0A/g, "\\n");
121 }
122 this.out.say (s);
123 if (diag && diag.length) {
124 this.out.sayPrefixed ('# ', diag);
125 }
126 this.failedTestNumber++;
127 }
128 }, // ok
129 ng: function (expr, message, diag) {
130 this.ok (!expr, message, diag);
131 }, // ng
132
133 is: function (actual, expected, message) {
134 this.ok (actual == expected, message, 'Actual value: "' + actual + '", Expected value: "' + expected + '"');
135 }, // is
136 isnt: function (actual, expected, message) {
137 this.ok (actual != expected, message, 'Actual value: "' + actual + '", Expected value: not "' + expected + '"');
138 }, // isnt
139
140 done: function (onsuccess, onfail) {
141 this.out.say ('1..' + this.lastTestNumber);
142 if (this.failedTestNumber == 0) {
143 if (onsuccess) onsuccess.apply (this);
144
145 SAMI.Element.deleteClassName (this.resultElement, 'FAIL');
146 SAMI.Element.addClassName (this.resultElement, 'PASS');
147 SAMI.Element.setTextContent (this.resultElement, 'PASS');
148 } else {
149 var s = this.failedTestNumber + ' tests failed';
150 this.out.sayPrefixed ('# ', s);
151 if (onfail) onfail.apply (this);
152
153 SAMI.Element.addClassName (this.resultElement, 'FAIL');
154 SAMI.Element.setTextContent (this.resultElement, 'FAIL (' + s + ')');
155 }
156 }, // done
157 abort: function (desc) {
158 var s = 'Ball out!';
159 if (desc && desc.length) {
160 s += ' - ' + desc.replace (/\x0D\x0A?|\x0A/g, "\\n");
161 }
162 this.out.say (s);
163
164 SAMI.Element.addClassName (this.resultElement, 'FAIL');
165 SAMI.Element.setTextContent (this.resultElement, 'FAIL (' + s + ')');
166 } // abort
167 }); // Manager
168
169 /* --- Utility --- */
170
171 SAMI.Test.Utility = {};
172 SAMI.Class.addClassMethods (SAMI.Test.Utility, {
173 getStatusElement: function () {
174 var el = document.getElementById ('test-status');
175 if (!el) {
176 el = document.createElement ('pre');
177 el.id = 'test-status';
178 if (document.body) {
179 document.body.insertBefore (el, document.body.firstChild);
180 } else {
181 document.documentElement.appendChild (el);
182 }
183 }
184 return el;
185 }, // getStatusElement
186
187 out: function (s) {
188 var el = SAMI.Test.Utility.getStatusElement ();
189 el.insertBefore (el.ownerDocument.createTextNode (s + "\n"), el.firstChild);
190 }, // out
191
192 outn: function (s) {
193 SAMI.Test.Utility.out (s + "\n");
194 }, // outn
195
196 install: function () {
197 for (var p in {out: true, outn: true}) {
198 self[p] = this[p];
199 }
200 } // install
201 }); // SAMI.Test.Utility class methods
202
203 /* --- Onload --- */
204
205 if (SAMI.Test.onLoadFunctions) {
206 new SAMI.List (SAMI.Test.onLoadFunctions).forEach (function (code) {
207 code ();
208 });
209 delete SAMI.Test.onLoadFunctions;
210 }
211
212 /* ***** BEGIN LICENSE BLOCK *****
213 * Copyright 2008-2009 Wakaba <[email protected]>. All rights reserved.
214 *
215 * This program is free software; you can redistribute it and/or
216 * modify it under the same terms as Perl itself.
217 *
218 * Alternatively, the contents of this file may be used
219 * under the following terms (the "MPL/GPL/LGPL"),
220 * in which case the provisions of the MPL/GPL/LGPL are applicable instead
221 * of those above. If you wish to allow use of your version of this file only
222 * under the terms of the MPL/GPL/LGPL, and not to allow others to
223 * use your version of this file under the terms of the Perl, indicate your
224 * decision by deleting the provisions above and replace them with the notice
225 * and other provisions required by the MPL/GPL/LGPL. If you do not delete
226 * the provisions above, a recipient may use your version of this file under
227 * the terms of any one of the Perl or the MPL/GPL/LGPL.
228 *
229 * "MPL/GPL/LGPL":
230 *
231 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
232 *
233 * The contents of this file are subject to the Mozilla Public License Version
234 * 1.1 (the "License"); you may not use this file except in compliance with
235 * the License. You may obtain a copy of the License at
236 * <http://www.mozilla.org/MPL/>
237 *
238 * Software distributed under the License is distributed on an "AS IS" basis,
239 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
240 * for the specific language governing rights and limitations under the
241 * License.
242 *
243 * The Original Code is sami-test.js code.
244 *
245 * The Initial Developer of the Original Code is Wakaba.
246 * Portions created by the Initial Developer are Copyright (C) 2008
247 * the Initial Developer. All Rights Reserved.
248 *
249 * Contributor(s):
250 * Wakaba <[email protected]>
251 *
252 * Alternatively, the contents of this file may be used under the terms of
253 * either the GNU General Public License Version 2 or later (the "GPL"), or
254 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
255 * in which case the provisions of the GPL or the LGPL are applicable instead
256 * of those above. If you wish to allow use of your version of this file only
257 * under the terms of either the GPL or the LGPL, and not to allow others to
258 * use your version of this file under the terms of the MPL, indicate your
259 * decision by deleting the provisions above and replace them with the notice
260 * and other provisions required by the LGPL or the GPL. If you do not delete
261 * the provisions above, a recipient may use your version of this file under
262 * the terms of any one of the MPL, the GPL or the LGPL.
263 *
264 * ***** END LICENSE BLOCK ***** */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24