Initial commit

This commit is contained in:
pasketti
2026-04-05 16:14:49 -04:00
commit ebee3a5534
14059 changed files with 2588797 additions and 0 deletions

View File

@@ -0,0 +1,261 @@
<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="procedures_defnoreturn" x="260" y="14">
<field name="NAME">test colour picker</field>
<statement name="STACK">
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">static colour</field>
</block>
</value>
<value name="ACTUAL">
<block type="colour_picker">
<field name="COLOUR">#ff6600</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">#ff6600</field>
</block>
</value>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="630" y="13">
<field name="NAME">test rgb</field>
<statement name="STACK">
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">from rgb</field>
</block>
</value>
<value name="ACTUAL">
<block type="colour_rgb" inline="false">
<value name="RED">
<block type="math_number">
<field name="NUM">100</field>
</block>
</value>
<value name="GREEN">
<block type="math_number">
<field name="NUM">40</field>
</block>
</value>
<value name="BLUE">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">#ff6600</field>
</block>
</value>
</block>
</statement>
</block>
<block type="unittest_main" x="-5" y="49">
<statement name="DO">
<block type="procedures_callnoreturn">
<mutation name="test colour picker"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test blend"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test rgb"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test colour random"></mutation>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="-7" y="223">
<field name="NAME">test colour random</field>
<statement name="STACK">
<block type="controls_repeat_ext" inline="true">
<value name="TIMES">
<block type="math_number">
<field name="NUM">100</field>
</block>
</value>
<statement name="DO">
<block type="variables_set" inline="false">
<field name="VAR">item</field>
<value name="VALUE">
<block type="colour_random"></block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">test name</field>
</block>
</value>
<value name="ACTUAL">
<block type="text_length" inline="false">
<value name="VALUE">
<block type="variables_get">
<field name="VAR">item</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">7</field>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">test name</field>
</block>
</value>
<value name="ACTUAL">
<block type="text_charAt">
<mutation at="false"></mutation>
<field name="WHERE">FIRST</field>
<value name="VALUE">
<block type="variables_get">
<field name="VAR">item</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">#</field>
</block>
</value>
<next>
<block type="controls_for">
<field name="VAR">i</field>
<value name="FROM">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">7</field>
</block>
</value>
<statement name="DO">
<block type="unittest_assertvalue" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">test name</field>
</block>
</value>
<field name="EXPECTED">TRUE</field>
<value name="ACTUAL">
<block type="logic_compare">
<field name="OP">NEQ</field>
<value name="A">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
<value name="B">
<block type="text_indexOf" inline="false">
<field name="END">FIRST</field>
<value name="VALUE">
<block type="text">
<field name="TEXT">abcdefABDEF0123456789</field>
</block>
</value>
<value name="FIND">
<block type="text_charAt">
<mutation at="true"></mutation>
<field name="WHERE">FROM_START</field>
<value name="VALUE">
<block type="variables_get">
<field name="VAR">item</field>
</block>
</value>
<value name="AT">
<block type="variables_get">
<field name="VAR">i</field>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</statement>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="520" y="205">
<field name="NAME">test blend</field>
<statement name="STACK">
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">blend</field>
</block>
</value>
<value name="ACTUAL">
<block type="colour_blend" inline="false">
<value name="COLOUR1">
<block type="colour_picker">
<field name="COLOUR">#ff0000</field>
</block>
</value>
<value name="COLOUR2">
<block type="colour_rgb" inline="false">
<value name="RED">
<block type="math_number">
<field name="NUM">100</field>
</block>
</value>
<value name="GREEN">
<block type="math_number">
<field name="NUM">40</field>
</block>
</value>
<value name="BLUE">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="RATIO">
<block type="math_number">
<field name="NUM">0.4</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">#ff2900</field>
</block>
</value>
</block>
</statement>
</block>
</xml>

View File

