1 |
|
2 | should = require 'should'
|
3 | misc = if process.env.MECANO_COV then require '../lib-cov/misc' else require '../lib/misc'
|
4 |
|
5 | describe 'merge', ->
|
6 |
|
7 | it 'should enrich 1st object', ->
|
8 | obj1 = { a_key: 'a value', b_key: 'b value'}
|
9 | obj2 = { b_key: 'new b value'}
|
10 | result = misc.merge obj1, obj2
|
11 | result.should.eql obj1
|
12 | obj1.b_key.should.eql 'new b value'
|
13 |
|
14 | it 'should create a new object', ->
|
15 | obj1 = { a_key: 'a value', b_key: 'b value'}
|
16 | obj2 = { b_key: 'new b value'}
|
17 | result = misc.merge {}, obj1, obj2
|
18 | result.b_key.should.eql 'new b value'
|
19 |
|
20 | it 'should overwrite arrays', ->
|
21 | obj1 = { a_key: 'a value', b_key: ['b value']}
|
22 | obj2 = { b_key: ['new b value']}
|
23 | misc.merge obj1, obj2
|
24 | obj1.b_key.should.eql ['new b value']
|
25 |
|
26 | it 'should give priority to the last objects', ->
|
27 | obj1 = { b_key: 'b value'}
|
28 | obj2 = { a_key: 'a value', b_key: 'new b value'}
|
29 | misc.merge true, obj1, obj2
|
30 | obj1.a_key.should.eql 'a value'
|
31 | obj1.b_key.should.eql 'b value'
|
32 |
|
33 | it 'should avoid infinite loop', ->
|
34 | obj1 = { a_key: { b_key : 'b value' } }
|
35 | obj2 = obj1
|
36 | misc.merge true, obj1, obj2
|
37 | obj1.a_key.b_key.should.eql 'b value'
|
38 |
|