1(function() { 2 var _ = typeof require == 'function' ? require('..') : window._; 3 4 QUnit.module('Functions'); 5 QUnit.config.asyncRetries = 3; 6 7 QUnit.test('bind', function(assert) { 8 var context = {name: 'moe'}; 9 var func = function(arg) { return 'name: ' + (this.name || arg); }; 10 var bound = _.bind(func, context); 11 assert.equal(bound(), 'name: moe', 'can bind a function to a context'); 12 13 bound = _(func).bind(context); 14 assert.equal(bound(), 'name: moe', 'can do OO-style binding'); 15 16 bound = _.bind(func, null, 'curly'); 17 var result = bound(); 18 // Work around a PhantomJS bug when applying a function with null|undefined. 19 assert.ok(result === 'name: curly' || result === 'name: ' + window.name, 'can bind without specifying a context'); 20 21 func = function(salutation, name) { return salutation + ': ' + name; }; 22 func = _.bind(func, this, 'hello'); 23 assert.equal(func('moe'), 'hello: moe', 'the function was partially applied in advance'); 24 25 func = _.bind(func, this, 'curly'); 26 assert.equal(func(), 'hello: curly', 'the function was completely applied in advance'); 27 28 func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname; }; 29 func = _.bind(func, this, 'hello', 'moe', 'curly'); 30 assert.equal(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments'); 31 32 func = function(ctx, message) { assert.equal(this, ctx, message); }; 33 _.bind(func, 0, 0, 'can bind a function to `0`')(); 34 _.bind(func, '', '', 'can bind a function to an empty string')(); 35 _.bind(func, false, false, 'can bind a function to `false`')(); 36 37 // These tests are only meaningful when using a browser without a native bind function 38 // To test this with a modern browser, set underscore's nativeBind to undefined 39 var F = function() { return this; }; 40 var boundf = _.bind(F, {hello: 'moe curly'}); 41 var Boundf = boundf; // make eslint happy. 42 var newBoundf = new Boundf(); 43 assert.equal(newBoundf.hello, void 0, 'function should not be bound to the context, to comply with ECMAScript 5'); 44 assert.equal(boundf().hello, 'moe curly', "When called without the new operator, it's OK to be bound to the context"); 45 assert.ok(newBoundf instanceof F, 'a bound instance is an instance of the original function'); 46 47 assert.raises(function() { _.bind('notafunction'); }, TypeError, 'throws an error when binding to a non-function'); 48 }); 49 50 QUnit.test('partial', function(assert) { 51 var obj = {name: 'moe'}; 52 var func = function() { return this.name + ' ' + _.toArray(arguments).join(' '); }; 53 54 obj.func = _.partial(func, 'a', 'b'); 55 assert.equal(obj.func('c', 'd'), 'moe a b c d', 'can partially apply'); 56 57 obj.func = _.partial(func, _, 'b', _, 'd'); 58 assert.equal(obj.func('a', 'c'), 'moe a b c d', 'can partially apply with placeholders'); 59 60 func = _.partial(function() { return arguments.length; }, _, 'b', _, 'd'); 61 assert.equal(func('a', 'c', 'e'), 5, 'accepts more arguments than the number of placeholders'); 62 assert.equal(func('a'), 4, 'accepts fewer arguments than the number of placeholders'); 63 64 func = _.partial(function() { return typeof arguments[2]; }, _, 'b', _, 'd'); 65 assert.equal(func('a'), 'undefined', 'unfilled placeholders are undefined'); 66 67 // passes context 68 function MyWidget(name, options) { 69 this.name = name; 70 this.options = options; 71 } 72 MyWidget.prototype.get = function() { 73 return this.name; 74 }; 75 var MyWidgetWithCoolOpts = _.partial(MyWidget, _, {a: 1}); 76 var widget = new MyWidgetWithCoolOpts('foo'); 77 assert.ok(widget instanceof MyWidget, 'Can partially bind a constructor'); 78 assert.equal(widget.get(), 'foo', 'keeps prototype'); 79 assert.deepEqual(widget.options, {a: 1}); 80 81 _.partial.placeholder = obj; 82 func = _.partial(function() { return arguments.length; }, obj, 'b', obj, 'd'); 83 assert.equal(func('a'), 4, 'allows the placeholder to be swapped out'); 84 85 _.partial.placeholder = {}; 86 func = _.partial(function() { return arguments.length; }, obj, 'b', obj, 'd'); 87 assert.equal(func('a'), 5, 'swapping the placeholder preserves previously bound arguments'); 88 89 _.partial.placeholder = _; 90 }); 91 92 QUnit.test('bindAll', function(assert) { 93 var curly = {name: 'curly'}; 94 var moe = { 95 name: 'moe', 96 getName: function() { return 'name: ' + this.name; }, 97 sayHi: function() { return 'hi: ' + this.name; } 98 }; 99 curly.getName = moe.getName; 100 _.bindAll(moe, 'getName', 'sayHi'); 101 curly.sayHi = moe.sayHi; 102 assert.equal(curly.getName(), 'name: curly', 'unbound function is bound to current object'); 103 assert.equal(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object'); 104 105 curly = {name: 'curly'}; 106 moe = { 107 name: 'moe', 108 getName: function() { return 'name: ' + this.name; }, 109 sayHi: function() { return 'hi: ' + this.name; }, 110 sayLast: function() { return this.sayHi(_.last(arguments)); } 111 }; 112 113 assert.raises(function() { _.bindAll(moe); }, Error, 'throws an error for bindAll with no functions named'); 114 assert.raises(function() { _.bindAll(moe, 'sayBye'); }, TypeError, 'throws an error for bindAll if the given key is undefined'); 115 assert.raises(function() { _.bindAll(moe, 'name'); }, TypeError, 'throws an error for bindAll if the given key is not a function'); 116 117 _.bindAll(moe, 'sayHi', 'sayLast'); 118 curly.sayHi = moe.sayHi; 119 assert.equal(curly.sayHi(), 'hi: moe'); 120 121 var sayLast = moe.sayLast; 122 assert.equal(sayLast(1, 2, 3, 4, 5, 6, 7, 'Tom'), 'hi: moe', 'createCallback works with any number of arguments'); 123 124 _.bindAll(moe, ['getName']); 125 var getName = moe.getName; 126 assert.equal(getName(), 'name: moe', 'flattens arguments into a single list'); 127 }); 128 129 QUnit.test('memoize', function(assert) { 130 var fib = function(n) { 131 return n < 2 ? n : fib(n - 1) + fib(n - 2); 132 }; 133 assert.equal(fib(10), 55, 'a memoized version of fibonacci produces identical results'); 134 fib = _.memoize(fib); // Redefine `fib` for memoization 135 assert.equal(fib(10), 55, 'a memoized version of fibonacci produces identical results'); 136 137 var o = function(str) { 138 return str; 139 }; 140 var fastO = _.memoize(o); 141 assert.equal(o('toString'), 'toString', 'checks hasOwnProperty'); 142 assert.equal(fastO('toString'), 'toString', 'checks hasOwnProperty'); 143 144 // Expose the cache. 145 var upper = _.memoize(function(s) { 146 return s.toUpperCase(); 147 }); 148 assert.equal(upper('foo'), 'FOO'); 149 assert.equal(upper('bar'), 'BAR'); 150 assert.deepEqual(upper.cache, {foo: 'FOO', bar: 'BAR'}); 151 upper.cache = {foo: 'BAR', bar: 'FOO'}; 152 assert.equal(upper('foo'), 'BAR'); 153 assert.equal(upper('bar'), 'FOO'); 154 155 var hashed = _.memoize(function(key) { 156 //https://github.com/jashkenas/underscore/pull/1679#discussion_r13736209 157 assert.ok(/[a-z]+/.test(key), 'hasher doesn\'t change keys'); 158 return key; 159 }, function(key) { 160 return key.toUpperCase(); 161 }); 162 hashed('yep'); 163 assert.deepEqual(hashed.cache, {YEP: 'yep'}, 'takes a hasher'); 164 165 // Test that the hash function can be used to swizzle the key. 166 var objCacher = _.memoize(function(value, key) { 167 return {key: key, value: value}; 168 }, function(value, key) { 169 return key; 170 }); 171 var myObj = objCacher('a', 'alpha'); 172 var myObjAlias = objCacher('b', 'alpha'); 173 assert.notStrictEqual(myObj, void 0, 'object is created if second argument used as key'); 174 assert.strictEqual(myObj, myObjAlias, 'object is cached if second argument used as key'); 175 assert.strictEqual(myObj.value, 'a', 'object is not modified if second argument used as key'); 176 }); 177 178 QUnit.test('delay', function(assert) { 179 assert.expect(2); 180 var done = assert.async(); 181 var delayed = false; 182 _.delay(function(){ delayed = true; }, 100); 183 setTimeout(function(){ assert.notOk(delayed, "didn't delay the function quite yet"); }, 50); 184 setTimeout(function(){ assert.ok(delayed, 'delayed the function'); done(); }, 150); 185 }); 186 187 QUnit.test('defer', function(assert) { 188 assert.expect(1); 189 var done = assert.async(); 190 var deferred = false; 191 _.defer(function(bool){ deferred = bool; }, true); 192 _.delay(function(){ assert.ok(deferred, 'deferred the function'); done(); }, 50); 193 }); 194 195 QUnit.test('throttle', function(assert) { 196 assert.expect(2); 197 var done = assert.async(); 198 var counter = 0; 199 var incr = function(){ counter++; }; 200 var throttledIncr = _.throttle(incr, 32); 201 throttledIncr(); throttledIncr(); 202 203 assert.equal(counter, 1, 'incr was called immediately'); 204 _.delay(function(){ assert.equal(counter, 2, 'incr was throttled'); done(); }, 64); 205 }); 206 207 QUnit.test('throttle arguments', function(assert) { 208 assert.expect(2); 209 var done = assert.async(); 210 var value = 0; 211 var update = function(val){ value = val; }; 212 var throttledUpdate = _.throttle(update, 32); 213 throttledUpdate(1); throttledUpdate(2); 214 _.delay(function(){ throttledUpdate(3); }, 64); 215 assert.equal(value, 1, 'updated to latest value'); 216 _.delay(function(){ assert.equal(value, 3, 'updated to latest value'); done(); }, 96); 217 }); 218 219 QUnit.test('throttle once', function(assert) { 220 assert.expect(2); 221 var done = assert.async(); 222 var counter = 0; 223 var incr = function(){ return ++counter; }; 224 var throttledIncr = _.throttle(incr, 32); 225 var result = throttledIncr(); 226 _.delay(function(){ 227 assert.equal(result, 1, 'throttled functions return their value'); 228 assert.equal(counter, 1, 'incr was called once'); done(); 229 }, 64); 230 }); 231 232 QUnit.test('throttle twice', function(assert) { 233 assert.expect(1); 234 var done = assert.async(); 235 var counter = 0; 236 var incr = function(){ counter++; }; 237 var throttledIncr = _.throttle(incr, 32); 238 throttledIncr(); throttledIncr(); 239 _.delay(function(){ assert.equal(counter, 2, 'incr was called twice'); done(); }, 64); 240 }); 241 242 QUnit.test('more throttling', function(assert) { 243 assert.expect(3); 244 var done = assert.async(); 245 var counter = 0; 246 var incr = function(){ counter++; }; 247 var throttledIncr = _.throttle(incr, 30); 248 throttledIncr(); throttledIncr(); 249 assert.equal(counter, 1); 250 _.delay(function(){ 251 assert.equal(counter, 2); 252 throttledIncr(); 253 assert.equal(counter, 3); 254 done(); 255 }, 85); 256 }); 257 258 QUnit.test('throttle repeatedly with results', function(assert) { 259 assert.expect(6); 260 var done = assert.async(); 261 var counter = 0; 262 var incr = function(){ return ++counter; }; 263 var throttledIncr = _.throttle(incr, 100); 264 var results = []; 265 var saveResult = function() { results.push(throttledIncr()); }; 266 saveResult(); saveResult(); 267 _.delay(saveResult, 50); 268 _.delay(saveResult, 150); 269 _.delay(saveResult, 160); 270 _.delay(saveResult, 230); 271 _.delay(function() { 272 assert.equal(results[0], 1, 'incr was called once'); 273 assert.equal(results[1], 1, 'incr was throttled'); 274 assert.equal(results[2], 1, 'incr was throttled'); 275 assert.equal(results[3], 2, 'incr was called twice'); 276 assert.equal(results[4], 2, 'incr was throttled'); 277 assert.equal(results[5], 3, 'incr was called trailing'); 278 done(); 279 }, 300); 280 }); 281 282 QUnit.test('throttle triggers trailing call when invoked repeatedly', function(assert) { 283 assert.expect(2); 284 var done = assert.async(); 285 var counter = 0; 286 var limit = 48; 287 var incr = function(){ counter++; }; 288 var throttledIncr = _.throttle(incr, 32); 289 290 var stamp = new Date; 291 while (new Date - stamp < limit) { 292 throttledIncr(); 293 } 294 var lastCount = counter; 295 assert.ok(counter > 1); 296 297 _.delay(function() { 298 assert.ok(counter > lastCount); 299 done(); 300 }, 96); 301 }); 302 303 QUnit.test('throttle does not trigger leading call when leading is set to false', function(assert) { 304 assert.expect(2); 305 var done = assert.async(); 306 var counter = 0; 307 var incr = function(){ counter++; }; 308 var throttledIncr = _.throttle(incr, 60, {leading: false}); 309 310 throttledIncr(); throttledIncr(); 311 assert.equal(counter, 0); 312 313 _.delay(function() { 314 assert.equal(counter, 1); 315 done(); 316 }, 96); 317 }); 318 319 QUnit.test('more throttle does not trigger leading call when leading is set to false', function(assert) { 320 assert.expect(3); 321 var done = assert.async(); 322 var counter = 0; 323 var incr = function(){ counter++; }; 324 var throttledIncr = _.throttle(incr, 100, {leading: false}); 325 326 throttledIncr(); 327 _.delay(throttledIncr, 50); 328 _.delay(throttledIncr, 60); 329 _.delay(throttledIncr, 200); 330 assert.equal(counter, 0); 331 332 _.delay(function() { 333 assert.equal(counter, 1); 334 }, 250); 335 336 _.delay(function() { 337 assert.equal(counter, 2); 338 done(); 339 }, 350); 340 }); 341 342 QUnit.test('one more throttle with leading: false test', function(assert) { 343 assert.expect(2); 344 var done = assert.async(); 345 var counter = 0; 346 var incr = function(){ counter++; }; 347 var throttledIncr = _.throttle(incr, 100, {leading: false}); 348 349 var time = new Date; 350 while (new Date - time < 350) throttledIncr(); 351 assert.ok(counter <= 3); 352 353 _.delay(function() { 354 assert.ok(counter <= 4); 355 done(); 356 }, 200); 357 }); 358 359 QUnit.test('throttle does not trigger trailing call when trailing is set to false', function(assert) { 360 assert.expect(4); 361 var done = assert.async(); 362 var counter = 0; 363 var incr = function(){ counter++; }; 364 var throttledIncr = _.throttle(incr, 60, {trailing: false}); 365 366 throttledIncr(); throttledIncr(); throttledIncr(); 367 assert.equal(counter, 1); 368 369 _.delay(function() { 370 assert.equal(counter, 1); 371 372 throttledIncr(); throttledIncr(); 373 assert.equal(counter, 2); 374 375 _.delay(function() { 376 assert.equal(counter, 2); 377 done(); 378 }, 96); 379 }, 96); 380 }); 381 382 QUnit.test('throttle continues to function after system time is set backwards', function(assert) { 383 assert.expect(2); 384 var done = assert.async(); 385 var counter = 0; 386 var incr = function(){ counter++; }; 387 var throttledIncr = _.throttle(incr, 100); 388 var origNowFunc = _.now; 389 390 throttledIncr(); 391 assert.equal(counter, 1); 392 _.now = function() { 393 return new Date(2013, 0, 1, 1, 1, 1); 394 }; 395 396 _.delay(function() { 397 throttledIncr(); 398 assert.equal(counter, 2); 399 done(); 400 _.now = origNowFunc; 401 }, 200); 402 }); 403 404 QUnit.test('throttle re-entrant', function(assert) { 405 assert.expect(2); 406 var done = assert.async(); 407 var sequence = [ 408 ['b1', 'b2'], 409 ['c1', 'c2'] 410 ]; 411 var value = ''; 412 var throttledAppend; 413 var append = function(arg){ 414 value += this + arg; 415 var args = sequence.pop(); 416 if (args) { 417 throttledAppend.call(args[0], args[1]); 418 } 419 }; 420 throttledAppend = _.throttle(append, 32); 421 throttledAppend.call('a1', 'a2'); 422 assert.equal(value, 'a1a2'); 423 _.delay(function(){ 424 assert.equal(value, 'a1a2c1c2b1b2', 'append was throttled successfully'); 425 done(); 426 }, 100); 427 }); 428 429 QUnit.test('throttle cancel', function(assert) { 430 var done = assert.async(); 431 var counter = 0; 432 var incr = function(){ counter++; }; 433 var throttledIncr = _.throttle(incr, 32); 434 throttledIncr(); 435 throttledIncr.cancel(); 436 throttledIncr(); 437 throttledIncr(); 438 439 assert.equal(counter, 2, 'incr was called immediately'); 440 _.delay(function(){ assert.equal(counter, 3, 'incr was throttled'); done(); }, 64); 441 }); 442 443 QUnit.test('throttle cancel with leading: false', function(assert) { 444 var done = assert.async(); 445 var counter = 0; 446 var incr = function(){ counter++; }; 447 var throttledIncr = _.throttle(incr, 32, {leading: false}); 448 throttledIncr(); 449 throttledIncr.cancel(); 450 451 assert.equal(counter, 0, 'incr was throttled'); 452 _.delay(function(){ assert.equal(counter, 0, 'incr was throttled'); done(); }, 64); 453 }); 454 455 QUnit.test('debounce', function(assert) { 456 assert.expect(1); 457 var done = assert.async(); 458 var counter = 0; 459 var incr = function(){ counter++; }; 460 var debouncedIncr = _.debounce(incr, 32); 461 debouncedIncr(); debouncedIncr(); 462 _.delay(debouncedIncr, 16); 463 _.delay(function(){ assert.equal(counter, 1, 'incr was debounced'); done(); }, 96); 464 }); 465 466 QUnit.test('debounce cancel', function(assert) { 467 assert.expect(1); 468 var done = assert.async(); 469 var counter = 0; 470 var incr = function(){ counter++; }; 471 var debouncedIncr = _.debounce(incr, 32); 472 debouncedIncr(); 473 debouncedIncr.cancel(); 474 _.delay(function(){ assert.equal(counter, 0, 'incr was not called'); done(); }, 96); 475 }); 476 477 QUnit.test('debounce asap', function(assert) { 478 assert.expect(6); 479 var done = assert.async(); 480 var a, b, c; 481 var counter = 0; 482 var incr = function(){ return ++counter; }; 483 var debouncedIncr = _.debounce(incr, 64, true); 484 a = debouncedIncr(); 485 b = debouncedIncr(); 486 assert.equal(a, 1); 487 assert.equal(b, 1); 488 assert.equal(counter, 1, 'incr was called immediately'); 489 _.delay(debouncedIncr, 16); 490 _.delay(debouncedIncr, 32); 491 _.delay(debouncedIncr, 48); 492 _.delay(function(){ 493 assert.equal(counter, 1, 'incr was debounced'); 494 c = debouncedIncr(); 495 assert.equal(c, 2); 496 assert.equal(counter, 2, 'incr was called again'); 497 done(); 498 }, 128); 499 }); 500 501 QUnit.test('debounce asap cancel', function(assert) { 502 assert.expect(4); 503 var done = assert.async(); 504 var a, b; 505 var counter = 0; 506 var incr = function(){ return ++counter; }; 507 var debouncedIncr = _.debounce(incr, 64, true); 508 a = debouncedIncr(); 509 debouncedIncr.cancel(); 510 b = debouncedIncr(); 511 assert.equal(a, 1); 512 assert.equal(b, 2); 513 assert.equal(counter, 2, 'incr was called immediately'); 514 _.delay(debouncedIncr, 16); 515 _.delay(debouncedIncr, 32); 516 _.delay(debouncedIncr, 48); 517 _.delay(function(){ assert.equal(counter, 2, 'incr was debounced'); done(); }, 128); 518 }); 519 520 QUnit.test('debounce asap recursively', function(assert) { 521 assert.expect(2); 522 var done = assert.async(); 523 var counter = 0; 524 var debouncedIncr = _.debounce(function(){ 525 counter++; 526 if (counter < 10) debouncedIncr(); 527 }, 32, true); 528 debouncedIncr(); 529 assert.equal(counter, 1, 'incr was called immediately'); 530 _.delay(function(){ assert.equal(counter, 1, 'incr was debounced'); done(); }, 96); 531 }); 532 533 QUnit.test('debounce after system time is set backwards', function(assert) { 534 assert.expect(2); 535 var done = assert.async(); 536 var counter = 0; 537 var origNowFunc = _.now; 538 var debouncedIncr = _.debounce(function(){ 539 counter++; 540 }, 100, true); 541 542 debouncedIncr(); 543 assert.equal(counter, 1, 'incr was called immediately'); 544 545 _.now = function() { 546 return new Date(2013, 0, 1, 1, 1, 1); 547 }; 548 549 _.delay(function() { 550 debouncedIncr(); 551 assert.equal(counter, 2, 'incr was debounced successfully'); 552 done(); 553 _.now = origNowFunc; 554 }, 200); 555 }); 556 557 QUnit.test('debounce re-entrant', function(assert) { 558 assert.expect(2); 559 var done = assert.async(); 560 var sequence = [ 561 ['b1', 'b2'] 562 ]; 563 var value = ''; 564 var debouncedAppend; 565 var append = function(arg){ 566 value += this + arg; 567 var args = sequence.pop(); 568 if (args) { 569 debouncedAppend.call(args[0], args[1]); 570 } 571 }; 572 debouncedAppend = _.debounce(append, 32); 573 debouncedAppend.call('a1', 'a2'); 574 assert.equal(value, ''); 575 _.delay(function(){ 576 assert.equal(value, 'a1a2b1b2', 'append was debounced successfully'); 577 done(); 578 }, 100); 579 }); 580 581 QUnit.test('once', function(assert) { 582 var num = 0; 583 var increment = _.once(function(){ return ++num; }); 584 increment(); 585 increment(); 586 assert.equal(num, 1); 587 588 assert.equal(increment(), 1, 'stores a memo to the last value'); 589 }); 590 591 QUnit.test('Recursive onced function.', function(assert) { 592 assert.expect(1); 593 var f = _.once(function(){ 594 assert.ok(true); 595 f(); 596 }); 597 f(); 598 }); 599 600 QUnit.test('wrap', function(assert) { 601 var greet = function(name){ return 'hi: ' + name; }; 602 var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); }); 603 assert.equal(backwards('moe'), 'hi: moe eom', 'wrapped the salutation function'); 604 605 var inner = function(){ return 'Hello '; }; 606 var obj = {name: 'Moe'}; 607 obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; }); 608 assert.equal(obj.hi(), 'Hello Moe'); 609 610 var noop = function(){}; 611 var wrapped = _.wrap(noop, function(){ return Array.prototype.slice.call(arguments, 0); }); 612 var ret = wrapped(['whats', 'your'], 'vector', 'victor'); 613 assert.deepEqual(ret, [noop, ['whats', 'your'], 'vector', 'victor']); 614 }); 615 616 QUnit.test('negate', function(assert) { 617 var isOdd = function(n){ return n & 1; }; 618 assert.equal(_.negate(isOdd)(2), true, 'should return the complement of the given function'); 619 assert.equal(_.negate(isOdd)(3), false, 'should return the complement of the given function'); 620 }); 621 622 QUnit.test('compose', function(assert) { 623 var greet = function(name){ return 'hi: ' + name; }; 624 var exclaim = function(sentence){ return sentence + '!'; }; 625 var composed = _.compose(exclaim, greet); 626 assert.equal(composed('moe'), 'hi: moe!', 'can compose a function that takes another'); 627 628 composed = _.compose(greet, exclaim); 629 assert.equal(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative'); 630 631 // f(g(h(x, y, z))) 632 function h(x, y, z) { 633 assert.equal(arguments.length, 3, 'First function called with multiple args'); 634 return z * y; 635 } 636 function g(x) { 637 assert.equal(arguments.length, 1, 'Composed function is called with 1 argument'); 638 return x; 639 } 640 function f(x) { 641 assert.equal(arguments.length, 1, 'Composed function is called with 1 argument'); 642 return x * 2; 643 } 644 composed = _.compose(f, g, h); 645 assert.equal(composed(1, 2, 3), 12); 646 }); 647 648 QUnit.test('after', function(assert) { 649 var testAfter = function(afterAmount, timesCalled) { 650 var afterCalled = 0; 651 var after = _.after(afterAmount, function() { 652 afterCalled++; 653 }); 654 while (timesCalled--) after(); 655 return afterCalled; 656 }; 657 658 assert.equal(testAfter(5, 5), 1, 'after(N) should fire after being called N times'); 659 assert.equal(testAfter(5, 4), 0, 'after(N) should not fire unless called N times'); 660 assert.equal(testAfter(0, 0), 0, 'after(0) should not fire immediately'); 661 assert.equal(testAfter(0, 1), 1, 'after(0) should fire when first invoked'); 662 }); 663 664 QUnit.test('before', function(assert) { 665 var testBefore = function(beforeAmount, timesCalled) { 666 var beforeCalled = 0; 667 var before = _.before(beforeAmount, function() { beforeCalled++; }); 668 while (timesCalled--) before(); 669 return beforeCalled; 670 }; 671 672 assert.equal(testBefore(5, 5), 4, 'before(N) should not fire after being called N times'); 673 assert.equal(testBefore(5, 4), 4, 'before(N) should fire before being called N times'); 674 assert.equal(testBefore(0, 0), 0, 'before(0) should not fire immediately'); 675 assert.equal(testBefore(0, 1), 0, 'before(0) should not fire when first invoked'); 676 677 var context = {num: 0}; 678 var increment = _.before(3, function(){ return ++this.num; }); 679 _.times(10, increment, context); 680 assert.equal(increment(), 2, 'stores a memo to the last value'); 681 assert.equal(context.num, 2, 'provides context'); 682 }); 683 684 QUnit.test('iteratee', function(assert) { 685 var identity = _.iteratee(); 686 assert.equal(identity, _.identity, '_.iteratee is exposed as an external function.'); 687 688 function fn() { 689 return arguments; 690 } 691 _.each([_.iteratee(fn), _.iteratee(fn, {})], function(cb) { 692 assert.equal(cb().length, 0); 693 assert.deepEqual(_.toArray(cb(1, 2, 3)), _.range(1, 4)); 694 assert.deepEqual(_.toArray(cb(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), _.range(1, 11)); 695 }); 696 697 // Test custom iteratee 698 var builtinIteratee = _.iteratee; 699 _.iteratee = function(value) { 700 // RegEx values return a function that returns the number of matches 701 if (_.isRegExp(value)) return function(obj) { 702 return (obj.match(value) || []).length; 703 }; 704 return value; 705 }; 706 707 var collection = ['foo', 'bar', 'bbiz']; 708 709 // Test all methods that claim to be transformed through `_.iteratee` 710 assert.deepEqual(_.countBy(collection, /b/g), {0: 1, 1: 1, 2: 1}); 711 assert.equal(_.every(collection, /b/g), false); 712 assert.deepEqual(_.filter(collection, /b/g), ['bar', 'bbiz']); 713 assert.equal(_.find(collection, /b/g), 'bar'); 714 assert.equal(_.findIndex(collection, /b/g), 1); 715 assert.equal(_.findKey(collection, /b/g), 1); 716 assert.equal(_.findLastIndex(collection, /b/g), 2); 717 assert.deepEqual(_.groupBy(collection, /b/g), {0: ['foo'], 1: ['bar'], 2: ['bbiz']}); 718 assert.deepEqual(_.indexBy(collection, /b/g), {0: 'foo', 1: 'bar', 2: 'bbiz'}); 719 assert.deepEqual(_.map(collection, /b/g), [0, 1, 2]); 720 assert.equal(_.max(collection, /b/g), 'bbiz'); 721 assert.equal(_.min(collection, /b/g), 'foo'); 722 assert.deepEqual(_.partition(collection, /b/g), [['bar', 'bbiz'], ['foo']]); 723 assert.deepEqual(_.reject(collection, /b/g), ['foo']); 724 assert.equal(_.some(collection, /b/g), true); 725 assert.deepEqual(_.sortBy(collection, /b/g), ['foo', 'bar', 'bbiz']); 726 assert.equal(_.sortedIndex(collection, 'blah', /b/g), 1); 727 assert.deepEqual(_.uniq(collection, /b/g), ['foo', 'bar', 'bbiz']); 728 729 var objCollection = {a: 'foo', b: 'bar', c: 'bbiz'}; 730 assert.deepEqual(_.mapObject(objCollection, /b/g), {a: 0, b: 1, c: 2}); 731 732 // Restore the builtin iteratee 733 _.iteratee = builtinIteratee; 734 }); 735 736 QUnit.test('restArgs', function(assert) { 737 assert.expect(10); 738 _.restArgs(function(a, args) { 739 assert.strictEqual(a, 1); 740 assert.deepEqual(args, [2, 3], 'collects rest arguments into an array'); 741 })(1, 2, 3); 742 743 _.restArgs(function(a, args) { 744 assert.strictEqual(a, void 0); 745 assert.deepEqual(args, [], 'passes empty array if there are not enough arguments'); 746 })(); 747 748 _.restArgs(function(a, b, c, args) { 749 assert.strictEqual(arguments.length, 4); 750 assert.deepEqual(args, [4, 5], 'works on functions with many named parameters'); 751 })(1, 2, 3, 4, 5); 752 753 var obj = {}; 754 _.restArgs(function() { 755 assert.strictEqual(this, obj, 'invokes function with this context'); 756 }).call(obj); 757 758 _.restArgs(function(array, iteratee, context) { 759 assert.deepEqual(array, [1, 2, 3, 4], 'startIndex can be used manually specify index of rest parameter'); 760 assert.strictEqual(iteratee, void 0); 761 assert.strictEqual(context, void 0); 762 }, 0)(1, 2, 3, 4); 763 }); 764 765}()); 766