Skip to content

Commit

Permalink
feat: add JSO serialization and deserialization of variables (google#…
Browse files Browse the repository at this point in the history
…5131)

* Add variable serialization tests

* Fix requires for new file

* Add serialization and deserialization of variables

* Remove only in tests

* Cleanup
  • Loading branch information
BeksOmega authored and alschmiedt committed Sep 20, 2021
1 parent 56d3cb6 commit 28ff2c8
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
1 change: 1 addition & 0 deletions core/requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ goog.require('Blockly.zelos.Renderer');
goog.require('Blockly.Themes.Classic');

goog.require('Blockly.serialization.blocks');
goog.require('Blockly.serialization.variables');
62 changes: 62 additions & 0 deletions core/serialization/variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Handles serializing variables to plain JavaScript objects, only
* containing state.
*/
'use strict';

goog.module('Blockly.serialization.variables');
goog.module.declareLegacyNamespace();

// eslint-disable-next-line no-unused-vars
const VariableModel = goog.requireType('Blockly.VariableModel');
// eslint-disable-next-line no-unused-vars
const Workspace = goog.requireType('Blockly.Workspace');


/**
* Represents the state of a given variable.
* @typedef {{
* name: string,
* id: string,
* type: (string|undefined),
* }}
*/
var State;
exports.State = State;

/**
* Returns the state of the variable as a plain JavaScript object.
* @param {!VariableModel} variableModel The variable to serialize.
* @return {!State} The serialized state of the variable.
*/
const save = function(variableModel) {
const state = {
'name': variableModel.name,
'id': variableModel.getId()
};
if (variableModel.type) {
state['type'] = variableModel.type;
}
return state;
};
/** @package */
exports.save = save;

/**
* Loads the variable represented by the given state into the given workspace.
* Do not call this directly, use workspace.createVariable instead.
* @param {!State} state The state of a variable to deserialize into the
* workspace.
* @param {!Workspace} workspace The workspace to add the variable to.
*/
const load = function(state, workspace) {
workspace.createVariable(state['name'], state['type'], state['id']);
};
/** @package */
exports.load = load;
3 changes: 2 additions & 1 deletion tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions tests/deps.mocha.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 27 additions & 7 deletions tests/mocha/jso_serialization_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ suite('JSO', function() {
sharedTestTeardown.call(this);
});

suite('Blocks', function() {
function assertProperty(obj, property, value) {
chai.assert.deepEqual(obj[property], value);
}
function assertProperty(obj, property, value) {
chai.assert.deepEqual(obj[property], value);
}

function assertNoProperty(obj, property) {
assertProperty(obj, property, undefined);
}

function assertNoProperty(obj, property) {
assertProperty(obj, property, undefined);
}

suite('Blocks', function() {
test('Null on insertionMarkers', function() {
const block = this.workspace.newBlock('row_block');
block.setInsertionMarker(true);
Expand Down Expand Up @@ -634,4 +635,23 @@ suite('JSO', function() {
});
});
});

suite('Variables', function() {
test('Without type', function() {
const variable = this.workspace.createVariable('testVar', '', 'testId');
const jso = Blockly.serialization.variables.save(variable);
assertProperty(jso, 'name', 'testVar');
assertProperty(jso, 'id', 'testId');
assertNoProperty(jso, 'type');
});

test('With type', function() {
const variable = this.workspace
.createVariable('testVar', 'testType', 'testId');
const jso = Blockly.serialization.variables.save(variable);
assertProperty(jso, 'name', 'testVar');
assertProperty(jso, 'id', 'testId');
assertProperty(jso, 'type', 'testType');
});
});
});

0 comments on commit 28ff2c8

Please sign in to comment.