Commits

Kenneth Jørgensen  committed 8d9ef2f

Some MapSpec and more bugs

  • Participants
  • Parent commits ca968ea

Comments (0)

Files changed (2)

File spec/MapSpec.coffee

 {Map} = require "../antifreeze"
 
 describe "Map", ->
-	it "should be empty when first created"
-	it "should add new values"
-	it "should overwrite existing values"
-	it "should remove values"
+	map = null
+	change = null
+	key1 = key:1
+	val1 = val:1
+	val2 = val:2
+
+	beforeEach ->
+		map = new Map
+		change = sinon.spy()
+
+	it "should be empty when first created", ->
+		expect(map.size()).toBe 0
+
+	it "should add new values", ->
+		expect(map.put key1, val1).toBe null
+		expect(map.size()).toBe 1
+
+	it "should overwrite existing values and return the old value when doing so", ->
+		map.put key1, val1
+		# Existing value should not change anything.
+		expect(map.put key1, val1).toBe null
+		# New value should return old one.
+		expect(map.put key1, val2).toBe val1
+		# Check size.
+		expect(map.size()).toBe 1
+
+	it "should remove values", ->
+		map.put key1, val1
+		expect(map.remove key1).toBe val1
+
 	it "should accept initial Map values and make a copy"
 	it "should allow iteration"
 

File src/Map.coffee

 			# Replace existing.
 		else
 			oldVal = @_items[index][1]
+			# Check identity of old value.
+			if val is oldVal
+				return null
 			@_items[index] = entry
 			returnVal = oldVal
 		# Fire change event.
 		index = @_getIndexForKey key
 		return null if index is false
 		# Remove and return previous value.
-		returnVal = @_items.splice(index, 1)[1]
+		returnVal = @_items.splice(index, 1)[0][1]
 		# Fire change event.
 		@trigger "change",
 			type: "remove"