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) {
Ox.test.data[file] = { var regexp = /(Ox\.test\()/;
callback: callback, if (arguments.length == 2) {
done: false, Ox.doc(file, function(items) {
results: [], var results = [];
tests: {} file = file.split('?')[0];
}; Ox.test.data[file] = {
Ox.doc(file, function(items) { callback: callback,
var results = []; done: false,
items.forEach(function(item) { results: [],
var actual, match; tests: {}
item.tests && item.tests.some(function(test) { };
return test.result; items.forEach(function(item) {
}) && item.tests.forEach(function(test) { item.tests && item.tests.some(function(test) {
Ox.Log('TEST', test.statement); return test.result;
actual = eval(test.statement); }) && item.tests.forEach(function(test) {
match = test.statement.match(/Ox\.test\.async\('([\w\.]+)'/); var actual, isAsync = regexp.test(test.statement);
if (match) { if (isAsync) {
Ox.test.data[file].tests[match[1]] = { Ox.test.data[file].tests[item.name] = {
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,34 +587,38 @@ Ox.test = function(file, callback) {
), actual) ), actual)
}); });
} }
} });
}); });
Ox.test.data[file].done = true;
if (Ox.isEmpty(Ox.test.data[file].tests)) {
callback(Ox.test.data[file].results);
}
}); });
Ox.test.data[file].done = true; } else {
if (Ox.isEmpty(Ox.test.data[file].tests)) { var name = arguments[0],
callback(Ox.test.data[file].results); result = arguments[1],
expected = arguments[2];
file = null;
Ox.forEach(Ox.test.data, function(v, k) {
if (v.tests[name]) {
file = k;
Ox.Break();
}
});
Ox.print('FILE::', file, name, Ox.test.data)
Ox.test.data[file].results.push({
actual: result,
expected: expected,
name: name,
section: Ox.test.data[file].tests[name].section,
statement: Ox.test.data[file].tests[name].statement,
passed: Ox.isEqual(result, expected)
});
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)) {
Ox.test.data[file].callback(Ox.test.data[file].results);
} }
});
};
Ox.test.async = function(name, result) {
var file;
Ox.forEach(Ox.test.data, function(v, k) {
if (v.tests[name]) {
file = k;
Ox.Break();
}
});
Ox.test.data[file].results.push({
actual: result,
expected: true,
name: name,
section: Ox.test.data[file].tests[name].section,
statement: Ox.test.data[file].tests[name].statement,
passed: result
});
delete Ox.test.data[file].tests[name];
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 = {}; Ox.test.data = {};