[79a0317] | 1 | require './_prepare'
|
---|
| 2 |
|
---|
| 3 | object = mod 'object'
|
---|
| 4 |
|
---|
| 5 | test 'isBareObject', ->
|
---|
| 6 |
|
---|
| 7 | object.isBareObject('a').should.equal false
|
---|
| 8 |
|
---|
| 9 | object.isBareObject({'a': 'a'}).should.equal true
|
---|
| 10 |
|
---|
| 11 | test 'typeOf', ->
|
---|
| 12 |
|
---|
| 13 | object.typeOf('s').should.equal 'string'
|
---|
| 14 | object.typeOf(0).should.equal 'number'
|
---|
| 15 | object.typeOf(false).should.equal 'boolean'
|
---|
| 16 | object.typeOf({}).should.equal 'object'
|
---|
| 17 | object.typeOf(arguments).should.equal 'arguments'
|
---|
| 18 | object.typeOf([]).should.equal 'array'
|
---|
| 19 |
|
---|
| 20 | test 'empty', ->
|
---|
| 21 |
|
---|
| 22 | o =
|
---|
| 23 |
|
---|
| 24 | a: 1
|
---|
| 25 | b: 2
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | object.empty o
|
---|
| 29 |
|
---|
| 30 | o.should.not.have.property 'a'
|
---|
| 31 | o.should.not.have.property 'b'
|
---|
| 32 |
|
---|
| 33 | test 'fastEmpty', ->
|
---|
| 34 |
|
---|
| 35 | o =
|
---|
| 36 | a: 1
|
---|
| 37 | b: 2
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | object.fastEmpty o
|
---|
| 41 |
|
---|
| 42 | o.should.not.have.property 'a'
|
---|
| 43 | o.should.not.have.property 'b'
|
---|
| 44 |
|
---|
| 45 | test 'clone', ->
|
---|
| 46 |
|
---|
| 47 | object.clone([1])[0].should.equal 1
|
---|
| 48 | object.clone({a:1}).a.should.equal 1
|
---|
| 49 |
|
---|
| 50 | o = {a: 1}
|
---|
| 51 |
|
---|
| 52 | object.clone(o).should.not.equal o
|
---|
| 53 |
|
---|
| 54 | test 'clone [include prototype]', ->
|
---|
| 55 |
|
---|
| 56 | class C
|
---|
| 57 |
|
---|
| 58 | constructor: (@a) ->
|
---|
| 59 |
|
---|
| 60 | sayA: -> @a + 'a'
|
---|
| 61 |
|
---|
| 62 | a = new C 'a'
|
---|
| 63 |
|
---|
| 64 | a.sayA().should.equal 'aa'
|
---|
| 65 |
|
---|
| 66 | b = object.clone a, yes
|
---|
| 67 |
|
---|
| 68 | b.should.not.equal a
|
---|
| 69 |
|
---|
| 70 | b.constructor.should.equal C
|
---|
| 71 |
|
---|
| 72 | b.a.should.equal 'a'
|
---|
| 73 |
|
---|
| 74 | b.a = 'a2'
|
---|
| 75 |
|
---|
| 76 | b.sayA().should.equal 'a2a'
|
---|
| 77 |
|
---|
| 78 | test 'clone [without prototype]', ->
|
---|
| 79 |
|
---|
| 80 | class C
|
---|
| 81 |
|
---|
| 82 | constructor: (@a) ->
|
---|
| 83 |
|
---|
| 84 | sayA: -> @a + 'a'
|
---|
| 85 |
|
---|
| 86 | a = new C 'a'
|
---|
| 87 |
|
---|
| 88 | a.sayA().should.equal 'aa'
|
---|
| 89 |
|
---|
| 90 | b = object.clone a, no
|
---|
| 91 |
|
---|
| 92 | b.should.equal a
|
---|
| 93 |
|
---|
| 94 | test 'overrideOnto [basic]', ->
|
---|
| 95 |
|
---|
| 96 | onto =
|
---|
| 97 | a: 'a'
|
---|
| 98 | b:
|
---|
| 99 | c: 'c'
|
---|
| 100 | d:
|
---|
| 101 | e: 'e'
|
---|
| 102 |
|
---|
| 103 | what =
|
---|
| 104 | a: 'a2'
|
---|
| 105 | b:
|
---|
| 106 | c: 'c2'
|
---|
| 107 | d:
|
---|
| 108 | f: 'f2'
|
---|
| 109 |
|
---|
| 110 | object.overrideOnto onto, what
|
---|
| 111 |
|
---|
| 112 | onto.a.should.equal 'a2'
|
---|
| 113 | onto.b.should.have.property 'c'
|
---|
| 114 | onto.b.c.should.equal 'c2'
|
---|
| 115 | onto.b.d.should.not.have.property 'f'
|
---|
| 116 | onto.b.d.e.should.equal 'e'
|
---|
| 117 |
|
---|
| 118 | test 'override', ->
|
---|
| 119 |
|
---|
| 120 | onto =
|
---|
| 121 |
|
---|
| 122 | a: 'a'
|
---|
| 123 |
|
---|
| 124 | b:
|
---|
| 125 |
|
---|
| 126 | c: 'c'
|
---|
| 127 |
|
---|
| 128 | d:
|
---|
| 129 |
|
---|
| 130 | e: 'e'
|
---|
| 131 |
|
---|
| 132 | what =
|
---|
| 133 |
|
---|
| 134 | a: 'a2'
|
---|
| 135 |
|
---|
| 136 | b:
|
---|
| 137 |
|
---|
| 138 | c: 'c2'
|
---|
| 139 |
|
---|
| 140 | d:
|
---|
| 141 |
|
---|
| 142 | f: 'f2'
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 | onto2 = object.override onto, what
|
---|
| 146 |
|
---|
| 147 | onto2.a.should.equal 'a2'
|
---|
| 148 | onto2.b.should.have.property 'c'
|
---|
| 149 | onto2.b.c.should.equal 'c2'
|
---|
| 150 | onto2.b.d.should.not.have.property 'f'
|
---|
| 151 | onto2.b.d.e.should.equal 'e'
|
---|
| 152 |
|
---|
| 153 | onto.should.not.equal onto2
|
---|
| 154 |
|
---|
| 155 | do ->
|
---|
| 156 |
|
---|
| 157 | what =
|
---|
| 158 |
|
---|
| 159 | a: 'a2'
|
---|
| 160 |
|
---|
| 161 | c: ->
|
---|
| 162 |
|
---|
| 163 | z: 'z'
|
---|
| 164 |
|
---|
| 165 | y:
|
---|
| 166 |
|
---|
| 167 | a: 'a'
|
---|
| 168 |
|
---|
| 169 | onto =
|
---|
| 170 |
|
---|
| 171 | a: 'a'
|
---|
| 172 |
|
---|
| 173 | b: 'b'
|
---|
| 174 |
|
---|
| 175 | test 'appendOnto [basic]', ->
|
---|
| 176 |
|
---|
| 177 | object.appendOnto onto, what
|
---|
| 178 |
|
---|
| 179 | onto.a.should.equal 'a2'
|
---|
| 180 | onto.b.should.equal 'b'
|
---|
| 181 | onto.z.should.equal 'z'
|
---|
| 182 |
|
---|
| 183 | test "appendOnto [shallow copies instances]", ->
|
---|
| 184 |
|
---|
| 185 | onto.c.should.be.instanceof Function
|
---|
| 186 | onto.c.should.equal what.c
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | test "appendOnto [clones objects]", ->
|
---|
| 190 |
|
---|
| 191 | onto.should.have.property 'y'
|
---|
| 192 | onto.y.a.should.equal 'a'
|
---|
| 193 | onto.y.should.not.equal what.y
|
---|
| 194 |
|
---|
| 195 | test 'groupProps', ->
|
---|
| 196 |
|
---|
| 197 | obj =
|
---|
| 198 |
|
---|
| 199 | a1: '1'
|
---|
| 200 | a2: '2'
|
---|
| 201 |
|
---|
| 202 | b1: '1'
|
---|
| 203 | b2: '2'
|
---|
| 204 |
|
---|
| 205 | c1: '1'
|
---|
| 206 | c2: '2'
|
---|
| 207 |
|
---|
| 208 | rest1: '1'
|
---|
| 209 | rest2: '2'
|
---|
| 210 |
|
---|
| 211 | groups = object.groupProps obj,
|
---|
| 212 |
|
---|
| 213 | a: ['a1', 'a2']
|
---|
| 214 |
|
---|
| 215 | b: [/^b[0-9]+$/]
|
---|
| 216 |
|
---|
| 217 | c: (key) -> key[0] is 'c'
|
---|
| 218 |
|
---|
| 219 | groups.a.should.have.property 'a1'
|
---|
| 220 | groups.a.a1.should.equal '1'
|
---|
| 221 |
|
---|
| 222 | groups.a.should.have.property 'a2'
|
---|
| 223 |
|
---|
| 224 | groups.b.should.have.property 'b1'
|
---|
| 225 | groups.b.should.have.property 'b2'
|
---|
| 226 |
|
---|
| 227 | groups.c.should.have.property 'c1'
|
---|
| 228 | groups.c.should.have.property 'c2'
|
---|
| 229 |
|
---|
| 230 | groups.rest.should.have.property 'rest1'
|
---|
| 231 | groups.rest.should.have.property 'rest1'
|
---|
| 232 |
|
---|
| 233 | groups.rest.should.not.have.property 'c1' |
---|