Clean up and create test utilities file.

This commit is contained in:
marisaleung
2017-06-01 10:18:38 -07:00
parent 47cc79eb19
commit cd0487e816
5 changed files with 113 additions and 91 deletions

View File

@@ -7,6 +7,7 @@
<script>goog.require('goog.testing.jsunit');</script>
</head>
<body>
<script src="test_utilities.js"></script>
<script src="utils_test.js"></script>
<script src="connection_test.js"></script>
<script src="connection_db_test.js"></script>

View File

@@ -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 Test utilities.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
goog.require('goog.testing');
/**
* Check that two arrays have the same content.
* @param {!Array.<string>} array1 The first array.
* @param {!Array.<string>} array2 The second array.
*/
function isEqualArrays(array1, array2) {
assertEquals(array1.length, array2.length);
for (var i = 0; i < array1.length; i++) {
assertEquals(array1[i], array2[i]);
}
}
/**
* Creates a controlled MethodMock. Sets the expected return values and
* the parameters if any exist. Sets the method to replay.
* @param {!goog.testing.MockControl} mockControl Object that holds a set
* of mocks for this test.
* @param {!Object} scope The scope of the method to be mocked out.
* @param {!string} funcName The name of the function we're going to mock.
* @param {Array<Object>} parameters The parameters to call the mock with.
* @param {Array<!Object>} return_values The values to return when called.
* @return {!goog.testing.MockInterface} The mocked method.
*/
function setUpMockMethod(mockControl, scope, funcName, parameters,
return_values) {
var mockMethod = mockControl.createMethodMock(scope, funcName);
if (return_values) {
for (var i = 0, return_value; return_value = return_values[i]; i++) {
if (parameters && i < parameters.length) {
mockMethod(parameters[i]).$returns(return_value);
}
else {
mockMethod().$returns(return_value);
}
}
}
// If there are no return values but there are parameters, we are only
// recording specific method calls.
else if (parameters) {
for (var i = 0; i < parameters.length; i++) {
mockMethod(parameters[i]);
}
}
mockMethod.$replay();
return mockMethod;
}

View File