@@ -0,0 +1,560 @@
<xml xmlns="http://www.w3.org/1999/xhtml">
<!-- Do not include <variables> here to test backward compatibility. -->
<block type="unittest_main" x="0" y="1">
<statement name="DO">
<block type="procedures_callnoreturn">
<mutation name="test procedure"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test function"></mutation>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">test recurse</field>
</block>
</value>
<value name="ACTUAL">
<block type="procedures_callreturn" inline="false">
<mutation name="recurse">
<arg name="n"></arg>
</mutation>
<value name="ARG0">
<block type="math_number">
<field name="NUM">3</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">-1-2-1-3-1-2-1-</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="-3" y="249">
<field name="NAME">test procedure</field>
<statement name="STACK">
<block type="procedures_callnoreturn" inline="false">
<mutation name="procedure 1">
<arg name="proc x"></arg>
<arg name="proc y"></arg>
</mutation>
<value name="ARG0">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="ARG1">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">procedure with global</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">proc z</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">4</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">proc w</field>
<value name="VALUE">
<block type="logic_boolean">
<field name="BOOL">FALSE</field>
</block>
</value>
<next>
<block type="procedures_callnoreturn" inline="false">
<mutation name="procedure 2">
<arg name="proc x"></arg>
</mutation>
<value name="ARG0">
<block type="logic_boolean">
<field name="BOOL">FALSE</field>
</block>
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">procedure no return</field>
</block>
</value>
<field name="EXPECTED">TRUE</field>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">proc w</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">proc w</field>
<value name="VALUE">
<block type="logic_boolean">
<field name="BOOL">FALSE</field>
</block>
</value>
<next>
<block type="procedures_callnoreturn" inline="false">
<mutation name="procedure 2">
<arg name="proc x"></arg>
</mutation>
<value name="ARG0">
<block type="logic_boolean">
<field name="BOOL">TRUE</field>
</block>
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">procedure return</field>
</block>
</value>
<field name="EXPECTED">FALSE</field>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">proc w</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="302" y="277">
<mutation>
<arg name="proc x"></arg>
<arg name="proc y"></arg>
</mutation>
<field name="NAME">procedure 1</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">proc z</field>
<value name="VALUE">
<block type="math_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<block type="variables_get">
<field name="VAR">proc x</field>
</block>
</value>
<value name="B">
<block type="variables_get">
<field name="VAR">proc y</field>
</block>
</value>
</block>
</value>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="307" y="435">
<mutation>
<arg name="proc x"></arg>
</mutation>
<field name="NAME">procedure 2</field>
<statement name="STACK">
<block type="procedures_ifreturn">
<mutation value="0"></mutation>
<value name="CONDITION">
<block type="variables_get">
<field name="VAR">proc x</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">proc w</field>
<value name="VALUE">
<block type="logic_boolean">
<field name="BOOL">TRUE</field>
</block>
</value>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="-2" y="731">
<field name="NAME">test function</field>
<statement name="STACK">
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">function with arguments</field>
</block>
</value>
<value name="ACTUAL">
<block type="procedures_callreturn" inline="false">
<mutation name="function 1">
<arg name="func x"></arg>
<arg name="func y"></arg>
</mutation>
<value name="ARG0">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<value name="ARG1">
<block type="math_number">
<field name="NUM">3</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">-1</field>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">function with side effect</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">func z</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">side effect</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">func a</field>
<value name="VALUE">
<block type="text">
<field name="TEXT">unchanged</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">func c</field>
<value name="VALUE">
<block type="text">
<field name="TEXT">global</field>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">function with global</field>
</block>
</value>
<value name="ACTUAL">
<block type="procedures_callreturn" inline="false">
<mutation name="function 2">
<arg name="func a"></arg>
</mutation>
<value name="ARG0">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">3global</field>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">function with scope</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">func a</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">unchanged</field>
</block>
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">function return</field>
</block>
</value>
<field name="EXPECTED">TRUE</field>
<value name="ACTUAL">
<block type="procedures_callreturn" inline="false">
<mutation name="function 3">
<arg name="func a"></arg>
</mutation>
<value name="ARG0">
<block type="logic_boolean">
<field name="BOOL">TRUE</field>
</block>
</value>
</block>
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">function no return</field>
</block>
</value>
<field name="EXPECTED">FALSE</field>
<value name="ACTUAL">
<block type="procedures_callreturn" inline="false">
<mutation name="function 3">
<arg name="func a"></arg>
</mutation>
<value name="ARG0">
<block type="logic_boolean">
<field name="BOOL">FALSE</field>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defreturn" inline="false" x="-3" y="1384">
<mutation>
<arg name="func x"></arg>
<arg name="func y"></arg>
</mutation>
<field name="NAME">function 1</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">func z</field>
<value name="VALUE">
<block type="text">
<field name="TEXT">side effect</field>
</block>
</value>
</block>
</statement>
<value name="RETURN">
<block type="math_arithmetic">
<field name="OP">MINUS</field>
<value name="A">
<block type="variables_get">
<field name="VAR">func x</field>
</block>
</value>
<value name="B">
<block type="variables_get">
<field name="VAR">func y</field>
</block>
</value>
</block>
</value>
</block>
<block type="procedures_defreturn" inline="false" x="-1" y="1503">
<mutation>
<arg name="func a"></arg>
</mutation>
<field name="NAME">function 2</field>
<statement name="STACK">
<block type="math_change" inline="false">
<field name="VAR">func a</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<value name="RETURN">
<block type="text_join" inline="false">
<mutation items="2"></mutation>
<value name="ADD0">
<block type="variables_get">
<field name="VAR">func a</field>
</block>
</value>
<value name="ADD1">
<block type="variables_get">
<field name="VAR">func c</field>
</block>
</value>
</block>
</value>
</block>
<block type="procedures_defreturn" inline="false" x="0" y="1655">
<mutation>
<arg name="func a"></arg>
</mutation>
<field name="NAME">function 3</field>
<statement name="STACK">
<block type="procedures_ifreturn">
<mutation value="1"></mutation>
<value name="CONDITION">
<block type="variables_get">
<field name="VAR">func a</field>
</block>
</value>
<value name="VALUE">
<block type="logic_boolean">
<field name="BOOL">TRUE</field>
</block>
</value>
</block>
</statement>
<value name="RETURN">
<block type="logic_boolean">
<field name="BOOL">FALSE</field>
</block>
</value>
</block>
<block type="procedures_defreturn" inline="false" x="0" y="1789">
<mutation>
<arg name="n"></arg>
</mutation>
<field name="NAME">recurse</field>
<statement name="STACK">
<block type="controls_if" inline="false">
<mutation else="1"></mutation>
<value name="IF0">
<block type="logic_compare">
<field name="OP">GT</field>
<value name="A">
<block type="variables_get">
<field name="VAR">n</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="variables_set" inline="false">
<field name="VAR">text</field>
<value name="VALUE">
<block type="text_join" inline="false">
<mutation items="3"></mutation>
<value name="ADD0">
<block type="procedures_callreturn" inline="false">
<mutation name="recurse">
<arg name="n"></arg>
</mutation>
<value name="ARG0">
<block type="math_arithmetic">
<field name="OP">MINUS</field>
<value name="A">
<block type="variables_get">
<field name="VAR">n</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</value>
</block>
</value>
<value name="ADD1">
<block type="variables_get">
<field name="VAR">n</field>
</block>
</value>
<value name="ADD2">
<block type="procedures_callreturn" inline="false">
<mutation name="recurse">
<arg name="n"></arg>
</mutation>
<value name="ARG0">
<block type="math_arithmetic">
<field name="OP">MINUS</field>
<value name="A">
<block type="variables_get">
<field name="VAR">n</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</statement>
<statement name="ELSE">
<block type="variables_set" inline="false">
<field name="VAR">text</field>
<value name="VALUE">
<block type="text">
<field name="TEXT">-</field>
</block>
</value>
</block>
</statement>
</block>
</statement>
<value name="RETURN">
<block type="variables_get">
<field name="VAR">text</field>
</block>
</value>
</block>
</xml>

View File

