From 2106ec0e93432cec7598e25aebf779b2e2973c9c Mon Sep 17 00:00:00 2001 From: Colin McLeod Date: Sat, 13 Jun 2015 00:43:10 -0700 Subject: [PATCH] Ship building edge case, plus unit test to cover change --- app/js/shipyard/factory-ship.js | 5 +++-- test/tests/test-factory-ship.js | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/app/js/shipyard/factory-ship.js b/app/js/shipyard/factory-ship.js index 18be47b7..48eb881c 100755 --- a/app/js/shipyard/factory-ship.js +++ b/app/js/shipyard/factory-ship.js @@ -57,7 +57,7 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength', cl = common.length, i, l; - // Reset Cumulative and aggragate stats + // Reset Cumulative stats this.fuelCapacity = 0; this.cargoCapacity = 0; this.ladenMass = 0; @@ -141,7 +141,7 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength', */ Ship.prototype.use = function(slot, id, component, preventUpdate) { if (slot.id != id) { // Selecting a different component - var slotIndex = this.internal.indexOf(slot); + var slotIndex = preventUpdate ? -1 : this.internal.indexOf(slot); // Slot is an internal slot, is not being emptied, and the selected component group/type must be of unique if (slotIndex != -1 && component && _.includes(['sg', 'rf', 'fs'], component.grp)) { // Find another internal slot that already has this type/group installed @@ -245,6 +245,7 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength', var powerChange = slot == this.common[0]; if (old) { // Old component now being removed + console.log('this shouldn\'t happen', old); switch (old.grp) { case 'ft': this.fuelCapacity -= old.capacity; diff --git a/test/tests/test-factory-ship.js b/test/tests/test-factory-ship.js index 084829dc..f4242270 100644 --- a/test/tests/test-factory-ship.js +++ b/test/tests/test-factory-ship.js @@ -31,4 +31,40 @@ describe("Ship Factory", function() { } }); + it("resets and rebuilds properly", function() { + var id = 'cobra_mk_iii'; + var cobra = DB.ships[id]; + var shipA = new Ship(id, cobra.properties, cobra.slots); + var shipB = new Ship(id, cobra.properties, cobra.slots); + var testShip = new Ship(id, cobra.properties, cobra.slots); + + var buildA = cobra.defaults; + var buildB = { + common:['4A', '4A', '4A', '3D', '3A', '3A', '4C'], + hardpoints: ['0s', '0s', '2d', '2d', 0, '04'], + internal: ['45', '03', '2b', '2o', '27', '53'] + }; + + shipA.buildWith(buildA); // Build A + shipB.buildWith(buildB);// Build B + testShip.buildWith(buildA); + + for(var p in testShip) { + expect(testShip[p]).toEqual(shipA[p], p + ' does not match'); + } + + testShip.buildWith(buildB); + + for(var p in testShip) { + expect(testShip[p]).toEqual(shipB[p], p + ' does not match'); + } + + testShip.buildWith(buildA); + + for(var p in testShip) { + expect(testShip[p]).toEqual(shipA[p], p + ' does not match'); + } + + }); + }); \ No newline at end of file