| 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 |
|
|
executeTestsByURL: function (url, fieldProps, code, onsuccess, onfail) { |
| 69 |
|
|
new SAMI.XHR (url, function () { |
| 70 |
|
|
var results = new SAMI.Test.Results; |
| 71 |
|
|
SAMI.Test.parseTestData (fieldProps, this.getText ()).forEach (function (test) { |
| 72 |
|
|
var result = code (test); |
| 73 |
|
|
results.add (result); |
| 74 |
|
|
}); |
| 75 |
|
|
results.report (onsuccess, onfail); |
| 76 |
|
|
}, function () { |
| 77 |
|
|
var e = new SAMI.Event ('error'); |
| 78 |
|
|
e.type = 'cannot retrieve test file'; |
| 79 |
|
|
e.text = this.getSimpleErrorInfo (); |
| 80 |
|
|
e.label = 'm'; |
| 81 |
|
|
this.dispatchEvent (e); |
| 82 |
|
|
}).get (); |
| 83 |
|
|
} |
| 84 |
|
|
}); // SAMI.Test class methods |
| 85 |
|
|
|
| 86 |
|
|
SAMI.Test.Item = new SAMI.Class (function () { |
| 87 |
|
|
this.fields = new SAMI.Hash; |
| 88 |
|
|
}, { |
| 89 |
|
|
getField: function (fieldName) { |
| 90 |
|
|
return this.fields.get (fieldName); |
| 91 |
|
|
}, // getField |
| 92 |
|
|
hasField: function (fieldName) { |
| 93 |
|
|
return this.fields.has (fieldName); |
| 94 |
|
|
}, // hasField |
| 95 |
|
|
setField: function (fieldName, fieldValue, fieldOpt) { |
| 96 |
|
|
fieldValue.options = fieldOpt; |
| 97 |
|
|
this.fields.set (fieldName, fieldValue); |
| 98 |
|
|
} // setField |
| 99 |
|
|
}); |
| 100 |
|
|
|
| 101 |
|
|
SAMI.Test.Results = new SAMI.Class (function () { |
| 102 |
|
|
//this.results = new SAMI.List; |
| 103 |
|
|
this.successCount = 0; |
| 104 |
|
|
this.failCount = 0; |
| 105 |
|
|
}, { |
| 106 |
|
|
add: function (result) { |
| 107 |
|
|
if (result === true) { |
| 108 |
|
|
result = new Boolean (true); |
| 109 |
|
|
result.success = true; |
| 110 |
|
|
} else if (result === false) { |
| 111 |
|
|
result = new Boolean (true); |
| 112 |
|
|
result.success = false; |
| 113 |
|
|
} else if (!result) { |
| 114 |
|
|
// result = new SAMI.Test.Result (false, 'Test code did not return the result'); |
| 115 |
|
|
} |
| 116 |
|
|
|
| 117 |
|
|
//this.results.push (result); |
| 118 |
|
|
if (result && result.success) { |
| 119 |
|
|
this.successCount++; |
| 120 |
|
|
} else { |
| 121 |
|
|
this.failCount++; |
| 122 |
|
|
} |
| 123 |
|
|
}, // add |
| 124 |
|
|
report: function (onsuccess, onfail) { |
| 125 |
|
|
if (this.failCount == 0) { |
| 126 |
|
|
if (onsuccess) onsuccess.apply (this); |
| 127 |
|
|
} else { |
| 128 |
|
|
if (onfail) onfail.apply (this); |
| 129 |
|
|
} |
| 130 |
|
|
} // report |
| 131 |
|
|
}); // Results |
| 132 |
|
|
|
| 133 |
|
|
SAMI.Result = new SAMI.Class (function (isSuccess, message) { |
| 134 |
|
|
this.success = isSuccess; |
| 135 |
|
|
this.message = message || 'Null'; |
| 136 |
|
|
}, { |
| 137 |
|
|
|
| 138 |
|
|
}); // Result |
| 139 |
|
|
|
| 140 |
|
|
/* --- Onload --- */ |
| 141 |
|
|
|
| 142 |
|
|
if (SAMI.Test.onLoadFunctions) { |
| 143 |
|
|
new SAMI.List (SAMI.Test.onLoadFunctions).forEach (function (code) { |
| 144 |
|
|
code (); |
| 145 |
|
|
}); |
| 146 |
|
|
delete SAMI.Test.onLoadFunctions; |
| 147 |
|
|
} |
| 148 |
|
|
|
| 149 |
|
|
/* ***** BEGIN LICENSE BLOCK ***** |
| 150 |
|
|
* Copyright 2008-2009 Wakaba <[email protected]>. All rights reserved. |
| 151 |
|
|
* |
| 152 |
|
|
* This program is free software; you can redistribute it and/or |
| 153 |
|
|
* modify it under the same terms as Perl itself. |
| 154 |
|
|
* |
| 155 |
|
|
* Alternatively, the contents of this file may be used |
| 156 |
|
|
* under the following terms (the "MPL/GPL/LGPL"), |
| 157 |
|
|
* in which case the provisions of the MPL/GPL/LGPL are applicable instead |
| 158 |
|
|
* of those above. If you wish to allow use of your version of this file only |
| 159 |
|
|
* under the terms of the MPL/GPL/LGPL, and not to allow others to |
| 160 |
|
|
* use your version of this file under the terms of the Perl, indicate your |
| 161 |
|
|
* decision by deleting the provisions above and replace them with the notice |
| 162 |
|
|
* and other provisions required by the MPL/GPL/LGPL. If you do not delete |
| 163 |
|
|
* the provisions above, a recipient may use your version of this file under |
| 164 |
|
|
* the terms of any one of the Perl or the MPL/GPL/LGPL. |
| 165 |
|
|
* |
| 166 |
|
|
* "MPL/GPL/LGPL": |
| 167 |
|
|
* |
| 168 |
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 169 |
|
|
* |
| 170 |
|
|
* The contents of this file are subject to the Mozilla Public License Version |
| 171 |
|
|
* 1.1 (the "License"); you may not use this file except in compliance with |
| 172 |
|
|
* the License. You may obtain a copy of the License at |
| 173 |
|
|
* <http://www.mozilla.org/MPL/> |
| 174 |
|
|
* |
| 175 |
|
|
* Software distributed under the License is distributed on an "AS IS" basis, |
| 176 |
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 177 |
|
|
* for the specific language governing rights and limitations under the |
| 178 |
|
|
* License. |
| 179 |
|
|
* |
| 180 |
|
|
* The Original Code is sami-test.js code. |
| 181 |
|
|
* |
| 182 |
|
|
* The Initial Developer of the Original Code is Wakaba. |
| 183 |
|
|
* Portions created by the Initial Developer are Copyright (C) 2008 |
| 184 |
|
|
* the Initial Developer. All Rights Reserved. |
| 185 |
|
|
* |
| 186 |
|
|
* Contributor(s): |
| 187 |
|
|
* Wakaba <[email protected]> |
| 188 |
|
|
* |
| 189 |
|
|
* Alternatively, the contents of this file may be used under the terms of |
| 190 |
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 191 |
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 192 |
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 193 |
|
|
* of those above. If you wish to allow use of your version of this file only |
| 194 |
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 195 |
|
|
* use your version of this file under the terms of the MPL, indicate your |
| 196 |
|
|
* decision by deleting the provisions above and replace them with the notice |
| 197 |
|
|
* and other provisions required by the LGPL or the GPL. If you do not delete |
| 198 |
|
|
* the provisions above, a recipient may use your version of this file under |
| 199 |
|
|
* the terms of any one of the MPL, the GPL or the LGPL. |
| 200 |
|
|
* |
| 201 |
|
|
* ***** END LICENSE BLOCK ***** */ |