mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
* Add basic attribute tests * Add basic block serialization * Add more attribute tests * Change save to use options dictionary * Add obeying save options * Add test for data attribute * Add saving data tag * Move to ES6 syntax * Fixup move to es6 syntax * Declare module * Format and lint * Add docs * Add returning null on insertion markers * Fixup for move to module * Switch to other function declarations * Cleanup for finalized style * Fix lint and types * Export State def * Switch disabled=true to enabled=false
242 lines
8.3 KiB
JavaScript
242 lines
8.3 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2021 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
goog.module('Blockly.test.jsoSerialization');
|
|
|
|
const {defineStackBlock, defineRowBlock, defineStatementBlock, createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers');
|
|
|
|
|
|
suite('JSO', function() {
|
|
setup(function() {
|
|
sharedTestSetup.call(this);
|
|
this.workspace = new Blockly.Workspace();
|
|
|
|
defineStackBlock();
|
|
defineRowBlock();
|
|
defineStatementBlock();
|
|
|
|
createGenUidStubWithReturns(new Array(10).fill().map((_, i) => 'id' + i));
|
|
});
|
|
|
|
teardown(function() {
|
|
workspaceTeardown.call(this, this.workspace);
|
|
sharedTestTeardown.call(this);
|
|
});
|
|
|
|
suite('Blocks', function() {
|
|
test('Null on insertionMarkers', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setInsertionMarker(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
chai.assert.isNull(jso);
|
|
});
|
|
|
|
suite('Save Single Block', function() {
|
|
|
|
function assertProperty(obj, property, value) {
|
|
chai.assert.equal(obj[property], value);
|
|
}
|
|
|
|
function assertNoProperty(obj, property) {
|
|
assertProperty(obj, property, undefined);
|
|
}
|
|
|
|
test('Basic', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'type', 'row_block');
|
|
assertProperty(jso, 'id', 'id0');
|
|
});
|
|
|
|
suite('Attributes', function() {
|
|
suite('Collapsed', function() {
|
|
test('True', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setCollapsed(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'collapsed', true);
|
|
});
|
|
|
|
test('False', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setCollapsed(false);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'collapsed');
|
|
});
|
|
});
|
|
|
|
suite('Enabled', function() {
|
|
test('False', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setEnabled(false);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'enabled', false);
|
|
});
|
|
|
|
test('True', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setEnabled(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'enabled');
|
|
});
|
|
});
|
|
|
|
suite('Deletable', function() {
|
|
test('False', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setDeletable(false);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'deletable', false);
|
|
});
|
|
|
|
test('True', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setDeletable(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'deletable');
|
|
});
|
|
|
|
test('False and Shadow', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setDeletable(false);
|
|
block.setShadow(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'deletable');
|
|
});
|
|
});
|
|
|
|
suite('Movable', function() {
|
|
test('False', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setMovable(false);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'movable', false);
|
|
});
|
|
|
|
test('True', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setMovable(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'movable');
|
|
});
|
|
|
|
test('False and Shadow', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setMovable(false);
|
|
block.setShadow(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'movable');
|
|
});
|
|
});
|
|
|
|
suite('Editable', function() {
|
|
test('False', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setEditable(false);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'editable', false);
|
|
});
|
|
|
|
test('True', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.setEditable(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'editable');
|
|
});
|
|
});
|
|
|
|
suite('Inline', function() {
|
|
test('True', function() {
|
|
const block = this.workspace.newBlock('statement_block');
|
|
block.setInputsInline(true);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'inline', true);
|
|
});
|
|
|
|
test('False', function() {
|
|
const block = this.workspace.newBlock('statement_block');
|
|
block.setInputsInline(false);
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'inline', false);
|
|
});
|
|
|
|
test('undefined', function() {
|
|
const block = this.workspace.newBlock('statement_block');
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'inline');
|
|
});
|
|
|
|
test('True, matching default', function() {
|
|
const block = this.workspace.newBlock('statement_block');
|
|
block.setInputsInline(true);
|
|
block.inputsInlineDefault = true;
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'inline');
|
|
});
|
|
|
|
test('False, matching default', function() {
|
|
const block = this.workspace.newBlock('statement_block');
|
|
block.setInputsInline(false);
|
|
block.inputsInlineDefault = false;
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'inline');
|
|
});
|
|
});
|
|
|
|
suite('Data', function() {
|
|
test('No data', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'data');
|
|
});
|
|
|
|
test('With data', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.data = 'some data';
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertProperty(jso, 'data', 'some data');
|
|
});
|
|
});
|
|
});
|
|
|
|
suite('Coords', function() {
|
|
test('No coordinates', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
assertNoProperty(jso, 'x');
|
|
assertNoProperty(jso, 'y');
|
|
});
|
|
|
|
test('Simple', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.moveBy(42, 42);
|
|
const jso =
|
|
Blockly.serialization.blocks.save(block, {addCoordinates: true});
|
|
assertProperty(jso, 'x', 42);
|
|
assertProperty(jso, 'y', 42);
|
|
});
|
|
|
|
test('Negative', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
block.moveBy(-42, -42);
|
|
const jso =
|
|
Blockly.serialization.blocks.save(block, {addCoordinates: true});
|
|
assertProperty(jso, 'x', -42);
|
|
assertProperty(jso, 'y', -42);
|
|
});
|
|
|
|
test('Zero', function() {
|
|
const block = this.workspace.newBlock('row_block');
|
|
const jso =
|
|
Blockly.serialization.blocks.save(block, {addCoordinates: true});
|
|
assertProperty(jso, 'x', 0);
|
|
assertProperty(jso, 'y', 0);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|