|
270 | 270 | strictEqual(_.result(null, 'x'), undefined); |
271 | 271 | }); |
272 | 272 |
|
| 273 | + test('result returns a default value if object is null or undefined', function() { |
| 274 | + strictEqual(_.result(null, 'b', 'default'), 'default'); |
| 275 | + strictEqual(_.result(undefined, 'c', 'default'), 'default'); |
| 276 | + strictEqual(_.result(''.match('missing'), 1, 'default'), 'default'); |
| 277 | + }); |
| 278 | + |
| 279 | + test('result returns a default value if property of object is missing', function() { |
| 280 | + strictEqual(_.result({d: null}, 'd', 'default'), null); |
| 281 | + strictEqual(_.result({e: false}, 'e', 'default'), false); |
| 282 | + }); |
| 283 | + |
| 284 | + test('result only returns the default value if the object does not have the property or is undefined', function() { |
| 285 | + strictEqual(_.result({}, 'b', 'default'), 'default'); |
| 286 | + strictEqual(_.result({d: undefined}, 'd', 'default'), 'default'); |
| 287 | + }); |
| 288 | + |
| 289 | + test('result does not return the default if the property of an object is found in the prototype', function() { |
| 290 | + var Foo = function(){}; |
| 291 | + Foo.prototype.bar = 1; |
| 292 | + strictEqual(_.result(new Foo, 'bar', 2), 1); |
| 293 | + }); |
| 294 | + |
| 295 | + test('result does use the fallback when the result of invoking the property is undefined', function() { |
| 296 | + var obj = {a: function() {}}; |
| 297 | + strictEqual(_.result(obj, 'a', 'failed'), undefined); |
| 298 | + }); |
| 299 | + |
273 | 300 | test('_.templateSettings.variable', function() { |
274 | 301 | var s = '<%=data.x%>'; |
275 | 302 | var data = {x: 'x'}; |
|
0 commit comments