From 8a2caf5c3272cc1deb2faadcd2d335731b45b145 Mon Sep 17 00:00:00 2001 From: Monica Kozbial Date: Tue, 9 Jun 2020 16:38:16 -0700 Subject: [PATCH] Distinguish between unset maxInstance and set to 0. (#3949) --- core/workspace.js | 7 +++++-- tests/mocha/workspace_test.js | 24 ++++++++---------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/core/workspace.js b/core/workspace.js index f07bcea3c..02287e10c 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -530,8 +530,11 @@ Blockly.Workspace.prototype.remainingCapacityOfType = function(type) { if (!this.options.maxInstances) { return Infinity; } - return (this.options.maxInstances[type] || Infinity) - - this.getBlocksByType(type, false).length; + + var maxInstanceOfType = (this.options.maxInstances[type] !== undefined) ? + this.options.maxInstances[type] : Infinity; + + return maxInstanceOfType - this.getBlocksByType(type, false).length; }; /** diff --git a/tests/mocha/workspace_test.js b/tests/mocha/workspace_test.js index 0ac317799..c02f1e77a 100644 --- a/tests/mocha/workspace_test.js +++ b/tests/mocha/workspace_test.js @@ -454,8 +454,7 @@ function testAWorkspace() { 0, 'With maxInstances limit 2'); }); - test.skip('At instance limit of 0 after clear', function() { - // TODO(3837): Un-skip test after resolving. + test('At instance limit of 0 after clear', function() { this.workspace.clear(); this.workspace.options.maxInstances['get_var_block'] = 0; chai.assert.equal(this.workspace.remainingCapacityOfType('get_var_block'), @@ -471,8 +470,7 @@ function testAWorkspace() { 0, 'With maxInstances limit 2'); }); - test.skip('At instance limit of 0 with multiple block types', function() { - // TODO(3837): Un-skip test after resolving. + test('At instance limit of 0 with multiple block types', function() { this.workspace.newBlock(''); this.workspace.newBlock(''); this.workspace.newBlock(''); @@ -488,8 +486,7 @@ function testAWorkspace() { -1,'With maxInstances limit 1'); }); - test.skip('Over instance limit of 0', function() { - // TODO(3837): Un-skip test after resolving. + test('Over instance limit of 0', function() { this.workspace.options.maxInstances['get_var_block'] = 0; chai.assert.equal(this.workspace.remainingCapacityOfType('get_var_block'), -2,'With maxInstances limit 0'); @@ -504,8 +501,7 @@ function testAWorkspace() { -1,'With maxInstances limit 1'); }); - test.skip('Over instance limit of 0 with multiple block types', function() { - // TODO(3837): Un-skip test after resolving. + test('Over instance limit of 0 with multiple block types', function() { this.workspace.newBlock(''); this.workspace.newBlock(''); this.workspace.newBlock(''); @@ -528,8 +524,7 @@ function testAWorkspace() { this.workspace.remainingCapacityOfType('get_var_block'), 0); }); - test.skip('Over block limit of 0 and no instance limit', function() { - // TODO(3837|3836): Un-skip test after resolving both. + test('Over block limit of 0 and no instance limit', function() { this.workspace.options.maxBlocks = 0; chai.assert.equal( this.workspace.remainingCapacityOfType('get_var_block'), -2); @@ -544,8 +539,7 @@ function testAWorkspace() { 'With maxBlocks limit 1 and maxInstances limit 3'); }); - test.skip('Over block limit of 0 but under instance limit', function() { - // TODO(3837|3836): Un-skip test after resolving both. + test('Over block limit of 0 but under instance limit', function() { this.workspace.options.maxBlocks = 0; this.workspace.options.maxInstances['get_var_block'] = 3; chai.assert.equal( @@ -572,8 +566,7 @@ function testAWorkspace() { 'With maxBlocks limit 1 and maxInstances limit 1'); }); - test.skip('Over block limit of 0 and over instance limit', function() { - // TODO(3837|3836): Un-skip test after resolving both. + test('Over block limit of 0 and over instance limit', function() { this.workspace.options.maxBlocks = 0; this.workspace.options.maxInstances['get_var_block'] = 1; chai.assert.equal( @@ -591,8 +584,7 @@ function testAWorkspace() { 'With maxBlocks limit 1 and maxInstances limit 0'); }); - test.skip('Over block limit of 0 and over instance limit of 0', function() { - // TODO(3837|3836): Un-skip test after resolving both. + test('Over block limit of 0 and over instance limit of 0', function() { this.workspace.options.maxBlocks = 0; this.workspace.options.maxInstances['get_var_block'] = 0; chai.assert.equal(