From f3b0bb6c8689578125e30bffc4600452ef440491 Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 27 May 2012 18:40:51 +0200 Subject: [PATCH] simplify async test syntax, allow for files with ?random suffix --- source/Ox/js/JavaScript.js | 110 ++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 49 deletions(-) diff --git a/source/Ox/js/JavaScript.js b/source/Ox/js/JavaScript.js index d54bb360..70f033c0 100644 --- a/source/Ox/js/JavaScript.js +++ b/source/Ox/js/JavaScript.js @@ -294,7 +294,7 @@ Ox.doc = (function() { counter = 0, items = []; files && files.forEach(function(file) { Ox.get(file, function(source) { - items = items.concat(parseSource(source, file)); + items = items.concat(parseSource(source, file.split('?')[0])); ++counter == files.length && callback(items); }); }); @@ -545,29 +545,37 @@ Ox.minify = function() { Ox.test 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 results = []; - items.forEach(function(item) { - var actual, match; - item.tests && item.tests.some(function(test) { - return test.result; - }) && item.tests.forEach(function(test) { - Ox.Log('TEST', test.statement); - actual = eval(test.statement); - match = test.statement.match(/Ox\.test\.async\('([\w\.]+)'/); - if (match) { - Ox.test.data[file].tests[match[1]] = { - section: item.section, - statement: test.statement - }; - } else { - if (test.result) { + var regexp = /(Ox\.test\()/; + if (arguments.length == 2) { + Ox.doc(file, function(items) { + var results = []; + file = file.split('?')[0]; + Ox.test.data[file] = { + callback: callback, + done: false, + results: [], + tests: {} + }; + items.forEach(function(item) { + item.tests && item.tests.some(function(test) { + return test.result; + }) && item.tests.forEach(function(test) { + var actual, isAsync = regexp.test(test.statement); + if (isAsync) { + Ox.test.data[file].tests[item.name] = { + section: item.section, + statement: test.statement + }; + test.statement = test.statement.replace( + 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({ actual: JSON.stringify(actual), expected: test.result, @@ -579,34 +587,38 @@ Ox.test = function(file, callback) { ), 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; - if (Ox.isEmpty(Ox.test.data[file].tests)) { - callback(Ox.test.data[file].results); + } else { + var name = arguments[0], + 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 = {};