Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/f6cab192b8615711605f to your computer and use it in GitHub Desktop.
Save tzkmx/f6cab192b8615711605f to your computer and use it in GitHub Desktop.
Jasmine custom class Test
class Set
set = {}
col = []
has: (item) ->
set.hasOwnProperty item
refresh: (item, opt_) ->
col = Object.keys set
valueOf: ->
col
add: (item, opt_) ->
if !@has item
set[item] ?= opt_ ? true
@refresh(item, opt_)
else
@valueOf()
del: (item) ->
if @has item
delete set[item];
@valueOf()
size: ->
col.length
describe "Set", ->
set = null
it "Its initialization should return an object", ->
expect(set = new Set()).toEqual(jasmine.any Object)
describe "Definition", ->
it "class should be defined", ->
expect(Set).toBeDefined()
it "has :Add to put an item in its Set", ->
expect(Set.prototype.add).toBeDefined()
expect(set.add).toEqual(jasmine.any Function)
it "has :Del to kick an item of its Set", ->
expect(Set.prototype.del).toBeDefined()
expect(set.del).toEqual(jasmine.any Function)
it "has :Has to check existence of item in its Set", ->
expect(Set.prototype.has).toBeDefined()
expect(set.has).toEqual(jasmine.any Function)
it "has :Size to know cardinality of the Set", ->
expect(Set.prototype.size).toBeDefined()
expect(set.size).toEqual(jasmine.any Function)
describe "Add method", ->
set = null
beforeEach ->
set = new Set()
afterEach ->
set = null
it "stores only one item after a single item added", ->
expect(set.add 1).toEqual ['1']
expect(set.valueOf().length).toEqual 1
it "stores only item after adding same item up to 99 times", ->
for [1..Math.random()*100]
set.add(1)
expect(set.add(1)).toEqual ['1']
expect(set.size()).toEqual 1
it "returns an array with same cardinality as its Set", ->
lengthTest = 1 + Math.round Math.random()*10
set.add(item) for item in [0..lengthTest-1]
expect(set.size()).toEqual lengthTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment