/[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.2 - (hide annotations) (download) (as text)
Sun May 3 08:37:12 2009 UTC (17 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +58 -36 lines
File MIME type: application/javascript
implemented basic part of TAP

1 wakaba 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 wakaba 1.2 executeTestsByURL: function (url, fieldProps, code, ondone, onerror) {
69 wakaba 1.1 new SAMI.XHR (url, function () {
70     SAMI.Test.parseTestData (fieldProps, this.getText ()).forEach (function (test) {
71 wakaba 1.2 code (test);
72 wakaba 1.1 });
73 wakaba 1.2
74     if (ondone) ondone ();
75 wakaba 1.1 }, 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 wakaba 1.2 if (onerror) onerror (e);
81 wakaba 1.1 }).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 wakaba 1.2 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 wakaba 1.1 }, {
109 wakaba 1.2 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]+/g, ' ');
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]+/g, ' ');
121     }
122     this.out.say (s);
123     if (diag && diag.length) {
124     this.out.sayPrefixed ('# ', diag);
125     }
126     this.failedTestNumber++;
127 wakaba 1.1 }
128 wakaba 1.2 }, // ok
129     ng: function (expr, message, diag) {
130     this.ok (!expr, message, diag);
131     }, // ng
132 wakaba 1.1
133 wakaba 1.2 done: function (onsuccess, onfail) {
134     this.out.say ('1..' + this.lastTestNumber);
135     if (this.failedTestNumber == 0) {
136 wakaba 1.1 if (onsuccess) onsuccess.apply (this);
137 wakaba 1.2
138     SAMI.Element.deleteClassName (this.resultElement, 'FAIL');
139     SAMI.Element.addClassName (this.resultElement, 'PASS');
140     SAMI.Element.setTextContent (this.resultElement, 'PASS');
141 wakaba 1.1 } else {
142 wakaba 1.2 var s = this.failedTestNumber + ' tests failed';
143     this.out.sayPrefixed ('# ', s);
144 wakaba 1.1 if (onfail) onfail.apply (this);
145 wakaba 1.2
146     SAMI.Element.addClassName (this.resultElement, 'FAIL');
147     SAMI.Element.setTextContent (this.resultElement, 'FAIL (' + s + ')');
148     }
149     }, // done
150     abort: function (desc) {
151     var s = 'Ball out!';
152     if (desc && desc.length) {
153     s += ' - ' + desc.replace (/[\x0D\x0A]+/g, ' ');
154 wakaba 1.1 }
155 wakaba 1.2 this.out.say (s);
156 wakaba 1.1
157 wakaba 1.2 SAMI.Element.addClassName (this.resultElement, 'FAIL');
158     SAMI.Element.setTextContent (this.resultElement, 'FAIL (' + s + ')');
159     } // abort
160     }); // Manager
161 wakaba 1.1
162     /* --- Onload --- */
163    
164     if (SAMI.Test.onLoadFunctions) {
165     new SAMI.List (SAMI.Test.onLoadFunctions).forEach (function (code) {
166     code ();
167     });
168     delete SAMI.Test.onLoadFunctions;
169     }
170    
171     /* ***** BEGIN LICENSE BLOCK *****
172     * Copyright 2008-2009 Wakaba <[email protected]>. All rights reserved.
173     *
174     * This program is free software; you can redistribute it and/or
175     * modify it under the same terms as Perl itself.
176     *
177     * Alternatively, the contents of this file may be used
178     * under the following terms (the "MPL/GPL/LGPL"),
179     * in which case the provisions of the MPL/GPL/LGPL are applicable instead
180     * of those above. If you wish to allow use of your version of this file only
181     * under the terms of the MPL/GPL/LGPL, and not to allow others to
182     * use your version of this file under the terms of the Perl, indicate your
183     * decision by deleting the provisions above and replace them with the notice
184     * and other provisions required by the MPL/GPL/LGPL. If you do not delete
185     * the provisions above, a recipient may use your version of this file under
186     * the terms of any one of the Perl or the MPL/GPL/LGPL.
187     *
188     * "MPL/GPL/LGPL":
189     *
190     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
191     *
192     * The contents of this file are subject to the Mozilla Public License Version
193     * 1.1 (the "License"); you may not use this file except in compliance with
194     * the License. You may obtain a copy of the License at
195     * <http://www.mozilla.org/MPL/>
196     *
197     * Software distributed under the License is distributed on an "AS IS" basis,
198     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
199     * for the specific language governing rights and limitations under the
200     * License.
201     *
202     * The Original Code is sami-test.js code.
203     *
204     * The Initial Developer of the Original Code is Wakaba.
205     * Portions created by the Initial Developer are Copyright (C) 2008
206     * the Initial Developer. All Rights Reserved.
207     *
208     * Contributor(s):
209     * Wakaba <[email protected]>
210     *
211     * Alternatively, the contents of this file may be used under the terms of
212     * either the GNU General Public License Version 2 or later (the "GPL"), or
213     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
214     * in which case the provisions of the GPL or the LGPL are applicable instead
215     * of those above. If you wish to allow use of your version of this file only
216     * under the terms of either the GPL or the LGPL, and not to allow others to
217     * use your version of this file under the terms of the MPL, indicate your
218     * decision by deleting the provisions above and replace them with the notice
219     * and other provisions required by the LGPL or the GPL. If you do not delete
220     * the provisions above, a recipient may use your version of this file under
221     * the terms of any one of the MPL, the GPL or the LGPL.
222     *
223     * ***** END LICENSE BLOCK ***** */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24