mirror of
https://github.com/google/blockly.git
synced 2026-01-09 01:50:11 +01:00
* refactor(tests): Move and rename prepare.js, blockly.mjs
Since prepare.js and blockly.mjs are going to be needed for running
all tests in uncompiled mode (not just the playgrounds), move them
tests/. Further, rename prepare.js to bootstrap.js to better reflect
its purpose.
* feat(tests): Introduce BLOCKLY_BOOTSTRAP_OPTIONS
Provide a mechanism for web pages that use bootstrap.js to control
what is loaded and how.
* fix(tests): Use the blockly repository path for all script src= URLs
Previously the (non-advanced) playground was only correctly loadging
on localhost because you can put an arbitrary number of "../"s in front
of a relative URL and it just takes you to the root directory.
* fix(tests): Don't use template literals in bootstrap.js
This is necessary (but not necessarily sufficient) to be able to
load the file in IE 11.
* fix(tests): Throw error if attempting to bootstrap in node.js
* feat(tests): Make bootstrap.js more configurable.
* Terminology change: use "compressed" and "uncompressed" to describe
what Closure Compiler calls "compiled" and "uncompiled", to reduce
confusion with the compilation that will be done by tsc.
* Get the list of modules to bootstrap (in compressed mode), or
scripts to load (in compressed mode) from BLOCKLY_BOOTSTRAP_OPTIONS,
to allow calling scripts to to specify exactly what to load.
* feat(tests): Use a proper quote function
We need to generate string literals. Best to use a quote function
instead of concatenating on quote marks withou escaping. Copy a
well-tested one from Code City.
* feat(tests): Support an additionalScripts option
This is a list of scripts to load (in order) once the required modules
have been bootstrapped.
We do this using goog.addDependency to make the first script depend
on the required modules, then each subsequent script depend on the
previous one, and then finally goog.bootstrapping the last such script.
* refactor(tests): Remove special handling of msg/messages.js
* refactor(tests): Use additionalScripts for all script loading
Use additionalScripts option for all script loading in
playground.html and advanced_playground.html.
* refactor(tests): Use bootstrap instead of uncompressed in Mocha tests
Use tests/bootstrap.js instead of blockly_uncompressed.js to load
blockly in uncompressed mode in the Mocha tests.
This entails adding a new item, despFiles, to BLOCKLY_BOOTSTRAP_OPTIONS,
to allow tests/deps.mocha.js to be loaded at the appropriate point.
Mention of blockly_uncompressed.js is removed from
tests/mocah/.mocharc.js; it's not clear to me what effect the "file:"
directive in this file might have previously had and I was not able to
find documentation for it on mochajs.org, but in any case removing it
appears to have had no ill effect.
* refactor(tests): Use bootstrap instead of uncompressed in generator tests
This entails adding an additional check in bootstrap so as to load
uncompressed when loading from a file: URL, since these are not
localhost URLs - though in fact the generator tests run equally well
in compressed mode, albeit against (for now) the previously-check-in
build products rather than the live code.
* refactor(test): Use bootstrap.js in multi_playground.html
This removes the last use of load_all.js, so remove it.
* chore(tests): Delete blockly_uncompressed.js
Its function has now been entirely subsumed by tests/bootstrap.js,
so remove it and update any remaining mentions of it.
Also fix formatting and positions of some comments in playground.html.
* fix(tests): Rewrite bootstrap sequencing code
An earlier commit modified the generated <script> to use
goog.addDependency to trick the debug module loader into loading
.additionalScripts (via goog.bootstrap), but it turns out there is
a small problem: scripts like msg/messages.js have undeclared
dependencies on the Blockly module, and without a call to
goog.require('Blockly') in them they can end up being run before
the Blockly module is fully loaded.
(This problem only occurs when there are ES Modules, rather than
merely goog.modules, in the mix.)
Fix this by adding a script, bootstrap_helper.js, to be loaded
options.requires and any options.additionalScripts that makes an
explicit call to goog.require for each of option.requires.
Also refactor the code so that instead of generating a loop which
calls goog.addDependency, we generate the addDependency calls
directly. This makes debugging a bit easer as we can use the browser's
dev tools to inspect the generated calls in the DOM tree.
* fix(tests): Prevent spurious transpilation warnings
For some reason when the debug module loader encounters ES modules
it starts to complain about being unable to transpile some ES202x
features in other (non-ESM) modules, even though it doesn't normally
try to transpile those.
Since uncompressed-mode testing is almost exclusively on modern
browsers we don't care about transpiling these features, so suppress
the warnings instead.
* refactor(tests): Rename blockly.mjs to bootstrap_done.mjs; simplify
Since blockly.mjs is no longer returning just the exports object
from core/blockly.js (see PR #5995), it might be better named after
its actual purpose: to wait for bootstrapping to be done.
Remove all the code that was used to pass the blockly.js exports
object along from the bootstrap callback to the blockly.mjs export,
since there's no reason to go to a lot of trouble to set a local
variable named Blockly to the same value as a global variable named
Blockly.
(Something like this may be needed again in future, but certainly in
a different form.)
* chore(tests): Use freshly-build files in compressed mode.
Use the freshly-built build/*_compresssed.js files when bootstrapping
in compressed mode, rather than using the checked-in files in the
repository root.
This helps ensure that compressed and uncompressed mode will be
testing (as closely as possible) the same code.
* chore(tests): Rename BlocklyLoader to blocklyLoader; record compressed
- Rename the BlocklyLoader global to blocklyLoader (since it is not
a class constructor).
- Create it regardless of whether we are bootstrapping in
uncompressed or loading compressed via <script> tags.
- Record which we are doing as .compressed, and use this property
to choose playground background colour.
* chore(tests): Resolve comments for PR #6214
Mostly documentation changes, but notably renaming blocklyLoader to
bootstrapInfo.
* Revert "chore(tests): Use freshly-build files in compressed mode."
This reverts commit de8d356838.
406 lines
13 KiB
HTML
406 lines
13 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Blockly Generator Tests</title>
|
|
<script>
|
|
var BLOCKLY_BOOTSTRAP_OPTIONS = {
|
|
additionalScripts: [
|
|
'msg/messages.js',
|
|
'tests/generators/unittest_javascript.js',
|
|
'tests/generators/unittest_python.js',
|
|
'tests/generators/unittest_php.js',
|
|
'tests/generators/unittest_lua.js',
|
|
'tests/generators/unittest_dart.js',
|
|
'tests/generators/unittest.js',
|
|
],
|
|
}
|
|
</script>
|
|
<script src="../bootstrap.js"></script>
|
|
<script>
|
|
var demoWorkspace = null;
|
|
var outputCode = '';
|
|
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* Run this test to load all of the tests in the selected suites. The contents
|
|
* will be loaded into the workspace in order. To test the generators:
|
|
* - select your language from the buttons above the text area
|
|
* - copy all of the generated code
|
|
* - run it in an interpreter of your choice
|
|
* - scan all of the results for "FAIL". All test suites are running in order,
|
|
* and each will report OK or FAIL based only on the tests in that suite.
|
|
* If some tests are failing, load test suites individually to continue
|
|
* debugging.
|
|
*/
|
|
function loadSelected() {
|
|
var output = document.getElementById('importExport');
|
|
output.style.background = 'gray';
|
|
|
|
var loadingElem = document.getElementById('loading');
|
|
loadingElem.textContent = 'loading...';
|
|
|
|
// Clear before adding all of the blocks.
|
|
demoWorkspace.clear();
|
|
|
|
var boxList = document.getElementsByClassName('suite_checkbox');
|
|
for (var i = 0; i < boxList.length; i++) {
|
|
if (boxList[i].checked) {
|
|
var testUrl = boxList[i].value;
|
|
if (testUrl) {
|
|
var xmlText = fetchFile(testUrl);
|
|
if (xmlText !== null) {
|
|
fromXml(testUrl, xmlText, /* opt_append */ true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
output.style.background = '';
|
|
loadingElem.textContent = 'done';
|
|
}
|
|
|
|
/**
|
|
* Ask the user for a file name, then load that file's contents.
|
|
*/
|
|
function loadOther() {
|
|
var 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.
|
|
* @param {boolean=} opt_append True if the XML should be appended to the
|
|
* workspace. Otherwise the workspace is cleared and the new XML is loaded.
|
|
*/
|
|
function fromXml(filename, xmlText, opt_append) {
|
|
var output = document.getElementById('importExport');
|
|
output.value = xmlText;
|
|
output.scrollTop = 0;
|
|
output.scrollLeft = 0;
|
|
if (!opt_append) {
|
|
demoWorkspace.clear();
|
|
}
|
|
try {
|
|
var xmlDoc = Blockly.Xml.textToDom(xmlText);
|
|
if (opt_append) {
|
|
Blockly.Xml.appendDomToWorkspace(xmlDoc, demoWorkspace);
|
|
} else {
|
|
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 checkAll() {
|
|
var boxList = document.getElementsByClassName('suite_checkbox');
|
|
for (var i = 0; i < boxList.length; i++) {
|
|
boxList[i].checked = true;
|
|
}
|
|
}
|
|
|
|
function uncheckAll() {
|
|
var boxList = document.getElementsByClassName('suite_checkbox');
|
|
for (var i = 0; i < boxList.length; i++) {
|
|
boxList[i].checked = false;
|
|
}
|
|
}
|
|
|
|
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);
|
|
outputCode = code;
|
|
}
|
|
|
|
function toPython() {
|
|
var code = Blockly.Python.workspaceToCode(demoWorkspace);
|
|
setOutput(code);
|
|
outputCode = code;
|
|
}
|
|
|
|
function toPhp() {
|
|
var code = Blockly.PHP.workspaceToCode(demoWorkspace);
|
|
setOutput(code);
|
|
outputCode = code;
|
|
}
|
|
|
|
function toLua() {
|
|
var code = Blockly.Lua.workspaceToCode(demoWorkspace);
|
|
setOutput(code);
|
|
outputCode = code;
|
|
}
|
|
|
|
function toDart() {
|
|
var code = Blockly.Dart.workspaceToCode(demoWorkspace);
|
|
setOutput(code);
|
|
outputCode = code;
|
|
}
|
|
|
|
function changeIndex() {
|
|
var oneBasedIndex = document.getElementById('indexing').checked;
|
|
demoWorkspace.options.oneBasedIndex = oneBasedIndex;
|
|
demoWorkspace.getToolbox().flyout_.workspace_.options.oneBasedIndex = oneBasedIndex;
|
|
}
|
|
</script>
|
|
<script type=module>
|
|
// Wait for Blockly to finish loading before running tests.
|
|
import '../bootstrap_done.mjs';
|
|
|
|
start();
|
|
</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%;
|
|
}
|
|
#loading {
|
|
color: red;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div id="blocklyDiv"></div>
|
|
|
|
<xml xmlns="https://developers.google.com/blockly/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>
|
|
<block type="math_atan2"></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>
|
|
<block type="text_multiline"></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.
|
|
|
|
|
|
<div id="checkboxes">
|
|
<input type="button" value="Check all" onclick="checkAll()">
|
|
<input type="button" value="Uncheck all" onclick="uncheckAll()"><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="logic.xml">Logic</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="loops1.xml">Loops 1 (repeat, while, foreach)</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="loops2.xml">Loops 2 (count)</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="loops3.xml">Loops 3 (continue, break)</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="math.xml">Math</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="text.xml">Text</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="lists.xml">Lists</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="colour.xml">Colour</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="variables.xml">Variables</input><br/>
|
|
<input type="checkbox" class="suite_checkbox" value="functions.xml">Functions</input><br/>
|
|
</div>
|
|
|
|
<p>
|
|
<input type="button" value="Load selected" id="loadSelectedBtn" onclick="loadSelected()">
|
|
<label for="loadSelectedBtn" id="loading"></label>
|
|
</p>
|
|
<p>
|
|
<input type="button" value="Load by file name" onclick="loadOther()">
|
|
</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/python3">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="https://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>
|