mirror of
https://github.com/google/blockly.git
synced 2026-01-30 12:10:12 +01:00
132 lines
3.2 KiB
JavaScript
132 lines
3.2 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2019 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/* exported assertArrayEquals, assertVariableValues, captureWarnings
|
|
defineRowBlock, defineStackBlock, defineStatementBlock, createTestBlock,
|
|
createEventsFireStub */
|
|
|
|
/**
|
|
* Check that two arrays have the same content.
|
|
* @param {!Array.<string>} array1 The first array.
|
|
* @param {!Array.<string>} array2 The second array.
|
|
* @param {?string} opt_message Optional message to pass into assert.
|
|
*/
|
|
function isEqualArrays(array1, array2, opt_message) {
|
|
chai.assert.equal(array1.length, array2.length, opt_message);
|
|
for (var i = 0; i < array1.length; i++) {
|
|
chai.assert.equal(array1[i], array2[i], opt_message);
|
|
}
|
|
}
|
|
|
|
function assertArrayEquals(actualArray, expectedArray, opt_message) {
|
|
isEqualArrays(actualArray, expectedArray, opt_message);
|
|
}
|
|
|
|
/**
|
|
* Check if a variable with the given values exists.
|
|
* @param {Blockly.Workspace|Blockly.VariableMap} container The workspace or
|
|
* variableMap the checked variable belongs to.
|
|
* @param {!string} name The expected name of the variable.
|
|
* @param {!string} type The expected type of the variable.
|
|
* @param {!string} id The expected id of the variable.
|
|
*/
|
|
function assertVariableValues(container, name, type, id) {
|
|
var variable = container.getVariableById(id);
|
|
chai.assert.isDefined(variable);
|
|
chai.assert.equal(variable.name, name);
|
|
chai.assert.equal(variable.type, type);
|
|
chai.assert.equal(variable.getId(), id);
|
|
}
|
|
|
|
/**
|
|
* Captures the strings sent to console.warn() when calling a function.
|
|
* @param {function} innerFunc The function where warnings may called.
|
|
* @return {string[]} The warning messages (only the first arguments).
|
|
*/
|
|
function captureWarnings(innerFunc) {
|
|
var msgs = [];
|
|
var nativeConsoleWarn = console.warn;
|
|
try {
|
|
console.warn = function(msg) {
|
|
msgs.push(msg);
|
|
};
|
|
innerFunc();
|
|
} finally {
|
|
console.warn = nativeConsoleWarn;
|
|
}
|
|
return msgs;
|
|
}
|
|
|
|
|
|
/**
|
|
* Creates stub for Blockly.Events.fire that fires events immediately instead of
|
|
* with timeout.
|
|
* @return {sinon.stub} The created stub.
|
|
*/
|
|
function createEventsFireStub() {
|
|
var stub = sinon.stub(Blockly.Events, 'fire');
|
|
stub.callsFake(function(event) {
|
|
if (!Blockly.Events.isEnabled()) {
|
|
return;
|
|
}
|
|
Blockly.Events.FIRE_QUEUE_.push(event);
|
|
Blockly.Events.fireNow_();
|
|
});
|
|
stub.firedEvents_ = [];
|
|
return stub;
|
|
}
|
|
|
|
function defineStackBlock() {
|
|
Blockly.defineBlocksWithJsonArray([{
|
|
"type": "stack_block",
|
|
"message0": "",
|
|
"previousStatement": null,
|
|
"nextStatement": null
|
|
}]);
|
|
}
|
|
|
|
function defineRowBlock() {
|
|
Blockly.defineBlocksWithJsonArray([{
|
|
"type": "row_block",
|
|
"message0": "%1",
|
|
"args0": [
|
|
{
|
|
"type": "input_value",
|
|
"name": "INPUT"
|
|
}
|
|
],
|
|
"output": null
|
|
}]);
|
|
}
|
|
|
|
function defineStatementBlock() {
|
|
Blockly.defineBlocksWithJsonArray([{
|
|
"type": "statement_block",
|
|
"message0": "%1",
|
|
"args0": [
|
|
{
|
|
"type": "input_statement",
|
|
"name": "NAME"
|
|
}
|
|
],
|
|
"previousStatement": null,
|
|
"nextStatement": null,
|
|
"colour": 230,
|
|
"tooltip": "",
|
|
"helpUrl": ""
|
|
}]);
|
|
}
|
|
|
|
function createTestBlock() {
|
|
return {
|
|
id: 'test',
|
|
rendered: false,
|
|
workspace: {
|
|
rendered: false
|
|
}
|
|
};
|
|
}
|