8000 feat: add throwing errors for bad shadows by BeksOmega · Pull Request #5330 · google/blockly · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add throwing errors for bad shadows #5330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions core/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,12 +781,23 @@ Blockly.Connection.prototype.createShadowBlock_ = function(attemptToConnect) {
if (shadowDom) {
blockShadow = Blockly.Xml.domToBlock(shadowDom, parentBlock.workspace);
if (attemptToConnect) {
if (blockShadow.outputConnection) {
this.connect(blockShadow.outputConnection);
} else if (blockShadow.previousConnection) {
this.connect(blockShadow.previousConnection);
if (this.type == Blockly.connectionTypes.INPUT_VALUE) {
if (!blockShadow.outputConnection) {
throw new Error('Shadow block is missing an output connection');
}
if (!this.connect(blockShadow.outputConnection)) {
throw new Error('Could not connect shadow block to connection');
}
} else if (this.type == Blockly.connectionTypes.NEXT_STATEMENT) {
if (!blockShadow.previousConnection) {
throw new Error('Shadow block is missing previous connection');
}
if (!this.connect(blockShadow.previousConnection)) {
throw new Error('Could not connect shadow block to connection');
}
} else {
throw Error('Shadow block does not have output or previous statement.');
throw new Error(
'Cannot connect a shadow block to a previous/output connection');
}
}
return blockShadow;
Expand Down
97 changes: 97 additions & 0 deletions tests/mocha/connection_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,56 @@ suite('Connection', function() {
assertNextNotHasBlock(parent);
});
});

suite('Invalid', function() {
test('Attach to output', function() {
const block = this.workspace.newBlock('row_block');
chai.assert.throws(() =>
block.outputConnection.setShadowDom(Blockly.Xml.textToDom(
'<block type="row_block">')));
});

test('Attach to previous', function() {
const block = this.workspace.newBlock('stack_block');
chai.assert.throws(() =>
block.previousConnection.setShadowDom(Blockly.Xml.textToDom(
'<block type="stack_block">')));
});

test('Missing output', function() {
const block = this.workspace.newBlock('row_block');
chai.assert.throws(() =>
block.outputConnection.setShadowDom(Blockly.Xml.textToDom(
'<block type="stack_block">')));
});

test('Missing previous', function() {
const block = this.workspace.newBlock('stack_block');
chai.assert.throws(() =>
block.previousConnection.setShadowDom(Blockly.Xml.textToDom(
'<block type="row_block">')));
});

test('Invalid connection checks, output', function() {
const block = this.workspace.newBlock('logic_operation');
chai.assert.throws(() =>
block.getInput('A').connection.setShadowDom(
Blockly.Xml.textToDom('<block type="stack_block">')));
});

test('Invalid connection checks, previous', function() {
Blockly.defineBlocksWithJsonArray([{
"type": "stack_checks_block",
"message0": "",
"previousStatement": "check 1",
"nextStatement": "check 2"
}]);
const block = this.workspace.newBlock('stack_checks_block');
chai.assert.throws(() =>
block.nextConnection.setShadowDom(Blockly.Xml.textToDom(
'<block type="stack_checks_block">')));
});
});
});

suite('setShadowState', function() {
Expand Down Expand Up @@ -2512,6 +2562,53 @@ suite('Connection', function() {
assertNextNotHasBlock(parent);
});
});

suite('Invalid', function() {
test('Attach to output', function() {
const block = this.workspace.newBlock('row_block');
chai.assert.throws(() =>
block.outputConnection.setShadowState({'type': 'row_block'}));
});

test('Attach to previous', function() {
const block = this.workspace.newBlock('stack_block');
chai.assert.throws(() =>
block.previousConnection.setShadowState(
{'type': 'stack_block'}));
});

test('Missing output', function() {
const block = this.workspace.newBlock('row_block');
chai.assert.throws(() =>
block.outputConnection.setShadowState({'type': 'stack_block'}));
});

test('Missing previous', function() {
const block = this.workspace.newBlock('stack_block');
chai.assert.throws(() =>
block.previousConnection.setShadowState({'type': 'row_block'}));
});

test('Invalid connection checks, output', function() {
const block = this.workspace.newBlock('logic_operation');
chai.assert.throws(() =>
block.getInput('A').connection.setShadowState(
{'type': 'math_number'}));
});

test('Invalid connection checks, previous', function() {
Blockly.defineBlocksWithJsonArray([{
"type": "stack_checks_block",
"message0": "",
"previousStatement": "check 1",
"nextStatement": "check 2"
}]);
const block = this.workspace.newBlock('stack_checks_block');
chai.assert.throws(() =>
block.nextConnection.setShadowState(
{'type': 'stack_checks_block'}));
});
});
});
});
});
Expand Down
0