@@ -0,0 +1,377 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Blockly Generator Tests</title>
<script src="../../blockly_uncompressed.js"></script>
<script src="../../generators/javascript.js"></script>
<script src="unittest_javascript.js"></script>
<script src="../../generators/javascript/logic.js"></script>
<script src="../../generators/javascript/loops.js"></script>
<script src="../../generators/javascript/math.js"></script>
<script src="../../generators/javascript/text.js"></script>
<script src="../../generators/javascript/lists.js"></script>
<script src="../../generators/javascript/colour.js"></script>
<script src="../../generators/javascript/variables.js"></script>
<script src="../../generators/javascript/procedures.js"></script>
<script src="../../generators/python.js"></script>
<script src="unittest_python.js"></script>
<script src="../../generators/python/logic.js"></script>
<script src="../../generators/python/loops.js"></script>
<script src="../../generators/python/math.js"></script>
<script src="../../generators/python/text.js"></script>
<script src="../../generators/python/lists.js"></script>
<script src="../../generators/python/colour.js"></script>
<script src="../../generators/python/variables.js"></script>
<script src="../../generators/python/procedures.js"></script>
<script src="../../generators/php.js"></script>
<script src="unittest_php.js"></script>
<script src="../../generators/php/logic.js"></script>
<script src="../../generators/php/loops.js"></script>
<script src="../../generators/php/math.js"></script>
<script src="../../generators/php/text.js"></script>
<script src="../../generators/php/lists.js"></script>
<script src="../../generators/php/colour.js"></script>
<script src="../../generators/php/variables.js"></script>
<script src="../../generators/php/procedures.js"></script>
<script src="../../generators/lua.js"></script>
<script src="unittest_lua.js"></script>
<script src="../../generators/lua/logic.js"></script>
<script src="../../generators/lua/loops.js"></script>
<script src="../../generators/lua/math.js"></script>
<script src="../../generators/lua/text.js"></script>
<script src="../../generators/lua/lists.js"></script>
<script src="../../generators/lua/colour.js"></script>
<script src="../../generators/lua/variables.js"></script>
<script src="../../generators/lua/procedures.js"></script>
<script src="../../generators/dart.js"></script>
<script src="unittest_dart.js"></script>
<script src="../../generators/dart/logic.js"></script>
<script src="../../generators/dart/loops.js"></script>
<script src="../../generators/dart/math.js"></script>
<script src="../../generators/dart/text.js"></script>
<script src="../../generators/dart/lists.js"></script>
<script src="../../generators/dart/colour.js"></script>
<script src="../../generators/dart/variables.js"></script>
<script src="../../generators/dart/procedures.js"></script>
<script src="unittest.js"></script>
<script src="../../msg/messages.js"></script>
<script src="../../blocks/logic.js"></script>
<script src="../../blocks/loops.js"></script>
<script src="../../blocks/math.js"></script>
<script src="../../blocks/text.js"></script>
<script src="../../blocks/lists.js"></script>
<script src="../../blocks/colour.js"></script>
<script src="../../blocks/variables.js"></script>
<script src="../../blocks/procedures.js"></script>
<script>
'use strict';
var demoWorkspace = null;
function start() {
demoWorkspace = Blockly.inject('blocklyDiv',
{grid:
{spacing: 25,
length: 3,
colour: '#ccc',
snap: true},
media: '../../media/',
toolbox: document.getElementById('toolbox'),
zoom: {controls: true, wheel: true}
});
changeIndex();
}
function loadXml() {
var dropdown = document.getElementById('testUrl');
var url = dropdown.options[dropdown.selectedIndex].value;
if (!url) {
url = window.prompt('Enter URL of test file.');
if (!url) {
return;
}
}
var xmlText = fetchFile(url);
if (xmlText !== null) {
fromXml(url, xmlText);
}
}
function fetchFile(xmlUrl) {
try {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', xmlUrl, false);
xmlHttp.setRequestHeader('Content-Type', 'text/xml');
xmlHttp.send('');
} catch (e) {
// Attempt to diagnose the problem.
var msg = 'Error: Unable to load XML data.\n';
if (window.location.protocol == 'file:') {
msg += 'This may be due to a security restriction preventing\n' +
'access when using the file:// protocol.\n' +
'Use an http webserver, or a less paranoid browser.\n';
}
alert(msg + '\n' + e);
return null;
}
return xmlHttp.responseText;
}
/**
* @param {string} filename The URL (or other name) of the XML, for reporting.
* @param {string} xmlText The actual XML text.
*/
function fromXml(filename, xmlText) {
var output = document.getElementById('importExport');
output.value = xmlText;
output.scrollTop = 0;
output.scrollLeft = 0;
demoWorkspace.clear();
try {
var xmlDoc = Blockly.Xml.textToDom(xmlText);
Blockly.Xml.domToWorkspace(xmlDoc, demoWorkspace);
} catch (e) {
var msg = 'Error parsing XML: ' + filename + '\n\n\t' + e;
if (e.stack) {
msg += '\n\nSee console for stack trace details.'
}
console.error(e.stack ? e : msg);
alert(msg);
return;
}
}
function setOutput(text) {
var output = document.getElementById('importExport');
output.value = text;
output.focus();
output.select();
}
function toXml() {
var xmlDom = Blockly.Xml.workspaceToDom(demoWorkspace,
/* opt_noId */ true);
var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
xmlText = xmlText.replace(/ id="\d+"/g, '');
setOutput(xmlText);
}
function toJavaScript() {
var code = '\'use strict\';\n\n'
code += Blockly.JavaScript.workspaceToCode(demoWorkspace);
setOutput(code);
}
function toPython() {
var code = Blockly.Python.workspaceToCode(demoWorkspace);
setOutput(code);
}
function toPhp() {
var code = Blockly.PHP.workspaceToCode(demoWorkspace);
setOutput(code);
}
function toLua() {
var code = Blockly.Lua.workspaceToCode(demoWorkspace);
setOutput(code);
}
function toDart() {
var code = Blockly.Dart.workspaceToCode(demoWorkspace);
setOutput(code);
}
function changeIndex() {
var oneBasedIndex = document.getElementById('indexing').checked;
demoWorkspace.options.oneBasedIndex = oneBasedIndex;
demoWorkspace.toolbox_.flyout_.workspace_.options.oneBasedIndex = oneBasedIndex;
}
</script>
<style>
html, body {
height: 100%;
overflow: hidden;
}
body {
background-color: #fff;
font-family: sans-serif;
margin: 0 5px;
}
h1 {
font-weight: normal;
font-size: 140%;
}
#blocklyDiv {
float: right;
height: 95%;
width: 69%;
margin-top: 5px;
}
#importExport {
height: 100%;
width: 100%;
}
</style>
</head>
<body onload="start()">
<div id="blocklyDiv"></div>
<xml id="toolbox" style="display: none">
<category name="Unit test" colour="65">
<block type="unittest_main"></block>
<block type="unittest_assertequals">
<value name="MESSAGE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
</block>
<block type="unittest_assertvalue">
<value name="MESSAGE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
</block>
<block type="unittest_fail"></block>
<block type="unittest_adjustindex"></block>
</category>
<category name="Logic" colour="210">
<block type="controls_if"></block>
<block type="controls_ifelse"></block>
<block type="logic_compare"></block>
<block type="logic_operation"></block>
<block type="logic_negate"></block>
<block type="logic_boolean"></block>
<block type="logic_null"></block>
<block type="logic_ternary"></block>
</category>
<category name="Loops" colour="120">
<block type="controls_repeat_ext"></block>
<block type="controls_whileUntil"></block>
<block type="controls_for"></block>
<block type="controls_forEach"></block>
<block type="controls_flow_statements"></block>
</category>
<category name="Math" colour="230">
<block type="math_number"></block>
<block type="math_arithmetic"></block>
<block type="math_single"></block>
<block type="math_trig"></block>
<block type="math_constant"></block>
<block type="math_number_property"></block>
<block type="math_round"></block>
<block type="math_on_list"></block>
<block type="math_modulo"></block>
<block type="math_constrain"></block>
<block type="math_random_int"></block>
<block type="math_random_float"></block>
</category>
<category name="Text" colour="160">
<block type="text"></block>
<block type="text_join"></block>
<block type="text_append"></block>
<block type="text_length"></block>
<block type="text_isEmpty"></block>
<block type="text_indexOf"></block>
<block type="text_charAt"></block>
<block type="text_getSubstring"></block>
<block type="text_changeCase"></block>
<block type="text_trim"></block>
<block type="text_print"></block>
<block type="text_prompt_ext"></block>
<block type="text_count"></block>
<block type="text_replace"></block>
<block type="text_reverse"></block>
</category>
<category name="Lists" colour="260">
<block type="lists_create_empty"></block>
<block type="lists_create_with"></block>
<block type="lists_repeat"></block>
<block type="lists_length"></block>
<block type="lists_isEmpty"></block>
<block type="lists_indexOf"></block>
<block type="lists_getIndex"></block>
<block type="lists_setIndex"></block>
<block type="lists_getSublist"></block>
<block type="lists_sort"></block>
<block type="lists_split"></block>
<block type="lists_reverse"></block>
</category>
<category name="Colour" colour="20">
<block type="colour_picker"></block>
<block type="colour_random"></block>
<block type="colour_rgb"></block>
<block type="colour_blend"></block>
</category>
<sep></sep>
<category name="Variables" colour="330" custom="VARIABLE"></category>
<category name="Functions" colour="290" custom="PROCEDURE"></category>
</xml>
<table height="95%" width="30%"><tr><td valign="top">
<h1>Blockly Generator Tests</h1>
<p><a href="https://developers.google.com/blockly/guides/modify/web/unit-testing">See the docs</a> for details on running the tests.
<p>
<select id="testUrl">
<option value="logic.xml">Logic</option>
<option value="loops1.xml">Loops 1 (repeat, while, foreach)</option>
<option value="loops2.xml">Loops 2 (count)</option>
<option value="loops3.xml">Loops 3 (continue, break)</option>
<option value="math.xml">Math</option>
<option value="text.xml">Text</option>
<option value="lists.xml">Lists</option>
<option value="colour.xml">Colour</option>
<option value="variables.xml">Variables</option>
<option value="functions.xml">Functions</option>
<option value="">Other...</option>
</select>
<input type="button" value="Load" onclick="loadXml()">
</p>
<p>
<input id="indexing" type="checkbox" onchange="changeIndex()" checked>
<label for="indexing">Generate with one-based indexing</label>
</p>
<p>
Generate:
<input type="button" value="XML" onclick="toXml()">
<div style="display: inline-block;">
<input type="button" value="JavaScript" onclick="toJavaScript()">
<br><a target="_blank" href="https://repl.it/languages/javascript">interpreter</a>
</div>
<div style="display: inline-block;">
<input type="button" value="Python" onclick="toPython()">
<br><a target="_blank" href="https://repl.it/languages/python">interpreter</a>
</div>
<div style="display: inline-block;">
<input type="button" value="PHP" onclick="toPhp()">
<br><a target="_blank" href="https://repl.it/languages/php">interpreter</a>
</div>
<div style="display: inline-block;">
<input type="button" value="Lua" onclick="toLua()">
<br><a target="_blank" href="http://rextester.com/l/lua_online_compiler">interpreter</a>
</div>
<div style="display: inline-block;">
<input type="button" value="Dart" onclick="toDart()">
<br><a target="_blank" href="https://dartpad.dartlang.org/">interpreter</a>
</div>
</p>
</td></tr><tr><td height="99%">
<textarea id="importExport" readonly="readonly" wrap="off"></textarea>
</td></tr></table>
</body>
</html>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,344 @@
<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="unittest_main" x="33" y="140">
<statement name="DO">
<block type="procedures_callnoreturn">
<mutation name="test repeat"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test repeat_ext"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test while"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test foreach"></mutation>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="31" y="296">
<mutation></mutation>
<field name="NAME">test foreach</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_forEach" inline="false">
<field name="VAR">x</field>
<value name="LIST">
<block type="lists_create_with" inline="false">
<mutation items="3"></mutation>
<value name="ADD0">
<block type="text">
<field name="TEXT">a</field>
</block>
</value>
<value name="ADD1">
<block type="text">
<field name="TEXT">b</field>
</block>
</value>
<value name="ADD2">
<block type="text">
<field name="TEXT">c</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">for loop</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">abc</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="30" y="624">
<mutation></mutation>
<field name="NAME">test while</field>
<statement name="STACK">
<block type="controls_whileUntil" inline="false">
<field name="MODE">WHILE</field>
<value name="BOOL">
<block type="logic_boolean">
<field name="BOOL">FALSE</field>
</block>
</value>
<statement name="DO">
<block type="unittest_fail">
<field name="MESSAGE">while 0</field>
</block>
</statement>
<next>
<block type="controls_whileUntil" inline="false">
<field name="MODE">UNTIL</field>
<value name="BOOL">
<block type="logic_boolean">
<field name="BOOL">TRUE</field>
</block>
</value>
<statement name="DO">
<block type="unittest_fail">
<field name="MESSAGE">until 0</field>
</block>
</statement>
<next>
<block type="variables_set" inline="false">
<field name="VAR">count</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<next>
<block type="controls_whileUntil" inline="false">
<field name="MODE">WHILE</field>
<value name="BOOL">
<block type="logic_compare">
<field name="OP">NEQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="math_change" inline="false">
<field name="VAR">count</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">while 10</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">count</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<next>
<block type="controls_whileUntil" inline="false">
<field name="MODE">UNTIL</field>
<value name="BOOL">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="math_change" inline="false">
<field name="VAR">count</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">until 10</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="397" y="448">
<mutation></mutation>
<field name="NAME">test repeat</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">count</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
<next>
<block type="controls_repeat" inline="true">
<field name="TIMES">10</field>
<statement name="DO">
<block type="math_change" inline="false">
<field name="VAR">count</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">repeat 10</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="397" y="668">
<mutation></mutation>
<field name="NAME">test repeat_ext</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">count</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
<next>
<block type="controls_repeat_ext" inline="true">
<value name="TIMES">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
<statement name="DO">
<block type="math_change" inline="false">
<field name="VAR">count</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">repeat 10</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</xml>

