1 | expect = require('chai').expect
|
2 |
|
3 | inject = require('inject')
|
4 | Binder = require('binder')
|
5 | Injector = require('injector')
|
6 |
|
7 | mapInits = 0
|
8 | funMapInits = 0
|
9 |
|
10 | class Map
|
11 | @scope: 'SINGLETON'
|
12 | createMap: -> throw Error('Abstract method createMap')
|
13 | init: -> @map = @createMap()
|
14 | constructor: ->
|
15 | mapInits += 1
|
16 | throw Error('One can never be too safe')
|
17 |
|
18 | class FunMap extends Map
|
19 | @inits = 0
|
20 | createMap: -> return 'Fun!'
|
21 | constructor: ->
|
22 |
|
23 | funMapInits += 1
|
24 |
|
25 | class App
|
26 | map: inject(Map)
|
27 |
|
28 | class AppBinder extends Binder
|
29 | configure: ->
|
30 | @bind(Map).to(FunMap)
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | @bindConstant('unrelated').to(666)
|
36 |
|
37 |
|
38 | describe 'Abstract classes', ->
|
39 | beforeEach ->
|
40 | @binder = new AppBinder()
|
41 | @injector = new Injector(@binder)
|
42 |
|
43 | afterEach ->
|
44 | mapInits = 0
|
45 | funMapInits = 0
|
46 |
|
47 | it 'should have classes in a default state', ->
|
48 | expect(mapInits).to.equal 0
|
49 | expect(funMapInits).to.equal 0
|
50 |
|
51 | it 'should obey FunMap not calling super', ->
|
52 | new FunMap()
|
53 | expect(mapInits).to.equal 0
|
54 | expect(funMapInits).to.equal 1
|
55 |
|
56 | it 'should only create the required impls', ->
|
57 | @injector.getInstance(FunMap)
|
58 | expect(mapInits).to.equal 0
|
59 | expect(funMapInits).to.equal 1
|
60 |
|
61 | @injector.getInstance(Map)
|
62 | expect(mapInits).to.equal 0
|
63 | expect(funMapInits).to.equal 1
|
64 |
|
65 | it 'should only create bound impls via two steps', ->
|
66 | @injector.getInstance(App)
|
67 | expect(mapInits).to.equal 0
|
68 | expect(funMapInits).to.equal 1
|