Skip to content

Commit

Permalink
0.0.4 - fix clone() not cloning tags
Browse files Browse the repository at this point in the history
Ref GH-1
  • Loading branch information
deathcap committed Dec 30, 2013
1 parent 1e315eb commit 1ea6fd1
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 13 deletions.
5 changes: 3 additions & 2 deletions index.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# vim: set shiftwidth=2 tabstop=2 softtabstop=2 expandtab:

deepEqual = require 'deep-equal'
clone = require 'clone'

module.exports =
class ItemPile
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]:deathcap/itempile.git"
Expand All @@ -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"
Expand Down
45 changes: 42 additions & 3 deletions test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
62 changes: 59 additions & 3 deletions test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ea6fd1

Please sign in to comment.