View File

@@ -0,0 +1,890 @@
<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="unittest_main" x="66" y="-2">
<statement name="DO">
<block type="procedures_callnoreturn">
<mutation name="test count"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test count by"></mutation>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="679" y="-10">
<field name="NAME">test count by</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="BY">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<statement name="DO">
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count up ints</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">1357</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="BY">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<statement name="DO">
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count down ints</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">8642</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">loglist</field>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="BY">
<block type="math_number">
<field name="NUM">1.5</field>
</block>
</value>
<statement name="DO">
<block type="lists_setIndex">
<mutation at="false"></mutation>
<field name="MODE">INSERT</field>
<field name="WHERE">LAST</field>
<value name="LIST">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="TO">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count with floats</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_with" inline="false">
<mutation items="5"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">2.5</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">4</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">5.5</field>
</block>
</value>
<value name="ADD4">
<block type="math_number">
<field name="NUM">7</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">loglist</field>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="TO">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="BY">
<block type="math_arithmetic">
<field name="OP">MINUS</field>
<value name="A">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="lists_setIndex">
<mutation at="false"></mutation>
<field name="MODE">INSERT</field>
<field name="WHERE">LAST</field>
<value name="LIST">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="TO">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count up non-trivial ints</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_with" inline="false">
<mutation items="8"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">3</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">4</field>
</block>
</value>
<value name="ADD4">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
<value name="ADD5">
<block type="math_number">
<field name="NUM">6</field>
</block>
</value>
<value name="ADD6">
<block type="math_number">
<field name="NUM">7</field>
</block>
</value>
<value name="ADD7">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">loglist</field>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="TO">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="BY">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<statement name="DO">
<block type="lists_setIndex">
<mutation at="false"></mutation>
<field name="MODE">INSERT</field>
<field name="WHERE">LAST</field>
<value name="LIST">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="TO">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count down non-trivial ints</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_with" inline="false">
<mutation items="4"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">6</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">4</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">loglist</field>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0.5</field>
</block>
</value>
</block>
</value>
<value name="TO">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="BY">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="lists_setIndex">
<mutation at="false"></mutation>
<field name="MODE">INSERT</field>
<field name="WHERE">LAST</field>
<value name="LIST">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="TO">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count with floats</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_with" inline="false">
<mutation items="5"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">5.5</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">4.5</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">3.5</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">2.5</field>
</block>
</value>
<value name="ADD4">
<block type="math_number">
<field name="NUM">1.5</field>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="29" y="184">
<field name="NAME">test count</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<statement name="DO">
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count up</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">12345678</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<statement name="DO">
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count down</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">87654321</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">loglist</field>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="TO">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">4</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="lists_setIndex">
<mutation at="false"></mutation>
<field name="MODE">INSERT</field>
<field name="WHERE">LAST</field>
<value name="LIST">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="TO">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count up non-trivial</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_with" inline="false">
<mutation items="4"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">3</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">4</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">loglist</field>
<value name="VALUE">
<block type="lists_create_empty"></block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">3</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</value>
<value name="TO">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="lists_setIndex">
<mutation at="false"></mutation>
<field name="MODE">INSERT</field>
<field name="WHERE">LAST</field>
<value name="LIST">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="TO">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count down non-trivial</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">loglist</field>
</block>
</value>
<value name="EXPECTED">
<block type="lists_create_with" inline="false">
<mutation items="4"></mutation>
<value name="ADD0">
<block type="math_number">
<field name="NUM">4</field>
</block>
</value>
<value name="ADD1">
<block type="math_number">
<field name="NUM">3</field>
</block>
</value>
<value name="ADD2">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
<value name="ADD3">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</xml>

