/[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.3 - (hide annotations) (download) (as text)
Mon May 4 07:44:46 2009 UTC (17 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.2: +7 -0 lines
File MIME type: application/javascript
implemented template macrosyntax tokenization

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24