Fixing event tests and removing duplicate tests. (#3810)

This commit is contained in:
Monica Kozbial
2020-04-14 13:22:06 -07:00
committed by GitHub
parent b1b2af2a48
commit e0521265b2
2 changed files with 26 additions and 475 deletions

View File

@@ -51,76 +51,6 @@ function eventTest_tearDownWithMockBlocks() {
delete Blockly.Blocks.field_variable_test_block;
}
function test_block_base_constructor() {
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, '1');
try {
var block = createSimpleTestBlock(workspace);
// Here's the event we care about.
var event = new Blockly.Events.BlockBase(block);
assertUndefined(event.varId);
checkExactEventValues(event, {'blockId': '1', 'workspaceId': workspace.id,
'group': '', 'recordUndo': true});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_var_base_constructor() {
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, '1');
try {
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarBase(variable);
assertUndefined(event.blockId);
checkExactEventValues(event, {'varId': 'id1',
'workspaceId': workspace.id, 'group': '', 'recordUndo': true});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_abstract_constructor() {
eventTest_setUpWithMockBlocks();
try {
var event = new Blockly.Events.Abstract();
assertUndefined(event.blockId);
assertUndefined(event.workspaceId);
assertUndefined(event.varId);
checkExactEventValues(event, {'group': '', 'recordUndo': true});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
// Test util
function checkCreateEventValues(event, block, ids, type) {
var expected_xml = Blockly.Xml.domToText(Blockly.Xml.blockToDom(block));
var result_xml = Blockly.Xml.domToText(event.xml);
assertEquals(expected_xml, result_xml);
isEqualArrays(ids, event.ids);
assertEquals(type, event.type);
}
// Test util
function checkDeleteEventValues(event, block, ids, type) {
var expected_xml = Blockly.Xml.domToText(Blockly.Xml.blockToDom(block));
var result_xml = Blockly.Xml.domToText(event.oldXml);
assertEquals(expected_xml, result_xml);
isEqualArrays(ids, event.ids);
assertEquals(type, event.type);
}
// Test util
function checkExactEventValues(event, values) {
var keys = Object.keys(values);
for (var i = 0, field; field = keys[i]; i++) {
assertEquals(values[field], event[field]);
}
}
// Test util
function createSimpleTestBlock(workspace) {
// Disable events while constructing the block: this is a test of the
@@ -131,379 +61,6 @@ function createSimpleTestBlock(workspace) {
return block;
}
function test_create_constructor() {
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1']);
try {
var block = createSimpleTestBlock(workspace);
var event = new Blockly.Events.Create(block);
checkCreateEventValues(event, block, ['1'], 'create');
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockCreate_constructor() {
// expect that blockCreate behaves the same as create.
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1']);
try {
var block = createSimpleTestBlock(workspace);
var event = new Blockly.Events.BlockCreate(block);
checkCreateEventValues(event, block, ['1'], 'create');
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_delete_constructor() {
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1']);
try {
var block = createSimpleTestBlock(workspace);
var event = new Blockly.Events.Delete(block);
checkDeleteEventValues(event, block, ['1'], 'delete');
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockDelete_constructor() {
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1']);
try {
var block = createSimpleTestBlock(workspace);
block.setCommentText('test comment');
var event = new Blockly.Events.BlockDelete(block);
checkDeleteEventValues(event, block, ['1'], 'delete');
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_change_constructor() {
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1']);
try {
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'field_variable_test_block');
Blockly.Events.enable();
var event = new Blockly.Events.Change(block, 'field', 'VAR', 'id1', 'id2');
checkExactEventValues(event, {'element': 'field', 'name': 'VAR',
'oldValue': 'id1', 'newValue': 'id2', 'type': 'change'});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockChange_constructor() {
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1']);
try {
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'field_variable_test_block');
Blockly.Events.enable();
var event = new Blockly.Events.BlockChange(block, 'field', 'VAR', 'id1',
'id2');
checkExactEventValues(event, {'element': 'field', 'name': 'VAR',
'oldValue': 'id1', 'newValue': 'id2', 'type': 'change'});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_move_constructorCoordinate() {
// Expect the oldCoordinate to be set.
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1', '2']);
try {
var block1 = createSimpleTestBlock(workspace);
var coordinate = new Blockly.utils.Coordinate(3, 4);
block1.xy_ = coordinate;
var event = new Blockly.Events.Move(block1);
checkExactEventValues(event, {'oldCoordinate': coordinate,
'type': 'move'});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_move_constructoroldParentId() {
// Expect the oldParentId to be set but not the oldCoordinate to be set.
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1', '2']);
try {
var block1 = createSimpleTestBlock(workspace);
var block2 = createSimpleTestBlock(workspace);
block1.parentBlock_ = block2;
block1.xy_ = new Blockly.utils.Coordinate(3, 4);
var event = new Blockly.Events.Move(block1);
checkExactEventValues(event, {'oldCoordinate': undefined,
'oldParentId': '2', 'type': 'move'});
block1.parentBlock_ = null;
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockMove_constructorCoordinate() {
// Expect the oldCoordinate to be set.
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1', '2']);
try {
var block1 = createSimpleTestBlock(workspace);
var coordinate = new Blockly.utils.Coordinate(3, 4);
block1.xy_ = coordinate;
var event = new Blockly.Events.BlockMove(block1);
checkExactEventValues(event, {'oldCoordinate': coordinate,
'type': 'move'});
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_blockMove_constructoroldParentId() {
// Expect the oldParentId to be set but not the oldCoordinate to be set.
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1', '2']);
try {
var block1 = createSimpleTestBlock(workspace);
var block2 = createSimpleTestBlock(workspace);
block1.parentBlock_ = block2;
block1.xy_ = new Blockly.utils.Coordinate(3, 4);
var event = new Blockly.Events.BlockMove(block1);
checkExactEventValues(event, {'oldCoordinate': undefined,
'oldParentId': '2', 'type': 'move'});
block1.parentBlock_ = null;
} finally {
eventTest_tearDownWithMockBlocks();
}
}
function test_uiEvent_constructor_null() {
try {
Blockly.Events.setGroup('testGroup');
var event = new Blockly.Events.Ui(null, 'foo', 'bar', 'baz');
checkExactEventValues(event,
{
'blockId': null,
'workspaceId': null,
'type': 'ui',
'oldValue': 'bar',
'newValue': 'baz',
'element': 'foo',
'recordUndo': false,
'group': 'testGroup'
}
);
} finally {
Blockly.Events.setGroup(false);
}
}
function test_uiEvent_constructor_block() {
eventTest_setUpWithMockBlocks();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1']);
try {
var block1 = createSimpleTestBlock(workspace);
Blockly.Events.setGroup('testGroup');
var event = new Blockly.Events.Ui(block1, 'foo', 'bar', 'baz');
checkExactEventValues(event,
{
'blockId': '1',
'workspaceId': workspace.id,
'type': 'ui',
'oldValue': 'bar',
'newValue': 'baz',
'element': 'foo',
'recordUndo': false,
'group': 'testGroup'
}
);
} finally {
Blockly.Events.setGroup(false);
eventTest_tearDownWithMockBlocks();
}
}
function test_varCreate_constructor() {
eventTest_setUp();
try {
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
checkExactEventValues(event, {'varName': 'name1', 'varType': 'type1',
'type': 'var_create'});
} finally {
eventTest_tearDown();
}
}
function test_varCreate_toJson() {
eventTest_setUp();
try {
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
var json = event.toJson();
var expectedJson = ({type: "var_create", varId: "id1", varType: "type1",
varName: "name1"});
assertEquals(JSON.stringify(expectedJson), JSON.stringify(json));
} finally {
eventTest_tearDown();
}
}
function test_varCreate_fromJson() {
eventTest_setUp();
try {
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
var event2 = new Blockly.Events.VarCreate(null);
var json = event.toJson();
event2.fromJson(json);
assertEquals(JSON.stringify(json), JSON.stringify(event2.toJson()));
} finally {
eventTest_tearDown();
}
}
function test_varCreate_runForward() {
eventTest_setUp();
var json = {type: "var_create", varId: "id1", varType: "type1",
varName: "name1"};
var event = Blockly.Events.fromJson(json, workspace);
assertNull(workspace.getVariableById('id1'));
event.run(true);
checkVariableValues(workspace, 'name1', 'type1', 'id1');
eventTest_tearDown();
}
function test_varCreate_runBackwards() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
assertNotNull(workspace.getVariableById('id1'));
event.run(false);
assertNull(workspace.getVariableById('id1'));
eventTest_tearDown();
}
function test_varDelete_constructor() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarDelete(variable);
checkExactEventValues(event, {'varName': 'name1', 'varType': 'type1',
'varId':'id1', 'type': 'var_delete'});
eventTest_tearDown();
}
function test_varDelete_toJson() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarDelete(variable);
var json = event.toJson();
var expectedJson = ({type: "var_delete", varId: "id1", varType: "type1",
varName: "name1"});
assertEquals(JSON.stringify(expectedJson), JSON.stringify(json));
eventTest_tearDown();
}
function test_varDelete_fromJson() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarDelete(variable);
var event2 = new Blockly.Events.VarDelete(null);
var json = event.toJson();
event2.fromJson(json);
assertEquals(JSON.stringify(json), JSON.stringify(event2.toJson()));
eventTest_tearDown();
}
function test_varDelete_runForwards() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarDelete(variable);
assertNotNull(workspace.getVariableById('id1'));
event.run(true);
assertNull(workspace.getVariableById('id1'));
eventTest_tearDown();
}
function test_varDelete_runBackwards() {
eventTest_setUp();
var json = {type: "var_delete", varId: "id1", varType: "type1",
varName: "name1"};
var event = Blockly.Events.fromJson(json, workspace);
assertNull(workspace.getVariableById('id1'));
event.run(false);
checkVariableValues(workspace, 'name1', 'type1', 'id1');
eventTest_tearDown();
}
function test_varRename_constructor() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, 'name2');
checkExactEventValues(event, {'varId': 'id1', 'oldName': 'name1',
'newName': 'name2', 'type': 'var_rename'});
eventTest_tearDown();
}
function test_varRename_toJson() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, 'name2');
var json = event.toJson();
var expectedJson = ({type: "var_rename", varId: "id1", oldName: "name1",
newName: "name2"});
assertEquals(JSON.stringify(expectedJson), JSON.stringify(json));
eventTest_tearDown();
}
function test_varRename_fromJson() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, '');
var event2 = new Blockly.Events.VarRename(null);
var json = event.toJson();
event2.fromJson(json);
assertEquals(JSON.stringify(json), JSON.stringify(event2.toJson()));
eventTest_tearDown();
}
function test_varRename_runForward() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, 'name2');
event.run(true);
assertNull(workspace.getVariable('name1'));
checkVariableValues(workspace, 'name2', 'type1', 'id1');
eventTest_tearDown();
}
function test_varBackard_runForward() {
eventTest_setUp();
var variable = workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarRename(variable, 'name2');
event.run(false);
assertNull(workspace.getVariable('name2'));
checkVariableValues(workspace, 'name1', 'type1', 'id1');
eventTest_tearDown();
}
function test_events_filter() {
eventTest_setUpWithMockBlocks();
try {

View File

@@ -58,6 +58,16 @@ suite('Events', function() {
assertEquals(type, event.type);
}
function createSimpleTestBlock(workspace, opt_prototypeName) {
// Disable events while constructing the block: this is a test of the
// Blockly.Event constructors, not the block constructor.
Blockly.Events.disable();
var block = new Blockly.Block(
workspace, opt_prototypeName || 'simple_test_block');
Blockly.Events.enable();
return block;
}
suite('Constructors', function() {
test('Abstract', function() {
var event = new Blockly.Events.Abstract();
@@ -87,12 +97,7 @@ suite('Events', function() {
setup(function() {
this.FAKE_ID = 'hedgehog';
sinon.stub(Blockly.utils, "genUid").returns(this.FAKE_ID);
// Disable events while constructing the block: this is a test of the
// Blockly.Event constructors, not the block constructor.
Blockly.Events.disable();
this.block = new Blockly.Block(this.workspace, 'simple_test_block');
Blockly.Events.enable();
this.block = createSimpleTestBlock(this.workspace);
sinon.restore();
});
@@ -169,9 +174,7 @@ suite('Events', function() {
suite('Move by parent', function() {
setup(function() {
sinon.stub(Blockly.utils, "genUid").returns("parent");
Blockly.Events.disable();
this.parentBlock = new Blockly.Block(this.workspace, 'simple_test_block');
Blockly.Events.enable();
this.parentBlock = createSimpleTestBlock(this.workspace);
sinon.restore();
this.block.parentBlock_ = this.parentBlock;
@@ -201,15 +204,7 @@ suite('Events', function() {
suite('With variable getter blocks', function() {
setup(function() {
// Disable events while constructing the block: this is a test of the
// Blockly.Event constructors, not the block constructor.
Blockly.Events.disable();
this.block = new Blockly.Block(this.workspace, 'field_variable_test_block');
Blockly.Events.enable();
});
teardown(function() {
this.block = createSimpleTestBlock(this.workspace, 'field_variable_test_block');
});
test('Change', function() {
@@ -304,8 +299,7 @@ suite('Events', function() {
suite('toJson', function() {
test('Var create', function() {
var variable = this.workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
var event = new Blockly.Events.VarCreate(this.variable);
var json = event.toJson();
var expectedJson = ({type: "var_create", varId: "id1", varType: "type1",
varName: "name1"});
@@ -332,14 +326,15 @@ suite('Events', function() {
});
});
suite.skip('Run Forward', function() {
suite('Run Forward', function() {
test('Var create', function() {
var json = {type: "var_create", varId: "id1", varType: "type1",
varName: "name1"};
var json = {type: "var_create", varId: "id2", varType: "type2",
varName: "name2"};
var event = Blockly.Events.fromJson(json, this.workspace);
assertNull(this.workspace.getVariableById('id1'));
var x = this.workspace.getVariableById('id2');
assertNull(x);
event.run(true);
checkVariableValues(this.workspace, 'name1', 'type1', 'id1');
checkVariableValues(this.workspace, 'name2', 'type2', 'id2');
});
test('Var delete', function() {
@@ -356,21 +351,20 @@ suite('Events', function() {
checkVariableValues(this.workspace, 'name2', 'type1', 'id1');
});
});
suite.skip('Run Backward', function() {
suite('Run Backward', function() {
test('Var create', function() {
var variable = this.workspace.createVariable('name1', 'type1', 'id1');
var event = new Blockly.Events.VarCreate(variable);
var event = new Blockly.Events.VarCreate(this.variable);
assertNotNull(this.workspace.getVariableById('id1'));
event.run(false);
});
test('Var delete', function() {
var json = {type: "var_delete", varId: "id1", varType: "type1",
varName: "name1"};
var json = {type: "var_delete", varId: "id2", varType: "type2",
varName: "name2"};
var event = Blockly.Events.fromJson(json, this.workspace);
assertNull(this.workspace.getVariableById('id1'));
assertNull(this.workspace.getVariableById('id2'));
event.run(false);
checkVariableValues(this.workspace, 'name1', 'type1', 'id1');
checkVariableValues(this.workspace, 'name2', 'type2', 'id2');
});
test('Var rename', function() {