Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow bond to restore after binding twice to the same property #34

Merged
merged 1 commit into from
Jan 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
npm-debug.log

48 changes: 43 additions & 5 deletions bond.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ nextTick = do ->
setTimeout(fn, 0)





_registry = []
_find = (obj) ->
for store in _registry
if store.obj == obj
return store

store =
obj: obj
props: {}
_registry.push(store)
store

registry =
set: (obj, prop, value, newValue) ->
store = _find(obj)
# ignore if it looks like we're
# bonding multiple times
if !store.props[prop]?
store.props[prop] = value

get: (obj, prop) ->
_find(obj).props[prop]

restore: (obj, prop) ->
obj[prop] = _find(obj).props[prop]

restoreAll: ->
for store in _registry
for prop, value of store.props
store.obj[prop] = value

_registry = []



allStubs = []
registered = false
registerCleanupHook = ->
Expand All @@ -80,9 +118,7 @@ registerCleanupHook = ->
throw new Error('bond.cleanup must be specified if your test runner does not use afterEach or testDone')

after ->
for stubRestore in allStubs
stubRestore()
allStubs = []
registry.restoreAll()

registered = true

Expand All @@ -93,9 +129,10 @@ bond = (obj, property) ->
previous = obj[property]

registerRestore = ->
allStubs.push restore
registry.set obj, property, previous

restore = ->
obj[property] = previous
registry.restore(obj, property)

to = (newValue) ->
registerRestore()
Expand All @@ -121,6 +158,7 @@ bond = (obj, property) ->
callback(returnValues...)

through = ->
registerRestore()
obj[property] = createThroughSpy(previous, this)
obj[property]

Expand Down
54 changes: 45 additions & 9 deletions lib/bond.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "Sean Massa <endangeredmassa@gmail.com>",
"scripts": {
"compile": "./node_modules/.bin/coffee --js <bond.coffee >lib/bond.js",
"test": "./node_modules/.bin/mocha --compilers coffee:coffee-script-redux/register --reporter spec --colors test.coffee"
"test": "npm run compile && ./node_modules/.bin/mocha --compilers coffee:coffee-script-redux/register --reporter spec --colors test.coffee"
},
"devDependencies": {
"mocha": "~1.6.0",
Expand Down
16 changes: 16 additions & 0 deletions test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe 'bond', ->
math =
PI: Math.PI

zero: 0

abs: Math.abs

add: (a, b) ->
Expand Down Expand Up @@ -42,6 +44,20 @@ describe 'bond', ->
expect api.restore

describe 'to', ->
describe 'can replace earlier bound values', ->
# these tests must be run in this specific order;
# do not bond math.zero in any other test
# because a failure here will cause test suite pollution
it 'setup', ->
bond(math, 'zero').to 3.14
bond(math, 'zero').to 12

equal math.zero, 12

it 'test', ->
# test that the old replacements have been cleared away
equal math.zero, 0

describe 'non function values', ->
it 'replaces values', ->
bond(math, 'PI').to 3.14
Expand Down