diff --git a/core/variable_model.js b/core/variable_model.js new file mode 100644 index 000000000..6f705d0bd --- /dev/null +++ b/core/variable_model.js @@ -0,0 +1,75 @@ +/** + * @license + * Visual Blocks Editor + * + * Copyright 2017 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Components for the variable model. + * @author marisaleung@google.com (Marisa Leung) + */ +'use strict'; + +goog.provide('Blockly.VariableModel'); + +/** + * Class for a variable model. + * Holds information for the variable including name, id, and type. + * @param {!string} name The name of the variable. This must be unique across + * variables and procedures. + * @param {?string} opt_type The type of the variable like 'int' or 'string'. + * Does not need to be unique. Field_variable can filter variables based on + * their type. This will default to '' which is a specific type. + * @param {?string} opt_id The unique id of the variable. This will default to + * a UUID. + * @see {Blockly.FieldVariable} + * @constructor + */ +Blockly.VariableModel = function(name, opt_type, opt_id) { + /** + * The name of the variable, typically defined by the user. It must be + * unique across all names used for procedures and variables. It may be + * changed by the user. + * @type {string} + */ + this.name = name; + + /** + * The type of the variable, such as 'int' or 'sound_effect'. This may be + * used to build a list of variables of a specific type. By default this is + * the empty string '', which is a specific type. + * @see {Blockly.FieldVariable} + * @type {string} + */ + this.type = opt_type || ''; + + /** + * A unique id for the variable. This should be defined at creation and + * not change, even if the name changes. In most cases this should be a + * UUID. + * @type {string} + * @private + */ + this.id_ = opt_id || Blockly.utils.genUid(); +}; + +/** + * @return {!string} The id for the variable. + */ +Blockly.VariableModel.prototype.getId = function() { + return this.id_; +}; diff --git a/tests/jsunit/index.html b/tests/jsunit/index.html index 294ad28b0..2b27758ce 100644 --- a/tests/jsunit/index.html +++ b/tests/jsunit/index.html @@ -20,5 +20,6 @@ + diff --git a/tests/jsunit/variable_model_test.js b/tests/jsunit/variable_model_test.js new file mode 100644 index 000000000..9a2d2ac3a --- /dev/null +++ b/tests/jsunit/variable_model_test.js @@ -0,0 +1,78 @@ +/** + * @license + * Blockly Tests + * + * Copyright 2017 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Tests for variable model. + * @author marisaleung@google.com (Marisa Leung) + */ +'use strict'; + +var variable; + +function variableTest_tearDown() { + variable = null; +} + +/** + * These tests check the constructor of the variable model. + */ +function testInit_Trivial() { + variable = new Blockly.VariableModel('test', 'test_type', 'test_id'); + assertEquals('test', variable.name); + assertEquals('test_type', variable.type); + assertEquals('test_id', variable.id_); + variableTest_tearDown(); +} + +function testInit_NullType() { + variable = new Blockly.VariableModel('test', null, 'test_id'); + assertEquals('', variable.type); + variableTest_tearDown(); +} + +function testInit_UndefinedType() { + variable = new Blockly.VariableModel('test', undefined, 'test_id'); + assertEquals('', variable.type); + variableTest_tearDown(); +} + +function testInit_NullId() { + variable = new Blockly.VariableModel('test', 'test_type', null); + assertEquals('test', variable.name); + assertEquals('test_type', variable.type); + assertNotNull(variable.id_); + variableTest_tearDown(); +} + +function testInit_UndefinedId() { + variable = new Blockly.VariableModel('test', 'test_type', undefined); + assertEquals('test', variable.name); + assertEquals('test_type', variable.type); + assertNotNull(variable.id_); + variableTest_tearDown(); +} + +function testInit_OnlyNameProvided() { + variable = new Blockly.VariableModel('test'); + assertEquals('test', variable.name); + assertEquals('', variable.type); + assertNotNull(variable.id_); + variableTest_tearDown(); +} \ No newline at end of file