From 1ea6fd1eb4df32ed16faed872a2ef0b5909542c9 Mon Sep 17 00:00:00 2001 From: deathcap Date: Mon, 30 Dec 2013 13:43:16 -0800 Subject: [PATCH] 0.0.4 - fix clone() not cloning tags Ref GH-1 --- index.coffee | 5 +++-- index.js | 8 ++++--- package.json | 5 +++-- test.coffee | 45 +++++++++++++++++++++++++++++++++++--- test.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 112 insertions(+), 13 deletions(-) diff --git a/index.coffee b/index.coffee index a55cd7a..abb91dd 100644 --- a/index.coffee +++ b/index.coffee @@ -1,6 +1,7 @@ # vim: set shiftwidth=2 tabstop=2 softtabstop=2 expandtab: deepEqual = require 'deep-equal' +clone = require 'clone' module.exports = class ItemPile @@ -11,7 +12,7 @@ class ItemPile @tags = tags ? {} clone: () -> - return new ItemPile(@item, @count, @tags) + return new ItemPile(@item, @count, clone(@tags, false)) # maximum size items should pile to @maxPileSize = 64 @@ -92,7 +93,7 @@ class ItemPile return false if n > @count @count -= n - return new ItemPile(@item, n, @tags) + return new ItemPile(@item, n, clone(@tags, false)) toString: () -> if @hasTags() diff --git a/index.js b/index.js index 0b85892..1d91886 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,11 @@ // Generated by CoffeeScript 1.6.3 (function() { - var ItemPile, deepEqual; + var ItemPile, clone, deepEqual; deepEqual = require('deep-equal'); + clone = require('clone'); + module.exports = ItemPile = (function() { function ItemPile(item, count, tags) { this.item = typeof item === 'string' ? ItemPile.itemFromString(item) : item; @@ -12,7 +14,7 @@ } ItemPile.prototype.clone = function() { - return new ItemPile(this.item, this.count, this.tags); + return new ItemPile(this.item, this.count, clone(this.tags, false)); }; ItemPile.maxPileSize = 64; @@ -120,7 +122,7 @@ return false; } this.count -= n; - return new ItemPile(this.item, n, this.tags); + return new ItemPile(this.item, n, clone(this.tags, false)); }; ItemPile.prototype.toString = function() { diff --git a/package.json b/package.json index 6304806..be1a710 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "itempile", "description": "split and merge 'piles' of items", - "version": "0.0.3", + "version": "0.0.4", "repository": { "type": "git", "url": "git@github.com:deathcap/itempile.git" @@ -10,7 +10,8 @@ "test": "node test.js" }, "dependencies": { - "deep-equal": "~0.1.0" + "deep-equal": "~0.1.0", + "clone": "~0.1.11" }, "devDependencies": { "tape": "2.3.0" diff --git a/test.coffee b/test.coffee index 9c108bc..da19b2f 100644 --- a/test.coffee +++ b/test.coffee @@ -10,11 +10,29 @@ test 'create default', (t) -> t.deepEqual a.tags, {} t.end() -test 'empty tags', (t) -> - a = new ItemPile('dirt', 1, {}) +test 'empty tags default', (t) -> + a = new ItemPile('dirt', 1) t.deepEqual a.tags, {} t.end() +test 'clone', (t) -> + a = new ItemPile('tool', 1, {damage:0}) + t.equal a.item, 'tool' + t.equal a.count, 1 + t.deepEqual a.tags, {damage:0} + + b = a.clone() + t.equal a.item, 'tool' + t.equal a.count, 1 + t.deepEqual a.tags, {damage:0} + + b.tags.damage += 1 + + t.deepEqual b.tags, {damage:1} + t.deepEqual a.tags, {damage:0} + + t.end() + test 'increase', (t) -> a = new ItemPile('dirt', 1) excess = a.increase(10) @@ -71,9 +89,30 @@ test 'split', (t) -> t.equal(a.count, 48) t.equal(b.count, 16) t.equal(a.item, b.item) - t.equal(a.tags, b.tags) + t.deepEqual(a.tags, b.tags) # (not equal() since is cloned, different object) t.end() +test 'split clone', (t) -> + a = new ItemPile('tool', 3, {damage:0}) + t.equal a.item, 'tool' + t.equal a.count, 3 + t.deepEqual a.tags, {damage:0} + + b = a.splitPile(1) + t.equal b.item, 'tool' + t.equal b.count, 1 + t.equal a.count, 2 + t.deepEqual a.tags, {damage:0} + t.deepEqual b.tags, {damage:0} + + b.tags.damage += 1 + + t.deepEqual b.tags, {damage:1} + t.deepEqual a.tags, {damage:0} + + t.end() + + test 'split bad', (t) -> a = new ItemPile('dirt', 10) b = a.splitPile(1000) diff --git a/test.js b/test.js index eabb42b..c5ac6ee 100644 --- a/test.js +++ b/test.js @@ -15,13 +15,39 @@ return t.end(); }); - test('empty tags', function(t) { + test('empty tags default', function(t) { var a; - a = new ItemPile('dirt', 1, {}); + a = new ItemPile('dirt', 1); t.deepEqual(a.tags, {}); return t.end(); }); + test('clone', function(t) { + var a, b; + a = new ItemPile('tool', 1, { + damage: 0 + }); + t.equal(a.item, 'tool'); + t.equal(a.count, 1); + t.deepEqual(a.tags, { + damage: 0 + }); + b = a.clone(); + t.equal(a.item, 'tool'); + t.equal(a.count, 1); + t.deepEqual(a.tags, { + damage: 0 + }); + b.tags.damage += 1; + t.deepEqual(b.tags, { + damage: 1 + }); + t.deepEqual(a.tags, { + damage: 0 + }); + return t.end(); + }); + test('increase', function(t) { var a, excess; a = new ItemPile('dirt', 1); @@ -79,7 +105,37 @@ t.equal(a.count, 48); t.equal(b.count, 16); t.equal(a.item, b.item); - t.equal(a.tags, b.tags); + t.deepEqual(a.tags, b.tags); + return t.end(); + }); + + test('split clone', function(t) { + var a, b; + a = new ItemPile('tool', 3, { + damage: 0 + }); + t.equal(a.item, 'tool'); + t.equal(a.count, 3); + t.deepEqual(a.tags, { + damage: 0 + }); + b = a.splitPile(1); + t.equal(b.item, 'tool'); + t.equal(b.count, 1); + t.equal(a.count, 2); + t.deepEqual(a.tags, { + damage: 0 + }); + t.deepEqual(b.tags, { + damage: 0 + }); + b.tags.damage += 1; + t.deepEqual(b.tags, { + damage: 1 + }); + t.deepEqual(a.tags, { + damage: 0 + }); return t.end(); });