in Ox.test, allow for async tests; in Ox.doc, rename the 'examples' property to 'tests'

This commit is contained in:
rolux 2012-05-27 16:52:48 +02:00
parent 6e7743efd3
commit 6a2ac97bec

View file

@ -27,6 +27,7 @@ Ox.doc <f> Generates documentation for annotated JavaScript
usage <[o]> Usage (array of doc objects)
Present if the <code>type</code> of the item is
<code>"function"</code>.
tests <[o]> Tests (array of test objects)
type <s> Type of the item
(file, callback) -> <u> undefined
(files, callback) -> <u> undefined
@ -36,7 +37,7 @@ Ox.doc <f> Generates documentation for annotated JavaScript
callback <f> Callback function
doc <[o]> Array of doc objects
# > Ox.doc("//@ My.FOO <n> Magic constant\nMy.FOO = 23;")
# [{"name": "Ox.foo", "summary": "just some string", "type": "string"}]
# [{"name": "Ox.foo", "summary": "Magic constant", "type": "number"}]
@*/
Ox.doc = (function() {
// fixme: dont require the trailing '@'
@ -91,10 +92,10 @@ Ox.doc = (function() {
var key, line = node.line, subitem;
if (!/^#/.test(node.line)) {
if (/^<script>/.test(line)) {
item.examples = [parseScript(line)];
item.tests = [parseScript(line)];
} else if (/^>/.test(line)) {
item.examples = item.examples || [];
item.examples.push(parseTest(line));
item.tests = item.tests || [];
item.tests.push(parseTest(line));
} else if ((subitem = parseItem(line))) {
if (/^\(/.test(subitem.name)) {
item.usage = item.usage || [];
@ -544,31 +545,70 @@ Ox.minify = function() {
Ox.test <f> Takes JavaScript, runs inline tests, returns results
@*/
Ox.test = function(file, callback) {
Ox.test.data[file] = {
callback: callback,
done: false,
results: [],
tests: {}
};
Ox.doc(file, function(items) {
var tests = [];
var results = [];
items.forEach(function(item) {
item.examples && item.examples.some(function(example) {
return example.result;
}) && item.examples.forEach(function(example) {
Ox.Log('TEST', example.statement);
var actual = eval(example.statement);
if (example.result) {
tests.push({
var actual, match;
item.tests && item.tests.some(function(test) {
return test.result;
}) && item.tests.forEach(function(test) {
match = test.statement.match(/Ox\.test\.async\('([\w\.]+)'/);
if (match) {
Ox.test.data[file].tests[match[1]] = {
section: item.section,
statement: test.statement
};
}
Ox.Log('TEST', test.statement);
actual = eval(test.statement);
if (test.result) {
Ox.test.data[file].results.push({
actual: JSON.stringify(actual),
expected: example.result,
expected: test.result,
name: item.name,
section: item.section,
statement: example.statement,
statement: test.statement,
passed: Ox.isEqual(eval(
'Ox.test.result = ' + example.result
'(' + test.result + ')'
), actual)
});
}
});
});
callback(tests);
Ox.test.data[file].done = true;
if (Ox.isEmpty(Ox.test.data[file].tests)) {
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.tokenize <f> Tokenizes JavaScript