simplify async test syntax, allow for files with ?random suffix

This commit is contained in:
rolux 2012-05-27 18:40:51 +02:00
parent 04e2326808
commit f3b0bb6c86

View file

@ -294,7 +294,7 @@ Ox.doc = (function() {
counter = 0, items = []; counter = 0, items = [];
files && files.forEach(function(file) { files && files.forEach(function(file) {
Ox.get(file, function(source) { Ox.get(file, function(source) {
items = items.concat(parseSource(source, file)); items = items.concat(parseSource(source, file.split('?')[0]));
++counter == files.length && callback(items); ++counter == files.length && callback(items);
}); });
}); });
@ -545,29 +545,37 @@ Ox.minify = function() {
Ox.test <f> Takes JavaScript, runs inline tests, returns results Ox.test <f> Takes JavaScript, runs inline tests, returns results
@*/ @*/
Ox.test = function(file, callback) { Ox.test = function(file, callback) {
var regexp = /(Ox\.test\()/;
if (arguments.length == 2) {
Ox.doc(file, function(items) {
var results = [];
file = file.split('?')[0];
Ox.test.data[file] = { Ox.test.data[file] = {
callback: callback, callback: callback,
done: false, done: false,
results: [], results: [],
tests: {} tests: {}
}; };
Ox.doc(file, function(items) {
var results = [];
items.forEach(function(item) { items.forEach(function(item) {
var actual, match;
item.tests && item.tests.some(function(test) { item.tests && item.tests.some(function(test) {
return test.result; return test.result;
}) && item.tests.forEach(function(test) { }) && item.tests.forEach(function(test) {
Ox.Log('TEST', test.statement); var actual, isAsync = regexp.test(test.statement);
actual = eval(test.statement); if (isAsync) {
match = test.statement.match(/Ox\.test\.async\('([\w\.]+)'/); Ox.test.data[file].tests[item.name] = {
if (match) {
Ox.test.data[file].tests[match[1]] = {
section: item.section, section: item.section,
statement: test.statement statement: test.statement
}; };
} else { test.statement = test.statement.replace(
if (test.result) { regexp, '$1\'' + item.name + '\', '
);
}
if (test.result || test.statement.match(/Ox\.test/)) {
// don't eval script tags without assignment to Ox.test.foo
actual = eval(test.statement);
Ox.Log('TEST', test.statement);
}
if (!isAsync && test.result) {
Ox.test.data[file].results.push({ Ox.test.data[file].results.push({
actual: JSON.stringify(actual), actual: JSON.stringify(actual),
expected: test.result, expected: test.result,
@ -579,7 +587,6 @@ Ox.test = function(file, callback) {
), actual) ), actual)
}); });
} }
}
}); });
}); });
Ox.test.data[file].done = true; Ox.test.data[file].done = true;
@ -587,27 +594,32 @@ Ox.test = function(file, callback) {
callback(Ox.test.data[file].results); callback(Ox.test.data[file].results);
} }
}); });
}; } else {
Ox.test.async = function(name, result) { var name = arguments[0],
var file; result = arguments[1],
expected = arguments[2];
file = null;
Ox.forEach(Ox.test.data, function(v, k) { Ox.forEach(Ox.test.data, function(v, k) {
if (v.tests[name]) { if (v.tests[name]) {
file = k; file = k;
Ox.Break(); Ox.Break();
} }
}); });
Ox.print('FILE::', file, name, Ox.test.data)
Ox.test.data[file].results.push({ Ox.test.data[file].results.push({
actual: result, actual: result,
expected: true, expected: expected,
name: name, name: name,
section: Ox.test.data[file].tests[name].section, section: Ox.test.data[file].tests[name].section,
statement: Ox.test.data[file].tests[name].statement, statement: Ox.test.data[file].tests[name].statement,
passed: result passed: Ox.isEqual(result, expected)
}); });
delete Ox.test.data[file].tests[name]; delete Ox.test.data[file].tests[name];
Ox.print('????::', file, name, Ox.test.data[file])
if (Ox.test.data[file].done && Ox.isEmpty(Ox.test.data[file].tests)) { if (Ox.test.data[file].done && Ox.isEmpty(Ox.test.data[file].tests)) {
Ox.test.data[file].callback(Ox.test.data[file].results); Ox.test.data[file].callback(Ox.test.data[file].results);
} }
}
}; };
Ox.test.data = {}; Ox.test.data = {};