in Ox.test, allow for async tests; in Ox.doc, rename the 'examples' property to 'tests'
This commit is contained in:
parent
6e7743efd3
commit
6a2ac97bec
1 changed files with 56 additions and 16 deletions
|
@ -27,6 +27,7 @@ Ox.doc <f> Generates documentation for annotated JavaScript
|
||||||
usage <[o]> Usage (array of doc objects)
|
usage <[o]> Usage (array of doc objects)
|
||||||
Present if the <code>type</code> of the item is
|
Present if the <code>type</code> of the item is
|
||||||
<code>"function"</code>.
|
<code>"function"</code>.
|
||||||
|
tests <[o]> Tests (array of test objects)
|
||||||
type <s> Type of the item
|
type <s> Type of the item
|
||||||
(file, callback) -> <u> undefined
|
(file, callback) -> <u> undefined
|
||||||
(files, callback) -> <u> undefined
|
(files, callback) -> <u> undefined
|
||||||
|
@ -36,7 +37,7 @@ Ox.doc <f> Generates documentation for annotated JavaScript
|
||||||
callback <f> Callback function
|
callback <f> Callback function
|
||||||
doc <[o]> Array of doc objects
|
doc <[o]> Array of doc objects
|
||||||
# > Ox.doc("//@ My.FOO <n> Magic constant\nMy.FOO = 23;")
|
# > 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() {
|
Ox.doc = (function() {
|
||||||
// fixme: dont require the trailing '@'
|
// fixme: dont require the trailing '@'
|
||||||
|
@ -91,10 +92,10 @@ Ox.doc = (function() {
|
||||||
var key, line = node.line, subitem;
|
var key, line = node.line, subitem;
|
||||||
if (!/^#/.test(node.line)) {
|
if (!/^#/.test(node.line)) {
|
||||||
if (/^<script>/.test(line)) {
|
if (/^<script>/.test(line)) {
|
||||||
item.examples = [parseScript(line)];
|
item.tests = [parseScript(line)];
|
||||||
} else if (/^>/.test(line)) {
|
} else if (/^>/.test(line)) {
|
||||||
item.examples = item.examples || [];
|
item.tests = item.tests || [];
|
||||||
item.examples.push(parseTest(line));
|
item.tests.push(parseTest(line));
|
||||||
} else if ((subitem = parseItem(line))) {
|
} else if ((subitem = parseItem(line))) {
|
||||||
if (/^\(/.test(subitem.name)) {
|
if (/^\(/.test(subitem.name)) {
|
||||||
item.usage = item.usage || [];
|
item.usage = item.usage || [];
|
||||||
|
@ -544,31 +545,70 @@ 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] = {
|
||||||
|
callback: callback,
|
||||||
|
done: false,
|
||||||
|
results: [],
|
||||||
|
tests: {}
|
||||||
|
};
|
||||||
Ox.doc(file, function(items) {
|
Ox.doc(file, function(items) {
|
||||||
var tests = [];
|
var results = [];
|
||||||
items.forEach(function(item) {
|
items.forEach(function(item) {
|
||||||
item.examples && item.examples.some(function(example) {
|
var actual, match;
|
||||||
return example.result;
|
item.tests && item.tests.some(function(test) {
|
||||||
}) && item.examples.forEach(function(example) {
|
return test.result;
|
||||||
Ox.Log('TEST', example.statement);
|
}) && item.tests.forEach(function(test) {
|
||||||
var actual = eval(example.statement);
|
match = test.statement.match(/Ox\.test\.async\('([\w\.]+)'/);
|
||||||
if (example.result) {
|
if (match) {
|
||||||
tests.push({
|
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),
|
actual: JSON.stringify(actual),
|
||||||
expected: example.result,
|
expected: test.result,
|
||||||
name: item.name,
|
name: item.name,
|
||||||
section: item.section,
|
section: item.section,
|
||||||
statement: example.statement,
|
statement: test.statement,
|
||||||
passed: Ox.isEqual(eval(
|
passed: Ox.isEqual(eval(
|
||||||
'Ox.test.result = ' + example.result
|
'(' + test.result + ')'
|
||||||
), actual)
|
), 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
|
Ox.tokenize <f> Tokenizes JavaScript
|
||||||
|
|
Loading…
Reference in a new issue