View File

@@ -0,0 +1,734 @@
<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="unittest_main" x="19" y="106">
<statement name="DO">
<block type="procedures_callnoreturn">
<mutation name="test break"></mutation>
<next>
<block type="procedures_callnoreturn">
<mutation name="test continue"></mutation>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="13" y="300">
<field name="NAME">test continue</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">count</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
<next>
<block type="controls_whileUntil" inline="false">
<field name="MODE">WHILE</field>
<value name="BOOL">
<block type="logic_compare">
<field name="OP">NEQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="math_change" inline="false">
<field name="VAR">count</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<next>
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_flow_statements">
<field name="FLOW">CONTINUE</field>
</block>
</statement>
<next>
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">while continue</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">1234678</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">count</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
<next>
<block type="controls_whileUntil" inline="false">
<field name="MODE">UNTIL</field>
<value name="BOOL">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="math_change" inline="false">
<field name="VAR">count</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<next>
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_flow_statements">
<field name="FLOW">CONTINUE</field>
</block>
</statement>
<next>
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">until continue</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">1234678</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<statement name="DO">
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_flow_statements">
<field name="FLOW">CONTINUE</field>
</block>
</statement>
<next>
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</next>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count continue</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">1234678</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_forEach" inline="false">
<field name="VAR">x</field>
<value name="LIST">
<block type="lists_create_with" inline="false">
<mutation items="4"></mutation>
<value name="ADD0">
<block type="text">
<field name="TEXT">a</field>
</block>
</value>
<value name="ADD1">
<block type="text">
<field name="TEXT">b</field>
</block>
</value>
<value name="ADD2">
<block type="text">
<field name="TEXT">c</field>
</block>
</value>
<value name="ADD3">
<block type="text">
<field name="TEXT">d</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<block type="text">
<field name="TEXT">c</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_flow_statements">
<field name="FLOW">CONTINUE</field>
</block>
</statement>
<next>
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</next>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">for continue</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">abd</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="600" y="300">
<field name="NAME">test break</field>
<statement name="STACK">
<block type="variables_set" inline="false">
<field name="VAR">count</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<next>
<block type="controls_whileUntil" inline="false">
<field name="MODE">WHILE</field>
<value name="BOOL">
<block type="logic_compare">
<field name="OP">NEQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_flow_statements">
<field name="FLOW">BREAK</field>
</block>
</statement>
<next>
<block type="math_change" inline="false">
<field name="VAR">count</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</next>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">while break</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">count</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<next>
<block type="controls_whileUntil" inline="false">
<field name="MODE">UNTIL</field>
<value name="BOOL">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">10</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_flow_statements">
<field name="FLOW">BREAK</field>
</block>
</statement>
<next>
<block type="math_change" inline="false">
<field name="VAR">count</field>
<value name="DELTA">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</next>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">until break</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">count</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_for">
<field name="VAR">x</field>
<value name="FROM">
<block type="math_number">
<field name="NUM">1</field>
</block>
</value>
<value name="TO">
<block type="math_number">
<field name="NUM">8</field>
</block>
</value>
<statement name="DO">
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_flow_statements">
<field name="FLOW">BREAK</field>
</block>
</statement>
<next>
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</next>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">count break</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">1234</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">log</field>
<value name="VALUE">
<block type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block type="controls_forEach" inline="false">
<field name="VAR">x</field>
<value name="LIST">
<block type="lists_create_with" inline="false">
<mutation items="4"></mutation>
<value name="ADD0">
<block type="text">
<field name="TEXT">a</field>
</block>
</value>
<value name="ADD1">
<block type="text">
<field name="TEXT">b</field>
</block>
</value>
<value name="ADD2">
<block type="text">
<field name="TEXT">c</field>
</block>
</value>
<value name="ADD3">
<block type="text">
<field name="TEXT">d</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block type="controls_if" inline="false">
<value name="IF0">
<block type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<block type="text">
<field name="TEXT">c</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="controls_flow_statements">
<field name="FLOW">BREAK</field>
</block>
</statement>
<next>
<block type="text_append" inline="false">
<field name="VAR">log</field>
<value name="TEXT">
<block type="variables_get">
<field name="VAR">x</field>
</block>
</value>
</block>
</next>
</block>
</statement>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">for break</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">log</field>
</block>
</value>
<value name="EXPECTED">
<block type="text">
<field name="TEXT">ab</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</xml>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,117 @@
/**
* @license
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* https://developers.google.com/blockly/
*
* 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 Unit test blocks for Blockly.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
Blockly.Blocks['unittest_main'] = {
// Container for unit tests.
init: function() {
this.setColour(65);
this.appendDummyInput()
.appendField('run tests');
this.appendStatementInput('DO');
this.setTooltip('Executes the enclosed unit tests,\n' +
'then prints a summary.');
},
getDeveloperVars: function() {
return ['unittestResults'];
}
};
Blockly.Blocks['unittest_assertequals'] = {
// Asserts that a value equals another value.
init: function() {
this.setColour(65);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.appendValueInput('MESSAGE')
.appendField('name')
.setCheck('String');
this.appendValueInput('ACTUAL')
.appendField('actual');
this.appendValueInput('EXPECTED')
.appendField('expected');
this.setTooltip('Tests that "actual == expected".');
},
getDeveloperVars: function() {
return ['unittestResults'];
}
};
Blockly.Blocks['unittest_assertvalue'] = {
// Asserts that a value is true, false, or null.
init: function() {
this.setColour(65);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.appendValueInput('MESSAGE', 'test name')
.appendField('name')
.setCheck('String');
this.appendValueInput('ACTUAL')
.appendField('assert')
.appendField(new Blockly.FieldDropdown(
[['true', 'TRUE'], ['false', 'FALSE'], ['null', 'NULL']]), 'EXPECTED');
this.setTooltip('Tests that the value is true, false, or null.');
},
getDeveloperVars: function() {
return ['unittestResults'];
}
};
Blockly.Blocks['unittest_fail'] = {
// Always assert an error.
init: function() {
this.setColour(65);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.appendDummyInput()
.appendField(new Blockly.FieldTextInput('test name'), 'MESSAGE')
.appendField('fail');
this.setTooltip('Records an error.');
},
getDeveloperVars: function() {
return ['unittestResults'];
}
};
Blockly.Blocks['unittest_adjustindex'] = {
// Adjusts the indexing based on current setting.
init: function() {
this.jsonInit({
"message0": "adjusted %1",
"args0": [
{
"type": "input_value",
"name": "INDEX",
"check": "Number"
}
],
"inputsInline": true,
"output": "Number",
"colour": 65,
"tooltip": "Adjusts the value based on whether generated code is using " +
"zero or one based indexing."
});
}
};

View File

@@ -0,0 +1,173 @@
/**
* @license
* Visual Blocks Language
*
* Copyright 2014 Google Inc.
* https://developers.google.com/blockly/
*
* 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 Dart for unit test blocks.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
Blockly.Dart['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.Dart.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.Dart.provideFunction_(
'unittest_report',
[ 'String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {',
' // Create test report.',
' List report = [];',
' StringBuffer summary = new StringBuffer();',
' int fails = 0;',
' for (int x = 0; x < ' + resultsVar + '.length; x++) {',
' if (' + resultsVar + '[x][0]) {',
' summary.write(".");',
' } else {',
' summary.write("F");',
' fails++;',
' report.add("");',
' report.add("FAIL: ${' + resultsVar + '[x][2]}");',
' report.add(' + resultsVar + '[x][1]);',
' }',
' }',
' report.insert(0, summary.toString());',
' report.add("");',
' report.add("Ran ${' + resultsVar + '.length} tests.");',
' report.add("");',
' if (fails != 0) {',
' report.add("FAILED (failures=$fails)");',
' } else {',
' report.add("OK");',
' }',
' return report.join("\\n");',
'}']);
// Setup global to hold test results.
var code = resultsVar + ' = [];\n';
// Run tests (unindented).
code += Blockly.Dart.statementToCode(block, 'DO')
.replace(/^ /, '').replace(/\n /g, '\n');
// Print the report to the console (that's where errors will go anyway).
code += 'print(' + functionName + '());\n';
// Destroy results.
code += resultsVar + ' = null;\n';
return code;
};
Blockly.Dart['unittest_main'].defineAssert_ = function() {
var resultsVar = Blockly.Dart.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.Dart.provideFunction_(
'unittest_assertequals',
[ 'void ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
'(dynamic actual, dynamic expected, String message) {',
' // Asserts that a value equals another value.',
' if (' + resultsVar + ' == null) {',
' throw "Orphaned assert: ${message}";',
' }',
' bool equals(a, b) {',
' if (a == b) {',
' return true;',
' } else if (a is List && b is List) {',
' if (a.length != b.length) {',
' return false;',
' }',
' for (num i = 0; i < a.length; i++) {',
' if (!equals(a[i], b[i])) {',
' return false;',
' }',
' }',
' return true;',
' }',
' return false;',
' }',
' if (equals(actual, expected)) {',
' ' + resultsVar + '.add([true, "OK", message]);',
' } else {',
' ' + resultsVar + '.add([false, ' +
'"Expected: $expected\\nActual: $actual", message]);',
' }',
'}']);
return functionName;
};
Blockly.Dart['unittest_assertequals'] = function(block) {
// Asserts that a value equals another value.
var message = Blockly.Dart.valueToCode(block, 'MESSAGE',
Blockly.Dart.ORDER_NONE) || '';
var actual = Blockly.Dart.valueToCode(block, 'ACTUAL',
Blockly.Dart.ORDER_NONE) || 'null';
var expected = Blockly.Dart.valueToCode(block, 'EXPECTED',
Blockly.Dart.ORDER_NONE) || 'null';
return Blockly.Dart['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.Dart['unittest_assertvalue'] = function(block) {
// Asserts that a value is true, false, or null.
var message = Blockly.Dart.valueToCode(block, 'MESSAGE',
Blockly.Dart.ORDER_NONE) || '';
var actual = Blockly.Dart.valueToCode(block, 'ACTUAL',
Blockly.Dart.ORDER_NONE) || '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.Dart['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.Dart['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.Dart.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var message = Blockly.Dart.quote_(block.getFieldValue('MESSAGE'));
var functionName = Blockly.Dart.provideFunction_(
'unittest_fail',
[ 'void ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
'(String message) {',
' // Always assert an error.',
' if (' + resultsVar + ' == null) {',
' throw "Orphaned assert fail: ${message}";',
' }',
' ' + resultsVar + '.add([false, "Fail.", message]);',
'}']);
return functionName + '(' + message + ');\n';
};
Blockly.Dart['unittest_adjustindex'] = function(block) {
var index = Blockly.Dart.valueToCode(block, 'INDEX',
Blockly.Dart.ORDER_ADDITIVE) || '0';
// Adjust index if using one-based indexing.
if (block.workspace.options.oneBasedIndex) {
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.Dart.ORDER_ATOMIC];
} else {
// If the index is dynamic, adjust it in code.
index = index + ' + 1';
}
} else if (Blockly.isNumber(index)) {
return [index, Blockly.Dart.ORDER_ATOMIC];
}
return [index, Blockly.Dart.ORDER_ADDITIVE];
};

View File

@@ -0,0 +1,177 @@
/**
* @license
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* https://developers.google.com/blockly/
*
* 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 JavaScript for unit test blocks.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
Blockly.JavaScript['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
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 i = 0; i < ' + resultsVar + '.length; i++) {',
' if (' + resultsVar + '[i][0]) {',
' summary.push(".");',
' } else {',
' summary.push("F");',
' fails++;',
' report.push("");',
' report.push("FAIL: " + ' + resultsVar + '[i][2]);',
' report.push(' + resultsVar + '[i][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.JavaScript.statementToCode(block, 'DO')
.replace(/^ /, '').replace(/\n /g, '\n');
// Send the report to the console (that's where errors will go anyway).
code += 'console.log(' + functionName + '());\n';
// Destroy results.
code += resultsVar + ' = null;\n';
return code;
};
Blockly.JavaScript['unittest_main'].defineAssert_ = function(block) {
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_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 ((typeof a == "number") && (typeof b == "number") &&',
' (a.toPrecision(15) == b.toPrecision(15))) {',
' 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 message = Blockly.JavaScript.valueToCode(block, 'MESSAGE',
Blockly.JavaScript.ORDER_NONE) || '';
var actual = Blockly.JavaScript.valueToCode(block, 'ACTUAL',
Blockly.JavaScript.ORDER_COMMA) || 'null';
var expected = Blockly.JavaScript.valueToCode(block, 'EXPECTED',
Blockly.JavaScript.ORDER_COMMA) || 'null';
return Blockly.JavaScript['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.JavaScript['unittest_assertvalue'] = function(block) {
// Asserts that a value is true, false, or null.
var message = Blockly.JavaScript.valueToCode(block, 'MESSAGE',
Blockly.JavaScript.ORDER_NONE) || '';
var actual = Blockly.JavaScript.valueToCode(block, 'ACTUAL',
Blockly.JavaScript.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.JavaScript['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.JavaScript['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var message = Blockly.JavaScript.quote_(block.getFieldValue('MESSAGE'));
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';
};
Blockly.JavaScript['unittest_adjustindex'] = function(block) {
var index = Blockly.JavaScript.valueToCode(block, 'INDEX',
Blockly.JavaScript.ORDER_ADDITION) || '0';
// Adjust index if using one-based indexing.
if (block.workspace.options.oneBasedIndex) {
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.JavaScript.ORDER_ATOMIC];
} else {
// If the index is dynamic, adjust it in code.
index = index + ' + 1';
}
} else if (Blockly.isNumber(index)) {
return [index, Blockly.JavaScript.ORDER_ATOMIC];
}
return [index, Blockly.JavaScript.ORDER_ADDITION];
};

View File

@@ -0,0 +1,175 @@
/**
* @license
* Visual Blocks Language
*
* Copyright 2016 Google Inc.
* https://developers.google.com/blockly/
*
* 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 Lua for unit test blocks.
* @author rodrigoq@google.com (Rodrigo Queiro)
*/
'use strict';
Blockly.Lua['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.Lua.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.Lua.provideFunction_(
'unittest_report',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '()',
' -- Create test report.',
' local report = {}',
' local summary = {}',
' local fails = 0',
' for _, v in pairs(' + resultsVar + ') do',
' if v["success"] then',
' table.insert(summary, ".")',
' else',
' table.insert(summary, "F")',
' fails = fails + 1',
' table.insert(report, "FAIL: " .. v["title"])',
' table.insert(report, v["log"])',
' end',
' end',
' table.insert(report, 1, table.concat(summary))',
' table.insert(report, "")',
' table.insert(report, "Number of tests run: " .. #' + resultsVar + ')',
' table.insert(report, "")',
' if fails > 0 then',
' table.insert(report, "FAILED (failures=" .. fails .. ")")',
' else',
' table.insert(report, "OK")',
' end',
' return table.concat(report, "\\n")',
'end']);
// Setup global to hold test results.
var code = resultsVar + ' = {}\n';
// Run tests (unindented).
code += Blockly.Lua.statementToCode(block, 'DO')
.replace(/^ /, '').replace(/\n /g, '\n');
// Print the report.
code += 'print(' + functionName + '())\n';
// Destroy results.
code += resultsVar + ' = nil\n';
return code;
};
Blockly.Lua['unittest_main'].defineAssert_ = function(block) {
var resultsVar = Blockly.Lua.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.Lua.provideFunction_(
'assertEquals',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
'(actual, expected, message)',
' -- Asserts that a value equals another value.',
' assert(' + resultsVar + ' ~= nil, ' +
'"Orphaned assert equals: " .. message)',
' if type(actual) == "table" and type(expected) == "table" then',
' local lists_match = #actual == #expected',
' if lists_match then',
' for i, v1 in ipairs(actual) do',
' local v2 = expected[i]',
' if type(v1) == "number" and type(v2) == "number" then',
' if math.abs(v1 - v2) > 1e-9 then',
' lists_match = false',
' end',
' elseif v1 ~= v2 then',
' lists_match = false',
' end',
' end',
' end',
' if lists_match then',
' table.insert(' + resultsVar +
', {success=true, log="OK", title=message})',
' return',
' else',
' -- produce the non-matching strings for a human-readable error',
' expected = "{" .. table.concat(expected, ", ") .. "}"',
' actual = "{" .. table.concat(actual, ", ") .. "}"',
' end',
' end',
' if actual == expected or (type(actual) == "number" and ' +
'type(expected) == "number" and math.abs(actual - expected) < ' +
'1e-9) then ',
' table.insert(' + resultsVar +
', {success=true, log="OK", title=message})',
' else',
' table.insert(' + resultsVar + ', {success=false, ' +
'log=string.format("Expected: %s\\nActual: %s"' +
', tostring(expected), tostring(actual)), title=message})',
' end',
'end']);
return functionName;
};
Blockly.Lua['unittest_assertequals'] = function(block) {
// Asserts that a value equals another value.
var message = Blockly.Lua.valueToCode(block, 'MESSAGE',
Blockly.Lua.ORDER_NONE) || '';
var actual = Blockly.Lua.valueToCode(block, 'ACTUAL',
Blockly.Lua.ORDER_NONE) || 'nil';
var expected = Blockly.Lua.valueToCode(block, 'EXPECTED',
Blockly.Lua.ORDER_NONE) || 'nil';
return Blockly.Lua['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ')\n';
};
Blockly.Lua['unittest_assertvalue'] = function(block) {
// Asserts that a value is true, false, or null.
var message = Blockly.Lua.valueToCode(block, 'MESSAGE',
Blockly.Lua.ORDER_NONE) || '';
var actual = Blockly.Lua.valueToCode(block, 'ACTUAL',
Blockly.Lua.ORDER_NONE) || 'nil';
var expected = block.getFieldValue('EXPECTED');
if (expected == 'TRUE') {
expected = 'true';
} else if (expected == 'FALSE') {
expected = 'false';
} else if (expected == 'NULL') {
expected = 'nil';
}
return Blockly.Lua.unittest_main.defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ')\n';
};
Blockly.Lua['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.Lua.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var message = Blockly.Lua.quote_(block.getFieldValue('MESSAGE'));
var functionName = Blockly.Lua.provideFunction_(
'unittest_fail',
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(message)',
' -- Always assert an error.',
' assert(' + resultsVar +
' ~= nil, "Orphaned assert fail: " .. message)',
' table.insert(' + resultsVar +
', {success=false, log="Fail.", title=message})',
'end']);
return functionName + '(' + message + ')\n';
};
Blockly.Lua['unittest_adjustindex'] = function(block) {
var index = Blockly.Lua.valueToCode(block, 'INDEX',
Blockly.Lua.ORDER_ADDITIVE) || '0';
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.Lua.ORDER_ATOMIC];
}
// If the index is dynamic, adjust it in code.
return [index + ' + 1', Blockly.Lua.ORDER_ATOMIC];
};