@@ -133,10 +133,9 @@ function test_createVariableNullAndUndefinedType() {
function test_createVariableNullId() {
variableMapTest_setUp();
var mockGenUid = setUpMockMethod(Blockly.utils, 'genUid', null, '1');
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
variable_map.createVariable('name1', 'type1', null);
mockGenUid.$verify();
variableMapTest_checkVariableValues('name1', 'type1', '1');
}
finally {
@@ -146,10 +145,9 @@ function test_createVariableNullId() {
function test_createVariableUndefinedId() {
variableMapTest_setUp();
var mockGenUid = setUpMockMethod(Blockly.utils, 'genUid', null, '1');
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
variable_map.createVariable('name1', 'type1', undefined);
mockGenUid.$verify();
variableMapTest_checkVariableValues('name1', 'type1', '1');
}
finally {
@@ -205,8 +203,8 @@ function test_getVariablesOfType_Trivial() {
variable_map.createVariable('name4', 'type3', 'id4');
var result_array_1 = variable_map.getVariablesOfType('type1');
var result_array_2 = variable_map.getVariablesOfType('type5');
this.isEqualArrays([var_1, var_2], result_array_1);
this.isEqualArrays([], result_array_2);
isEqualArrays([var_1, var_2], result_array_1);
isEqualArrays([], result_array_2);
variableMapTest_tearDown();
}
@@ -217,7 +215,7 @@ function test_getVariablesOfType_Null() {
var var_3 = variable_map.createVariable('name3', '', 'id3');
variable_map.createVariable('name4', 'type1', 'id4');
var result_array = variable_map.getVariablesOfType(null);
this.isEqualArrays([var_1, var_2, var_3], result_array);
isEqualArrays([var_1, var_2, var_3], result_array);
variableMapTest_tearDown();
}
@@ -226,7 +224,7 @@ function test_getVariablesOfType_EmptyString() {
var var_1 = variable_map.createVariable('name1', null, 'id1');
var var_2 = variable_map.createVariable('name2', null, 'id2');
var result_array = variable_map.getVariablesOfType('');
this.isEqualArrays([var_1, var_2], result_array);
isEqualArrays([var_1, var_2], result_array);
variableMapTest_tearDown();
}
@@ -235,14 +233,14 @@ function test_getVariablesOfType_Deleted() {
var variable = variable_map.createVariable('name1', null, 'id1');
variable_map.deleteVariable(variable);
var result_array = variable_map.getVariablesOfType('');
this.isEqualArrays([], result_array);
isEqualArrays([], result_array);
variableMapTest_tearDown();
}
function test_getVariablesOfType_DoesNotExist() {
variableMapTest_setUp();
var result_array = variable_map.getVariablesOfType('type1');
this.isEqualArrays([], result_array);
isEqualArrays([], result_array);
variableMapTest_tearDown();
}
@@ -253,14 +251,14 @@ function test_getVariableTypes_Trivial() {
variable_map.createVariable('name3', 'type2', 'id3');
variable_map.createVariable('name4', 'type3', 'id4');
var result_array = variable_map.getVariableTypes();
this.isEqualArrays(['type1', 'type2', 'type3'], result_array);
isEqualArrays(['type1', 'type2', 'type3'], result_array);
variableMapTest_tearDown();
}
function test_getVariableTypes_None() {
variableMapTest_setUp();
var result_array = variable_map.getVariableTypes();
this.isEqualArrays([], result_array);
isEqualArrays([], result_array);
variableMapTest_tearDown();
}
@@ -270,13 +268,13 @@ function test_getAllVariables_Trivial() {
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
var var_3 = variable_map.createVariable('name3', 'type2', 'id3');
var result_array = variable_map.getAllVariables();
this.isEqualArrays([var_1, var_2, var_3], result_array);
isEqualArrays([var_1, var_2, var_3], result_array);
variableMapTest_tearDown();
}
function test_getAllVariables_None() {
variableMapTest_setUp();
var result_array = variable_map.getAllVariables();
this.isEqualArrays([], result_array);
isEqualArrays([], result_array);
variableMapTest_tearDown();
}

View File

@@ -71,18 +71,6 @@ function createMockBlock(variable_name) {
return block;
}
/**
* Check that two arrays have the same content.
* @param {!Array.<string>} array1 The first array.
* @param {!Array.<string>} array2 The second array.
*/
function isEqualArrays(array1, array2) {
assertEquals(array1.length, array2.length);
for (var i = 0; i < array1.length; i++) {
assertEquals(array1[i], array2[i]);
}
}
/**
* Check if a variable with the given values exists.
* @param {!string} name The expected name of the variable.
@@ -97,27 +85,6 @@ function workspaceTest_checkVariableValues(name, type, id) {
assertEquals(id, variable.getId());
}
/**
* Creates a controlled MethodMock. Set the expected return values. Set the
* method to replay.
* @param {!Object} scope The scope of the method to be mocked out.
* @param {!string} funcName The name of the function we're going to mock.
* @param {Object} parameters The parameters to call the mock with.
* @param {!Object} return_value The value to return when called.
* @return {!goog.testing.MockInterface} The mocked method.
*/
function setUpMockMethod(scope, funcName, parameters, return_value) {
var mockMethod = mockControl_.createMethodMock(scope, funcName);
if (parameters) {
mockMethod(parameters).$returns(return_value);
}
else {
mockMethod().$returns(return_value);
}
mockMethod.$replay();
return mockMethod;
}
function test_emptyWorkspace() {
workspaceTest_setUp();
try {
@@ -242,12 +209,11 @@ function test_updateVariableStore_TrivialNoClear() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
var mockAllUsedVariables = setUpMockMethod(Blockly.Variables,
'allUsedVariables', workspace, ['name1', 'name2']);
setUpMockMethod(mockControl_, Blockly.Variables, 'allUsedVariables',
[workspace], [['name1', 'name2']]);
try {
workspace.updateVariableStore();
mockAllUsedVariables.$verify();
workspaceTest_checkVariableValues('name1', 'type1', 'id1');
workspaceTest_checkVariableValues('name2', 'type2', 'id2');
}
@@ -258,12 +224,12 @@ function test_updateVariableStore_TrivialNoClear() {
function test_updateVariableStore_NameNotInvariableMap_NoClear() {
workspaceTest_setUp();
setUpMockMethod(Blockly.Variables, 'allUsedVariables', workspace, ['name1']);
setUpMockMethod(Blockly.utils, 'genUid', null, '1');
setUpMockMethod(mockControl_, Blockly.Variables, 'allUsedVariables',
[workspace], [['name1']]);
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
workspace.updateVariableStore();
mockControl_.$verifyAll();
workspaceTest_checkVariableValues('name1', '', '1');
}
finally {
@@ -275,12 +241,11 @@ function test_updateVariableStore_ClearAndAllInUse() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
var mockAllUsedVariables = setUpMockMethod(Blockly.Variables,
'allUsedVariables', workspace, ['name1', 'name2']);
setUpMockMethod(mockControl_, Blockly.Variables, 'allUsedVariables',
[workspace], [['name1', 'name2']]);
try {
workspace.updateVariableStore(true);
mockAllUsedVariables.$verify();
workspaceTest_checkVariableValues('name1', 'type1', 'id1');
workspaceTest_checkVariableValues('name2', 'type2', 'id2');
}
@@ -293,12 +258,11 @@ function test_updateVariableStore_ClearAndOneInUse() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
var mockAllUsedVariables = setUpMockMethod(Blockly.Variables,
'allUsedVariables', workspace, ['name1']);
setUpMockMethod(mockControl_, Blockly.Variables, 'allUsedVariables',
[workspace], [['name1']]);
try {
workspace.updateVariableStore(true);
mockAllUsedVariables.$verify();
workspaceTest_checkVariableValues('name1', 'type1', 'id1');
var variabe = workspace.getVariable('name2');
assertNull(variable);
@@ -313,12 +277,12 @@ function test_addTopBlock_TrivialFlyoutIsTrue() {
workspace.isFlyout = true;
var block = createMockBlock();
workspace.removeTopBlock(block);
setUpMockMethod(Blockly.Variables, 'allUsedVariables', block, ['name1']);
setUpMockMethod(Blockly.utils, 'genUid', null, '1');
setUpMockMethod(mockControl_, Blockly.Variables, 'allUsedVariables', [block],
[['name1']]);
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
workspace.addTopBlock(block);
mockControl_.$verifyAll();
workspaceTest_checkVariableValues('name1', '', '1');
}
finally {
@@ -330,14 +294,11 @@ function test_clear_Trivial() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
var mockSetGroup = mockControl_.createMethodMock(Blockly.Events, 'setGroup');
mockSetGroup(true);
mockSetGroup(false);
mockSetGroup.$replay();
setUpMockMethod(mockControl_, Blockly.Events, 'setGroup', [true, false],
null);
try {
workspace.clear();
mockControl_.$verifyAll();
var topBlocks_length = workspace.topBlocks_.length;
var varMapLength = Object.keys(workspace.variableMap_.variableMap_).length;
assertEquals(0, topBlocks_length);
@@ -350,14 +311,11 @@ function test_clear_Trivial() {
function test_clear_NoVariables() {
workspaceTest_setUp();
var mockSetGroup = mockControl_.createMethodMock(Blockly.Events, 'setGroup');
mockSetGroup(true);
mockSetGroup(false);
mockSetGroup.$replay();
setUpMockMethod(mockControl_, Blockly.Events, 'setGroup', [true, false],
null);
try {
workspace.clear();
mockSetGroup.$verify();
var topBlocks_length = workspace.topBlocks_.length;
var varMapLength = Object.keys(workspace.variableMap_.variableMap_).length;
assertEquals(0, topBlocks_length);
@@ -373,17 +331,13 @@ function test_renameVariable_NoBlocks() {
workspaceTest_setUp();
var oldName = 'name1';
var newName = 'name2';
var mockSetGroup = mockControl_.createMethodMock(Blockly.Events, 'setGroup');
var mockGenUid = mockControl_.createMethodMock(Blockly.utils, 'genUid');
// Mocked setGroup to ensure only one call to the mocked genUid.
mockSetGroup(true);
mockSetGroup(false);
mockGenUid().$returns('1');
mockControl_.$replayAll();
// Mocked setGroup to ensure only one call to the mocked genUid.
setUpMockMethod(mockControl_, Blockly.Events, 'setGroup', [true, false],
null);
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
try {
workspace.renameVariable(oldName, newName);
mockControl_.$verifyAll();
workspaceTest_checkVariableValues('name2', '', '1');
var variable = workspace.getVariable(oldName);
assertNull(variable);

View File

@@ -159,10 +159,7 @@ function test_domToText() {
function test_domToWorkspace_BackwardCompatibility() {
// Expect that workspace still loads without serialized variables.
xmlTest_setUpWithMockBlocks();
var mockGenUid = mockControl_.createMethodMock(Blockly.utils, 'genUid');
mockGenUid().$returns('1');
mockGenUid().$returns('1');
mockGenUid().$replay();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '1']);
try {
var dom = Blockly.Xml.textToDom(
'<xml>' +
@@ -313,15 +310,14 @@ function test_blockToDom_fieldToDom_trivial() {
var block = new Blockly.Block(workspace, 'field_variable_test_block');
block.inputList[0].fieldRow[0].setValue('name1');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
xmlTest_checkVariableFieldDomValues(resultFieldDom, 'VAR', 'type1', 'id1', 'name1')
xmlTest_checkVariableFieldDomValues(resultFieldDom, 'VAR', 'type1', 'id1',
'name1');
xmlTest_tearDownWithMockBlocks();
}
function test_blockToDom_fieldToDom_defaultCase() {
xmlTest_setUpWithMockBlocks();
var mockGenUid = mockControl_.createMethodMock(Blockly.utils, 'genUid');
mockGenUid().$returns('1');
mockGenUid().$replay();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1', '1']);
workspace.createVariable('name1');
var block = new Blockly.Block(workspace, 'field_variable_test_block');
block.inputList[0].fieldRow[0].setValue('name1');
@@ -353,9 +349,7 @@ function test_blockToDom_fieldToDom_notAFieldVariable() {
function test_variablesToDom_oneVariable() {
xmlTest_setUp();
var mockGenUid = mockControl_.createMethodMock(Blockly.utils, 'genUid');
mockGenUid().$returns('1');
mockGenUid().$replay();
setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']);
workspace.createVariable('name1');
var resultDom = Blockly.Xml.variablesToDom(workspace.getAllVariables());