Minor update to the unit test, work still required.

parent 97c905e3
/**
* @license
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* https://blockly.googlecode.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Generating Arduino for loop blocks.
* @author gasolin@gmail.com (Fred Lin)
*/
'use strict';
goog.provide('Blockly.Arduino.loops');
goog.require('Blockly.Arduino');
Blockly.Arduino['controls_repeat'] = function(block) {
// Repeat n times (internal number).
var repeats = Number(block.getFieldValue('TIMES'));
var branch = Blockly.Arduino.statementToCode(block, 'DO');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var loopVar = Blockly.Arduino.variableDB_.getDistinctName(
'count', Blockly.Variables.NAME_TYPE);
var code = 'for (int ' + loopVar + ' = 0; ' +
loopVar + ' < ' + repeats + '; ' +
loopVar + '++) {\n' +
branch + '}\n';
return code;
};
Blockly.Arduino['controls_repeat_ext'] = function(block) {
// Repeat n times (external number).
var repeats = Blockly.Arduino.valueToCode(block, 'TIMES',
Blockly.Arduino.ORDER_ADDITIVE) || '0';
var branch = Blockly.Arduino.statementToCode(block, 'DO');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var code = '';
var loopVar = Blockly.Arduino.variableDB_.getDistinctName(
'count', Blockly.Variables.NAME_TYPE);
var endVar = repeats;
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
var endVar = Blockly.Arduino.variableDB_.getDistinctName(
'repeat_end', Blockly.Variables.NAME_TYPE);
code += 'int ' + endVar + ' = ' + repeats + ';\n';
}
code += 'for (int ' + loopVar + ' = 0; ' +
loopVar + ' < ' + endVar + '; ' +
loopVar + '++) {\n' +
branch + '}\n';
return code;
};
Blockly.Arduino['controls_whileUntil'] = function(block) {
// Do while/until loop.
var until = block.getFieldValue('MODE') == 'UNTIL';
var argument0 = Blockly.Arduino.valueToCode(block, 'BOOL',
until ? Blockly.Arduino.ORDER_LOGICAL_OR :
Blockly.Arduino.ORDER_NONE) || 'false';
var branch = Blockly.Arduino.statementToCode(block, 'DO');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
if (until) {
if (!argument0.match(/^\w+$/)) {
argument0 = '(' + argument0 + ')';
}
argument0 = '!' + argument0;
}
return 'while (' + argument0 + ') {\n' + branch + '}\n';
};
Blockly.Arduino['controls_for'] = function(block) {
// For loop.
var variable0 = Blockly.Arduino.variableDB_.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.Arduino.valueToCode(block, 'FROM',
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
var argument1 = Blockly.Arduino.valueToCode(block, 'TO',
Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
var increment = Blockly.Arduino.valueToCode(block, 'BY',
Blockly.JavaScript.ORDER_ASSIGNMENT) || '1';
var branch = Blockly.Arduino.statementToCode(block, 'DO');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var code;
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
Blockly.isNumber(increment)) {
// All arguments are simple numbers.
var up = parseFloat(argument0) <= parseFloat(argument1);
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
variable0;
var step = Math.abs(parseFloat(increment));
if (step == 1) {
code += up ? '++' : '--';
} else {
code += (up ? ' += ' : ' -= ') + step;
}
code += ') {\n' + branch + '}\n';
} else {
code = '';
// Cache non-trivial values to variables to prevent repeated look-ups.
var startVar = argument0;
if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
var startVar = Blockly.Arduino.variableDB_.getDistinctName(
variable0 + '_start', Blockly.Variables.NAME_TYPE);
code += 'int ' + startVar + ' = ' + argument0 + ';\n';
}
var endVar = argument1;
if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
var endVar = Blockly.Arduino.variableDB_.getDistinctName(
variable0 + '_end', Blockly.Variables.NAME_TYPE);
code += 'int ' + endVar + ' = ' + argument1 + ';\n';
}
// Determine loop direction at start, in case one of the bounds
// changes during loop execution.
var incVar = Blockly.Arduino.variableDB_.getDistinctName(
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
code += 'var ' + incVar + ' = ';
if (Blockly.isNumber(increment)) {
code += Math.abs(increment) + ';\n';
} else {
code += 'abs(' + increment + ');\n';
}
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
code += Blockly.Arduino.INDENT + incVar + ' = -' + incVar +';\n';
code += '}\n';
code += 'for (' + variable0 + ' = ' + startVar + ';\n' +
' ' + incVar + ' >= 0 ? ' +
variable0 + ' <= ' + endVar + ' : ' +
variable0 + ' >= ' + endVar + ';\n' +
' ' + variable0 + ' += ' + incVar + ') {\n' +
branch + '}\n';
}
return code;
};
Blockly.Arduino['controls_forEach'] = function(block) {
//TODO: for each does not exists in C++, need to implement this properly
// For each loop.
var variable0 = Blockly.Arduino.variableDB_.getName(
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
var argument0 = Blockly.Arduino.valueToCode(block, 'LIST',
Blockly.Arduino.ORDER_ASSIGNMENT) || '[]';
var branch = Blockly.Arduino.statementToCode(block, 'DO');
branch = Blockly.Arduino.addLoopTrap(branch, block.id);
var indexVar = Blockly.Arduino.variableDB_.getDistinctName(
variable0 + '_index', Blockly.Variables.NAME_TYPE);
branch = Blockly.Arduino.INDENT + variable0 + ' = ' + argument0 + '[' + indexVar + '];\n' +
branch;
var code = 'for (int ' + indexVar + ' in ' + argument0 + ') {\n' +
branch + '}\n';
return code;
};
Blockly.Arduino['controls_flow_statements'] = function(block) {
// Flow statements: continue, break.
switch (block.getFieldValue('FLOW')) {
case 'BREAK':
return 'break;\n';
case 'CONTINUE':
return 'continue;\n';
}
throw 'Unknown flow statement.';
};
......@@ -15,7 +15,7 @@
<script type="text/javascript" src="../../generators/javascript/variables.js"></script>
<script type="text/javascript" src="../../generators/javascript/procedures.js"></script>
<script type="text/javascript" src="../../generators/arduino.js"></script>
<!--script type="text/javascript" src="unittest_arduino.js"></script-->
<script type="text/javascript" src="unittest_arduino.js"></script>
<script type="text/javascript" src="../../generators/arduino/logic.js"></script>
<script type="text/javascript" src="../../generators/arduino/loops.js"></script>
<script type="text/javascript" src="../../generators/arduino/math.js"></script>
......
/**
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* https://blockly.googlecode.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Generating Arduino for unit test blocks.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
Blockly.Arduino['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.Arduino.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
var functionName = Blockly.Arduino.provideFunction_(
'unittest_report',
[ 'function ' + Blockly.Arduino.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).
code += Blockly.Arduino.statementToCode(block, 'DO')
.replace(/^ /, '').replace(/\n /g, '\n');
var reportVar = Blockly.Arduino.variableDB_.getDistinctName(
'report', Blockly.Variables.NAME_TYPE);
code += 'var ' + reportVar + ' = ' + functionName + '();\n';
// Destroy results.
code += resultsVar + ' = null;\n';
// Send the report to the console (that's where errors will go anyway).
code += 'console.log(' + reportVar + ');\n';
return code;
};
Blockly.Arduino['unittest_main'].defineAssert_ = function(block) {
var resultsVar = Blockly.Arduino.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
var functionName = Blockly.Arduino.provideFunction_(
'assertEquals',
[ 'function ' + Blockly.Arduino.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.Arduino['unittest_assertequals'] = function(block) {
// Asserts that a value equals another value.
var message = Blockly.Arduino.quote_(block.getFieldValue('MESSAGE'));
var actual = Blockly.Arduino.valueToCode(block, 'ACTUAL',
Blockly.Arduino.ORDER_COMMA) || 'null';
var expected = Blockly.Arduino.valueToCode(block, 'EXPECTED',
Blockly.Arduino.ORDER_COMMA) || 'null';
return Blockly.Arduino['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.Arduino['unittest_assertvalue'] = function(block) {
// Asserts that a value is true, false, or null.
var message = Blockly.Arduino.quote_(block.getFieldValue('MESSAGE'));
var actual = Blockly.Arduino.valueToCode(block, 'ACTUAL',
Blockly.Arduino.ORDER_COMMA) || 'null';
var expected = block.getFieldValue('EXPECTED');
if (expected == 'TRUE') {
expected = 'true';
} else if (expected == 'FALSE') {
expected = 'false';
} else if (expected == 'NULL') {
expected = 'null';
}
return Blockly.Arduino['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.Arduino['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.Arduino.variableDB_.getName('unittestResults',
Blockly.Variables.NAME_TYPE);
var message = Blockly.Arduino.quote_(block.getFieldValue('MESSAGE'));
var functionName = Blockly.Arduino.provideFunction_(
'unittest_fail',
[ 'function ' + Blockly.Arduino.FUNCTION_NAME_PLACEHOLDER_ +
'(message) {',
' // Always assert an error.',
' if (!' + resultsVar + ') {',
' throw "Orphaned assert fail: " + message;',
' }',
' ' + resultsVar + '.push([false, "Fail.", message]);',
'}']);
return functionName + '(' + message + ');\n';
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment