Commits

Kenneth Jørgensen  committed ca968ea

Implemented CollectionSpec and fixed a few bugs

  • Participants
  • Parent commits 7e88188

Comments (0)

Files changed (6)

File spec/CollectionSpec.coffee

 _ = require "underscore"
 sinon = require "sinon"
-{Collection} = require "../antifreeze"
+{Collection, Model} = require "../antifreeze"
 
 describe "Collection", ->
-	it "should be empty when first created"
-	it "should add new values given to it"
-	it "should remove values"
-	it "should accept initial array values"
-	it "should accept initial Collection values and make a copy"
+	collection = null
+	val1 = val:1
+	val2 = val:2
+	val3 = val:3
+	beforeEach ->
+		collection = new Collection
+
+	it "should be empty when first created", ->
+		expect(collection.size()).toBe 0
+
+	it "should add new values given to it", ->
+		expect(collection.add val1).toBe true
+		expect(collection.size()).toBe 1
+		# Add new value.
+		expect(collection.add val2).toBe true
+		expect(collection.size()).toBe 2
+		# Add first value again.
+		expect(collection.add val1).toBe true
+		expect(collection.size()).toBe 3
+		# Check correct contains.
+		expect(collection.contains val1).toBe true
+		expect(collection.contains val2).toBe true
+		expect(collection.contains val3).toBe false
+
+	it "should remove values", ->
+		# Remove non-existant value.
+		expect(collection.remove val1).toBe false
+		# Remove existing value.
+		collection.add val2
+		expect(collection.remove val2).toBe true
+
+	it "should accept initial array values", ->
+		collection = new Collection [val1, val2]
+		expect(collection.size()).toBe 2
+		expect(collection.contains val1).toBe true
+		expect(collection.contains val2).toBe true
+		expect(collection.contains val3).toBe false
+
+	it "should accept initial Collection values and make a copy", ->
+		collection1 = new Collection [val1, val2]
+		collection2 = new Collection collection1
+		collection1.add val3
+		expect(collection2.size()).toBe 2
+		expect(collection2.contains val3).toBe false
+
+	it "should allow iteration", ->
+		vals = [val1, val2, val3]
+		collection = new Collection vals
+		i = 0
+		collection.each (val, key) ->
+			expect(key).toBe i
+			expect(val).toBe vals[i]
+			i++
+		expect(i).toBe 3
 
 	describe "change events", ->
-		it "should trigger cahnge events when adding an element"
-		it "should trigger change events when removing an element"
-		it "should trigger change events on sub-models"
-		it "should not trigger change events on detached sub-models"
+		change = null
+		changeEvent = null
+		beforeEach ->
+			change = sinon.spy (event) -> changeEvent = event
+			changeEvent = null
+			collection.on "change", change
+
+		it "should trigger when adding an element", ->
+			collection.add val1
+			waitsFor (-> change.called), "Change not triggered", 100
+			runs ->
+				expect(change.callCount).toBe 1
+				expect(changeEvent.data.type).toBe "add"
+				expect(changeEvent.data.value).toBe val1
+
+		it "should trigger when removing an element", ->
+			collection.add val1
+			collection.remove val1
+			waitsFor (-> change.callCount is 2), "Change not triggered", 100
+			runs ->
+				expect(change.callCount).toBe 2
+				expect(changeEvent.data.type).toBe "remove"
+				expect(changeEvent.data.value).toBe undefined
+				expect(changeEvent.data.oldValue).toBe val1
 
 	describe "serialization", ->
-		it "should serialize to a json array"
-		it "should not recursively serialize models"
+		beforeEach ->
+			collection = new Collection [
+				"foo",
+				new Model
+					bar: "bar"
+			]
+
+		it "should serialize to a json array and not recursively serialize contained models", ->
+			json = collection.toJSON()
+			expect(_.isArray json).toBe true
+			expect(json.length).toBe 2
+			expect(json[0]).toBe "foo"
+			expect(json[1] instanceof Model).toBe true

File spec/MapSpec.coffee

 	it "should overwrite existing values"
 	it "should remove values"
 	it "should accept initial Map values and make a copy"
+	it "should allow iteration"
 
 	describe "change events", ->
 		it "should trigger cahnge events when adding an element"
 		it "should trigger change events when removing an element"
-		it "should trigger change events on sub-models"
-		it "should not trigger change events on detached sub-models"
 
 	describe "serialization", ->
 		it "should serialize to a json object"

File spec/ModelSpec.coffee

 		changeFooEvent = null
 		beforeEach ->
 			model = new Model
+			# General change event.
 			change = sinon.spy (event) -> changeEvent = event
 			model.on "change", change
 			changeEvent = null
+			# Specific change event.
 			changeFoo = sinon.spy (event) -> changeFooEvent = event
 			model.on "change:foo", changeFoo
 			changeFooEvent = null
 			runs ->
 				expect(change.callCount).toBe 1
 
-		it "should report changes on sub-models"
-
-		it "should not report changes on detached sub-models"
-
 	describe "serialization", ->
 		it "should serialize to a json object", ->
 			model = new Model
 			expect(json.foo).toBe "bar"
 			expect(json.abc).toBe "def"
 
-		it "should not recursively serialize models"
-
-		xit "should should serialize models recursively", ->
+		it "should not recursively serialize models", ->
 			model = new Model
 				foo: "foo"
 				bar: new Model
 					qwerty: "zxcvbn"
-			json = JSON.stringify model.toJSON()
-			expect(json).toBe '{"foo":"foo","bar":{"qwerty":"zxcvbn"}}'
-
-		xit "should should detect circular references in serialization"
+			json = model.toJSON()
+			expect(typeof json).toBe "object"
+			expect(json.foo).toBe "foo"
+			expect(json.bar instanceof Model).toBe true

File spec/SetSpec.coffee

+_ = require "underscore"
+sinon = require "sinon"
+{Set} = require "../antifreeze"
+
+describe "Set", ->
+	it "should be empty when first created"
+	it "should add new values given to it"
+	it "should not accept the same value twice"
+	it "should remove values"
+	it "should accept initial array values"
+	it "should accept initial Set values and make a copy"
+	it "should accept initial Collection values, make a copy, and remove duplicates"
+	it "should allow iteration"
+
+	describe "change events", ->
+		it "should trigger change events when adding an element"
+		it "should not trigger change events when adding an element"
+		it "should trigger change events when removing an element"
+
+	describe "serialization", ->
+		it "should serialize to a json array"
+		it "should not recursively serialize models"

File src/Collection.coffee

 Antifreeze.Collection = class Collection
 	Calamity.emitter @prototype
 
-	constructor: ->
-		@_items = []
+	constructor: (values) ->
+		# Process initial values.
+		if values?
+			# Convert supplied collection to array.
+			if values instanceof Collection
+				values = values.toJSON()
+			# Complain if not array supplied array.
+			unless _.isArray values
+				throw new Error "Initial values for Collection must be either Array or Collection"
+		# Default.
+		values or= []
+		@_items = values
 
 	# Adds an element to the collection.
 	# Returns true if the element was added.
 		index = @_getIndex(obj)
 		return false if index is false
 		# Remove element.
-		oldVal = @_items.splice(index, 1)[1]
+		oldVal = @_items.splice(index, 1)[0]
 		# Fire change event.
 		@trigger "change",
 			type: "remove"
 
 	# Iterator.
 	each: (fn) ->
-		for entry in @_items
-			fn.apply @, [entry]
+		for entry, index in @_items
+			fn.apply @, [entry, index]
 		return @
 
 	toJSON: ->
 		json = []
-		for o in @_items
-			json.push o
+		@each (val) ->
+			json.push val
 		return json
 
 	# Returns the internal array index for the object, or false if it not found.

File src/SetSpec.coffee

-_ = require "underscore"
-sinon = require "sinon"
-{Set} = require "../antifreeze"
-
-describe "Set", ->
-	it "should be empty when first created"
-	it "should add new values given to it"
-	it "should not accept the same value twice"
-	it "should remove values"
-	it "should accept initial array values"
-	it "should accept initial Collection values and make a copy"
-
-	describe "change events", ->
-		it "should trigger change events when adding an element"
-		it "should not trigger change events when adding an element"
-		it "should trigger change events when removing an element"
-		it "should trigger change events on sub-models"
-		it "should not trigger change events on detached sub-models"
-
-	describe "serialization", ->
-		it "should serialize to a json array"
-		it "should not recursively serialize models"