View File

@@ -0,0 +1,164 @@
/**
* @license
* Visual Blocks Language
*
* Copyright 2015 Google Inc.
* https://developers.google.com/blockly/
*
* 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 PHP for unit test blocks.
* @author daarond@gmail.com (Daaron Dwyer)
*/
'use strict';
Blockly.PHP['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.PHP.provideFunction_(
'unittest_report',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '() {',
' global ' + resultsVar + ';',
' // Create test report.',
' $report = array();',
' $summary = array();',
' $fails = 0;',
' for ($x = 0; $x < count(' + resultsVar + '); $x++) {',
' if (' + resultsVar + '[$x][0]) {',
' array_push($summary, ".");',
' } else {',
' array_push($summary, "F");',
' $fails++;',
' array_push($report, "");',
' array_push($report, "FAIL: " . ' + resultsVar + '[$x][2]);',
' array_push($report, ' + resultsVar + '[$x][1]);',
' }',
' }',
' array_unshift($report, implode("", $summary));',
' array_push($report, "");',
' array_push($report, "Number of tests run: " . count(' + resultsVar + '));',
' array_push($report, "");',
' if ($fails) {',
' array_push($report, "FAILED (failures=" . $fails + ")");',
' } else {',
' array_push($report, "OK");',
' }',
' return implode("\\n", $report);',
'}']);
// Setup global to hold test results.
var code = resultsVar + ' = array();\n';
// Run tests (unindented).
code += Blockly.PHP.statementToCode(block, 'DO')
.replace(/^ /, '').replace(/\n /g, '\n');
// Send the report to the console (that's where errors will go anyway).
code += 'print(' + functionName + '());\n';
// Destroy results.
code += resultsVar + ' = null;\n';
return code;
};
Blockly.PHP['unittest_main'].defineAssert_ = function(block) {
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.PHP.provideFunction_(
'assertEquals',
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($actual, $expected, $message) {',
' global ' + resultsVar + ';',
' // Asserts that a value equals another value.',
' if (!is_array(' + resultsVar + ')) {',
' throw new Exception("Orphaned assert: " . $message);',
' }',
' if ($actual == $expected) {',
' array_push(' + resultsVar + ', [true, "OK", $message]);',
' } else {',
' $expected = is_array($expected) ? implode(" ", $expected) : ' +
'$expected;',
' $actual = is_array($actual) ? implode(" ", $actual) : ' +
'$actual;',
' array_push(' + resultsVar + ', [false, ' +
'"Expected: " . $expected . "\\nActual: " . $actual, $message]);',
' }',
'}']);
return functionName;
};
Blockly.PHP['unittest_assertequals'] = function(block) {
// Asserts that a value equals another value.
var message = Blockly.PHP.valueToCode(block, 'MESSAGE',
Blockly.PHP.ORDER_NONE) || '';
var actual = Blockly.PHP.valueToCode(block, 'ACTUAL',
Blockly.PHP.ORDER_COMMA) || 'null';
var expected = Blockly.PHP.valueToCode(block, 'EXPECTED',
Blockly.PHP.ORDER_COMMA) || 'null';
return Blockly.PHP['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.PHP['unittest_assertvalue'] = function(block) {
// Asserts that a value is true, false, or null.
var message = Blockly.PHP.valueToCode(block, 'MESSAGE',
Blockly.PHP.ORDER_NONE) || '';
var actual = Blockly.PHP.valueToCode(block, 'ACTUAL',
Blockly.PHP.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.PHP['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ');\n';
};
Blockly.PHP['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var message = Blockly.PHP.quote_(block.getFieldValue('MESSAGE'));
var functionName = Blockly.PHP.provideFunction_(
'unittest_fail',
[ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($message) {',
' global ' + resultsVar + ';',
' // Always assert an error.',
' if (!' + resultsVar + ') {',
' throw new Exception("Orphaned assert fail: " . $message);',
' }',
' array_push(' + resultsVar + ', [false, "Fail.", $message]);',
'}']);
return functionName + '(' + message + ');\n';
};
Blockly.PHP['unittest_adjustindex'] = function(block) {
var index = Blockly.PHP.valueToCode(block, 'INDEX',
Blockly.PHP.ORDER_ADDITION) || '0';
// Adjust index if using one-based indexing.
if (block.workspace.options.oneBasedIndex) {
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.PHP.ORDER_ATOMIC];
} else {
// If the index is dynamic, adjust it in code.
index = index + ' + 1';
}
} else if (Blockly.isNumber(index)) {
return [index, Blockly.PHP.ORDER_ATOMIC];
}
return [index, Blockly.PHP.ORDER_ADDITION];
};

View File

@@ -0,0 +1,148 @@
/**
* @license
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* https://developers.google.com/blockly/
*
* 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 Python for unit test blocks.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
Blockly.Python['unittest_main'] = function(block) {
// Container for unit tests.
var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.Python.provideFunction_(
'unittest_report',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '():',
' # Create test report.',
' report = []',
' summary = []',
' fails = 0',
' for (success, log, message) in ' + resultsVar + ':',
' if success:',
' summary.append(".")',
' else:',
' summary.append("F")',
' fails += 1',
' report.append("")',
' report.append("FAIL: " + message)',
' report.append(log)',
' report.insert(0, "".join(summary))',
' report.append("")',
' report.append("Number of tests run: %d" % len(' + resultsVar + '))',
' report.append("")',
' if fails:',
' report.append("FAILED (failures=%d)" % fails)',
' else:',
' report.append("OK")',
' return "\\n".join(report)']);
// Setup global to hold test results.
var code = resultsVar + ' = []\n';
// Run tests (unindented).
code += Blockly.Python.statementToCode(block, 'DO')
.replace(/^ /, '').replace(/\n /g, '\n');
// Print the report.
code += 'print(' + functionName + '())\n';
// Destroy results.
code += resultsVar + ' = None\n';
return code;
};
Blockly.Python['unittest_main'].defineAssert_ = function() {
var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var functionName = Blockly.Python.provideFunction_(
'assertEquals',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
'(actual, expected, message):',
' # Asserts that a value equals another value.',
' if ' + resultsVar + ' == None:',
' raise Exception("Orphaned assert equals: " + message)',
' if actual == expected:',
' ' + resultsVar + '.append((True, "OK", message))',
' else:',
' ' + resultsVar + '.append((False, ' +
'"Expected: %s\\nActual: %s" % (expected, actual), message))']);
return functionName;
};
Blockly.Python['unittest_assertequals'] = function(block) {
// Asserts that a value equals another value.
var message = Blockly.Python.valueToCode(block, 'MESSAGE',
Blockly.Python.ORDER_NONE) || '';
var actual = Blockly.Python.valueToCode(block, 'ACTUAL',
Blockly.Python.ORDER_NONE) || 'None';
var expected = Blockly.Python.valueToCode(block, 'EXPECTED',
Blockly.Python.ORDER_NONE) || 'None';
return Blockly.Python['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ')\n';
};
Blockly.Python['unittest_assertvalue'] = function(block) {
// Asserts that a value is true, false, or null.
var message = Blockly.Python.valueToCode(block, 'MESSAGE',
Blockly.Python.ORDER_NONE) || '';
var actual = Blockly.Python.valueToCode(block, 'ACTUAL',
Blockly.Python.ORDER_NONE) || 'None';
var expected = block.getFieldValue('EXPECTED');
if (expected == 'TRUE') {
expected = 'True';
} else if (expected == 'FALSE') {
expected = 'False';
} else if (expected == 'NULL') {
expected = 'None';
}
return Blockly.Python['unittest_main'].defineAssert_() +
'(' + actual + ', ' + expected + ', ' + message + ')\n';
};
Blockly.Python['unittest_fail'] = function(block) {
// Always assert an error.
var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
Blockly.Names.DEVELOPER_VARIABLE_TYPE);
var message = Blockly.Python.quote_(block.getFieldValue('MESSAGE'));
var functionName = Blockly.Python.provideFunction_(
'fail',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(message):',
' # Always assert an error.',
' if ' + resultsVar + ' == None:',
' raise Exception("Orphaned assert equals: " + message)',
' ' + resultsVar + '.append((False, "Fail.", message))']);
return functionName + '(' + message + ')\n';
};
Blockly.Python['unittest_adjustindex'] = function(block) {
var index = Blockly.Python.valueToCode(block, 'INDEX',
Blockly.Python.ORDER_ADDITIVE) || '0';
// Adjust index if using one-based indexing.
if (block.workspace.options.oneBasedIndex) {
if (Blockly.isNumber(index)) {
// If the index is a naked number, adjust it right now.
return [parseFloat(index) + 1, Blockly.Python.ORDER_ATOMIC];
} else {
// If the index is dynamic, adjust it in code.
index = index + ' + 1';
}
} else if (Blockly.isNumber(index)) {
return [index, Blockly.Python.ORDER_ATOMIC];
}
return [index, Blockly.Python.ORDER_ADDITIVE];
};

View File

@@ -0,0 +1,67 @@
<xml xmlns="http://www.w3.org/1999/xhtml">
<!-- Do not include <variables> here to test backward compatibility. -->
<block type="unittest_main" x="0" y="0">
<statement name="DO">
<block type="variables_set" inline="false">
<field name="VAR">item</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">123</field>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">variable</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">item</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">123</field>
</block>
</value>
<next>
<block type="variables_set" inline="false">
<field name="VAR">if</field>
<value name="VALUE">
<block type="math_number">
<field name="NUM">123</field>
</block>
</value>
<next>
<block type="unittest_assertequals" inline="false">
<value name="MESSAGE">
<block type="text">
<field name="TEXT">reserved variable</field>
</block>
</value>
<value name="ACTUAL">
<block type="variables_get">
<field name="VAR">if</field>
</block>
</value>
<value name="EXPECTED">
<block type="math_number">
<field name="NUM">123</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
<block type="variables_get" x="300" y="100">
<field name="VAR">naked</field>
<comment pinned="true" h="80" w="160">Intentionally non-connected variable.</comment>
</block>
</xml>