Convert workspace comment tests to mocha. (#3850)

This commit is contained in:
Monica Kozbial
2020-04-22 14:49:42 -07:00
committed by GitHub
parent 3fa6ffa2c6
commit 2f33a37fa4
4 changed files with 210 additions and 144 deletions

View File

@@ -18,8 +18,6 @@
<script src="test_utilities.js"></script>
<!-- Disable workspace comment tests until we include them in our build. -->
<!-- <script src="workspace_comment_test.js"></script> -->
<script src="workspace_undo_redo_test.js"></script>
<script src="mocha_jsunit_test_runner.js"></script>

View File

@@ -1,142 +0,0 @@
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
var workspace;
function workspaceCommentTest_setUp() {
workspace = new Blockly.Workspace();
}
function workspaceCommentTest_tearDown() {
workspace.dispose();
}
function test_noWorkspaceComments() {
workspaceCommentTest_setUp();
try {
assertEquals('Empty workspace: no comments (1).', 0, workspace.getTopComments(true).length);
assertEquals('Empty workspace: no comments (2).', 0, workspace.getTopComments(false).length);
workspace.clear();
assertEquals('Empty workspace: no comments (3).', 0, workspace.getTopComments(true).length);
assertEquals('Empty workspace: no comments (4).', 0, workspace.getTopComments(false).length);
} finally {
workspaceCommentTest_tearDown();
}
}
function test_oneWorkspaceComment() {
workspaceCommentTest_setUp();
try {
var comment = new Blockly.WorkspaceComment(workspace, 'comment text', 0, 0, 'comment id');
assertEquals('One comment on workspace (1).', 1, workspace.getTopComments(true).length);
assertEquals('One comment on workspace (2).', 1, workspace.getTopComments(false).length);
assertEquals('Comment db contains this comment.', comment, workspace.commentDB_['comment id']);
workspace.clear();
assertEquals('Cleared workspace: no comments (3).', 0, workspace.getTopComments(true).length);
assertEquals('Cleared workspace: no comments (4).', 0, workspace.getTopComments(false).length);
assertFalse('Comment DB does not contain this comment.', 'comment id' in workspace.commentDB_);
} finally {
workspaceCommentTest_tearDown();
}
}
function test_getWorkspaceCommentById() {
workspaceCommentTest_setUp();
try {
var comment = new Blockly.WorkspaceComment(workspace, 'comment text', 0, 0, 'comment id');
assertEquals('Getting a comment by id.', comment, workspace.getCommentById('comment id'));
assertEquals('No comment found.', null, workspace.getCommentById('not a comment'));
comment.dispose();
assertEquals('Can\'t find the comment.', null, workspace.getCommentById('comment id'));
} finally {
workspaceCommentTest_tearDown();
}
}
function test_disposeWsCommentTwice() {
workspaceCommentTest_setUp();
try {
var comment = new Blockly.WorkspaceComment(workspace, 'comment text', 0, 0, 'comment id');
comment.dispose();
// Nothing should go wrong the second time dispose is called.
comment.dispose();
} finally {
workspaceCommentTest_tearDown();
}
}
function test_wsCommentHeightWidth() {
workspaceCommentTest_setUp();
try {
var comment =
new Blockly.WorkspaceComment(workspace, 'comment text', 10, 20, 'comment id');
assertEquals('Initial width', 20, comment.getWidth());
assertEquals('Initial height', 10, comment.getHeight());
comment.setWidth(30);
assertEquals('New width should be different', 30, comment.getWidth());
assertEquals('New height should not be different', 10, comment.getHeight());
comment.setHeight(40);
assertEquals('New width should not be different', 30, comment.getWidth());
assertEquals('New height should be different', 40, comment.getHeight());
comment.dispose();
} finally {
workspaceCommentTest_tearDown();
}
}
function test_wsCommentXY() {
workspaceCommentTest_setUp();
try {
var comment =
new Blockly.WorkspaceComment(workspace, 'comment text', 10, 20, 'comment id');
var xy = comment.getXY();
assertEquals('Initial X position', 0, xy.x);
assertEquals('Initial Y position', 0, xy.y);
comment.moveBy(10, 100);
xy = comment.getXY();
assertEquals('New X position', 10, xy.x);
assertEquals('New Y position', 100, xy.y);
comment.dispose();
} finally {
workspaceCommentTest_tearDown();
}
}
function test_wsCommentContent() {
workspaceCommentTest_setUp();
Blockly.Events.fire = temporary_fireEvent;
temporary_fireEvent.firedEvents_ = [];
try {
var comment =
new Blockly.WorkspaceComment(workspace, 'comment text', 10, 20, 'comment id');
assertEquals(
'Check comment text', 'comment text', comment.getContent());
assertEquals(
'Workspace undo stack has one event', 1, workspace.undoStack_.length);
comment.setContent('comment text');
assertEquals(
'Comment text has not changed', 'comment text', comment.getContent());
// Setting the text to the old value does not fire an event.
assertEquals(
'Workspace undo stack has one event', 1, workspace.undoStack_.length);
comment.setContent('new comment text');
assertEquals(
'Comment text has changed', 'new comment text', comment.getContent());
assertEquals(
'Workspace undo stack has two events', 2, workspace.undoStack_.length);
comment.dispose();
} finally {
workspaceCommentTest_tearDown();
Blockly.Events.fire = savedFireFunc;
}
}

View File

@@ -71,6 +71,8 @@
<script src="variables_test.js"></script>
<script src="widget_div_test.js"></script>
<script src="workspace_test.js"></script>
<!-- Disable workspace comment tests until we include them in our build. -->
<!-- <script src="workspace_comment_test.js"></script> -->
<script src="xml_procedures_test.js"></script>
<script src="xml_test.js"></script>

View File

@@ -0,0 +1,208 @@
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.require('Blockly.WorkspaceComment');
suite('Workspace comment', function() {
setup(function() {
this.workspace = new Blockly.Workspace();
});
teardown(function() {
this.workspace.dispose();
});
suite('getTopComments(ordered=true)', function() {
test('No comments', function() {
chai.assert.equal(this.workspace.getTopComments(true).length, 0);
});
test('One comment', function() {
var comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
chai.assert.equal(this.workspace.getTopComments(true).length, 1);
chai.assert.equal(this.workspace.commentDB_['comment id'], comment);
});
test('After clear empty workspace', function() {
this.workspace.clear();
chai.assert.equal(this.workspace.getTopComments(true).length, 0);
});
test('After clear non-empty workspace', function() {
new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
this.workspace.clear();
chai.assert.equal(this.workspace.getTopComments(true).length, 0);
chai.assert.isFalse('comment id' in this.workspace.commentDB_);
});
test('After dispose', function() {
var comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
comment.dispose();
chai.assert.equal(this.workspace.getTopComments(true).length, 0);
chai.assert.isFalse('comment id' in this.workspace.commentDB_);
});
});
suite('getTopComments(ordered=false)', function() {
test('No comments', function() {
chai.assert.equal(this.workspace.getTopComments(false).length, 0);
});
test('One comment', function() {
var comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
chai.assert.equal(this.workspace.getTopComments(false).length, 1);
chai.assert.equal(this.workspace.commentDB_['comment id'], comment);
});
test('After clear empty workspace', function() {
this.workspace.clear();
chai.assert.equal(this.workspace.getTopComments(false).length, 0);
});
test('After clear non-empty workspace', function() {
new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
this.workspace.clear();
chai.assert.equal(this.workspace.getTopComments(false).length, 0);
chai.assert.isFalse('comment id' in this.workspace.commentDB_);
});
test('After dispose', function() {
var comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
comment.dispose();
chai.assert.equal(this.workspace.getTopComments(false).length, 0);
chai.assert.isFalse('comment id' in this.workspace.commentDB_);
});
});
suite('getCommentById', function() {
test('Trivial', function() {
var comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
chai.assert.equal(this.workspace.getCommentById(comment.id), comment);
});
test('Null id', function() {
chai.assert.isNull(this.workspace.getCommentById(null));
});
test('Non-existent id', function() {
chai.assert.isNull(this.workspace.getCommentById('badId'));
});
test('After dispose', function() {
var comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
comment.dispose();
chai.assert.isNull(this.workspace.getCommentById(comment.id));
});
});
suite('dispose', function() {
test('Called twice', function() {
var comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
comment.dispose();
// Nothing should go wrong the second time dispose is called.
comment.dispose();
});
});
suite('Width and height', function() {
setup(function() {
this.comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 10, 20, 'comment id');
});
test('Initial values', function() {
chai.assert.equal(this.comment.getWidth(), 20, 'Width');
chai.assert.equal(this.comment.getHeight(), 10, 'Height');
});
test('setWidth does not affect height', function() {
this.comment.setWidth(30);
chai.assert.equal(this.comment.getWidth(), 30, 'Width');
chai.assert.equal(this.comment.getHeight(), 10, 'Height');
});
test('setHeight does not affect width', function() {
this.comment.setHeight(30);
chai.assert.equal(this.comment.getWidth(), 20, 'Width');
chai.assert.equal(this.comment.getHeight(), 30, 'Height');
});
});
suite('XY position', function() {
setup(function() {
this.comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 10, 20, 'comment id');
});
test('Initial position', function() {
var xy = this.comment.getXY();
chai.assert.equal(xy.x, 0, 'Initial X position');
chai.assert.equal(xy.y, 0,'Initial Y position');
});
test('moveBy', function() {
this.comment.moveBy(10, 100);
var xy = this.comment.getXY();
chai.assert.equal(xy.x, 10, 'New X position');
chai.assert.equal(xy.y, 100, 'New Y position');
});
});
suite('Content', function() {
function temporary_fireEvent(event) {
if (!Blockly.Events.isEnabled()) {
return;
}
Blockly.Events.FIRE_QUEUE_.push(event);
Blockly.Events.fireNow_();
}
setup(function() {
this.savedFireFunc_ = Blockly.Events.fire;
Blockly.Events.fire = temporary_fireEvent;
temporary_fireEvent.firedEvents_ = [];
this.comment = new Blockly.WorkspaceComment(
this.workspace, 'comment text', 0, 0, 'comment id');
});
teardown(function() {
Blockly.Events.fire = this.savedFireFunc_;
});
test('After creation', function() {
chai.assert.equal(
this.comment.getContent(), 'comment text');
chai.assert.equal(
this.workspace.undoStack_.length, 1,'Workspace undo stack');
});
test('Set to same value', function() {
this.comment.setContent('comment text');
chai.assert.equal(this.comment.getContent(), 'comment text');
// Setting the text to the old value does not fire an event.
chai.assert.equal(
this.workspace.undoStack_.length, 1, 'Workspace undo stack');
});
test('Set to different value', function() {
this.comment.setContent('new comment text');
chai.assert.equal(this.comment.getContent(), 'new comment text');
chai.assert.equal(
this.workspace.undoStack_.length, 2, 'Workspace undo stack');
});
});
});