Files
blockly/tests/mocha/test_helpers.js
2020-06-10 10:25:59 -07:00

114 lines
2.6 KiB
JavaScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/* exported assertVariableValues, captureWarnings, defineRowBlock,
defineStackBlock, defineStatementBlock, createTestBlock, createEventsFireStub */
/**
* 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
}
};
}