Ox.test: eval all scripts; add stringifyResult method; properly stringify actual async result; in async path, rename 'result' to 'actual'

This commit is contained in:
rolux 2012-06-24 15:40:48 +02:00
parent 966ceeb1f1
commit 9f1b86ab87

View file

@ -823,18 +823,12 @@ Ox.test = function(argument, callback) {
"$1'" + statement.replace(/'/g, "\\'") + "', " "$1'" + statement.replace(/'/g, "\\'") + "', "
); );
} }
if (test.expected || statement.match(/Ox\.test\./)) { Ox.Log('TEST', statement);
// Eval the statement, unless it's a script tag that doesn't actual = eval(statement);
// add a property to Ox.test Ox.print(JSON.stringify(actual))
Ox.Log('TEST', statement);
actual = eval(statement);
}
if (!isAsync && test.expected) { if (!isAsync && test.expected) {
Ox.test.data[id].results.push({ Ox.test.data[id].results.push({
actual: Ox.isEqual(actual, -0) ? '-0' actual: stringifyResult(actual),
: Ox.isNaN(actual) ? 'NaN'
: Ox.isUndefined(actual) ? 'undefined'
: JSON.stringify(actual),
expected: test.expected, expected: test.expected,
name: item.name, name: item.name,
section: item.section, section: item.section,
@ -852,6 +846,12 @@ Ox.test = function(argument, callback) {
delete Ox.test.data[id]; delete Ox.test.data[id];
} }
} }
function stringifyResult(result) {
return Ox.isEqual(result, -0) ? '-0'
: Ox.isNaN(result) ? 'NaN'
: Ox.isUndefined(result) ? 'undefined'
: JSON.stringify(result);
}
if (arguments.length == 2) { if (arguments.length == 2) {
if (Ox.typeOf(argument) == 'string' && Ox.contains(argument, '\n')) { if (Ox.typeOf(argument) == 'string' && Ox.contains(argument, '\n')) {
// source code // source code
@ -868,7 +868,7 @@ Ox.test = function(argument, callback) {
} }
} else { } else {
var statement = arguments[0], var statement = arguments[0],
result = arguments[1], actual = arguments[1],
expected = arguments[2], expected = arguments[2],
id, test; id, test;
Ox.forEach(Ox.test.data, function(v, k) { Ox.forEach(Ox.test.data, function(v, k) {
@ -879,9 +879,9 @@ Ox.test = function(argument, callback) {
} }
}); });
Ox.test.data[id].results.push(Ox.extend(test, { Ox.test.data[id].results.push(Ox.extend(test, {
actual: result, actual: stringifyResult(actual),
statement: statement, statement: statement,
passed: Ox.isEqual(result, expected) passed: Ox.isEqual(actual, expected)
})); }));
delete Ox.test.data[id].tests[statement]; delete Ox.test.data[id].tests[statement];
if (Ox.test.data[id].done && Ox.isEmpty(Ox.test.data[id].tests)) { if (Ox.test.data[id].done && Ox.isEmpty(Ox.test.data[id].tests)) {