mirror of
https://github.com/google/blockly.git
synced 2026-01-09 10:00:09 +01:00
Automatic commit Sat Jan 11 03:00:02 PST 2014
This commit is contained in:
@@ -27,42 +27,36 @@ Blockly.JavaScript['unittest_main'] = function(block) {
|
||||
// Container for unit tests.
|
||||
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
if (!Blockly.JavaScript.definitions_['unittest_report']) {
|
||||
var functionName = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
'testReport', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.JavaScript['unittest_main'].report = functionName;
|
||||
var func = [];
|
||||
func.push('function ' + functionName + '() {');
|
||||
func.push(' // Create test report.');
|
||||
func.push(' var report = [];');
|
||||
func.push(' var summary = [];');
|
||||
func.push(' var fails = 0;');
|
||||
func.push(' for (var x = 0; x < ' + resultsVar + '.length; x++) {');
|
||||
func.push(' if (' + resultsVar + '[x][0]) {');
|
||||
func.push(' summary.push(".");');
|
||||
func.push(' } else {');
|
||||
func.push(' summary.push("F");');
|
||||
func.push(' fails++;');
|
||||
func.push(' report.push("");');
|
||||
func.push(' report.push("FAIL: " + ' + resultsVar + '[x][2]);');
|
||||
func.push(' report.push(' + resultsVar + '[x][1]);');
|
||||
func.push(' }');
|
||||
func.push(' }');
|
||||
func.push(' report.unshift(summary.join(""));');
|
||||
func.push(' report.push("");');
|
||||
func.push(' report.push("Number of tests run: " + ' + resultsVar +
|
||||
'.length);');
|
||||
func.push(' report.push("");');
|
||||
func.push(' if (fails) {');
|
||||
func.push(' report.push("FAILED (failures=" + fails + ")");');
|
||||
func.push(' } else {');
|
||||
func.push(' report.push("OK");');
|
||||
func.push(' }');
|
||||
func.push(' return report.join("\\n");');
|
||||
func.push('}');
|
||||
func.push('');
|
||||
Blockly.JavaScript.definitions_['unittest_report'] = func.join('\n');
|
||||
}
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'unittest_report',
|
||||
[ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '() {',
|
||||
' // Create test report.',
|
||||
' var report = [];',
|
||||
' var summary = [];',
|
||||
' var fails = 0;',
|
||||
' for (var x = 0; x < ' + resultsVar + '.length; x++) {',
|
||||
' if (' + resultsVar + '[x][0]) {',
|
||||
' summary.push(".");',
|
||||
' } else {',
|
||||
' summary.push("F");',
|
||||
' fails++;',
|
||||
' report.push("");',
|
||||
' report.push("FAIL: " + ' + resultsVar + '[x][2]);',
|
||||
' report.push(' + resultsVar + '[x][1]);',
|
||||
' }',
|
||||
' }',
|
||||
' report.unshift(summary.join(""));',
|
||||
' report.push("");',
|
||||
' report.push("Number of tests run: " + ' + resultsVar +
|
||||
'.length);',
|
||||
' report.push("");',
|
||||
' if (fails) {',
|
||||
' report.push("FAILED (failures=" + fails + ")");',
|
||||
' } else {',
|
||||
' report.push("OK");',
|
||||
' }',
|
||||
' return report.join("\\n");',
|
||||
'}']);
|
||||
// Setup global to hold test results.
|
||||
var code = resultsVar + ' = [];\n';
|
||||
// Run tests (unindented).
|
||||
@@ -70,8 +64,7 @@ Blockly.JavaScript['unittest_main'] = function(block) {
|
||||
.replace(/^ /, '').replace(/\n /g, '\n');
|
||||
var reportVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
'report', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + reportVar + ' = ' +
|
||||
Blockly.JavaScript['unittest_main'].report + '();\n';
|
||||
code += 'var ' + reportVar + ' = ' + functionName + '();\n';
|
||||
// Destroy results.
|
||||
code += resultsVar + ' = null;\n';
|
||||
// Send the report to the console (that's where errors will go anyway).
|
||||
@@ -79,66 +72,45 @@ Blockly.JavaScript['unittest_main'] = function(block) {
|
||||
return code;
|
||||
};
|
||||
|
||||
// Code by Tomáš Zato on Stack Overflow [overflow.com/questions/7837456]
|
||||
// with minor changes for style.
|
||||
Array.prototype.equals = function(array) {
|
||||
// Is it an array?
|
||||
if (!array || !(array instanceof Array)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Are they the same length?
|
||||
if (this.length != array.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
// Check for nested arrays.
|
||||
if (this[i] instanceof Array && array[i] instanceof Array) {
|
||||
// Recurse into the nested arrays.
|
||||
if (!this[i].equals(array[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (this[i] !== array[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['unittest_main'].defineAssert_ = function(block) {
|
||||
if (!Blockly.JavaScript.definitions_['unittest_assertequals']) {
|
||||
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var functionName = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
'assertEquals', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.JavaScript['unittest_main'].assert_ = functionName;
|
||||
var func = [];
|
||||
func.push('function ' + functionName + '(actual, expected, message) {');
|
||||
func.push(' // Asserts that a value equals another value.');
|
||||
func.push(' if (!' + resultsVar + ') {');
|
||||
func.push(' throw "Orphaned assert: " + message;');
|
||||
func.push(' }');
|
||||
func.push(' if ((expected instanceof Array &&');
|
||||
func.push(' expected.equals(actual)) ||');
|
||||
func.push(' actual === expected) {');
|
||||
func.push(' ' + resultsVar + '.push([true, "OK", message]);');
|
||||
func.push(' } else {');
|
||||
func.push(' ' + resultsVar + '.push([false, ' +
|
||||
'"Expected: " + expected + "\\nActual: " + actual, message]);');
|
||||
func.push(' }');
|
||||
func.push('}');
|
||||
func.push('');
|
||||
Blockly.JavaScript.definitions_['unittest_assertequals'] = func.join('\n');
|
||||
}
|
||||
return Blockly.JavaScript['unittest_main'].assert_;
|
||||
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'assertEquals',
|
||||
[ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(actual, expected, message) {',
|
||||
' // Asserts that a value equals another value.',
|
||||
' if (!' + resultsVar + ') {',
|
||||
' throw "Orphaned assert: " + message;',
|
||||
' }',
|
||||
' function equals(a, b) {',
|
||||
' if (a === b) {',
|
||||
' return true;',
|
||||
' } else if (a instanceof Array && b instanceof Array) {',
|
||||
' if (a.length != b.length) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' for (var i = 0; i < a.length; i++) {',
|
||||
' if (!equals(a[i], b[i])) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
' }',
|
||||
' return false;',
|
||||
' }',
|
||||
' if (equals(actual, expected)) {',
|
||||
' ' + resultsVar + '.push([true, "OK", message]);',
|
||||
' } else {',
|
||||
' ' + resultsVar + '.push([false, ' +
|
||||
'"Expected: " + expected + "\\nActual: " + actual, message]);',
|
||||
' }',
|
||||
'}']);
|
||||
return functionName;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['unittest_assertequals'] = function(block) {
|
||||
// Asserts that a value equals another value.
|
||||
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var message = Blockly.JavaScript.quote_(block.getFieldValue('MESSAGE'));
|
||||
var actual = Blockly.JavaScript.valueToCode(block, 'ACTUAL',
|
||||
Blockly.JavaScript.ORDER_COMMA) || 'null';
|
||||
@@ -150,8 +122,6 @@ Blockly.JavaScript['unittest_assertequals'] = function(block) {
|
||||
|
||||
Blockly.JavaScript['unittest_assertvalue'] = function(block) {
|
||||
// Asserts that a value is true, false, or null.
|
||||
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var message = Blockly.JavaScript.quote_(block.getFieldValue('MESSAGE'));
|
||||
var actual = Blockly.JavaScript.valueToCode(block, 'ACTUAL',
|
||||
Blockly.JavaScript.ORDER_COMMA) || 'null';
|
||||
@@ -172,20 +142,15 @@ Blockly.JavaScript['unittest_fail'] = function(block) {
|
||||
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var message = Blockly.JavaScript.quote_(block.getFieldValue('MESSAGE'));
|
||||
if (!Blockly.JavaScript.definitions_['unittest_fail']) {
|
||||
var functionName = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
'fail', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.JavaScript['unittest_fail'].assert = functionName;
|
||||
var func = [];
|
||||
func.push('function ' + functionName + '(message) {');
|
||||
func.push(' // Always assert an error.');
|
||||
func.push(' if (!' + resultsVar + ') {');
|
||||
func.push(' throw "Orphaned assert fail: ' + message + '";');
|
||||
func.push(' }');
|
||||
func.push(' ' + resultsVar + '.push([false, "Fail.", message]);');
|
||||
func.push('}');
|
||||
func.push('');
|
||||
Blockly.JavaScript.definitions_['unittest_fail'] = func.join('\n');
|
||||
}
|
||||
return Blockly.JavaScript['unittest_fail'].assert + '(' + message + ');\n';
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'unittest_fail',
|
||||
[ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(message) {',
|
||||
' // Always assert an error.',
|
||||
' if (!' + resultsVar + ') {',
|
||||
' throw "Orphaned assert fail: " + message;',
|
||||
' }',
|
||||
' ' + resultsVar + '.push([false, "Fail.", message]);',
|
||||
'}']);
|
||||
return functionName + '(' + message + ');\n';
|
||||
};
|
||||
|
||||
@@ -124,7 +124,7 @@ Blockly.Python['unittest_fail'] = function(block) {
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(message):',
|
||||
' # Always assert an error.',
|
||||
' if ' + resultsVar + ' == None:',
|
||||
' raise Exception("Orphaned assert equals: ' + message + '")',
|
||||
' raise Exception("Orphaned assert equals: " + message)',
|
||||
' ' + resultsVar + '.append((False, "Fail.", message))']);
|
||||
return functionName + '(' + message + ')\n';
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user