ember-data-factory-guy 0.7.5 → 0.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/tests/index.html CHANGED
@@ -13,6 +13,9 @@
13
13
  <script src='../bower_components/ember/ember.js'></script>
14
14
  <script src='../bower_components/ember-data/ember-data.js'></script>
15
15
  <script src="../bower_components/qunit/qunit/qunit.js"></script>
16
+ <script src="../bower_components/sinon/index.js"></script>
17
+ <script src="../bower_components/sinon-qunit/lib/sinon-qunit.js"></script>
18
+ <script src="../bower_components/jquery-mockjax/jquery.mockjax.js"></script>
16
19
 
17
20
  <script src='../dist/ember-data-factory-guy.js'></script>
18
21
  <!--<script src='../src/has_many.js'></script>-->
@@ -26,11 +29,11 @@
26
29
  </head>
27
30
  <body>
28
31
  <div id="qunit"></div> <!-- QUnit fills this with results, etc -->
29
- <!--<script src='active_model_adapter_factory_test.js'></script>-->
30
- <!--<script src='rest_adapter_factory_test.js'></script>-->
32
+ <script src='active_model_adapter_factory_test.js'></script>
33
+ <script src='rest_adapter_factory_test.js'></script>
31
34
  <!--<script src='fixture_adapter_factory_test.js'></script>-->
32
- <!--<script src='store_test.js'></script>-->
33
- <!--<script src='factory_guy_test.js'></script>-->
35
+ <script src='store_test.js'></script>
36
+ <script src='factory_guy_test.js'></script>
34
37
  <script src='factory_guy_test_mixin_test.js'></script>
35
38
  <div id='qunit-fixture'>
36
39
 
@@ -38,4 +41,4 @@
38
41
 
39
42
  </div>
40
43
  </body>
41
- </html>
44
+ </html>
@@ -5,6 +5,9 @@ FactoryGuy.define('profile', {
5
5
  traits: {
6
6
  goofy_description: {
7
7
  description: 'goofy'
8
+ },
9
+ with_company: {
10
+ company: FactoryGuy.belongsTo('company')
8
11
  }
9
12
  }
10
13
  })
data/tests/test_setup.js CHANGED
@@ -95,6 +95,9 @@ FactoryGuy.define('profile', {
95
95
  traits: {
96
96
  goofy_description: {
97
97
  description: 'goofy'
98
+ },
99
+ with_company: {
100
+ company: FactoryGuy.belongsTo('company')
98
101
  }
99
102
  }
100
103
  })
@@ -303,4891 +306,3 @@ User = DS.Model.extend({
303
306
  hats: DS.hasMany('hat', {polymorphic: true})
304
307
  });
305
308
 
306
-
307
- /*!
308
- * MockJax - jQuery Plugin to Mock Ajax requests
309
- *
310
- * Version: 1.5.3
311
- * Released:
312
- * Home: http://github.com/appendto/jquery-mockjax
313
- * Author: Jonathan Sharp (http://jdsharp.com)
314
- * License: MIT,GPL
315
- *
316
- * Copyright (c) 2011 appendTo LLC.
317
- * Dual licensed under the MIT or GPL licenses.
318
- * http://appendto.com/open-source-licenses
319
- */
320
- (function($) {
321
- var _ajax = $.ajax,
322
- mockHandlers = [],
323
- mockedAjaxCalls = [],
324
- CALLBACK_REGEX = /=\?(&|$)/,
325
- jsc = (new Date()).getTime();
326
-
327
-
328
- // Parse the given XML string.
329
- function parseXML(xml) {
330
- if ( window.DOMParser == undefined && window.ActiveXObject ) {
331
- DOMParser = function() { };
332
-
333
- DOMParser.prototype.parseFromString = function( xmlString ) {
334
- var doc = new ActiveXObject('Microsoft.XMLDOM');
335
- doc.async = 'false';
336
- doc.loadXML( xmlString );
337
- return doc;
338
- };
339
- }
340
-
341
- try {
342
- var xmlDoc = ( new DOMParser() ).parseFromString( xml, 'text/xml' );
343
- if ( $.isXMLDoc( xmlDoc ) ) {
344
- var err = $('parsererror', xmlDoc);
345
- if ( err.length == 1 ) {
346
- throw('Error: ' + $(xmlDoc).text() );
347
- }
348
- } else {
349
- throw('Unable to parse XML');
350
- }
351
- return xmlDoc;
352
- } catch( e ) {
353
- var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
354
- $(document).trigger('xmlParseError', [ msg ]);
355
- return undefined;
356
- }
357
- }
358
-
359
- // Trigger a jQuery event
360
- function trigger(s, type, args) {
361
- (s.context ? $(s.context) : $.event).trigger(type, args);
362
- }
363
-
364
- // Check if the data field on the mock handler and the request match. This
365
- // can be used to restrict a mock handler to being used only when a certain
366
- // set of data is passed to it.
367
- function isMockDataEqual( mock, live ) {
368
- var identical = true;
369
- // Test for situations where the data is a querystring (not an object)
370
- if (typeof live === 'string') {
371
- // Querystring may be a regex
372
- return $.isFunction( mock.test ) ? mock.test(live) : mock == live;
373
- }
374
- $.each(mock, function(k) {
375
- if ( live[k] === undefined ) {
376
- identical = false;
377
- return identical;
378
- } else {
379
- // This will allow to compare Arrays
380
- if ( typeof live[k] === 'object' && live[k] !== null ) {
381
- identical = identical && isMockDataEqual(mock[k], live[k]);
382
- } else {
383
- if ( mock[k] && $.isFunction( mock[k].test ) ) {
384
- identical = identical && mock[k].test(live[k]);
385
- } else {
386
- identical = identical && ( mock[k] == live[k] );
387
- }
388
- }
389
- }
390
- });
391
-
392
- return identical;
393
- }
394
-
395
- // See if a mock handler property matches the default settings
396
- function isDefaultSetting(handler, property) {
397
- return handler[property] === $.mockjaxSettings[property];
398
- }
399
-
400
- // Check the given handler should mock the given request
401
- function getMockForRequest( handler, requestSettings ) {
402
-
403
- // If the mock was registered with a function, let the function decide if we
404
- // want to mock this request
405
- if ( $.isFunction(handler) ) {
406
- return handler( requestSettings );
407
- }
408
-
409
- // Inspect the URL of the request and check if the mock handler's url
410
- // matches the url for this ajax request
411
- if ( $.isFunction(handler.url.test) ) {
412
- // The user provided a regex for the url, test it
413
- if ( !handler.url.test( requestSettings.url ) ) {
414
- return null;
415
- }
416
- } else {
417
- // Look for a simple wildcard '*' or a direct URL match
418
- var star = handler.url.indexOf('*');
419
- if (handler.url !== requestSettings.url && star === -1 ||
420
- !new RegExp(handler.url.replace(/[-[\]{}()+?.,\\^$|#\s]/g, "\\$&").replace(/\*/g, '.+')).test(requestSettings.url)) {
421
- return null;
422
- }
423
- }
424
- // console.log(handler.url, handler.data, requestSettings.data)
425
- // Inspect the data submitted in the request (either POST body or GET query string)
426
- if ( handler.data && requestSettings.data ) {
427
- if ( !isMockDataEqual(handler.data, requestSettings.data) ) {
428
- // They're not identical, do not mock this request
429
- return null;
430
- }
431
- }
432
- // Inspect the request type
433
- if ( handler && handler.type &&
434
- handler.type.toLowerCase() != requestSettings.type.toLowerCase() ) {
435
- // The request type doesn't match (GET vs. POST)
436
- return null;
437
- }
438
-
439
- return handler;
440
- }
441
-
442
- // Process the xhr objects send operation
443
- function _xhrSend(mockHandler, requestSettings, origSettings) {
444
-
445
- // This is a substitute for < 1.4 which lacks $.proxy
446
- var process = (function(that) {
447
- return function() {
448
- return (function() {
449
- var onReady;
450
-
451
- // The request has returned
452
- this.status = mockHandler.status;
453
- this.statusText = mockHandler.statusText;
454
- this.readyState = 4;
455
-
456
- // We have an executable function, call it to give
457
- // the mock handler a chance to update it's data
458
- if ( $.isFunction(mockHandler.response) ) {
459
- mockHandler.response(origSettings);
460
- }
461
- // Copy over our mock to our xhr object before passing control back to
462
- // jQuery's onreadystatechange callback
463
- if ( requestSettings.dataType == 'json' && ( typeof mockHandler.responseText == 'object' ) ) {
464
- this.responseText = JSON.stringify(mockHandler.responseText);
465
- } else if ( requestSettings.dataType == 'xml' ) {
466
- if ( typeof mockHandler.responseXML == 'string' ) {
467
- this.responseXML = parseXML(mockHandler.responseXML);
468
- //in jQuery 1.9.1+, responseXML is processed differently and relies on responseText
469
- this.responseText = mockHandler.responseXML;
470
- } else {
471
- this.responseXML = mockHandler.responseXML;
472
- }
473
- } else {
474
- this.responseText = mockHandler.responseText;
475
- }
476
- if( typeof mockHandler.status == 'number' || typeof mockHandler.status == 'string' ) {
477
- this.status = mockHandler.status;
478
- }
479
- if( typeof mockHandler.statusText === "string") {
480
- this.statusText = mockHandler.statusText;
481
- }
482
- // jQuery 2.0 renamed onreadystatechange to onload
483
- onReady = this.onreadystatechange || this.onload;
484
-
485
- // jQuery < 1.4 doesn't have onreadystate change for xhr
486
- if ( $.isFunction( onReady ) ) {
487
- if( mockHandler.isTimeout) {
488
- this.status = -1;
489
- }
490
- onReady.call( this, mockHandler.isTimeout ? 'timeout' : undefined );
491
- } else if ( mockHandler.isTimeout ) {
492
- // Fix for 1.3.2 timeout to keep success from firing.
493
- this.status = -1;
494
- }
495
- }).apply(that);
496
- };
497
- })(this);
498
-
499
- if ( mockHandler.proxy ) {
500
- // We're proxying this request and loading in an external file instead
501
- _ajax({
502
- global: false,
503
- url: mockHandler.proxy,
504
- type: mockHandler.proxyType,
505
- data: mockHandler.data,
506
- dataType: requestSettings.dataType === "script" ? "text/plain" : requestSettings.dataType,
507
- complete: function(xhr) {
508
- mockHandler.responseXML = xhr.responseXML;
509
- mockHandler.responseText = xhr.responseText;
510
- // Don't override the handler status/statusText if it's specified by the config
511
- if (isDefaultSetting(mockHandler, 'status')) {
512
- mockHandler.status = xhr.status;
513
- }
514
- if (isDefaultSetting(mockHandler, 'statusText')) {
515
- mockHandler.statusText = xhr.statusText;
516
- }
517
-
518
- this.responseTimer = setTimeout(process, mockHandler.responseTime || 0);
519
- }
520
- });
521
- } else {
522
- // type == 'POST' || 'GET' || 'DELETE'
523
- if ( requestSettings.async === false ) {
524
- // TODO: Blocking delay
525
- process();
526
- } else {
527
- this.responseTimer = setTimeout(process, mockHandler.responseTime || 50);
528
- }
529
- }
530
- }
531
-
532
- // Construct a mocked XHR Object
533
- function xhr(mockHandler, requestSettings, origSettings, origHandler) {
534
- // Extend with our default mockjax settings
535
- mockHandler = $.extend(true, {}, $.mockjaxSettings, mockHandler);
536
-
537
- if (typeof mockHandler.headers === 'undefined') {
538
- mockHandler.headers = {};
539
- }
540
- if ( mockHandler.contentType ) {
541
- mockHandler.headers['content-type'] = mockHandler.contentType;
542
- }
543
-
544
- return {
545
- status: mockHandler.status,
546
- statusText: mockHandler.statusText,
547
- readyState: 1,
548
- open: function() { },
549
- send: function() {
550
- origHandler.fired = true;
551
- _xhrSend.call(this, mockHandler, requestSettings, origSettings);
552
- },
553
- abort: function() {
554
- clearTimeout(this.responseTimer);
555
- },
556
- setRequestHeader: function(header, value) {
557
- mockHandler.headers[header] = value;
558
- },
559
- getResponseHeader: function(header) {
560
- // 'Last-modified', 'Etag', 'content-type' are all checked by jQuery
561
- if ( mockHandler.headers && mockHandler.headers[header] ) {
562
- // Return arbitrary headers
563
- return mockHandler.headers[header];
564
- } else if ( header.toLowerCase() == 'last-modified' ) {
565
- return mockHandler.lastModified || (new Date()).toString();
566
- } else if ( header.toLowerCase() == 'etag' ) {
567
- return mockHandler.etag || '';
568
- } else if ( header.toLowerCase() == 'content-type' ) {
569
- return mockHandler.contentType || 'text/plain';
570
- }
571
- },
572
- getAllResponseHeaders: function() {
573
- var headers = '';
574
- $.each(mockHandler.headers, function(k, v) {
575
- headers += k + ': ' + v + "\n";
576
- });
577
- return headers;
578
- }
579
- };
580
- }
581
-
582
- // Process a JSONP mock request.
583
- function processJsonpMock( requestSettings, mockHandler, origSettings ) {
584
- // Handle JSONP Parameter Callbacks, we need to replicate some of the jQuery core here
585
- // because there isn't an easy hook for the cross domain script tag of jsonp
586
-
587
- processJsonpUrl( requestSettings );
588
-
589
- requestSettings.dataType = "json";
590
- if(requestSettings.data && CALLBACK_REGEX.test(requestSettings.data) || CALLBACK_REGEX.test(requestSettings.url)) {
591
- createJsonpCallback(requestSettings, mockHandler, origSettings);
592
-
593
- // We need to make sure
594
- // that a JSONP style response is executed properly
595
-
596
- var rurl = /^(\w+:)?\/\/([^\/?#]+)/,
597
- parts = rurl.exec( requestSettings.url ),
598
- remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
599
-
600
- requestSettings.dataType = "script";
601
- if(requestSettings.type.toUpperCase() === "GET" && remote ) {
602
- var newMockReturn = processJsonpRequest( requestSettings, mockHandler, origSettings );
603
-
604
- // Check if we are supposed to return a Deferred back to the mock call, or just
605
- // signal success
606
- if(newMockReturn) {
607
- return newMockReturn;
608
- } else {
609
- return true;
610
- }
611
- }
612
- }
613
- return null;
614
- }
615
-
616
- // Append the required callback parameter to the end of the request URL, for a JSONP request
617
- function processJsonpUrl( requestSettings ) {
618
- if ( requestSettings.type.toUpperCase() === "GET" ) {
619
- if ( !CALLBACK_REGEX.test( requestSettings.url ) ) {
620
- requestSettings.url += (/\?/.test( requestSettings.url ) ? "&" : "?") +
621
- (requestSettings.jsonp || "callback") + "=?";
622
- }
623
- } else if ( !requestSettings.data || !CALLBACK_REGEX.test(requestSettings.data) ) {
624
- requestSettings.data = (requestSettings.data ? requestSettings.data + "&" : "") + (requestSettings.jsonp || "callback") + "=?";
625
- }
626
- }
627
-
628
- // Process a JSONP request by evaluating the mocked response text
629
- function processJsonpRequest( requestSettings, mockHandler, origSettings ) {
630
- // Synthesize the mock request for adding a script tag
631
- var callbackContext = origSettings && origSettings.context || requestSettings,
632
- newMock = null;
633
-
634
-
635
- // If the response handler on the moock is a function, call it
636
- if ( mockHandler.response && $.isFunction(mockHandler.response) ) {
637
- mockHandler.response(origSettings);
638
- } else {
639
-
640
- // Evaluate the responseText javascript in a global context
641
- if( typeof mockHandler.responseText === 'object' ) {
642
- $.globalEval( '(' + JSON.stringify( mockHandler.responseText ) + ')');
643
- } else {
644
- $.globalEval( '(' + mockHandler.responseText + ')');
645
- }
646
- }
647
-
648
- // Successful response
649
- jsonpSuccess( requestSettings, callbackContext, mockHandler );
650
- jsonpComplete( requestSettings, callbackContext, mockHandler );
651
-
652
- // If we are running under jQuery 1.5+, return a deferred object
653
- if($.Deferred){
654
- newMock = new $.Deferred();
655
- if(typeof mockHandler.responseText == "object"){
656
- newMock.resolveWith( callbackContext, [mockHandler.responseText] );
657
- }
658
- else{
659
- newMock.resolveWith( callbackContext, [$.parseJSON( mockHandler.responseText )] );
660
- }
661
- }
662
- return newMock;
663
- }
664
-
665
-
666
- // Create the required JSONP callback function for the request
667
- function createJsonpCallback( requestSettings, mockHandler, origSettings ) {
668
- var callbackContext = origSettings && origSettings.context || requestSettings;
669
- var jsonp = requestSettings.jsonpCallback || ("jsonp" + jsc++);
670
-
671
- // Replace the =? sequence both in the query string and the data
672
- if ( requestSettings.data ) {
673
- requestSettings.data = (requestSettings.data + "").replace(CALLBACK_REGEX, "=" + jsonp + "$1");
674
- }
675
-
676
- requestSettings.url = requestSettings.url.replace(CALLBACK_REGEX, "=" + jsonp + "$1");
677
-
678
-
679
- // Handle JSONP-style loading
680
- window[ jsonp ] = window[ jsonp ] || function( tmp ) {
681
- data = tmp;
682
- jsonpSuccess( requestSettings, callbackContext, mockHandler );
683
- jsonpComplete( requestSettings, callbackContext, mockHandler );
684
- // Garbage collect
685
- window[ jsonp ] = undefined;
686
-
687
- try {
688
- delete window[ jsonp ];
689
- } catch(e) {}
690
-
691
- if ( head ) {
692
- head.removeChild( script );
693
- }
694
- };
695
- }
696
-
697
- // The JSONP request was successful
698
- function jsonpSuccess(requestSettings, callbackContext, mockHandler) {
699
- // If a local callback was specified, fire it and pass it the data
700
- if ( requestSettings.success ) {
701
- requestSettings.success.call( callbackContext, mockHandler.responseText || "", status, {} );
702
- }
703
-
704
- // Fire the global callback
705
- if ( requestSettings.global ) {
706
- trigger(requestSettings, "ajaxSuccess", [{}, requestSettings] );
707
- }
708
- }
709
-
710
- // The JSONP request was completed
711
- function jsonpComplete(requestSettings, callbackContext) {
712
- // Process result
713
- if ( requestSettings.complete ) {
714
- requestSettings.complete.call( callbackContext, {} , status );
715
- }
716
-
717
- // The request was completed
718
- if ( requestSettings.global ) {
719
- trigger( "ajaxComplete", [{}, requestSettings] );
720
- }
721
-
722
- // Handle the global AJAX counter
723
- if ( requestSettings.global && ! --$.active ) {
724
- $.event.trigger( "ajaxStop" );
725
- }
726
- }
727
-
728
-
729
- // The core $.ajax replacement.
730
- function handleAjax( url, origSettings ) {
731
- var mockRequest, requestSettings, mockHandler;
732
- // console.log('handleAjax', url)
733
- // If url is an object, simulate pre-1.5 signature
734
- if ( typeof url === "object" ) {
735
- origSettings = url;
736
- url = undefined;
737
- } else {
738
- // work around to support 1.5 signature
739
- origSettings.url = url;
740
- }
741
-
742
- // Extend the original settings for the request
743
- requestSettings = $.extend(true, {}, $.ajaxSettings, origSettings);
744
-
745
- // Iterate over our mock handlers (in registration order) until we find
746
- // one that is willing to intercept the request
747
- for(var k = 0; k < mockHandlers.length; k++) {
748
- if ( !mockHandlers[k] ) {
749
- continue;
750
- }
751
-
752
- mockHandler = getMockForRequest( mockHandlers[k], requestSettings );
753
- if(!mockHandler) {
754
- // No valid mock found for this request
755
- continue;
756
- }
757
-
758
- mockedAjaxCalls.push(requestSettings);
759
-
760
- // If logging is enabled, log the mock to the console
761
- $.mockjaxSettings.log( mockHandler, requestSettings );
762
-
763
-
764
- if ( requestSettings.dataType === "jsonp" ) {
765
- if ((mockRequest = processJsonpMock( requestSettings, mockHandler, origSettings ))) {
766
- // This mock will handle the JSONP request
767
- return mockRequest;
768
- }
769
- }
770
-
771
-
772
- // Removed to fix #54 - keep the mocking data object intact
773
- //mockHandler.data = requestSettings.data;
774
-
775
- mockHandler.cache = requestSettings.cache;
776
- mockHandler.timeout = requestSettings.timeout;
777
- mockHandler.global = requestSettings.global;
778
-
779
- copyUrlParameters(mockHandler, origSettings);
780
-
781
- (function(mockHandler, requestSettings, origSettings, origHandler) {
782
- mockRequest = _ajax.call($, $.extend(true, {}, origSettings, {
783
- // Mock the XHR object
784
- xhr: function() { return xhr( mockHandler, requestSettings, origSettings, origHandler ); }
785
- }));
786
- })(mockHandler, requestSettings, origSettings, mockHandlers[k]);
787
-
788
- return mockRequest;
789
- }
790
-
791
- // We don't have a mock request
792
- if($.mockjaxSettings.throwUnmocked === true) {
793
- throw('AJAX not mocked: ' + origSettings.url);
794
- }
795
- else { // trigger a normal request
796
- return _ajax.apply($, [origSettings]);
797
- }
798
- }
799
-
800
- /**
801
- * Copies URL parameter values if they were captured by a regular expression
802
- * @param {Object} mockHandler
803
- * @param {Object} origSettings
804
- */
805
- function copyUrlParameters(mockHandler, origSettings) {
806
- //parameters aren't captured if the URL isn't a RegExp
807
- if (!(mockHandler.url instanceof RegExp)) {
808
- return;
809
- }
810
- //if no URL params were defined on the handler, don't attempt a capture
811
- if (!mockHandler.hasOwnProperty('urlParams')) {
812
- return;
813
- }
814
- var captures = mockHandler.url.exec(origSettings.url);
815
- //the whole RegExp match is always the first value in the capture results
816
- if (captures.length === 1) {
817
- return;
818
- }
819
- captures.shift();
820
- //use handler params as keys and capture resuts as values
821
- var i = 0,
822
- capturesLength = captures.length,
823
- paramsLength = mockHandler.urlParams.length,
824
- //in case the number of params specified is less than actual captures
825
- maxIterations = Math.min(capturesLength, paramsLength),
826
- paramValues = {};
827
- for (i; i < maxIterations; i++) {
828
- var key = mockHandler.urlParams[i];
829
- paramValues[key] = captures[i];
830
- }
831
- origSettings.urlParams = paramValues;
832
- }
833
-
834
-
835
- // Public
836
-
837
- $.extend({
838
- ajax: handleAjax
839
- });
840
-
841
- $.mockjaxSettings = {
842
- //url: null,
843
- //type: 'GET',
844
- log: function( mockHandler, requestSettings ) {
845
- if ( mockHandler.logging === false ||
846
- ( typeof mockHandler.logging === 'undefined' && $.mockjaxSettings.logging === false ) ) {
847
- return;
848
- }
849
- if ( window.console && console.log ) {
850
- var message = 'MOCK ' + requestSettings.type.toUpperCase() + ': ' + requestSettings.url;
851
- var request = $.extend({}, requestSettings);
852
-
853
- if (typeof console.log === 'function') {
854
- console.log(message, request);
855
- } else {
856
- try {
857
- console.log( message + ' ' + JSON.stringify(request) );
858
- } catch (e) {
859
- console.log(message);
860
- }
861
- }
862
- }
863
- },
864
- logging: true,
865
- status: 200,
866
- statusText: "OK",
867
- responseTime: 500,
868
- isTimeout: false,
869
- throwUnmocked: false,
870
- contentType: 'text/plain',
871
- response: '',
872
- responseText: '',
873
- responseXML: '',
874
- proxy: '',
875
- proxyType: 'GET',
876
-
877
- lastModified: null,
878
- etag: '',
879
- headers: {
880
- etag: 'IJF@H#@923uf8023hFO@I#H#',
881
- 'content-type' : 'text/plain'
882
- }
883
- };
884
-
885
- $.mockjax = function(settings) {
886
- var i = mockHandlers.length;
887
- mockHandlers[i] = settings;
888
- return i;
889
- };
890
- $.mockjaxClear = function(i) {
891
- if ( arguments.length == 1 ) {
892
- mockHandlers[i] = null;
893
- } else {
894
- mockHandlers = [];
895
- }
896
- mockedAjaxCalls = [];
897
- };
898
- $.mockjax.handler = function(i) {
899
- if ( arguments.length == 1 ) {
900
- return mockHandlers[i];
901
- }
902
- };
903
- $.mockjax.mockedAjaxCalls = function() {
904
- return mockedAjaxCalls;
905
- };
906
- })(jQuery);
907
- /**
908
- * Sinon.JS 1.6.0, 2013/02/18
909
- *
910
- * @author Christian Johansen (christian@cjohansen.no)
911
- * @author Contributors: https://github.com/cjohansen/Sinon.JS/blob/master/AUTHORS
912
- *
913
- * (The BSD License)
914
- *
915
- * Copyright (c) 2010-2013, Christian Johansen, christian@cjohansen.no
916
- * All rights reserved.
917
- *
918
- * Redistribution and use in source and binary forms, with or without modification,
919
- * are permitted provided that the following conditions are met:
920
- *
921
- * * Redistributions of source code must retain the above copyright notice,
922
- * this list of conditions and the following disclaimer.
923
- * * Redistributions in binary form must reproduce the above copyright notice,
924
- * this list of conditions and the following disclaimer in the documentation
925
- * and/or other materials provided with the distribution.
926
- * * Neither the name of Christian Johansen nor the names of his contributors
927
- * may be used to endorse or promote products derived from this software
928
- * without specific prior written permission.
929
- *
930
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
931
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
932
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
933
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
934
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
935
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
936
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
937
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
938
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
939
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
940
- */
941
-
942
- var sinon = (function () {
943
- "use strict";
944
-
945
- var buster = (function (setTimeout, B) {
946
- var isNode = typeof require == "function" && typeof module == "object";
947
- var div = typeof document != "undefined" && document.createElement("div");
948
- var F = function () {
949
- };
950
-
951
- var buster = {
952
- bind: function bind(obj, methOrProp) {
953
- var method = typeof methOrProp == "string" ? obj[methOrProp] : methOrProp;
954
- var args = Array.prototype.slice.call(arguments, 2);
955
- return function () {
956
- var allArgs = args.concat(Array.prototype.slice.call(arguments));
957
- return method.apply(obj, allArgs);
958
- };
959
- },
960
-
961
- partial: function partial(fn) {
962
- var args = [].slice.call(arguments, 1);
963
- return function () {
964
- return fn.apply(this, args.concat([].slice.call(arguments)));
965
- };
966
- },
967
-
968
- create: function create(object) {
969
- F.prototype = object;
970
- return new F();
971
- },
972
-
973
- extend: function extend(target) {
974
- if (!target) {
975
- return;
976
- }
977
- for (var i = 1, l = arguments.length, prop; i < l; ++i) {
978
- for (prop in arguments[i]) {
979
- target[prop] = arguments[i][prop];
980
- }
981
- }
982
- return target;
983
- },
984
-
985
- nextTick: function nextTick(callback) {
986
- if (typeof process != "undefined" && process.nextTick) {
987
- return process.nextTick(callback);
988
- }
989
- setTimeout(callback, 0);
990
- },
991
-
992
- functionName: function functionName(func) {
993
- if (!func) return "";
994
- if (func.displayName) return func.displayName;
995
- if (func.name) return func.name;
996
- var matches = func.toString().match(/function\s+([^\(]+)/m);
997
- return matches && matches[1] || "";
998
- },
999
-
1000
- isNode: function isNode(obj) {
1001
- if (!div) return false;
1002
- try {
1003
- obj.appendChild(div);
1004
- obj.removeChild(div);
1005
- } catch (e) {
1006
- return false;
1007
- }
1008
- return true;
1009
- },
1010
-
1011
- isElement: function isElement(obj) {
1012
- return obj && obj.nodeType === 1 && buster.isNode(obj);
1013
- },
1014
-
1015
- isArray: function isArray(arr) {
1016
- return Object.prototype.toString.call(arr) == "[object Array]";
1017
- },
1018
-
1019
- flatten: function flatten(arr) {
1020
- var result = [], arr = arr || [];
1021
- for (var i = 0, l = arr.length; i < l; ++i) {
1022
- result = result.concat(buster.isArray(arr[i]) ? flatten(arr[i]) : arr[i]);
1023
- }
1024
- return result;
1025
- },
1026
-
1027
- each: function each(arr, callback) {
1028
- for (var i = 0, l = arr.length; i < l; ++i) {
1029
- callback(arr[i]);
1030
- }
1031
- },
1032
-
1033
- map: function map(arr, callback) {
1034
- var results = [];
1035
- for (var i = 0, l = arr.length; i < l; ++i) {
1036
- results.push(callback(arr[i]));
1037
- }
1038
- return results;
1039
- },
1040
-
1041
- parallel: function parallel(fns, callback) {
1042
- function cb(err, res) {
1043
- if (typeof callback == "function") {
1044
- callback(err, res);
1045
- callback = null;
1046
- }
1047
- }
1048
-
1049
- if (fns.length == 0) {
1050
- return cb(null, []);
1051
- }
1052
- var remaining = fns.length, results = [];
1053
-
1054
- function makeDone(num) {
1055
- return function done(err, result) {
1056
- if (err) {
1057
- return cb(err);
1058
- }
1059
- results[num] = result;
1060
- if (--remaining == 0) {
1061
- cb(null, results);
1062
- }
1063
- };
1064
- }
1065
-
1066
- for (var i = 0, l = fns.length; i < l; ++i) {
1067
- fns[i](makeDone(i));
1068
- }
1069
- },
1070
-
1071
- series: function series(fns, callback) {
1072
- function cb(err, res) {
1073
- if (typeof callback == "function") {
1074
- callback(err, res);
1075
- }
1076
- }
1077
-
1078
- var remaining = fns.slice();
1079
- var results = [];
1080
-
1081
- function callNext() {
1082
- if (remaining.length == 0) return cb(null, results);
1083
- var promise = remaining.shift()(next);
1084
- if (promise && typeof promise.then == "function") {
1085
- promise.then(buster.partial(next, null), next);
1086
- }
1087
- }
1088
-
1089
- function next(err, result) {
1090
- if (err) return cb(err);
1091
- results.push(result);
1092
- callNext();
1093
- }
1094
-
1095
- callNext();
1096
- },
1097
-
1098
- countdown: function countdown(num, done) {
1099
- return function () {
1100
- if (--num == 0) done();
1101
- };
1102
- }
1103
- };
1104
-
1105
- if (typeof process === "object" &&
1106
- typeof require === "function" && typeof module === "object") {
1107
- var crypto = require("crypto");
1108
- var path = require("path");
1109
-
1110
- buster.tmpFile = function (fileName) {
1111
- var hashed = crypto.createHash("sha1");
1112
- hashed.update(fileName);
1113
- var tmpfileName = hashed.digest("hex");
1114
-
1115
- if (process.platform == "win32") {
1116
- return path.join(process.env["TEMP"], tmpfileName);
1117
- } else {
1118
- return path.join("/tmp", tmpfileName);
1119
- }
1120
- };
1121
- }
1122
-
1123
- if (Array.prototype.some) {
1124
- buster.some = function (arr, fn, thisp) {
1125
- return arr.some(fn, thisp);
1126
- };
1127
- } else {
1128
- // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
1129
- buster.some = function (arr, fun, thisp) {
1130
- if (arr == null) {
1131
- throw new TypeError();
1132
- }
1133
- arr = Object(arr);
1134
- var len = arr.length >>> 0;
1135
- if (typeof fun !== "function") {
1136
- throw new TypeError();
1137
- }
1138
-
1139
- for (var i = 0; i < len; i++) {
1140
- if (arr.hasOwnProperty(i) && fun.call(thisp, arr[i], i, arr)) {
1141
- return true;
1142
- }
1143
- }
1144
-
1145
- return false;
1146
- };
1147
- }
1148
-
1149
- if (Array.prototype.filter) {
1150
- buster.filter = function (arr, fn, thisp) {
1151
- return arr.filter(fn, thisp);
1152
- };
1153
- } else {
1154
- // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
1155
- buster.filter = function (fn, thisp) {
1156
- if (this == null) {
1157
- throw new TypeError();
1158
- }
1159
-
1160
- var t = Object(this);
1161
- var len = t.length >>> 0;
1162
- if (typeof fn != "function") {
1163
- throw new TypeError();
1164
- }
1165
-
1166
- var res = [];
1167
- for (var i = 0; i < len; i++) {
1168
- if (i in t) {
1169
- var val = t[i]; // in case fun mutates this
1170
- if (fn.call(thisp, val, i, t)) {
1171
- res.push(val);
1172
- }
1173
- }
1174
- }
1175
-
1176
- return res;
1177
- };
1178
- }
1179
-
1180
- if (isNode) {
1181
- module.exports = buster;
1182
- buster.eventEmitter = require("./buster-event-emitter");
1183
- Object.defineProperty(buster, "defineVersionGetter", {
1184
- get: function () {
1185
- return require("./define-version-getter");
1186
- }
1187
- });
1188
- }
1189
-
1190
- return buster.extend(B || {}, buster);
1191
- }(setTimeout, buster));
1192
- if (typeof buster === "undefined") {
1193
- var buster = {};
1194
- }
1195
-
1196
- if (typeof module === "object" && typeof require === "function") {
1197
- buster = require("buster-core");
1198
- }
1199
-
1200
- buster.format = buster.format || {};
1201
- buster.format.excludeConstructors = ["Object", /^.$/];
1202
- buster.format.quoteStrings = true;
1203
-
1204
- buster.format.ascii = (function () {
1205
-
1206
- var hasOwn = Object.prototype.hasOwnProperty;
1207
-
1208
- var specialObjects = [];
1209
- if (typeof global != "undefined") {
1210
- specialObjects.push({ obj: global, value: "[object global]" });
1211
- }
1212
- if (typeof document != "undefined") {
1213
- specialObjects.push({ obj: document, value: "[object HTMLDocument]" });
1214
- }
1215
- if (typeof window != "undefined") {
1216
- specialObjects.push({ obj: window, value: "[object Window]" });
1217
- }
1218
-
1219
- function keys(object) {
1220
- var k = Object.keys && Object.keys(object) || [];
1221
-
1222
- if (k.length == 0) {
1223
- for (var prop in object) {
1224
- if (hasOwn.call(object, prop)) {
1225
- k.push(prop);
1226
- }
1227
- }
1228
- }
1229
-
1230
- return k.sort();
1231
- }
1232
-
1233
- function isCircular(object, objects) {
1234
- if (typeof object != "object") {
1235
- return false;
1236
- }
1237
-
1238
- for (var i = 0, l = objects.length; i < l; ++i) {
1239
- if (objects[i] === object) {
1240
- return true;
1241
- }
1242
- }
1243
-
1244
- return false;
1245
- }
1246
-
1247
- function ascii(object, processed, indent) {
1248
- if (typeof object == "string") {
1249
- var quote = typeof this.quoteStrings != "boolean" || this.quoteStrings;
1250
- return processed || quote ? '"' + object + '"' : object;
1251
- }
1252
-
1253
- if (typeof object == "function" && !(object instanceof RegExp)) {
1254
- return ascii.func(object);
1255
- }
1256
-
1257
- processed = processed || [];
1258
-
1259
- if (isCircular(object, processed)) {
1260
- return "[Circular]";
1261
- }
1262
-
1263
- if (Object.prototype.toString.call(object) == "[object Array]") {
1264
- return ascii.array.call(this, object, processed);
1265
- }
1266
-
1267
- if (!object) {
1268
- return "" + object;
1269
- }
1270
-
1271
- if (buster.isElement(object)) {
1272
- return ascii.element(object);
1273
- }
1274
-
1275
- if (typeof object.toString == "function" &&
1276
- object.toString !== Object.prototype.toString) {
1277
- return object.toString();
1278
- }
1279
-
1280
- for (var i = 0, l = specialObjects.length; i < l; i++) {
1281
- if (object === specialObjects[i].obj) {
1282
- return specialObjects[i].value;
1283
- }
1284
- }
1285
-
1286
- return ascii.object.call(this, object, processed, indent);
1287
- }
1288
-
1289
- ascii.func = function (func) {
1290
- return "function " + buster.functionName(func) + "() {}";
1291
- };
1292
-
1293
- ascii.array = function (array, processed) {
1294
- processed = processed || [];
1295
- processed.push(array);
1296
- var pieces = [];
1297
-
1298
- for (var i = 0, l = array.length; i < l; ++i) {
1299
- pieces.push(ascii.call(this, array[i], processed));
1300
- }
1301
-
1302
- return "[" + pieces.join(", ") + "]";
1303
- };
1304
-
1305
- ascii.object = function (object, processed, indent) {
1306
- processed = processed || [];
1307
- processed.push(object);
1308
- indent = indent || 0;
1309
- var pieces = [], properties = keys(object), prop, str, obj;
1310
- var is = "";
1311
- var length = 3;
1312
-
1313
- for (var i = 0, l = indent; i < l; ++i) {
1314
- is += " ";
1315
- }
1316
-
1317
- for (i = 0, l = properties.length; i < l; ++i) {
1318
- prop = properties[i];
1319
- obj = object[prop];
1320
-
1321
- if (isCircular(obj, processed)) {
1322
- str = "[Circular]";
1323
- } else {
1324
- str = ascii.call(this, obj, processed, indent + 2);
1325
- }
1326
-
1327
- str = (/\s/.test(prop) ? '"' + prop + '"' : prop) + ": " + str;
1328
- length += str.length;
1329
- pieces.push(str);
1330
- }
1331
-
1332
- var cons = ascii.constructorName.call(this, object);
1333
- var prefix = cons ? "[" + cons + "] " : ""
1334
-
1335
- return (length + indent) > 80 ?
1336
- prefix + "{\n " + is + pieces.join(",\n " + is) + "\n" + is + "}" :
1337
- prefix + "{ " + pieces.join(", ") + " }";
1338
- };
1339
-
1340
- ascii.element = function (element) {
1341
- var tagName = element.tagName.toLowerCase();
1342
- var attrs = element.attributes, attribute, pairs = [], attrName;
1343
-
1344
- for (var i = 0, l = attrs.length; i < l; ++i) {
1345
- attribute = attrs.item(i);
1346
- attrName = attribute.nodeName.toLowerCase().replace("html:", "");
1347
-
1348
- if (attrName == "contenteditable" && attribute.nodeValue == "inherit") {
1349
- continue;
1350
- }
1351
-
1352
- if (!!attribute.nodeValue) {
1353
- pairs.push(attrName + "=\"" + attribute.nodeValue + "\"");
1354
- }
1355
- }
1356
-
1357
- var formatted = "<" + tagName + (pairs.length > 0 ? " " : "");
1358
- var content = element.innerHTML;
1359
-
1360
- if (content.length > 20) {
1361
- content = content.substr(0, 20) + "[...]";
1362
- }
1363
-
1364
- var res = formatted + pairs.join(" ") + ">" + content + "</" + tagName + ">";
1365
-
1366
- return res.replace(/ contentEditable="inherit"/, "");
1367
- };
1368
-
1369
- ascii.constructorName = function (object) {
1370
- var name = buster.functionName(object && object.constructor);
1371
- var excludes = this.excludeConstructors || buster.format.excludeConstructors || [];
1372
-
1373
- for (var i = 0, l = excludes.length; i < l; ++i) {
1374
- if (typeof excludes[i] == "string" && excludes[i] == name) {
1375
- return "";
1376
- } else if (excludes[i].test && excludes[i].test(name)) {
1377
- return "";
1378
- }
1379
- }
1380
-
1381
- return name;
1382
- };
1383
-
1384
- return ascii;
1385
- }());
1386
-
1387
- if (typeof module != "undefined") {
1388
- module.exports = buster.format;
1389
- }
1390
- /*jslint eqeqeq: false, onevar: false, forin: true, nomen: false, regexp: false, plusplus: false*/
1391
- /*global module, require, __dirname, document*/
1392
- /**
1393
- * Sinon core utilities. For internal use only.
1394
- *
1395
- * @author Christian Johansen (christian@cjohansen.no)
1396
- * @license BSD
1397
- *
1398
- * Copyright (c) 2010-2013 Christian Johansen
1399
- */
1400
-
1401
- var sinon = (function (buster) {
1402
- var div = typeof document != "undefined" && document.createElement("div");
1403
- var hasOwn = Object.prototype.hasOwnProperty;
1404
-
1405
- function isDOMNode(obj) {
1406
- var success = false;
1407
-
1408
- try {
1409
- obj.appendChild(div);
1410
- success = div.parentNode == obj;
1411
- } catch (e) {
1412
- return false;
1413
- } finally {
1414
- try {
1415
- obj.removeChild(div);
1416
- } catch (e) {
1417
- // Remove failed, not much we can do about that
1418
- }
1419
- }
1420
-
1421
- return success;
1422
- }
1423
-
1424
- function isElement(obj) {
1425
- return div && obj && obj.nodeType === 1 && isDOMNode(obj);
1426
- }
1427
-
1428
- function isFunction(obj) {
1429
- return typeof obj === "function" || !!(obj && obj.constructor && obj.call && obj.apply);
1430
- }
1431
-
1432
- function mirrorProperties(target, source) {
1433
- for (var prop in source) {
1434
- if (!hasOwn.call(target, prop)) {
1435
- target[prop] = source[prop];
1436
- }
1437
- }
1438
- }
1439
-
1440
- var sinon = {
1441
- wrapMethod: function wrapMethod(object, property, method) {
1442
- if (!object) {
1443
- throw new TypeError("Should wrap property of object");
1444
- }
1445
-
1446
- if (typeof method != "function") {
1447
- throw new TypeError("Method wrapper should be function");
1448
- }
1449
-
1450
- var wrappedMethod = object[property];
1451
-
1452
- if (!isFunction(wrappedMethod)) {
1453
- throw new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
1454
- property + " as function");
1455
- }
1456
-
1457
- if (wrappedMethod.restore && wrappedMethod.restore.sinon) {
1458
- throw new TypeError("Attempted to wrap " + property + " which is already wrapped");
1459
- }
1460
-
1461
- if (wrappedMethod.calledBefore) {
1462
- var verb = !!wrappedMethod.returns ? "stubbed" : "spied on";
1463
- throw new TypeError("Attempted to wrap " + property + " which is already " + verb);
1464
- }
1465
-
1466
- // IE 8 does not support hasOwnProperty on the window object.
1467
- var owned = hasOwn.call(object, property);
1468
- object[property] = method;
1469
- method.displayName = property;
1470
-
1471
- method.restore = function () {
1472
- // For prototype properties try to reset by delete first.
1473
- // If this fails (ex: localStorage on mobile safari) then force a reset
1474
- // via direct assignment.
1475
- if (!owned) {
1476
- delete object[property];
1477
- }
1478
- if (object[property] === method) {
1479
- object[property] = wrappedMethod;
1480
- }
1481
- };
1482
-
1483
- method.restore.sinon = true;
1484
- mirrorProperties(method, wrappedMethod);
1485
-
1486
- return method;
1487
- },
1488
-
1489
- extend: function extend(target) {
1490
- for (var i = 1, l = arguments.length; i < l; i += 1) {
1491
- for (var prop in arguments[i]) {
1492
- if (arguments[i].hasOwnProperty(prop)) {
1493
- target[prop] = arguments[i][prop];
1494
- }
1495
-
1496
- // DONT ENUM bug, only care about toString
1497
- if (arguments[i].hasOwnProperty("toString") &&
1498
- arguments[i].toString != target.toString) {
1499
- target.toString = arguments[i].toString;
1500
- }
1501
- }
1502
- }
1503
-
1504
- return target;
1505
- },
1506
-
1507
- create: function create(proto) {
1508
- var F = function () {
1509
- };
1510
- F.prototype = proto;
1511
- return new F();
1512
- },
1513
-
1514
- deepEqual: function deepEqual(a, b) {
1515
- if (sinon.match && sinon.match.isMatcher(a)) {
1516
- return a.test(b);
1517
- }
1518
- if (typeof a != "object" || typeof b != "object") {
1519
- return a === b;
1520
- }
1521
-
1522
- if (isElement(a) || isElement(b)) {
1523
- return a === b;
1524
- }
1525
-
1526
- if (a === b) {
1527
- return true;
1528
- }
1529
-
1530
- if ((a === null && b !== null) || (a !== null && b === null)) {
1531
- return false;
1532
- }
1533
-
1534
- var aString = Object.prototype.toString.call(a);
1535
- if (aString != Object.prototype.toString.call(b)) {
1536
- return false;
1537
- }
1538
-
1539
- if (aString == "[object Array]") {
1540
- if (a.length !== b.length) {
1541
- return false;
1542
- }
1543
-
1544
- for (var i = 0, l = a.length; i < l; i += 1) {
1545
- if (!deepEqual(a[i], b[i])) {
1546
- return false;
1547
- }
1548
- }
1549
-
1550
- return true;
1551
- }
1552
-
1553
- var prop, aLength = 0, bLength = 0;
1554
-
1555
- for (prop in a) {
1556
- aLength += 1;
1557
-
1558
- if (!deepEqual(a[prop], b[prop])) {
1559
- return false;
1560
- }
1561
- }
1562
-
1563
- for (prop in b) {
1564
- bLength += 1;
1565
- }
1566
-
1567
- if (aLength != bLength) {
1568
- return false;
1569
- }
1570
-
1571
- return true;
1572
- },
1573
-
1574
- functionName: function functionName(func) {
1575
- var name = func.displayName || func.name;
1576
-
1577
- // Use function decomposition as a last resort to get function
1578
- // name. Does not rely on function decomposition to work - if it
1579
- // doesn't debugging will be slightly less informative
1580
- // (i.e. toString will say 'spy' rather than 'myFunc').
1581
- if (!name) {
1582
- var matches = func.toString().match(/function ([^\s\(]+)/);
1583
- name = matches && matches[1];
1584
- }
1585
-
1586
- return name;
1587
- },
1588
-
1589
- functionToString: function toString() {
1590
- if (this.getCall && this.callCount) {
1591
- var thisValue, prop, i = this.callCount;
1592
-
1593
- while (i--) {
1594
- thisValue = this.getCall(i).thisValue;
1595
-
1596
- for (prop in thisValue) {
1597
- if (thisValue[prop] === this) {
1598
- return prop;
1599
- }
1600
- }
1601
- }
1602
- }
1603
-
1604
- return this.displayName || "sinon fake";
1605
- },
1606
-
1607
- getConfig: function (custom) {
1608
- var config = {};
1609
- custom = custom || {};
1610
- var defaults = sinon.defaultConfig;
1611
-
1612
- for (var prop in defaults) {
1613
- if (defaults.hasOwnProperty(prop)) {
1614
- config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaults[prop];
1615
- }
1616
- }
1617
-
1618
- return config;
1619
- },
1620
-
1621
- format: function (val) {
1622
- return "" + val;
1623
- },
1624
-
1625
- defaultConfig: {
1626
- injectIntoThis: true,
1627
- injectInto: null,
1628
- properties: ["spy", "stub", "mock", "clock", "server", "requests"],
1629
- useFakeTimers: true,
1630
- useFakeServer: true
1631
- },
1632
-
1633
- timesInWords: function timesInWords(count) {
1634
- return count == 1 && "once" ||
1635
- count == 2 && "twice" ||
1636
- count == 3 && "thrice" ||
1637
- (count || 0) + " times";
1638
- },
1639
-
1640
- calledInOrder: function (spies) {
1641
- for (var i = 1, l = spies.length; i < l; i++) {
1642
- if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) {
1643
- return false;
1644
- }
1645
- }
1646
-
1647
- return true;
1648
- },
1649
-
1650
- orderByFirstCall: function (spies) {
1651
- return spies.sort(function (a, b) {
1652
- // uuid, won't ever be equal
1653
- var aCall = a.getCall(0);
1654
- var bCall = b.getCall(0);
1655
- var aId = aCall && aCall.callId || -1;
1656
- var bId = bCall && bCall.callId || -1;
1657
-
1658
- return aId < bId ? -1 : 1;
1659
- });
1660
- },
1661
-
1662
- log: function () {
1663
- },
1664
-
1665
- logError: function (label, err) {
1666
- var msg = label + " threw exception: "
1667
- sinon.log(msg + "[" + err.name + "] " + err.message);
1668
- if (err.stack) {
1669
- sinon.log(err.stack);
1670
- }
1671
-
1672
- setTimeout(function () {
1673
- err.message = msg + err.message;
1674
- throw err;
1675
- }, 0);
1676
- },
1677
-
1678
- typeOf: function (value) {
1679
- if (value === null) {
1680
- return "null";
1681
- }
1682
- else if (value === undefined) {
1683
- return "undefined";
1684
- }
1685
- var string = Object.prototype.toString.call(value);
1686
- return string.substring(8, string.length - 1).toLowerCase();
1687
- },
1688
-
1689
- createStubInstance: function (constructor) {
1690
- if (typeof constructor !== "function") {
1691
- throw new TypeError("The constructor should be a function.");
1692
- }
1693
- return sinon.stub(sinon.create(constructor.prototype));
1694
- }
1695
- };
1696
-
1697
- var isNode = typeof module == "object" && typeof require == "function";
1698
-
1699
- if (isNode) {
1700
- try {
1701
- buster = { format: require("buster-format") };
1702
- } catch (e) {
1703
- }
1704
- module.exports = sinon;
1705
- module.exports.spy = require("./sinon/spy");
1706
- module.exports.stub = require("./sinon/stub");
1707
- module.exports.mock = require("./sinon/mock");
1708
- module.exports.collection = require("./sinon/collection");
1709
- module.exports.assert = require("./sinon/assert");
1710
- module.exports.sandbox = require("./sinon/sandbox");
1711
- module.exports.test = require("./sinon/test");
1712
- module.exports.testCase = require("./sinon/test_case");
1713
- module.exports.assert = require("./sinon/assert");
1714
- module.exports.match = require("./sinon/match");
1715
- }
1716
-
1717
- if (buster) {
1718
- var formatter = sinon.create(buster.format);
1719
- formatter.quoteStrings = false;
1720
- sinon.format = function () {
1721
- return formatter.ascii.apply(formatter, arguments);
1722
- };
1723
- } else if (isNode) {
1724
- try {
1725
- var util = require("util");
1726
- sinon.format = function (value) {
1727
- return typeof value == "object" && value.toString === Object.prototype.toString ? util.inspect(value) : value;
1728
- };
1729
- } catch (e) {
1730
- /* Node, but no util module - would be very old, but better safe than
1731
- sorry */
1732
- }
1733
- }
1734
-
1735
- return sinon;
1736
- }(typeof buster == "object" && buster));
1737
-
1738
- /* @depend ../sinon.js */
1739
- /*jslint eqeqeq: false, onevar: false, plusplus: false*/
1740
- /*global module, require, sinon*/
1741
- /**
1742
- * Match functions
1743
- *
1744
- * @author Maximilian Antoni (mail@maxantoni.de)
1745
- * @license BSD
1746
- *
1747
- * Copyright (c) 2012 Maximilian Antoni
1748
- */
1749
-
1750
- (function (sinon) {
1751
- var commonJSModule = typeof module == "object" && typeof require == "function";
1752
-
1753
- if (!sinon && commonJSModule) {
1754
- sinon = require("../sinon");
1755
- }
1756
-
1757
- if (!sinon) {
1758
- return;
1759
- }
1760
-
1761
- function assertType(value, type, name) {
1762
- var actual = sinon.typeOf(value);
1763
- if (actual !== type) {
1764
- throw new TypeError("Expected type of " + name + " to be " +
1765
- type + ", but was " + actual);
1766
- }
1767
- }
1768
-
1769
- var matcher = {
1770
- toString: function () {
1771
- return this.message;
1772
- }
1773
- };
1774
-
1775
- function isMatcher(object) {
1776
- return matcher.isPrototypeOf(object);
1777
- }
1778
-
1779
- function matchObject(expectation, actual) {
1780
- if (actual === null || actual === undefined) {
1781
- return false;
1782
- }
1783
- for (var key in expectation) {
1784
- if (expectation.hasOwnProperty(key)) {
1785
- var exp = expectation[key];
1786
- var act = actual[key];
1787
- if (match.isMatcher(exp)) {
1788
- if (!exp.test(act)) {
1789
- return false;
1790
- }
1791
- } else if (sinon.typeOf(exp) === "object") {
1792
- if (!matchObject(exp, act)) {
1793
- return false;
1794
- }
1795
- } else if (!sinon.deepEqual(exp, act)) {
1796
- return false;
1797
- }
1798
- }
1799
- }
1800
- return true;
1801
- }
1802
-
1803
- matcher.or = function (m2) {
1804
- if (!isMatcher(m2)) {
1805
- throw new TypeError("Matcher expected");
1806
- }
1807
- var m1 = this;
1808
- var or = sinon.create(matcher);
1809
- or.test = function (actual) {
1810
- return m1.test(actual) || m2.test(actual);
1811
- };
1812
- or.message = m1.message + ".or(" + m2.message + ")";
1813
- return or;
1814
- };
1815
-
1816
- matcher.and = function (m2) {
1817
- if (!isMatcher(m2)) {
1818
- throw new TypeError("Matcher expected");
1819
- }
1820
- var m1 = this;
1821
- var and = sinon.create(matcher);
1822
- and.test = function (actual) {
1823
- return m1.test(actual) && m2.test(actual);
1824
- };
1825
- and.message = m1.message + ".and(" + m2.message + ")";
1826
- return and;
1827
- };
1828
-
1829
- var match = function (expectation, message) {
1830
- var m = sinon.create(matcher);
1831
- var type = sinon.typeOf(expectation);
1832
- switch (type) {
1833
- case "object":
1834
- if (typeof expectation.test === "function") {
1835
- m.test = function (actual) {
1836
- return expectation.test(actual) === true;
1837
- };
1838
- m.message = "match(" + sinon.functionName(expectation.test) + ")";
1839
- return m;
1840
- }
1841
- var str = [];
1842
- for (var key in expectation) {
1843
- if (expectation.hasOwnProperty(key)) {
1844
- str.push(key + ": " + expectation[key]);
1845
- }
1846
- }
1847
- m.test = function (actual) {
1848
- return matchObject(expectation, actual);
1849
- };
1850
- m.message = "match(" + str.join(", ") + ")";
1851
- break;
1852
- case "number":
1853
- m.test = function (actual) {
1854
- return expectation == actual;
1855
- };
1856
- break;
1857
- case "string":
1858
- m.test = function (actual) {
1859
- if (typeof actual !== "string") {
1860
- return false;
1861
- }
1862
- return actual.indexOf(expectation) !== -1;
1863
- };
1864
- m.message = "match(\"" + expectation + "\")";
1865
- break;
1866
- case "regexp":
1867
- m.test = function (actual) {
1868
- if (typeof actual !== "string") {
1869
- return false;
1870
- }
1871
- return expectation.test(actual);
1872
- };
1873
- break;
1874
- case "function":
1875
- m.test = expectation;
1876
- if (message) {
1877
- m.message = message;
1878
- } else {
1879
- m.message = "match(" + sinon.functionName(expectation) + ")";
1880
- }
1881
- break;
1882
- default:
1883
- m.test = function (actual) {
1884
- return sinon.deepEqual(expectation, actual);
1885
- };
1886
- }
1887
- if (!m.message) {
1888
- m.message = "match(" + expectation + ")";
1889
- }
1890
- return m;
1891
- };
1892
-
1893
- match.isMatcher = isMatcher;
1894
-
1895
- match.any = match(function () {
1896
- return true;
1897
- }, "any");
1898
-
1899
- match.defined = match(function (actual) {
1900
- return actual !== null && actual !== undefined;
1901
- }, "defined");
1902
-
1903
- match.truthy = match(function (actual) {
1904
- return !!actual;
1905
- }, "truthy");
1906
-
1907
- match.falsy = match(function (actual) {
1908
- return !actual;
1909
- }, "falsy");
1910
-
1911
- match.same = function (expectation) {
1912
- return match(function (actual) {
1913
- return expectation === actual;
1914
- }, "same(" + expectation + ")");
1915
- };
1916
-
1917
- match.typeOf = function (type) {
1918
- assertType(type, "string", "type");
1919
- return match(function (actual) {
1920
- return sinon.typeOf(actual) === type;
1921
- }, "typeOf(\"" + type + "\")");
1922
- };
1923
-
1924
- match.instanceOf = function (type) {
1925
- assertType(type, "function", "type");
1926
- return match(function (actual) {
1927
- return actual instanceof type;
1928
- }, "instanceOf(" + sinon.functionName(type) + ")");
1929
- };
1930
-
1931
- function createPropertyMatcher(propertyTest, messagePrefix) {
1932
- return function (property, value) {
1933
- assertType(property, "string", "property");
1934
- var onlyProperty = arguments.length === 1;
1935
- var message = messagePrefix + "(\"" + property + "\"";
1936
- if (!onlyProperty) {
1937
- message += ", " + value;
1938
- }
1939
- message += ")";
1940
- return match(function (actual) {
1941
- if (actual === undefined || actual === null ||
1942
- !propertyTest(actual, property)) {
1943
- return false;
1944
- }
1945
- return onlyProperty || sinon.deepEqual(value, actual[property]);
1946
- }, message);
1947
- };
1948
- }
1949
-
1950
- match.has = createPropertyMatcher(function (actual, property) {
1951
- if (typeof actual === "object") {
1952
- return property in actual;
1953
- }
1954
- return actual[property] !== undefined;
1955
- }, "has");
1956
-
1957
- match.hasOwn = createPropertyMatcher(function (actual, property) {
1958
- return actual.hasOwnProperty(property);
1959
- }, "hasOwn");
1960
-
1961
- match.bool = match.typeOf("boolean");
1962
- match.number = match.typeOf("number");
1963
- match.string = match.typeOf("string");
1964
- match.object = match.typeOf("object");
1965
- match.func = match.typeOf("function");
1966
- match.array = match.typeOf("array");
1967
- match.regexp = match.typeOf("regexp");
1968
- match.date = match.typeOf("date");
1969
-
1970
- if (commonJSModule) {
1971
- module.exports = match;
1972
- } else {
1973
- sinon.match = match;
1974
- }
1975
- }(typeof sinon == "object" && sinon || null));
1976
-
1977
- /**
1978
- * @depend ../sinon.js
1979
- * @depend match.js
1980
- */
1981
- /*jslint eqeqeq: false, onevar: false, plusplus: false*/
1982
- /*global module, require, sinon*/
1983
- /**
1984
- * Spy functions
1985
- *
1986
- * @author Christian Johansen (christian@cjohansen.no)
1987
- * @license BSD
1988
- *
1989
- * Copyright (c) 2010-2013 Christian Johansen
1990
- */
1991
-
1992
- (function (sinon) {
1993
- var commonJSModule = typeof module == "object" && typeof require == "function";
1994
- var spyCall;
1995
- var callId = 0;
1996
- var push = [].push;
1997
- var slice = Array.prototype.slice;
1998
-
1999
- if (!sinon && commonJSModule) {
2000
- sinon = require("../sinon");
2001
- }
2002
-
2003
- if (!sinon) {
2004
- return;
2005
- }
2006
-
2007
- function spy(object, property) {
2008
- if (!property && typeof object == "function") {
2009
- return spy.create(object);
2010
- }
2011
-
2012
- if (!object && !property) {
2013
- return spy.create(function () {
2014
- });
2015
- }
2016
-
2017
- var method = object[property];
2018
- return sinon.wrapMethod(object, property, spy.create(method));
2019
- }
2020
-
2021
- sinon.extend(spy, (function () {
2022
-
2023
- function delegateToCalls(api, method, matchAny, actual, notCalled) {
2024
- api[method] = function () {
2025
- if (!this.called) {
2026
- if (notCalled) {
2027
- return notCalled.apply(this, arguments);
2028
- }
2029
- return false;
2030
- }
2031
-
2032
- var currentCall;
2033
- var matches = 0;
2034
-
2035
- for (var i = 0, l = this.callCount; i < l; i += 1) {
2036
- currentCall = this.getCall(i);
2037
-
2038
- if (currentCall[actual || method].apply(currentCall, arguments)) {
2039
- matches += 1;
2040
-
2041
- if (matchAny) {
2042
- return true;
2043
- }
2044
- }
2045
- }
2046
-
2047
- return matches === this.callCount;
2048
- };
2049
- }
2050
-
2051
- function matchingFake(fakes, args, strict) {
2052
- if (!fakes) {
2053
- return;
2054
- }
2055
-
2056
- var alen = args.length;
2057
-
2058
- for (var i = 0, l = fakes.length; i < l; i++) {
2059
- if (fakes[i].matches(args, strict)) {
2060
- return fakes[i];
2061
- }
2062
- }
2063
- }
2064
-
2065
- function incrementCallCount() {
2066
- this.called = true;
2067
- this.callCount += 1;
2068
- this.notCalled = false;
2069
- this.calledOnce = this.callCount == 1;
2070
- this.calledTwice = this.callCount == 2;
2071
- this.calledThrice = this.callCount == 3;
2072
- }
2073
-
2074
- function createCallProperties() {
2075
- this.firstCall = this.getCall(0);
2076
- this.secondCall = this.getCall(1);
2077
- this.thirdCall = this.getCall(2);
2078
- this.lastCall = this.getCall(this.callCount - 1);
2079
- }
2080
-
2081
- var vars = "a,b,c,d,e,f,g,h,i,j,k,l";
2082
-
2083
- function createProxy(func) {
2084
- // Retain the function length:
2085
- var p;
2086
- if (func.length) {
2087
- eval("p = (function proxy(" + vars.substring(0, func.length * 2 - 1) +
2088
- ") { return p.invoke(func, this, slice.call(arguments)); });");
2089
- }
2090
- else {
2091
- p = function proxy() {
2092
- return p.invoke(func, this, slice.call(arguments));
2093
- };
2094
- }
2095
- return p;
2096
- }
2097
-
2098
- var uuid = 0;
2099
-
2100
- // Public API
2101
- var spyApi = {
2102
- reset: function () {
2103
- this.called = false;
2104
- this.notCalled = true;
2105
- this.calledOnce = false;
2106
- this.calledTwice = false;
2107
- this.calledThrice = false;
2108
- this.callCount = 0;
2109
- this.firstCall = null;
2110
- this.secondCall = null;
2111
- this.thirdCall = null;
2112
- this.lastCall = null;
2113
- this.args = [];
2114
- this.returnValues = [];
2115
- this.thisValues = [];
2116
- this.exceptions = [];
2117
- this.callIds = [];
2118
- if (this.fakes) {
2119
- for (var i = 0; i < this.fakes.length; i++) {
2120
- this.fakes[i].reset();
2121
- }
2122
- }
2123
- },
2124
-
2125
- create: function create(func) {
2126
- var name;
2127
-
2128
- if (typeof func != "function") {
2129
- func = function () {
2130
- };
2131
- } else {
2132
- name = sinon.functionName(func);
2133
- }
2134
-
2135
- var proxy = createProxy(func);
2136
-
2137
- sinon.extend(proxy, spy);
2138
- delete proxy.create;
2139
- sinon.extend(proxy, func);
2140
-
2141
- proxy.reset();
2142
- proxy.prototype = func.prototype;
2143
- proxy.displayName = name || "spy";
2144
- proxy.toString = sinon.functionToString;
2145
- proxy._create = sinon.spy.create;
2146
- proxy.id = "spy#" + uuid++;
2147
-
2148
- return proxy;
2149
- },
2150
-
2151
- invoke: function invoke(func, thisValue, args) {
2152
- var matching = matchingFake(this.fakes, args);
2153
- var exception, returnValue;
2154
-
2155
- incrementCallCount.call(this);
2156
- push.call(this.thisValues, thisValue);
2157
- push.call(this.args, args);
2158
- push.call(this.callIds, callId++);
2159
-
2160
- try {
2161
- if (matching) {
2162
- returnValue = matching.invoke(func, thisValue, args);
2163
- } else {
2164
- returnValue = (this.func || func).apply(thisValue, args);
2165
- }
2166
- } catch (e) {
2167
- push.call(this.returnValues, undefined);
2168
- exception = e;
2169
- throw e;
2170
- } finally {
2171
- push.call(this.exceptions, exception);
2172
- }
2173
-
2174
- push.call(this.returnValues, returnValue);
2175
-
2176
- createCallProperties.call(this);
2177
-
2178
- return returnValue;
2179
- },
2180
-
2181
- getCall: function getCall(i) {
2182
- if (i < 0 || i >= this.callCount) {
2183
- return null;
2184
- }
2185
-
2186
- return spyCall.create(this, this.thisValues[i], this.args[i],
2187
- this.returnValues[i], this.exceptions[i],
2188
- this.callIds[i]);
2189
- },
2190
-
2191
- calledBefore: function calledBefore(spyFn) {
2192
- if (!this.called) {
2193
- return false;
2194
- }
2195
-
2196
- if (!spyFn.called) {
2197
- return true;
2198
- }
2199
-
2200
- return this.callIds[0] < spyFn.callIds[spyFn.callIds.length - 1];
2201
- },
2202
-
2203
- calledAfter: function calledAfter(spyFn) {
2204
- if (!this.called || !spyFn.called) {
2205
- return false;
2206
- }
2207
-
2208
- return this.callIds[this.callCount - 1] > spyFn.callIds[spyFn.callCount - 1];
2209
- },
2210
-
2211
- withArgs: function () {
2212
- var args = slice.call(arguments);
2213
-
2214
- if (this.fakes) {
2215
- var match = matchingFake(this.fakes, args, true);
2216
-
2217
- if (match) {
2218
- return match;
2219
- }
2220
- } else {
2221
- this.fakes = [];
2222
- }
2223
-
2224
- var original = this;
2225
- var fake = this._create();
2226
- fake.matchingAguments = args;
2227
- push.call(this.fakes, fake);
2228
-
2229
- fake.withArgs = function () {
2230
- return original.withArgs.apply(original, arguments);
2231
- };
2232
-
2233
- for (var i = 0; i < this.args.length; i++) {
2234
- if (fake.matches(this.args[i])) {
2235
- incrementCallCount.call(fake);
2236
- push.call(fake.thisValues, this.thisValues[i]);
2237
- push.call(fake.args, this.args[i]);
2238
- push.call(fake.returnValues, this.returnValues[i]);
2239
- push.call(fake.exceptions, this.exceptions[i]);
2240
- push.call(fake.callIds, this.callIds[i]);
2241
- }
2242
- }
2243
- createCallProperties.call(fake);
2244
-
2245
- return fake;
2246
- },
2247
-
2248
- matches: function (args, strict) {
2249
- var margs = this.matchingAguments;
2250
-
2251
- if (margs.length <= args.length &&
2252
- sinon.deepEqual(margs, args.slice(0, margs.length))) {
2253
- return !strict || margs.length == args.length;
2254
- }
2255
- },
2256
-
2257
- printf: function (format) {
2258
- var spy = this;
2259
- var args = slice.call(arguments, 1);
2260
- var formatter;
2261
-
2262
- return (format || "").replace(/%(.)/g, function (match, specifyer) {
2263
- formatter = spyApi.formatters[specifyer];
2264
-
2265
- if (typeof formatter == "function") {
2266
- return formatter.call(null, spy, args);
2267
- } else if (!isNaN(parseInt(specifyer), 10)) {
2268
- return sinon.format(args[specifyer - 1]);
2269
- }
2270
-
2271
- return "%" + specifyer;
2272
- });
2273
- }
2274
- };
2275
-
2276
- delegateToCalls(spyApi, "calledOn", true);
2277
- delegateToCalls(spyApi, "alwaysCalledOn", false, "calledOn");
2278
- delegateToCalls(spyApi, "calledWith", true);
2279
- delegateToCalls(spyApi, "calledWithMatch", true);
2280
- delegateToCalls(spyApi, "alwaysCalledWith", false, "calledWith");
2281
- delegateToCalls(spyApi, "alwaysCalledWithMatch", false, "calledWithMatch");
2282
- delegateToCalls(spyApi, "calledWithExactly", true);
2283
- delegateToCalls(spyApi, "alwaysCalledWithExactly", false, "calledWithExactly");
2284
- delegateToCalls(spyApi, "neverCalledWith", false, "notCalledWith",
2285
- function () {
2286
- return true;
2287
- });
2288
- delegateToCalls(spyApi, "neverCalledWithMatch", false, "notCalledWithMatch",
2289
- function () {
2290
- return true;
2291
- });
2292
- delegateToCalls(spyApi, "threw", true);
2293
- delegateToCalls(spyApi, "alwaysThrew", false, "threw");
2294
- delegateToCalls(spyApi, "returned", true);
2295
- delegateToCalls(spyApi, "alwaysReturned", false, "returned");
2296
- delegateToCalls(spyApi, "calledWithNew", true);
2297
- delegateToCalls(spyApi, "alwaysCalledWithNew", false, "calledWithNew");
2298
- delegateToCalls(spyApi, "callArg", false, "callArgWith", function () {
2299
- throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
2300
- });
2301
- spyApi.callArgWith = spyApi.callArg;
2302
- delegateToCalls(spyApi, "callArgOn", false, "callArgOnWith", function () {
2303
- throw new Error(this.toString() + " cannot call arg since it was not yet invoked.");
2304
- });
2305
- spyApi.callArgOnWith = spyApi.callArgOn;
2306
- delegateToCalls(spyApi, "yield", false, "yield", function () {
2307
- throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
2308
- });
2309
- // "invokeCallback" is an alias for "yield" since "yield" is invalid in strict mode.
2310
- spyApi.invokeCallback = spyApi.yield;
2311
- delegateToCalls(spyApi, "yieldOn", false, "yieldOn", function () {
2312
- throw new Error(this.toString() + " cannot yield since it was not yet invoked.");
2313
- });
2314
- delegateToCalls(spyApi, "yieldTo", false, "yieldTo", function (property) {
2315
- throw new Error(this.toString() + " cannot yield to '" + property +
2316
- "' since it was not yet invoked.");
2317
- });
2318
- delegateToCalls(spyApi, "yieldToOn", false, "yieldToOn", function (property) {
2319
- throw new Error(this.toString() + " cannot yield to '" + property +
2320
- "' since it was not yet invoked.");
2321
- });
2322
-
2323
- spyApi.formatters = {
2324
- "c": function (spy) {
2325
- return sinon.timesInWords(spy.callCount);
2326
- },
2327
-
2328
- "n": function (spy) {
2329
- return spy.toString();
2330
- },
2331
-
2332
- "C": function (spy) {
2333
- var calls = [];
2334
-
2335
- for (var i = 0, l = spy.callCount; i < l; ++i) {
2336
- var stringifiedCall = " " + spy.getCall(i).toString();
2337
- if (/\n/.test(calls[i - 1])) {
2338
- stringifiedCall = "\n" + stringifiedCall;
2339
- }
2340
- push.call(calls, stringifiedCall);
2341
- }
2342
-
2343
- return calls.length > 0 ? "\n" + calls.join("\n") : "";
2344
- },
2345
-
2346
- "t": function (spy) {
2347
- var objects = [];
2348
-
2349
- for (var i = 0, l = spy.callCount; i < l; ++i) {
2350
- push.call(objects, sinon.format(spy.thisValues[i]));
2351
- }
2352
-
2353
- return objects.join(", ");
2354
- },
2355
-
2356
- "*": function (spy, args) {
2357
- var formatted = [];
2358
-
2359
- for (var i = 0, l = args.length; i < l; ++i) {
2360
- push.call(formatted, sinon.format(args[i]));
2361
- }
2362
-
2363
- return formatted.join(", ");
2364
- }
2365
- };
2366
-
2367
- return spyApi;
2368
- }()));
2369
-
2370
- spyCall = (function () {
2371
-
2372
- function throwYieldError(proxy, text, args) {
2373
- var msg = sinon.functionName(proxy) + text;
2374
- if (args.length) {
2375
- msg += " Received [" + slice.call(args).join(", ") + "]";
2376
- }
2377
- throw new Error(msg);
2378
- }
2379
-
2380
- var callApi = {
2381
- create: function create(spy, thisValue, args, returnValue, exception, id) {
2382
- var proxyCall = sinon.create(spyCall);
2383
- delete proxyCall.create;
2384
- proxyCall.proxy = spy;
2385
- proxyCall.thisValue = thisValue;
2386
- proxyCall.args = args;
2387
- proxyCall.returnValue = returnValue;
2388
- proxyCall.exception = exception;
2389
- proxyCall.callId = typeof id == "number" && id || callId++;
2390
-
2391
- return proxyCall;
2392
- },
2393
-
2394
- calledOn: function calledOn(thisValue) {
2395
- if (sinon.match && sinon.match.isMatcher(thisValue)) {
2396
- return thisValue.test(this.thisValue);
2397
- }
2398
- return this.thisValue === thisValue;
2399
- },
2400
-
2401
- calledWith: function calledWith() {
2402
- for (var i = 0, l = arguments.length; i < l; i += 1) {
2403
- if (!sinon.deepEqual(arguments[i], this.args[i])) {
2404
- return false;
2405
- }
2406
- }
2407
-
2408
- return true;
2409
- },
2410
-
2411
- calledWithMatch: function calledWithMatch() {
2412
- for (var i = 0, l = arguments.length; i < l; i += 1) {
2413
- var actual = this.args[i];
2414
- var expectation = arguments[i];
2415
- if (!sinon.match || !sinon.match(expectation).test(actual)) {
2416
- return false;
2417
- }
2418
- }
2419
- return true;
2420
- },
2421
-
2422
- calledWithExactly: function calledWithExactly() {
2423
- return arguments.length == this.args.length &&
2424
- this.calledWith.apply(this, arguments);
2425
- },
2426
-
2427
- notCalledWith: function notCalledWith() {
2428
- return !this.calledWith.apply(this, arguments);
2429
- },
2430
-
2431
- notCalledWithMatch: function notCalledWithMatch() {
2432
- return !this.calledWithMatch.apply(this, arguments);
2433
- },
2434
-
2435
- returned: function returned(value) {
2436
- return sinon.deepEqual(value, this.returnValue);
2437
- },
2438
-
2439
- threw: function threw(error) {
2440
- if (typeof error == "undefined" || !this.exception) {
2441
- return !!this.exception;
2442
- }
2443
-
2444
- if (typeof error == "string") {
2445
- return this.exception.name == error;
2446
- }
2447
-
2448
- return this.exception === error;
2449
- },
2450
-
2451
- calledWithNew: function calledWithNew(thisValue) {
2452
- return this.thisValue instanceof this.proxy;
2453
- },
2454
-
2455
- calledBefore: function (other) {
2456
- return this.callId < other.callId;
2457
- },
2458
-
2459
- calledAfter: function (other) {
2460
- return this.callId > other.callId;
2461
- },
2462
-
2463
- callArg: function (pos) {
2464
- this.args[pos]();
2465
- },
2466
-
2467
- callArgOn: function (pos, thisValue) {
2468
- this.args[pos].apply(thisValue);
2469
- },
2470
-
2471
- callArgWith: function (pos) {
2472
- this.callArgOnWith.apply(this, [pos, null].concat(slice.call(arguments, 1)));
2473
- },
2474
-
2475
- callArgOnWith: function (pos, thisValue) {
2476
- var args = slice.call(arguments, 2);
2477
- this.args[pos].apply(thisValue, args);
2478
- },
2479
-
2480
- "yield": function () {
2481
- this.yieldOn.apply(this, [null].concat(slice.call(arguments, 0)));
2482
- },
2483
-
2484
- yieldOn: function (thisValue) {
2485
- var args = this.args;
2486
- for (var i = 0, l = args.length; i < l; ++i) {
2487
- if (typeof args[i] === "function") {
2488
- args[i].apply(thisValue, slice.call(arguments, 1));
2489
- return;
2490
- }
2491
- }
2492
- throwYieldError(this.proxy, " cannot yield since no callback was passed.", args);
2493
- },
2494
-
2495
- yieldTo: function (prop) {
2496
- this.yieldToOn.apply(this, [prop, null].concat(slice.call(arguments, 1)));
2497
- },
2498
-
2499
- yieldToOn: function (prop, thisValue) {
2500
- var args = this.args;
2501
- for (var i = 0, l = args.length; i < l; ++i) {
2502
- if (args[i] && typeof args[i][prop] === "function") {
2503
- args[i][prop].apply(thisValue, slice.call(arguments, 2));
2504
- return;
2505
- }
2506
- }
2507
- throwYieldError(this.proxy, " cannot yield to '" + prop +
2508
- "' since no callback was passed.", args);
2509
- },
2510
-
2511
- toString: function () {
2512
- var callStr = this.proxy.toString() + "(";
2513
- var args = [];
2514
-
2515
- for (var i = 0, l = this.args.length; i < l; ++i) {
2516
- push.call(args, sinon.format(this.args[i]));
2517
- }
2518
-
2519
- callStr = callStr + args.join(", ") + ")";
2520
-
2521
- if (typeof this.returnValue != "undefined") {
2522
- callStr += " => " + sinon.format(this.returnValue);
2523
- }
2524
-
2525
- if (this.exception) {
2526
- callStr += " !" + this.exception.name;
2527
-
2528
- if (this.exception.message) {
2529
- callStr += "(" + this.exception.message + ")";
2530
- }
2531
- }
2532
-
2533
- return callStr;
2534
- }
2535
- };
2536
- callApi.invokeCallback = callApi.yield;
2537
- return callApi;
2538
- }());
2539
-
2540
- spy.spyCall = spyCall;
2541
-
2542
- // This steps outside the module sandbox and will be removed
2543
- sinon.spyCall = spyCall;
2544
-
2545
- if (commonJSModule) {
2546
- module.exports = spy;
2547
- } else {
2548
- sinon.spy = spy;
2549
- }
2550
- }(typeof sinon == "object" && sinon || null));
2551
-
2552
- /**
2553
- * @depend ../sinon.js
2554
- * @depend spy.js
2555
- */
2556
- /*jslint eqeqeq: false, onevar: false*/
2557
- /*global module, require, sinon*/
2558
- /**
2559
- * Stub functions
2560
- *
2561
- * @author Christian Johansen (christian@cjohansen.no)
2562
- * @license BSD
2563
- *
2564
- * Copyright (c) 2010-2013 Christian Johansen
2565
- */
2566
-
2567
- (function (sinon) {
2568
- var commonJSModule = typeof module == "object" && typeof require == "function";
2569
-
2570
- if (!sinon && commonJSModule) {
2571
- sinon = require("../sinon");
2572
- }
2573
-
2574
- if (!sinon) {
2575
- return;
2576
- }
2577
-
2578
- function stub(object, property, func) {
2579
- if (!!func && typeof func != "function") {
2580
- throw new TypeError("Custom stub should be function");
2581
- }
2582
-
2583
- var wrapper;
2584
-
2585
- if (func) {
2586
- wrapper = sinon.spy && sinon.spy.create ? sinon.spy.create(func) : func;
2587
- } else {
2588
- wrapper = stub.create();
2589
- }
2590
-
2591
- if (!object && !property) {
2592
- return sinon.stub.create();
2593
- }
2594
-
2595
- if (!property && !!object && typeof object == "object") {
2596
- for (var prop in object) {
2597
- if (typeof object[prop] === "function") {
2598
- stub(object, prop);
2599
- }
2600
- }
2601
-
2602
- return object;
2603
- }
2604
-
2605
- return sinon.wrapMethod(object, property, wrapper);
2606
- }
2607
-
2608
- function getChangingValue(stub, property) {
2609
- var index = stub.callCount - 1;
2610
- var values = stub[property];
2611
- var prop = index in values ? values[index] : values[values.length - 1];
2612
- stub[property + "Last"] = prop;
2613
-
2614
- return prop;
2615
- }
2616
-
2617
- function getCallback(stub, args) {
2618
- var callArgAt = getChangingValue(stub, "callArgAts");
2619
-
2620
- if (callArgAt < 0) {
2621
- var callArgProp = getChangingValue(stub, "callArgProps");
2622
-
2623
- for (var i = 0, l = args.length; i < l; ++i) {
2624
- if (!callArgProp && typeof args[i] == "function") {
2625
- return args[i];
2626
- }
2627
-
2628
- if (callArgProp && args[i] &&
2629
- typeof args[i][callArgProp] == "function") {
2630
- return args[i][callArgProp];
2631
- }
2632
- }
2633
-
2634
- return null;
2635
- }
2636
-
2637
- return args[callArgAt];
2638
- }
2639
-
2640
- var join = Array.prototype.join;
2641
-
2642
- function getCallbackError(stub, func, args) {
2643
- if (stub.callArgAtsLast < 0) {
2644
- var msg;
2645
-
2646
- if (stub.callArgPropsLast) {
2647
- msg = sinon.functionName(stub) +
2648
- " expected to yield to '" + stub.callArgPropsLast +
2649
- "', but no object with such a property was passed."
2650
- } else {
2651
- msg = sinon.functionName(stub) +
2652
- " expected to yield, but no callback was passed."
2653
- }
2654
-
2655
- if (args.length > 0) {
2656
- msg += " Received [" + join.call(args, ", ") + "]";
2657
- }
2658
-
2659
- return msg;
2660
- }
2661
-
2662
- return "argument at index " + stub.callArgAtsLast + " is not a function: " + func;
2663
- }
2664
-
2665
- var nextTick = (function () {
2666
- if (typeof process === "object" && typeof process.nextTick === "function") {
2667
- return process.nextTick;
2668
- } else if (typeof setImmediate === "function") {
2669
- return setImmediate;
2670
- } else {
2671
- return function (callback) {
2672
- setTimeout(callback, 0);
2673
- };
2674
- }
2675
- })();
2676
-
2677
- function callCallback(stub, args) {
2678
- if (stub.callArgAts.length > 0) {
2679
- var func = getCallback(stub, args);
2680
-
2681
- if (typeof func != "function") {
2682
- throw new TypeError(getCallbackError(stub, func, args));
2683
- }
2684
-
2685
- var callbackArguments = getChangingValue(stub, "callbackArguments");
2686
- var callbackContext = getChangingValue(stub, "callbackContexts");
2687
-
2688
- if (stub.callbackAsync) {
2689
- nextTick(function () {
2690
- func.apply(callbackContext, callbackArguments);
2691
- });
2692
- } else {
2693
- func.apply(callbackContext, callbackArguments);
2694
- }
2695
- }
2696
- }
2697
-
2698
- var uuid = 0;
2699
-
2700
- sinon.extend(stub, (function () {
2701
- var slice = Array.prototype.slice, proto;
2702
-
2703
- function throwsException(error, message) {
2704
- if (typeof error == "string") {
2705
- this.exception = new Error(message || "");
2706
- this.exception.name = error;
2707
- } else if (!error) {
2708
- this.exception = new Error("Error");
2709
- } else {
2710
- this.exception = error;
2711
- }
2712
-
2713
- return this;
2714
- }
2715
-
2716
- proto = {
2717
- create: function create() {
2718
- var functionStub = function () {
2719
-
2720
- callCallback(functionStub, arguments);
2721
-
2722
- if (functionStub.exception) {
2723
- throw functionStub.exception;
2724
- } else if (typeof functionStub.returnArgAt == 'number') {
2725
- return arguments[functionStub.returnArgAt];
2726
- } else if (functionStub.returnThis) {
2727
- return this;
2728
- }
2729
- return functionStub.returnValue;
2730
- };
2731
-
2732
- functionStub.id = "stub#" + uuid++;
2733
- var orig = functionStub;
2734
- functionStub = sinon.spy.create(functionStub);
2735
- functionStub.func = orig;
2736
-
2737
- functionStub.callArgAts = [];
2738
- functionStub.callbackArguments = [];
2739
- functionStub.callbackContexts = [];
2740
- functionStub.callArgProps = [];
2741
-
2742
- sinon.extend(functionStub, stub);
2743
- functionStub._create = sinon.stub.create;
2744
- functionStub.displayName = "stub";
2745
- functionStub.toString = sinon.functionToString;
2746
-
2747
- return functionStub;
2748
- },
2749
-
2750
- resetBehavior: function () {
2751
- var i;
2752
-
2753
- this.callArgAts = [];
2754
- this.callbackArguments = [];
2755
- this.callbackContexts = [];
2756
- this.callArgProps = [];
2757
-
2758
- delete this.returnValue;
2759
- delete this.returnArgAt;
2760
- this.returnThis = false;
2761
-
2762
- if (this.fakes) {
2763
- for (i = 0; i < this.fakes.length; i++) {
2764
- this.fakes[i].resetBehavior();
2765
- }
2766
- }
2767
- },
2768
-
2769
- returns: function returns(value) {
2770
- this.returnValue = value;
2771
-
2772
- return this;
2773
- },
2774
-
2775
- returnsArg: function returnsArg(pos) {
2776
- if (typeof pos != "number") {
2777
- throw new TypeError("argument index is not number");
2778
- }
2779
-
2780
- this.returnArgAt = pos;
2781
-
2782
- return this;
2783
- },
2784
-
2785
- returnsThis: function returnsThis() {
2786
- this.returnThis = true;
2787
-
2788
- return this;
2789
- },
2790
-
2791
- "throws": throwsException,
2792
- throwsException: throwsException,
2793
-
2794
- callsArg: function callsArg(pos) {
2795
- if (typeof pos != "number") {
2796
- throw new TypeError("argument index is not number");
2797
- }
2798
-
2799
- this.callArgAts.push(pos);
2800
- this.callbackArguments.push([]);
2801
- this.callbackContexts.push(undefined);
2802
- this.callArgProps.push(undefined);
2803
-
2804
- return this;
2805
- },
2806
-
2807
- callsArgOn: function callsArgOn(pos, context) {
2808
- if (typeof pos != "number") {
2809
- throw new TypeError("argument index is not number");
2810
- }
2811
- if (typeof context != "object") {
2812
- throw new TypeError("argument context is not an object");
2813
- }
2814
-
2815
- this.callArgAts.push(pos);
2816
- this.callbackArguments.push([]);
2817
- this.callbackContexts.push(context);
2818
- this.callArgProps.push(undefined);
2819
-
2820
- return this;
2821
- },
2822
-
2823
- callsArgWith: function callsArgWith(pos) {
2824
- if (typeof pos != "number") {
2825
- throw new TypeError("argument index is not number");
2826
- }
2827
-
2828
- this.callArgAts.push(pos);
2829
- this.callbackArguments.push(slice.call(arguments, 1));
2830
- this.callbackContexts.push(undefined);
2831
- this.callArgProps.push(undefined);
2832
-
2833
- return this;
2834
- },
2835
-
2836
- callsArgOnWith: function callsArgWith(pos, context) {
2837
- if (typeof pos != "number") {
2838
- throw new TypeError("argument index is not number");
2839
- }
2840
- if (typeof context != "object") {
2841
- throw new TypeError("argument context is not an object");
2842
- }
2843
-
2844
- this.callArgAts.push(pos);
2845
- this.callbackArguments.push(slice.call(arguments, 2));
2846
- this.callbackContexts.push(context);
2847
- this.callArgProps.push(undefined);
2848
-
2849
- return this;
2850
- },
2851
-
2852
- yields: function () {
2853
- this.callArgAts.push(-1);
2854
- this.callbackArguments.push(slice.call(arguments, 0));
2855
- this.callbackContexts.push(undefined);
2856
- this.callArgProps.push(undefined);
2857
-
2858
- return this;
2859
- },
2860
-
2861
- yieldsOn: function (context) {
2862
- if (typeof context != "object") {
2863
- throw new TypeError("argument context is not an object");
2864
- }
2865
-
2866
- this.callArgAts.push(-1);
2867
- this.callbackArguments.push(slice.call(arguments, 1));
2868
- this.callbackContexts.push(context);
2869
- this.callArgProps.push(undefined);
2870
-
2871
- return this;
2872
- },
2873
-
2874
- yieldsTo: function (prop) {
2875
- this.callArgAts.push(-1);
2876
- this.callbackArguments.push(slice.call(arguments, 1));
2877
- this.callbackContexts.push(undefined);
2878
- this.callArgProps.push(prop);
2879
-
2880
- return this;
2881
- },
2882
-
2883
- yieldsToOn: function (prop, context) {
2884
- if (typeof context != "object") {
2885
- throw new TypeError("argument context is not an object");
2886
- }
2887
-
2888
- this.callArgAts.push(-1);
2889
- this.callbackArguments.push(slice.call(arguments, 2));
2890
- this.callbackContexts.push(context);
2891
- this.callArgProps.push(prop);
2892
-
2893
- return this;
2894
- }
2895
- };
2896
-
2897
- // create asynchronous versions of callsArg* and yields* methods
2898
- for (var method in proto) {
2899
- // need to avoid creating anotherasync versions of the newly added async methods
2900
- if (proto.hasOwnProperty(method) &&
2901
- method.match(/^(callsArg|yields|thenYields$)/) &&
2902
- !method.match(/Async/)) {
2903
- proto[method + 'Async'] = (function (syncFnName) {
2904
- return function () {
2905
- this.callbackAsync = true;
2906
- return this[syncFnName].apply(this, arguments);
2907
- };
2908
- })(method);
2909
- }
2910
- }
2911
-
2912
- return proto;
2913
-
2914
- }()));
2915
-
2916
- if (commonJSModule) {
2917
- module.exports = stub;
2918
- } else {
2919
- sinon.stub = stub;
2920
- }
2921
- }(typeof sinon == "object" && sinon || null));
2922
-
2923
- /**
2924
- * @depend ../sinon.js
2925
- * @depend stub.js
2926
- */
2927
- /*jslint eqeqeq: false, onevar: false, nomen: false*/
2928
- /*global module, require, sinon*/
2929
- /**
2930
- * Mock functions.
2931
- *
2932
- * @author Christian Johansen (christian@cjohansen.no)
2933
- * @license BSD
2934
- *
2935
- * Copyright (c) 2010-2013 Christian Johansen
2936
- */
2937
-
2938
- (function (sinon) {
2939
- var commonJSModule = typeof module == "object" && typeof require == "function";
2940
- var push = [].push;
2941
-
2942
- if (!sinon && commonJSModule) {
2943
- sinon = require("../sinon");
2944
- }
2945
-
2946
- if (!sinon) {
2947
- return;
2948
- }
2949
-
2950
- function mock(object) {
2951
- if (!object) {
2952
- return sinon.expectation.create("Anonymous mock");
2953
- }
2954
-
2955
- return mock.create(object);
2956
- }
2957
-
2958
- sinon.mock = mock;
2959
-
2960
- sinon.extend(mock, (function () {
2961
- function each(collection, callback) {
2962
- if (!collection) {
2963
- return;
2964
- }
2965
-
2966
- for (var i = 0, l = collection.length; i < l; i += 1) {
2967
- callback(collection[i]);
2968
- }
2969
- }
2970
-
2971
- return {
2972
- create: function create(object) {
2973
- if (!object) {
2974
- throw new TypeError("object is null");
2975
- }
2976
-
2977
- var mockObject = sinon.extend({}, mock);
2978
- mockObject.object = object;
2979
- delete mockObject.create;
2980
-
2981
- return mockObject;
2982
- },
2983
-
2984
- expects: function expects(method) {
2985
- if (!method) {
2986
- throw new TypeError("method is falsy");
2987
- }
2988
-
2989
- if (!this.expectations) {
2990
- this.expectations = {};
2991
- this.proxies = [];
2992
- }
2993
-
2994
- if (!this.expectations[method]) {
2995
- this.expectations[method] = [];
2996
- var mockObject = this;
2997
-
2998
- sinon.wrapMethod(this.object, method, function () {
2999
- return mockObject.invokeMethod(method, this, arguments);
3000
- });
3001
-
3002
- push.call(this.proxies, method);
3003
- }
3004
-
3005
- var expectation = sinon.expectation.create(method);
3006
- push.call(this.expectations[method], expectation);
3007
-
3008
- return expectation;
3009
- },
3010
-
3011
- restore: function restore() {
3012
- var object = this.object;
3013
-
3014
- each(this.proxies, function (proxy) {
3015
- if (typeof object[proxy].restore == "function") {
3016
- object[proxy].restore();
3017
- }
3018
- });
3019
- },
3020
-
3021
- verify: function verify() {
3022
- var expectations = this.expectations || {};
3023
- var messages = [], met = [];
3024
-
3025
- each(this.proxies, function (proxy) {
3026
- each(expectations[proxy], function (expectation) {
3027
- if (!expectation.met()) {
3028
- push.call(messages, expectation.toString());
3029
- } else {
3030
- push.call(met, expectation.toString());
3031
- }
3032
- });
3033
- });
3034
-
3035
- this.restore();
3036
-
3037
- if (messages.length > 0) {
3038
- sinon.expectation.fail(messages.concat(met).join("\n"));
3039
- } else {
3040
- sinon.expectation.pass(messages.concat(met).join("\n"));
3041
- }
3042
-
3043
- return true;
3044
- },
3045
-
3046
- invokeMethod: function invokeMethod(method, thisValue, args) {
3047
- var expectations = this.expectations && this.expectations[method];
3048
- var length = expectations && expectations.length || 0, i;
3049
-
3050
- for (i = 0; i < length; i += 1) {
3051
- if (!expectations[i].met() &&
3052
- expectations[i].allowsCall(thisValue, args)) {
3053
- return expectations[i].apply(thisValue, args);
3054
- }
3055
- }
3056
-
3057
- var messages = [], available, exhausted = 0;
3058
-
3059
- for (i = 0; i < length; i += 1) {
3060
- if (expectations[i].allowsCall(thisValue, args)) {
3061
- available = available || expectations[i];
3062
- } else {
3063
- exhausted += 1;
3064
- }
3065
- push.call(messages, " " + expectations[i].toString());
3066
- }
3067
-
3068
- if (exhausted === 0) {
3069
- return available.apply(thisValue, args);
3070
- }
3071
-
3072
- messages.unshift("Unexpected call: " + sinon.spyCall.toString.call({
3073
- proxy: method,
3074
- args: args
3075
- }));
3076
-
3077
- sinon.expectation.fail(messages.join("\n"));
3078
- }
3079
- };
3080
- }()));
3081
-
3082
- var times = sinon.timesInWords;
3083
-
3084
- sinon.expectation = (function () {
3085
- var slice = Array.prototype.slice;
3086
- var _invoke = sinon.spy.invoke;
3087
-
3088
- function callCountInWords(callCount) {
3089
- if (callCount == 0) {
3090
- return "never called";
3091
- } else {
3092
- return "called " + times(callCount);
3093
- }
3094
- }
3095
-
3096
- function expectedCallCountInWords(expectation) {
3097
- var min = expectation.minCalls;
3098
- var max = expectation.maxCalls;
3099
-
3100
- if (typeof min == "number" && typeof max == "number") {
3101
- var str = times(min);
3102
-
3103
- if (min != max) {
3104
- str = "at least " + str + " and at most " + times(max);
3105
- }
3106
-
3107
- return str;
3108
- }
3109
-
3110
- if (typeof min == "number") {
3111
- return "at least " + times(min);
3112
- }
3113
-
3114
- return "at most " + times(max);
3115
- }
3116
-
3117
- function receivedMinCalls(expectation) {
3118
- var hasMinLimit = typeof expectation.minCalls == "number";
3119
- return !hasMinLimit || expectation.callCount >= expectation.minCalls;
3120
- }
3121
-
3122
- function receivedMaxCalls(expectation) {
3123
- if (typeof expectation.maxCalls != "number") {
3124
- return false;
3125
- }
3126
-
3127
- return expectation.callCount == expectation.maxCalls;
3128
- }
3129
-
3130
- return {
3131
- minCalls: 1,
3132
- maxCalls: 1,
3133
-
3134
- create: function create(methodName) {
3135
- var expectation = sinon.extend(sinon.stub.create(), sinon.expectation);
3136
- delete expectation.create;
3137
- expectation.method = methodName;
3138
-
3139
- return expectation;
3140
- },
3141
-
3142
- invoke: function invoke(func, thisValue, args) {
3143
- this.verifyCallAllowed(thisValue, args);
3144
-
3145
- return _invoke.apply(this, arguments);
3146
- },
3147
-
3148
- atLeast: function atLeast(num) {
3149
- if (typeof num != "number") {
3150
- throw new TypeError("'" + num + "' is not number");
3151
- }
3152
-
3153
- if (!this.limitsSet) {
3154
- this.maxCalls = null;
3155
- this.limitsSet = true;
3156
- }
3157
-
3158
- this.minCalls = num;
3159
-
3160
- return this;
3161
- },
3162
-
3163
- atMost: function atMost(num) {
3164
- if (typeof num != "number") {
3165
- throw new TypeError("'" + num + "' is not number");
3166
- }
3167
-
3168
- if (!this.limitsSet) {
3169
- this.minCalls = null;
3170
- this.limitsSet = true;
3171
- }
3172
-
3173
- this.maxCalls = num;
3174
-
3175
- return this;
3176
- },
3177
-
3178
- never: function never() {
3179
- return this.exactly(0);
3180
- },
3181
-
3182
- once: function once() {
3183
- return this.exactly(1);
3184
- },
3185
-
3186
- twice: function twice() {
3187
- return this.exactly(2);
3188
- },
3189
-
3190
- thrice: function thrice() {
3191
- return this.exactly(3);
3192
- },
3193
-
3194
- exactly: function exactly(num) {
3195
- if (typeof num != "number") {
3196
- throw new TypeError("'" + num + "' is not a number");
3197
- }
3198
-
3199
- this.atLeast(num);
3200
- return this.atMost(num);
3201
- },
3202
-
3203
- met: function met() {
3204
- return !this.failed && receivedMinCalls(this);
3205
- },
3206
-
3207
- verifyCallAllowed: function verifyCallAllowed(thisValue, args) {
3208
- if (receivedMaxCalls(this)) {
3209
- this.failed = true;
3210
- sinon.expectation.fail(this.method + " already called " + times(this.maxCalls));
3211
- }
3212
-
3213
- if ("expectedThis" in this && this.expectedThis !== thisValue) {
3214
- sinon.expectation.fail(this.method + " called with " + thisValue + " as thisValue, expected " +
3215
- this.expectedThis);
3216
- }
3217
-
3218
- if (!("expectedArguments" in this)) {
3219
- return;
3220
- }
3221
-
3222
- if (!args) {
3223
- sinon.expectation.fail(this.method + " received no arguments, expected " +
3224
- sinon.format(this.expectedArguments));
3225
- }
3226
-
3227
- if (args.length < this.expectedArguments.length) {
3228
- sinon.expectation.fail(this.method + " received too few arguments (" + sinon.format(args) +
3229
- "), expected " + sinon.format(this.expectedArguments));
3230
- }
3231
-
3232
- if (this.expectsExactArgCount &&
3233
- args.length != this.expectedArguments.length) {
3234
- sinon.expectation.fail(this.method + " received too many arguments (" + sinon.format(args) +
3235
- "), expected " + sinon.format(this.expectedArguments));
3236
- }
3237
-
3238
- for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
3239
- if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
3240
- sinon.expectation.fail(this.method + " received wrong arguments " + sinon.format(args) +
3241
- ", expected " + sinon.format(this.expectedArguments));
3242
- }
3243
- }
3244
- },
3245
-
3246
- allowsCall: function allowsCall(thisValue, args) {
3247
- if (this.met() && receivedMaxCalls(this)) {
3248
- return false;
3249
- }
3250
-
3251
- if ("expectedThis" in this && this.expectedThis !== thisValue) {
3252
- return false;
3253
- }
3254
-
3255
- if (!("expectedArguments" in this)) {
3256
- return true;
3257
- }
3258
-
3259
- args = args || [];
3260
-
3261
- if (args.length < this.expectedArguments.length) {
3262
- return false;
3263
- }
3264
-
3265
- if (this.expectsExactArgCount &&
3266
- args.length != this.expectedArguments.length) {
3267
- return false;
3268
- }
3269
-
3270
- for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) {
3271
- if (!sinon.deepEqual(this.expectedArguments[i], args[i])) {
3272
- return false;
3273
- }
3274
- }
3275
-
3276
- return true;
3277
- },
3278
-
3279
- withArgs: function withArgs() {
3280
- this.expectedArguments = slice.call(arguments);
3281
- return this;
3282
- },
3283
-
3284
- withExactArgs: function withExactArgs() {
3285
- this.withArgs.apply(this, arguments);
3286
- this.expectsExactArgCount = true;
3287
- return this;
3288
- },
3289
-
3290
- on: function on(thisValue) {
3291
- this.expectedThis = thisValue;
3292
- return this;
3293
- },
3294
-
3295
- toString: function () {
3296
- var args = (this.expectedArguments || []).slice();
3297
-
3298
- if (!this.expectsExactArgCount) {
3299
- push.call(args, "[...]");
3300
- }
3301
-
3302
- var callStr = sinon.spyCall.toString.call({
3303
- proxy: this.method || "anonymous mock expectation",
3304
- args: args
3305
- });
3306
-
3307
- var message = callStr.replace(", [...", "[, ...") + " " +
3308
- expectedCallCountInWords(this);
3309
-
3310
- if (this.met()) {
3311
- return "Expectation met: " + message;
3312
- }
3313
-
3314
- return "Expected " + message + " (" +
3315
- callCountInWords(this.callCount) + ")";
3316
- },
3317
-
3318
- verify: function verify() {
3319
- if (!this.met()) {
3320
- sinon.expectation.fail(this.toString());
3321
- } else {
3322
- sinon.expectation.pass(this.toString());
3323
- }
3324
-
3325
- return true;
3326
- },
3327
-
3328
- pass: function (message) {
3329
- sinon.assert.pass(message);
3330
- },
3331
- fail: function (message) {
3332
- var exception = new Error(message);
3333
- exception.name = "ExpectationError";
3334
-
3335
- throw exception;
3336
- }
3337
- };
3338
- }());
3339
-
3340
- if (commonJSModule) {
3341
- module.exports = mock;
3342
- } else {
3343
- sinon.mock = mock;
3344
- }
3345
- }(typeof sinon == "object" && sinon || null));
3346
-
3347
- /**
3348
- * @depend ../sinon.js
3349
- * @depend stub.js
3350
- * @depend mock.js
3351
- */
3352
- /*jslint eqeqeq: false, onevar: false, forin: true*/
3353
- /*global module, require, sinon*/
3354
- /**
3355
- * Collections of stubs, spies and mocks.
3356
- *
3357
- * @author Christian Johansen (christian@cjohansen.no)
3358
- * @license BSD
3359
- *
3360
- * Copyright (c) 2010-2013 Christian Johansen
3361
- */
3362
-
3363
- (function (sinon) {
3364
- var commonJSModule = typeof module == "object" && typeof require == "function";
3365
- var push = [].push;
3366
- var hasOwnProperty = Object.prototype.hasOwnProperty;
3367
-
3368
- if (!sinon && commonJSModule) {
3369
- sinon = require("../sinon");
3370
- }
3371
-
3372
- if (!sinon) {
3373
- return;
3374
- }
3375
-
3376
- function getFakes(fakeCollection) {
3377
- if (!fakeCollection.fakes) {
3378
- fakeCollection.fakes = [];
3379
- }
3380
-
3381
- return fakeCollection.fakes;
3382
- }
3383
-
3384
- function each(fakeCollection, method) {
3385
- var fakes = getFakes(fakeCollection);
3386
-
3387
- for (var i = 0, l = fakes.length; i < l; i += 1) {
3388
- if (typeof fakes[i][method] == "function") {
3389
- fakes[i][method]();
3390
- }
3391
- }
3392
- }
3393
-
3394
- function compact(fakeCollection) {
3395
- var fakes = getFakes(fakeCollection);
3396
- var i = 0;
3397
- while (i < fakes.length) {
3398
- fakes.splice(i, 1);
3399
- }
3400
- }
3401
-
3402
- var collection = {
3403
- verify: function resolve() {
3404
- each(this, "verify");
3405
- },
3406
-
3407
- restore: function restore() {
3408
- each(this, "restore");
3409
- compact(this);
3410
- },
3411
-
3412
- verifyAndRestore: function verifyAndRestore() {
3413
- var exception;
3414
-
3415
- try {
3416
- this.verify();
3417
- } catch (e) {
3418
- exception = e;
3419
- }
3420
-
3421
- this.restore();
3422
-
3423
- if (exception) {
3424
- throw exception;
3425
- }
3426
- },
3427
-
3428
- add: function add(fake) {
3429
- push.call(getFakes(this), fake);
3430
- return fake;
3431
- },
3432
-
3433
- spy: function spy() {
3434
- return this.add(sinon.spy.apply(sinon, arguments));
3435
- },
3436
-
3437
- stub: function stub(object, property, value) {
3438
- if (property) {
3439
- var original = object[property];
3440
-
3441
- if (typeof original != "function") {
3442
- if (!hasOwnProperty.call(object, property)) {
3443
- throw new TypeError("Cannot stub non-existent own property " + property);
3444
- }
3445
-
3446
- object[property] = value;
3447
-
3448
- return this.add({
3449
- restore: function () {
3450
- object[property] = original;
3451
- }
3452
- });
3453
- }
3454
- }
3455
- if (!property && !!object && typeof object == "object") {
3456
- var stubbedObj = sinon.stub.apply(sinon, arguments);
3457
-
3458
- for (var prop in stubbedObj) {
3459
- if (typeof stubbedObj[prop] === "function") {
3460
- this.add(stubbedObj[prop]);
3461
- }
3462
- }
3463
-
3464
- return stubbedObj;
3465
- }
3466
-
3467
- return this.add(sinon.stub.apply(sinon, arguments));
3468
- },
3469
-
3470
- mock: function mock() {
3471
- return this.add(sinon.mock.apply(sinon, arguments));
3472
- },
3473
-
3474
- inject: function inject(obj) {
3475
- var col = this;
3476
-
3477
- obj.spy = function () {
3478
- return col.spy.apply(col, arguments);
3479
- };
3480
-
3481
- obj.stub = function () {
3482
- return col.stub.apply(col, arguments);
3483
- };
3484
-
3485
- obj.mock = function () {
3486
- return col.mock.apply(col, arguments);
3487
- };
3488
-
3489
- return obj;
3490
- }
3491
- };
3492
-
3493
- if (commonJSModule) {
3494
- module.exports = collection;
3495
- } else {
3496
- sinon.collection = collection;
3497
- }
3498
- }(typeof sinon == "object" && sinon || null));
3499
-
3500
- /*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/
3501
- /*global module, require, window*/
3502
- /**
3503
- * Fake timer API
3504
- * setTimeout
3505
- * setInterval
3506
- * clearTimeout
3507
- * clearInterval
3508
- * tick
3509
- * reset
3510
- * Date
3511
- *
3512
- * Inspired by jsUnitMockTimeOut from JsUnit
3513
- *
3514
- * @author Christian Johansen (christian@cjohansen.no)
3515
- * @license BSD
3516
- *
3517
- * Copyright (c) 2010-2013 Christian Johansen
3518
- */
3519
-
3520
- if (typeof sinon == "undefined") {
3521
- var sinon = {};
3522
- }
3523
-
3524
- (function (global) {
3525
- var id = 1;
3526
-
3527
- function addTimer(args, recurring) {
3528
- if (args.length === 0) {
3529
- throw new Error("Function requires at least 1 parameter");
3530
- }
3531
-
3532
- var toId = id++;
3533
- var delay = args[1] || 0;
3534
-
3535
- if (!this.timeouts) {
3536
- this.timeouts = {};
3537
- }
3538
-
3539
- this.timeouts[toId] = {
3540
- id: toId,
3541
- func: args[0],
3542
- callAt: this.now + delay,
3543
- invokeArgs: Array.prototype.slice.call(args, 2)
3544
- };
3545
-
3546
- if (recurring === true) {
3547
- this.timeouts[toId].interval = delay;
3548
- }
3549
-
3550
- return toId;
3551
- }
3552
-
3553
- function parseTime(str) {
3554
- if (!str) {
3555
- return 0;
3556
- }
3557
-
3558
- var strings = str.split(":");
3559
- var l = strings.length, i = l;
3560
- var ms = 0, parsed;
3561
-
3562
- if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
3563
- throw new Error("tick only understands numbers and 'h:m:s'");
3564
- }
3565
-
3566
- while (i--) {
3567
- parsed = parseInt(strings[i], 10);
3568
-
3569
- if (parsed >= 60) {
3570
- throw new Error("Invalid time " + str);
3571
- }
3572
-
3573
- ms += parsed * Math.pow(60, (l - i - 1));
3574
- }
3575
-
3576
- return ms * 1000;
3577
- }
3578
-
3579
- function createObject(object) {
3580
- var newObject;
3581
-
3582
- if (Object.create) {
3583
- newObject = Object.create(object);
3584
- } else {
3585
- var F = function () {
3586
- };
3587
- F.prototype = object;
3588
- newObject = new F();
3589
- }
3590
-
3591
- newObject.Date.clock = newObject;
3592
- return newObject;
3593
- }
3594
-
3595
- sinon.clock = {
3596
- now: 0,
3597
-
3598
- create: function create(now) {
3599
- var clock = createObject(this);
3600
-
3601
- if (typeof now == "number") {
3602
- clock.now = now;
3603
- }
3604
-
3605
- if (!!now && typeof now == "object") {
3606
- throw new TypeError("now should be milliseconds since UNIX epoch");
3607
- }
3608
-
3609
- return clock;
3610
- },
3611
-
3612
- setTimeout: function setTimeout(callback, timeout) {
3613
- return addTimer.call(this, arguments, false);
3614
- },
3615
-
3616
- clearTimeout: function clearTimeout(timerId) {
3617
- if (!this.timeouts) {
3618
- this.timeouts = [];
3619
- }
3620
-
3621
- if (timerId in this.timeouts) {
3622
- delete this.timeouts[timerId];
3623
- }
3624
- },
3625
-
3626
- setInterval: function setInterval(callback, timeout) {
3627
- return addTimer.call(this, arguments, true);
3628
- },
3629
-
3630
- clearInterval: function clearInterval(timerId) {
3631
- this.clearTimeout(timerId);
3632
- },
3633
-
3634
- tick: function tick(ms) {
3635
- ms = typeof ms == "number" ? ms : parseTime(ms);
3636
- var tickFrom = this.now, tickTo = this.now + ms, previous = this.now;
3637
- var timer = this.firstTimerInRange(tickFrom, tickTo);
3638
-
3639
- var firstException;
3640
- while (timer && tickFrom <= tickTo) {
3641
- if (this.timeouts[timer.id]) {
3642
- tickFrom = this.now = timer.callAt;
3643
- try {
3644
- this.callTimer(timer);
3645
- } catch (e) {
3646
- firstException = firstException || e;
3647
- }
3648
- }
3649
-
3650
- timer = this.firstTimerInRange(previous, tickTo);
3651
- previous = tickFrom;
3652
- }
3653
-
3654
- this.now = tickTo;
3655
-
3656
- if (firstException) {
3657
- throw firstException;
3658
- }
3659
-
3660
- return this.now;
3661
- },
3662
-
3663
- firstTimerInRange: function (from, to) {
3664
- var timer, smallest, originalTimer;
3665
-
3666
- for (var id in this.timeouts) {
3667
- if (this.timeouts.hasOwnProperty(id)) {
3668
- if (this.timeouts[id].callAt < from || this.timeouts[id].callAt > to) {
3669
- continue;
3670
- }
3671
-
3672
- if (!smallest || this.timeouts[id].callAt < smallest) {
3673
- originalTimer = this.timeouts[id];
3674
- smallest = this.timeouts[id].callAt;
3675
-
3676
- timer = {
3677
- func: this.timeouts[id].func,
3678
- callAt: this.timeouts[id].callAt,
3679
- interval: this.timeouts[id].interval,
3680
- id: this.timeouts[id].id,
3681
- invokeArgs: this.timeouts[id].invokeArgs
3682
- };
3683
- }
3684
- }
3685
- }
3686
-
3687
- return timer || null;
3688
- },
3689
-
3690
- callTimer: function (timer) {
3691
- if (typeof timer.interval == "number") {
3692
- this.timeouts[timer.id].callAt += timer.interval;
3693
- } else {
3694
- delete this.timeouts[timer.id];
3695
- }
3696
-
3697
- try {
3698
- if (typeof timer.func == "function") {
3699
- timer.func.apply(null, timer.invokeArgs);
3700
- } else {
3701
- eval(timer.func);
3702
- }
3703
- } catch (e) {
3704
- var exception = e;
3705
- }
3706
-
3707
- if (!this.timeouts[timer.id]) {
3708
- if (exception) {
3709
- throw exception;
3710
- }
3711
- return;
3712
- }
3713
-
3714
- if (exception) {
3715
- throw exception;
3716
- }
3717
- },
3718
-
3719
- reset: function reset() {
3720
- this.timeouts = {};
3721
- },
3722
-
3723
- Date: (function () {
3724
- var NativeDate = Date;
3725
-
3726
- function ClockDate(year, month, date, hour, minute, second, ms) {
3727
- // Defensive and verbose to avoid potential harm in passing
3728
- // explicit undefined when user does not pass argument
3729
- switch (arguments.length) {
3730
- case 0:
3731
- return new NativeDate(ClockDate.clock.now);
3732
- case 1:
3733
- return new NativeDate(year);
3734
- case 2:
3735
- return new NativeDate(year, month);
3736
- case 3:
3737
- return new NativeDate(year, month, date);
3738
- case 4:
3739
- return new NativeDate(year, month, date, hour);
3740
- case 5:
3741
- return new NativeDate(year, month, date, hour, minute);
3742
- case 6:
3743
- return new NativeDate(year, month, date, hour, minute, second);
3744
- default:
3745
- return new NativeDate(year, month, date, hour, minute, second, ms);
3746
- }
3747
- }
3748
-
3749
- return mirrorDateProperties(ClockDate, NativeDate);
3750
- }())
3751
- };
3752
-
3753
- function mirrorDateProperties(target, source) {
3754
- if (source.now) {
3755
- target.now = function now() {
3756
- return target.clock.now;
3757
- };
3758
- } else {
3759
- delete target.now;
3760
- }
3761
-
3762
- if (source.toSource) {
3763
- target.toSource = function toSource() {
3764
- return source.toSource();
3765
- };
3766
- } else {
3767
- delete target.toSource;
3768
- }
3769
-
3770
- target.toString = function toString() {
3771
- return source.toString();
3772
- };
3773
-
3774
- target.prototype = source.prototype;
3775
- target.parse = source.parse;
3776
- target.UTC = source.UTC;
3777
- target.prototype.toUTCString = source.prototype.toUTCString;
3778
- return target;
3779
- }
3780
-
3781
- var methods = ["Date", "setTimeout", "setInterval",
3782
- "clearTimeout", "clearInterval"];
3783
-
3784
- function restore() {
3785
- var method;
3786
-
3787
- for (var i = 0, l = this.methods.length; i < l; i++) {
3788
- method = this.methods[i];
3789
- if (global[method].hadOwnProperty) {
3790
- global[method] = this["_" + method];
3791
- } else {
3792
- delete global[method];
3793
- }
3794
- }
3795
-
3796
- // Prevent multiple executions which will completely remove these props
3797
- this.methods = [];
3798
- }
3799
-
3800
- function stubGlobal(method, clock) {
3801
- clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(global, method);
3802
- clock["_" + method] = global[method];
3803
-
3804
- if (method == "Date") {
3805
- var date = mirrorDateProperties(clock[method], global[method]);
3806
- global[method] = date;
3807
- } else {
3808
- global[method] = function () {
3809
- return clock[method].apply(clock, arguments);
3810
- };
3811
-
3812
- for (var prop in clock[method]) {
3813
- if (clock[method].hasOwnProperty(prop)) {
3814
- global[method][prop] = clock[method][prop];
3815
- }
3816
- }
3817
- }
3818
-
3819
- global[method].clock = clock;
3820
- }
3821
-
3822
- sinon.useFakeTimers = function useFakeTimers(now) {
3823
- var clock = sinon.clock.create(now);
3824
- clock.restore = restore;
3825
- clock.methods = Array.prototype.slice.call(arguments,
3826
- typeof now == "number" ? 1 : 0);
3827
-
3828
- if (clock.methods.length === 0) {
3829
- clock.methods = methods;
3830
- }
3831
-
3832
- for (var i = 0, l = clock.methods.length; i < l; i++) {
3833
- stubGlobal(clock.methods[i], clock);
3834
- }
3835
-
3836
- return clock;
3837
- };
3838
- }(typeof global != "undefined" && typeof global !== "function" ? global : this));
3839
-
3840
- sinon.timers = {
3841
- setTimeout: setTimeout,
3842
- clearTimeout: clearTimeout,
3843
- setInterval: setInterval,
3844
- clearInterval: clearInterval,
3845
- Date: Date
3846
- };
3847
-
3848
- if (typeof module == "object" && typeof require == "function") {
3849
- module.exports = sinon;
3850
- }
3851
-
3852
- /*jslint eqeqeq: false, onevar: false*/
3853
- /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
3854
- /**
3855
- * Minimal Event interface implementation
3856
- *
3857
- * Original implementation by Sven Fuchs: https://gist.github.com/995028
3858
- * Modifications and tests by Christian Johansen.
3859
- *
3860
- * @author Sven Fuchs (svenfuchs@artweb-design.de)
3861
- * @author Christian Johansen (christian@cjohansen.no)
3862
- * @license BSD
3863
- *
3864
- * Copyright (c) 2011 Sven Fuchs, Christian Johansen
3865
- */
3866
-
3867
- if (typeof sinon == "undefined") {
3868
- this.sinon = {};
3869
- }
3870
-
3871
- (function () {
3872
- var push = [].push;
3873
-
3874
- sinon.Event = function Event(type, bubbles, cancelable) {
3875
- this.initEvent(type, bubbles, cancelable);
3876
- };
3877
-
3878
- sinon.Event.prototype = {
3879
- initEvent: function (type, bubbles, cancelable) {
3880
- this.type = type;
3881
- this.bubbles = bubbles;
3882
- this.cancelable = cancelable;
3883
- },
3884
-
3885
- stopPropagation: function () {
3886
- },
3887
-
3888
- preventDefault: function () {
3889
- this.defaultPrevented = true;
3890
- }
3891
- };
3892
-
3893
- sinon.EventTarget = {
3894
- addEventListener: function addEventListener(event, listener, useCapture) {
3895
- this.eventListeners = this.eventListeners || {};
3896
- this.eventListeners[event] = this.eventListeners[event] || [];
3897
- push.call(this.eventListeners[event], listener);
3898
- },
3899
-
3900
- removeEventListener: function removeEventListener(event, listener, useCapture) {
3901
- var listeners = this.eventListeners && this.eventListeners[event] || [];
3902
-
3903
- for (var i = 0, l = listeners.length; i < l; ++i) {
3904
- if (listeners[i] == listener) {
3905
- return listeners.splice(i, 1);
3906
- }
3907
- }
3908
- },
3909
-
3910
- dispatchEvent: function dispatchEvent(event) {
3911
- var type = event.type;
3912
- var listeners = this.eventListeners && this.eventListeners[type] || [];
3913
-
3914
- for (var i = 0; i < listeners.length; i++) {
3915
- if (typeof listeners[i] == "function") {
3916
- listeners[i].call(this, event);
3917
- } else {
3918
- listeners[i].handleEvent(event);
3919
- }
3920
- }
3921
-
3922
- return !!event.defaultPrevented;
3923
- }
3924
- };
3925
- }());
3926
-
3927
- /**
3928
- * @depend ../../sinon.js
3929
- * @depend event.js
3930
- */
3931
- /*jslint eqeqeq: false, onevar: false*/
3932
- /*global sinon, module, require, ActiveXObject, XMLHttpRequest, DOMParser*/
3933
- /**
3934
- * Fake XMLHttpRequest object
3935
- *
3936
- * @author Christian Johansen (christian@cjohansen.no)
3937
- * @license BSD
3938
- *
3939
- * Copyright (c) 2010-2013 Christian Johansen
3940
- */
3941
-
3942
- if (typeof sinon == "undefined") {
3943
- this.sinon = {};
3944
- }
3945
- sinon.xhr = { XMLHttpRequest: this.XMLHttpRequest };
3946
-
3947
- // wrapper for global
3948
- (function (global) {
3949
- var xhr = sinon.xhr;
3950
- xhr.GlobalXMLHttpRequest = global.XMLHttpRequest;
3951
- xhr.GlobalActiveXObject = global.ActiveXObject;
3952
- xhr.supportsActiveX = typeof xhr.GlobalActiveXObject != "undefined";
3953
- xhr.supportsXHR = typeof xhr.GlobalXMLHttpRequest != "undefined";
3954
- xhr.workingXHR = xhr.supportsXHR ? xhr.GlobalXMLHttpRequest : xhr.supportsActiveX
3955
- ? function () {
3956
- return new xhr.GlobalActiveXObject("MSXML2.XMLHTTP.3.0")
3957
- } : false;
3958
-
3959
- /*jsl:ignore*/
3960
- var unsafeHeaders = {
3961
- "Accept-Charset": true,
3962
- "Accept-Encoding": true,
3963
- "Connection": true,
3964
- "Content-Length": true,
3965
- "Cookie": true,
3966
- "Cookie2": true,
3967
- "Content-Transfer-Encoding": true,
3968
- "Date": true,
3969
- "Expect": true,
3970
- "Host": true,
3971
- "Keep-Alive": true,
3972
- "Referer": true,
3973
- "TE": true,
3974
- "Trailer": true,
3975
- "Transfer-Encoding": true,
3976
- "Upgrade": true,
3977
- "User-Agent": true,
3978
- "Via": true
3979
- };
3980
- /*jsl:end*/
3981
-
3982
- function FakeXMLHttpRequest() {
3983
- this.readyState = FakeXMLHttpRequest.UNSENT;
3984
- this.requestHeaders = {};
3985
- this.requestBody = null;
3986
- this.status = 0;
3987
- this.statusText = "";
3988
-
3989
- if (typeof FakeXMLHttpRequest.onCreate == "function") {
3990
- FakeXMLHttpRequest.onCreate(this);
3991
- }
3992
- }
3993
-
3994
- function verifyState(xhr) {
3995
- if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
3996
- throw new Error("INVALID_STATE_ERR");
3997
- }
3998
-
3999
- if (xhr.sendFlag) {
4000
- throw new Error("INVALID_STATE_ERR");
4001
- }
4002
- }
4003
-
4004
- // filtering to enable a white-list version of Sinon FakeXhr,
4005
- // where whitelisted requests are passed through to real XHR
4006
- function each(collection, callback) {
4007
- if (!collection) return;
4008
- for (var i = 0, l = collection.length; i < l; i += 1) {
4009
- callback(collection[i]);
4010
- }
4011
- }
4012
-
4013
- function some(collection, callback) {
4014
- for (var index = 0; index < collection.length; index++) {
4015
- if (callback(collection[index]) === true) return true;
4016
- }
4017
- ;
4018
- return false;
4019
- }
4020
-
4021
- // largest arity in XHR is 5 - XHR#open
4022
- var apply = function (obj, method, args) {
4023
- switch (args.length) {
4024
- case 0:
4025
- return obj[method]();
4026
- case 1:
4027
- return obj[method](args[0]);
4028
- case 2:
4029
- return obj[method](args[0], args[1]);
4030
- case 3:
4031
- return obj[method](args[0], args[1], args[2]);
4032
- case 4:
4033
- return obj[method](args[0], args[1], args[2], args[3]);
4034
- case 5:
4035
- return obj[method](args[0], args[1], args[2], args[3], args[4]);
4036
- }
4037
- ;
4038
- };
4039
-
4040
- FakeXMLHttpRequest.filters = [];
4041
- FakeXMLHttpRequest.addFilter = function (fn) {
4042
- this.filters.push(fn)
4043
- };
4044
- var IE6Re = /MSIE 6/;
4045
- FakeXMLHttpRequest.defake = function (fakeXhr, xhrArgs) {
4046
- var xhr = new sinon.xhr.workingXHR();
4047
- each(["open", "setRequestHeader", "send", "abort", "getResponseHeader",
4048
- "getAllResponseHeaders", "addEventListener", "overrideMimeType", "removeEventListener"],
4049
- function (method) {
4050
- fakeXhr[method] = function () {
4051
- return apply(xhr, method, arguments);
4052
- };
4053
- });
4054
-
4055
- var copyAttrs = function (args) {
4056
- each(args, function (attr) {
4057
- try {
4058
- fakeXhr[attr] = xhr[attr]
4059
- } catch (e) {
4060
- if (!IE6Re.test(navigator.userAgent)) throw e;
4061
- }
4062
- });
4063
- };
4064
-
4065
- var stateChange = function () {
4066
- fakeXhr.readyState = xhr.readyState;
4067
- if (xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) {
4068
- copyAttrs(["status", "statusText"]);
4069
- }
4070
- if (xhr.readyState >= FakeXMLHttpRequest.LOADING) {
4071
- copyAttrs(["responseText"]);
4072
- }
4073
- if (xhr.readyState === FakeXMLHttpRequest.DONE) {
4074
- copyAttrs(["responseXML"]);
4075
- }
4076
- if (fakeXhr.onreadystatechange) fakeXhr.onreadystatechange.call(fakeXhr);
4077
- };
4078
- if (xhr.addEventListener) {
4079
- for (var event in fakeXhr.eventListeners) {
4080
- if (fakeXhr.eventListeners.hasOwnProperty(event)) {
4081
- each(fakeXhr.eventListeners[event], function (handler) {
4082
- xhr.addEventListener(event, handler);
4083
- });
4084
- }
4085
- }
4086
- xhr.addEventListener("readystatechange", stateChange);
4087
- } else {
4088
- xhr.onreadystatechange = stateChange;
4089
- }
4090
- apply(xhr, "open", xhrArgs);
4091
- };
4092
- FakeXMLHttpRequest.useFilters = false;
4093
-
4094
- function verifyRequestSent(xhr) {
4095
- if (xhr.readyState == FakeXMLHttpRequest.DONE) {
4096
- throw new Error("Request done");
4097
- }
4098
- }
4099
-
4100
- function verifyHeadersReceived(xhr) {
4101
- if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
4102
- throw new Error("No headers received");
4103
- }
4104
- }
4105
-
4106
- function verifyResponseBodyType(body) {
4107
- if (typeof body != "string") {
4108
- var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
4109
- body + ", which is not a string.");
4110
- error.name = "InvalidBodyException";
4111
- throw error;
4112
- }
4113
- }
4114
-
4115
- sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
4116
- async: true,
4117
-
4118
- open: function open(method, url, async, username, password) {
4119
- this.method = method;
4120
- this.url = url;
4121
- this.async = typeof async == "boolean" ? async : true;
4122
- this.username = username;
4123
- this.password = password;
4124
- this.responseText = null;
4125
- this.responseXML = null;
4126
- this.requestHeaders = {};
4127
- this.sendFlag = false;
4128
- if (sinon.FakeXMLHttpRequest.useFilters === true) {
4129
- var xhrArgs = arguments;
4130
- var defake = some(FakeXMLHttpRequest.filters, function (filter) {
4131
- return filter.apply(this, xhrArgs)
4132
- });
4133
- if (defake) {
4134
- return sinon.FakeXMLHttpRequest.defake(this, arguments);
4135
- }
4136
- }
4137
- this.readyStateChange(FakeXMLHttpRequest.OPENED);
4138
- },
4139
-
4140
- readyStateChange: function readyStateChange(state) {
4141
- this.readyState = state;
4142
-
4143
- if (typeof this.onreadystatechange == "function") {
4144
- try {
4145
- this.onreadystatechange();
4146
- } catch (e) {
4147
- sinon.logError("Fake XHR onreadystatechange handler", e);
4148
- }
4149
- }
4150
-
4151
- this.dispatchEvent(new sinon.Event("readystatechange"));
4152
- },
4153
-
4154
- setRequestHeader: function setRequestHeader(header, value) {
4155
- verifyState(this);
4156
-
4157
- if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
4158
- throw new Error("Refused to set unsafe header \"" + header + "\"");
4159
- }
4160
-
4161
- if (this.requestHeaders[header]) {
4162
- this.requestHeaders[header] += "," + value;
4163
- } else {
4164
- this.requestHeaders[header] = value;
4165
- }
4166
- },
4167
-
4168
- // Helps testing
4169
- setResponseHeaders: function setResponseHeaders(headers) {
4170
- this.responseHeaders = {};
4171
-
4172
- for (var header in headers) {
4173
- if (headers.hasOwnProperty(header)) {
4174
- this.responseHeaders[header] = headers[header];
4175
- }
4176
- }
4177
-
4178
- if (this.async) {
4179
- this.readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
4180
- } else {
4181
- this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
4182
- }
4183
- },
4184
-
4185
- // Currently treats ALL data as a DOMString (i.e. no Document)
4186
- send: function send(data) {
4187
- verifyState(this);
4188
-
4189
- if (!/^(get|head)$/i.test(this.method)) {
4190
- if (this.requestHeaders["Content-Type"]) {
4191
- var value = this.requestHeaders["Content-Type"].split(";");
4192
- this.requestHeaders["Content-Type"] = value[0] + ";charset=utf-8";
4193
- } else {
4194
- this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
4195
- }
4196
-
4197
- this.requestBody = data;
4198
- }
4199
-
4200
- this.errorFlag = false;
4201
- this.sendFlag = this.async;
4202
- this.readyStateChange(FakeXMLHttpRequest.OPENED);
4203
-
4204
- if (typeof this.onSend == "function") {
4205
- this.onSend(this);
4206
- }
4207
- },
4208
-
4209
- abort: function abort() {
4210
- this.aborted = true;
4211
- this.responseText = null;
4212
- this.errorFlag = true;
4213
- this.requestHeaders = {};
4214
-
4215
- if (this.readyState > sinon.FakeXMLHttpRequest.UNSENT && this.sendFlag) {
4216
- this.readyStateChange(sinon.FakeXMLHttpRequest.DONE);
4217
- this.sendFlag = false;
4218
- }
4219
-
4220
- this.readyState = sinon.FakeXMLHttpRequest.UNSENT;
4221
- },
4222
-
4223
- getResponseHeader: function getResponseHeader(header) {
4224
- if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
4225
- return null;
4226
- }
4227
-
4228
- if (/^Set-Cookie2?$/i.test(header)) {
4229
- return null;
4230
- }
4231
-
4232
- header = header.toLowerCase();
4233
-
4234
- for (var h in this.responseHeaders) {
4235
- if (h.toLowerCase() == header) {
4236
- return this.responseHeaders[h];
4237
- }
4238
- }
4239
-
4240
- return null;
4241
- },
4242
-
4243
- getAllResponseHeaders: function getAllResponseHeaders() {
4244
- if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
4245
- return "";
4246
- }
4247
-
4248
- var headers = "";
4249
-
4250
- for (var header in this.responseHeaders) {
4251
- if (this.responseHeaders.hasOwnProperty(header) &&
4252
- !/^Set-Cookie2?$/i.test(header)) {
4253
- headers += header + ": " + this.responseHeaders[header] + "\r\n";
4254
- }
4255
- }
4256
-
4257
- return headers;
4258
- },
4259
-
4260
- setResponseBody: function setResponseBody(body) {
4261
- verifyRequestSent(this);
4262
- verifyHeadersReceived(this);
4263
- verifyResponseBodyType(body);
4264
-
4265
- var chunkSize = this.chunkSize || 10;
4266
- var index = 0;
4267
- this.responseText = "";
4268
-
4269
- do {
4270
- if (this.async) {
4271
- this.readyStateChange(FakeXMLHttpRequest.LOADING);
4272
- }
4273
-
4274
- this.responseText += body.substring(index, index + chunkSize);
4275
- index += chunkSize;
4276
- } while (index < body.length);
4277
-
4278
- var type = this.getResponseHeader("Content-Type");
4279
-
4280
- if (this.responseText &&
4281
- (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
4282
- try {
4283
- this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText);
4284
- } catch (e) {
4285
- // Unable to parse XML - no biggie
4286
- }
4287
- }
4288
-
4289
- if (this.async) {
4290
- this.readyStateChange(FakeXMLHttpRequest.DONE);
4291
- } else {
4292
- this.readyState = FakeXMLHttpRequest.DONE;
4293
- }
4294
- },
4295
-
4296
- respond: function respond(status, headers, body) {
4297
- this.setResponseHeaders(headers || {});
4298
- this.status = typeof status == "number" ? status : 200;
4299
- this.statusText = FakeXMLHttpRequest.statusCodes[this.status];
4300
- this.setResponseBody(body || "");
4301
- }
4302
- });
4303
-
4304
- sinon.extend(FakeXMLHttpRequest, {
4305
- UNSENT: 0,
4306
- OPENED: 1,
4307
- HEADERS_RECEIVED: 2,
4308
- LOADING: 3,
4309
- DONE: 4
4310
- });
4311
-
4312
- // Borrowed from JSpec
4313
- FakeXMLHttpRequest.parseXML = function parseXML(text) {
4314
- var xmlDoc;
4315
-
4316
- if (typeof DOMParser != "undefined") {
4317
- var parser = new DOMParser();
4318
- xmlDoc = parser.parseFromString(text, "text/xml");
4319
- } else {
4320
- xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
4321
- xmlDoc.async = "false";
4322
- xmlDoc.loadXML(text);
4323
- }
4324
-
4325
- return xmlDoc;
4326
- };
4327
-
4328
- FakeXMLHttpRequest.statusCodes = {
4329
- 100: "Continue",
4330
- 101: "Switching Protocols",
4331
- 200: "OK",
4332
- 201: "Created",
4333
- 202: "Accepted",
4334
- 203: "Non-Authoritative Information",
4335
- 204: "No Content",
4336
- 205: "Reset Content",
4337
- 206: "Partial Content",
4338
- 300: "Multiple Choice",
4339
- 301: "Moved Permanently",
4340
- 302: "Found",
4341
- 303: "See Other",
4342
- 304: "Not Modified",
4343
- 305: "Use Proxy",
4344
- 307: "Temporary Redirect",
4345
- 400: "Bad Request",
4346
- 401: "Unauthorized",
4347
- 402: "Payment Required",
4348
- 403: "Forbidden",
4349
- 404: "Not Found",
4350
- 405: "Method Not Allowed",
4351
- 406: "Not Acceptable",
4352
- 407: "Proxy Authentication Required",
4353
- 408: "Request Timeout",
4354
- 409: "Conflict",
4355
- 410: "Gone",
4356
- 411: "Length Required",
4357
- 412: "Precondition Failed",
4358
- 413: "Request Entity Too Large",
4359
- 414: "Request-URI Too Long",
4360
- 415: "Unsupported Media Type",
4361
- 416: "Requested Range Not Satisfiable",
4362
- 417: "Expectation Failed",
4363
- 422: "Unprocessable Entity",
4364
- 500: "Internal Server Error",
4365
- 501: "Not Implemented",
4366
- 502: "Bad Gateway",
4367
- 503: "Service Unavailable",
4368
- 504: "Gateway Timeout",
4369
- 505: "HTTP Version Not Supported"
4370
- };
4371
-
4372
- sinon.useFakeXMLHttpRequest = function () {
4373
- sinon.FakeXMLHttpRequest.restore = function restore(keepOnCreate) {
4374
- if (xhr.supportsXHR) {
4375
- global.XMLHttpRequest = xhr.GlobalXMLHttpRequest;
4376
- }
4377
-
4378
- if (xhr.supportsActiveX) {
4379
- global.ActiveXObject = xhr.GlobalActiveXObject;
4380
- }
4381
-
4382
- delete sinon.FakeXMLHttpRequest.restore;
4383
-
4384
- if (keepOnCreate !== true) {
4385
- delete sinon.FakeXMLHttpRequest.onCreate;
4386
- }
4387
- };
4388
- if (xhr.supportsXHR) {
4389
- global.XMLHttpRequest = sinon.FakeXMLHttpRequest;
4390
- }
4391
-
4392
- if (xhr.supportsActiveX) {
4393
- global.ActiveXObject = function ActiveXObject(objId) {
4394
- if (objId == "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) {
4395
-
4396
- return new sinon.FakeXMLHttpRequest();
4397
- }
4398
-
4399
- return new xhr.GlobalActiveXObject(objId);
4400
- };
4401
- }
4402
-
4403
- return sinon.FakeXMLHttpRequest;
4404
- };
4405
-
4406
- sinon.FakeXMLHttpRequest = FakeXMLHttpRequest;
4407
- })(this);
4408
-
4409
- if (typeof module == "object" && typeof require == "function") {
4410
- module.exports = sinon;
4411
- }
4412
-
4413
- /**
4414
- * @depend fake_xml_http_request.js
4415
- */
4416
- /*jslint eqeqeq: false, onevar: false, regexp: false, plusplus: false*/
4417
- /*global module, require, window*/
4418
- /**
4419
- * The Sinon "server" mimics a web server that receives requests from
4420
- * sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
4421
- * both synchronously and asynchronously. To respond synchronuously, canned
4422
- * answers have to be provided upfront.
4423
- *
4424
- * @author Christian Johansen (christian@cjohansen.no)
4425
- * @license BSD
4426
- *
4427
- * Copyright (c) 2010-2013 Christian Johansen
4428
- */
4429
-
4430
- if (typeof sinon == "undefined") {
4431
- var sinon = {};
4432
- }
4433
-
4434
- sinon.fakeServer = (function () {
4435
- var push = [].push;
4436
-
4437
- function F() {
4438
- }
4439
-
4440
- function create(proto) {
4441
- F.prototype = proto;
4442
- return new F();
4443
- }
4444
-
4445
- function responseArray(handler) {
4446
- var response = handler;
4447
-
4448
- if (Object.prototype.toString.call(handler) != "[object Array]") {
4449
- response = [200, {}, handler];
4450
- }
4451
-
4452
- if (typeof response[2] != "string") {
4453
- throw new TypeError("Fake server response body should be string, but was " +
4454
- typeof response[2]);
4455
- }
4456
-
4457
- return response;
4458
- }
4459
-
4460
- var wloc = typeof window !== "undefined" ? window.location : {};
4461
- var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
4462
-
4463
- function matchOne(response, reqMethod, reqUrl) {
4464
- var rmeth = response.method;
4465
- var matchMethod = !rmeth || rmeth.toLowerCase() == reqMethod.toLowerCase();
4466
- var url = response.url;
4467
- var matchUrl = !url || url == reqUrl || (typeof url.test == "function" && url.test(reqUrl));
4468
-
4469
- return matchMethod && matchUrl;
4470
- }
4471
-
4472
- function match(response, request) {
4473
- var requestMethod = this.getHTTPMethod(request);
4474
- var requestUrl = request.url;
4475
-
4476
- if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
4477
- requestUrl = requestUrl.replace(rCurrLoc, "");
4478
- }
4479
-
4480
- if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
4481
- if (typeof response.response == "function") {
4482
- var ru = response.url;
4483
- var args = [request].concat(!ru ? [] : requestUrl.match(ru).slice(1));
4484
- return response.response.apply(response, args);
4485
- }
4486
-
4487
- return true;
4488
- }
4489
-
4490
- return false;
4491
- }
4492
-
4493
- function log(response, request) {
4494
- var str;
4495
-
4496
- str = "Request:\n" + sinon.format(request) + "\n\n";
4497
- str += "Response:\n" + sinon.format(response) + "\n\n";
4498
-
4499
- sinon.log(str);
4500
- }
4501
-
4502
- return {
4503
- create: function () {
4504
- var server = create(this);
4505
- this.xhr = sinon.useFakeXMLHttpRequest();
4506
- server.requests = [];
4507
-
4508
- this.xhr.onCreate = function (xhrObj) {
4509
- server.addRequest(xhrObj);
4510
- };
4511
-
4512
- return server;
4513
- },
4514
-
4515
- addRequest: function addRequest(xhrObj) {
4516
- var server = this;
4517
- push.call(this.requests, xhrObj);
4518
-
4519
- xhrObj.onSend = function () {
4520
- server.handleRequest(this);
4521
- };
4522
-
4523
- if (this.autoRespond && !this.responding) {
4524
- setTimeout(function () {
4525
- server.responding = false;
4526
- server.respond();
4527
- }, this.autoRespondAfter || 10);
4528
-
4529
- this.responding = true;
4530
- }
4531
- },
4532
-
4533
- getHTTPMethod: function getHTTPMethod(request) {
4534
- if (this.fakeHTTPMethods && /post/i.test(request.method)) {
4535
- var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
4536
- return !!matches ? matches[1] : request.method;
4537
- }
4538
-
4539
- return request.method;
4540
- },
4541
-
4542
- handleRequest: function handleRequest(xhr) {
4543
- if (xhr.async) {
4544
- if (!this.queue) {
4545
- this.queue = [];
4546
- }
4547
-
4548
- push.call(this.queue, xhr);
4549
- } else {
4550
- this.processRequest(xhr);
4551
- }
4552
- },
4553
-
4554
- respondWith: function respondWith(method, url, body) {
4555
- if (arguments.length == 1 && typeof method != "function") {
4556
- this.response = responseArray(method);
4557
- return;
4558
- }
4559
-
4560
- if (!this.responses) {
4561
- this.responses = [];
4562
- }
4563
-
4564
- if (arguments.length == 1) {
4565
- body = method;
4566
- url = method = null;
4567
- }
4568
-
4569
- if (arguments.length == 2) {
4570
- body = url;
4571
- url = method;
4572
- method = null;
4573
- }
4574
-
4575
- push.call(this.responses, {
4576
- method: method,
4577
- url: url,
4578
- response: typeof body == "function" ? body : responseArray(body)
4579
- });
4580
- },
4581
-
4582
- respond: function respond() {
4583
- if (arguments.length > 0) this.respondWith.apply(this, arguments);
4584
- var queue = this.queue || [];
4585
- var request;
4586
-
4587
- while (request = queue.shift()) {
4588
- this.processRequest(request);
4589
- }
4590
- },
4591
-
4592
- processRequest: function processRequest(request) {
4593
- try {
4594
- if (request.aborted) {
4595
- return;
4596
- }
4597
-
4598
- var response = this.response || [404, {}, ""];
4599
-
4600
- if (this.responses) {
4601
- for (var i = 0, l = this.responses.length; i < l; i++) {
4602
- if (match.call(this, this.responses[i], request)) {
4603
- response = this.responses[i].response;
4604
- break;
4605
- }
4606
- }
4607
- }
4608
-
4609
- if (request.readyState != 4) {
4610
- log(response, request);
4611
-
4612
- request.respond(response[0], response[1], response[2]);
4613
- }
4614
- } catch (e) {
4615
- sinon.logError("Fake server request processing", e);
4616
- }
4617
- },
4618
-
4619
- restore: function restore() {
4620
- return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
4621
- }
4622
- };
4623
- }());
4624
-
4625
- if (typeof module == "object" && typeof require == "function") {
4626
- module.exports = sinon;
4627
- }
4628
-
4629
- /**
4630
- * @depend fake_server.js
4631
- * @depend fake_timers.js
4632
- */
4633
- /*jslint browser: true, eqeqeq: false, onevar: false*/
4634
- /*global sinon*/
4635
- /**
4636
- * Add-on for sinon.fakeServer that automatically handles a fake timer along with
4637
- * the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
4638
- * 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
4639
- * it polls the object for completion with setInterval. Dispite the direct
4640
- * motivation, there is nothing jQuery-specific in this file, so it can be used
4641
- * in any environment where the ajax implementation depends on setInterval or
4642
- * setTimeout.
4643
- *
4644
- * @author Christian Johansen (christian@cjohansen.no)
4645
- * @license BSD
4646
- *
4647
- * Copyright (c) 2010-2013 Christian Johansen
4648
- */
4649
-
4650
- (function () {
4651
- function Server() {
4652
- }
4653
-
4654
- Server.prototype = sinon.fakeServer;
4655
-
4656
- sinon.fakeServerWithClock = new Server();
4657
-
4658
- sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
4659
- if (xhr.async) {
4660
- if (typeof setTimeout.clock == "object") {
4661
- this.clock = setTimeout.clock;
4662
- } else {
4663
- this.clock = sinon.useFakeTimers();
4664
- this.resetClock = true;
4665
- }
4666
-
4667
- if (!this.longestTimeout) {
4668
- var clockSetTimeout = this.clock.setTimeout;
4669
- var clockSetInterval = this.clock.setInterval;
4670
- var server = this;
4671
-
4672
- this.clock.setTimeout = function (fn, timeout) {
4673
- server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
4674
-
4675
- return clockSetTimeout.apply(this, arguments);
4676
- };
4677
-
4678
- this.clock.setInterval = function (fn, timeout) {
4679
- server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
4680
-
4681
- return clockSetInterval.apply(this, arguments);
4682
- };
4683
- }
4684
- }
4685
-
4686
- return sinon.fakeServer.addRequest.call(this, xhr);
4687
- };
4688
-
4689
- sinon.fakeServerWithClock.respond = function respond() {
4690
- var returnVal = sinon.fakeServer.respond.apply(this, arguments);
4691
-
4692
- if (this.clock) {
4693
- this.clock.tick(this.longestTimeout || 0);
4694
- this.longestTimeout = 0;
4695
-
4696
- if (this.resetClock) {
4697
- this.clock.restore();
4698
- this.resetClock = false;
4699
- }
4700
- }
4701
-
4702
- return returnVal;
4703
- };
4704
-
4705
- sinon.fakeServerWithClock.restore = function restore() {
4706
- if (this.clock) {
4707
- this.clock.restore();
4708
- }
4709
-
4710
- return sinon.fakeServer.restore.apply(this, arguments);
4711
- };
4712
- }());
4713
-
4714
- /**
4715
- * @depend ../sinon.js
4716
- * @depend collection.js
4717
- * @depend util/fake_timers.js
4718
- * @depend util/fake_server_with_clock.js
4719
- */
4720
- /*jslint eqeqeq: false, onevar: false, plusplus: false*/
4721
- /*global require, module*/
4722
- /**
4723
- * Manages fake collections as well as fake utilities such as Sinon's
4724
- * timers and fake XHR implementation in one convenient object.
4725
- *
4726
- * @author Christian Johansen (christian@cjohansen.no)
4727
- * @license BSD
4728
- *
4729
- * Copyright (c) 2010-2013 Christian Johansen
4730
- */
4731
-
4732
- if (typeof module == "object" && typeof require == "function") {
4733
- var sinon = require("../sinon");
4734
- sinon.extend(sinon, require("./util/fake_timers"));
4735
- }
4736
-
4737
- (function () {
4738
- var push = [].push;
4739
-
4740
- function exposeValue(sandbox, config, key, value) {
4741
- if (!value) {
4742
- return;
4743
- }
4744
-
4745
- if (config.injectInto) {
4746
- config.injectInto[key] = value;
4747
- } else {
4748
- push.call(sandbox.args, value);
4749
- }
4750
- }
4751
-
4752
- function prepareSandboxFromConfig(config) {
4753
- var sandbox = sinon.create(sinon.sandbox);
4754
-
4755
- if (config.useFakeServer) {
4756
- if (typeof config.useFakeServer == "object") {
4757
- sandbox.serverPrototype = config.useFakeServer;
4758
- }
4759
-
4760
- sandbox.useFakeServer();
4761
- }
4762
-
4763
- if (config.useFakeTimers) {
4764
- if (typeof config.useFakeTimers == "object") {
4765
- sandbox.useFakeTimers.apply(sandbox, config.useFakeTimers);
4766
- } else {
4767
- sandbox.useFakeTimers();
4768
- }
4769
- }
4770
-
4771
- return sandbox;
4772
- }
4773
-
4774
- sinon.sandbox = sinon.extend(sinon.create(sinon.collection), {
4775
- useFakeTimers: function useFakeTimers() {
4776
- this.clock = sinon.useFakeTimers.apply(sinon, arguments);
4777
-
4778
- return this.add(this.clock);
4779
- },
4780
-
4781
- serverPrototype: sinon.fakeServer,
4782
-
4783
- useFakeServer: function useFakeServer() {
4784
- var proto = this.serverPrototype || sinon.fakeServer;
4785
-
4786
- if (!proto || !proto.create) {
4787
- return null;
4788
- }
4789
-
4790
- this.server = proto.create();
4791
- return this.add(this.server);
4792
- },
4793
-
4794
- inject: function (obj) {
4795
- sinon.collection.inject.call(this, obj);
4796
-
4797
- if (this.clock) {
4798
- obj.clock = this.clock;
4799
- }
4800
-
4801
- if (this.server) {
4802
- obj.server = this.server;
4803
- obj.requests = this.server.requests;
4804
- }
4805
-
4806
- return obj;
4807
- },
4808
-
4809
- create: function (config) {
4810
- if (!config) {
4811
- return sinon.create(sinon.sandbox);
4812
- }
4813
-
4814
- var sandbox = prepareSandboxFromConfig(config);
4815
- sandbox.args = sandbox.args || [];
4816
- var prop, value, exposed = sandbox.inject({});
4817
-
4818
- if (config.properties) {
4819
- for (var i = 0, l = config.properties.length; i < l; i++) {
4820
- prop = config.properties[i];
4821
- value = exposed[prop] || prop == "sandbox" && sandbox;
4822
- exposeValue(sandbox, config, prop, value);
4823
- }
4824
- } else {
4825
- exposeValue(sandbox, config, "sandbox", value);
4826
- }
4827
-
4828
- return sandbox;
4829
- }
4830
- });
4831
-
4832
- sinon.sandbox.useFakeXMLHttpRequest = sinon.sandbox.useFakeServer;
4833
-
4834
- if (typeof module == "object" && typeof require == "function") {
4835
- module.exports = sinon.sandbox;
4836
- }
4837
- }());
4838
-
4839
- /**
4840
- * @depend ../sinon.js
4841
- * @depend stub.js
4842
- * @depend mock.js
4843
- * @depend sandbox.js
4844
- */
4845
- /*jslint eqeqeq: false, onevar: false, forin: true, plusplus: false*/
4846
- /*global module, require, sinon*/
4847
- /**
4848
- * Test function, sandboxes fakes
4849
- *
4850
- * @author Christian Johansen (christian@cjohansen.no)
4851
- * @license BSD
4852
- *
4853
- * Copyright (c) 2010-2013 Christian Johansen
4854
- */
4855
-
4856
- (function (sinon) {
4857
- var commonJSModule = typeof module == "object" && typeof require == "function";
4858
-
4859
- if (!sinon && commonJSModule) {
4860
- sinon = require("../sinon");
4861
- }
4862
-
4863
- if (!sinon) {
4864
- return;
4865
- }
4866
-
4867
- function test(callback) {
4868
- var type = typeof callback;
4869
-
4870
- if (type != "function") {
4871
- throw new TypeError("sinon.test needs to wrap a test function, got " + type);
4872
- }
4873
-
4874
- return function () {
4875
- var config = sinon.getConfig(sinon.config);
4876
- config.injectInto = config.injectIntoThis && this || config.injectInto;
4877
- var sandbox = sinon.sandbox.create(config);
4878
- var exception, result;
4879
- var args = Array.prototype.slice.call(arguments).concat(sandbox.args);
4880
-
4881
- try {
4882
- result = callback.apply(this, args);
4883
- } catch (e) {
4884
- exception = e;
4885
- }
4886
-
4887
- if (typeof exception !== "undefined") {
4888
- sandbox.restore();
4889
- throw exception;
4890
- }
4891
- else {
4892
- sandbox.verifyAndRestore();
4893
- }
4894
-
4895
- return result;
4896
- };
4897
- }
4898
-
4899
- test.config = {
4900
- injectIntoThis: true,
4901
- injectInto: null,
4902
- properties: ["spy", "stub", "mock", "clock", "server", "requests"],
4903
- useFakeTimers: true,
4904
- useFakeServer: true
4905
- };
4906
-
4907
- if (commonJSModule) {
4908
- module.exports = test;
4909
- } else {
4910
- sinon.test = test;
4911
- }
4912
- }(typeof sinon == "object" && sinon || null));
4913
-
4914
- /**
4915
- * @depend ../sinon.js
4916
- * @depend test.js
4917
- */
4918
- /*jslint eqeqeq: false, onevar: false, eqeqeq: false*/
4919
- /*global module, require, sinon*/
4920
- /**
4921
- * Test case, sandboxes all test functions
4922
- *
4923
- * @author Christian Johansen (christian@cjohansen.no)
4924
- * @license BSD
4925
- *
4926
- * Copyright (c) 2010-2013 Christian Johansen
4927
- */
4928
-
4929
- (function (sinon) {
4930
- var commonJSModule = typeof module == "object" && typeof require == "function";
4931
-
4932
- if (!sinon && commonJSModule) {
4933
- sinon = require("../sinon");
4934
- }
4935
-
4936
- if (!sinon || !Object.prototype.hasOwnProperty) {
4937
- return;
4938
- }
4939
-
4940
- function createTest(property, setUp, tearDown) {
4941
- return function () {
4942
- if (setUp) {
4943
- setUp.apply(this, arguments);
4944
- }
4945
-
4946
- var exception, result;
4947
-
4948
- try {
4949
- result = property.apply(this, arguments);
4950
- } catch (e) {
4951
- exception = e;
4952
- }
4953
-
4954
- if (tearDown) {
4955
- tearDown.apply(this, arguments);
4956
- }
4957
-
4958
- if (exception) {
4959
- throw exception;
4960
- }
4961
-
4962
- return result;
4963
- };
4964
- }
4965
-
4966
- function testCase(tests, prefix) {
4967
- /*jsl:ignore*/
4968
- if (!tests || typeof tests != "object") {
4969
- throw new TypeError("sinon.testCase needs an object with test functions");
4970
- }
4971
- /*jsl:end*/
4972
-
4973
- prefix = prefix || "test";
4974
- var rPrefix = new RegExp("^" + prefix);
4975
- var methods = {}, testName, property, method;
4976
- var setUp = tests.setUp;
4977
- var tearDown = tests.tearDown;
4978
-
4979
- for (testName in tests) {
4980
- if (tests.hasOwnProperty(testName)) {
4981
- property = tests[testName];
4982
-
4983
- if (/^(setUp|tearDown)$/.test(testName)) {
4984
- continue;
4985
- }
4986
-
4987
- if (typeof property == "function" && rPrefix.test(testName)) {
4988
- method = property;
4989
-
4990
- if (setUp || tearDown) {
4991
- method = createTest(property, setUp, tearDown);
4992
- }
4993
-
4994
- methods[testName] = sinon.test(method);
4995
- } else {
4996
- methods[testName] = tests[testName];
4997
- }
4998
- }
4999
- }
5000
-
5001
- return methods;
5002
- }
5003
-
5004
- if (commonJSModule) {
5005
- module.exports = testCase;
5006
- } else {
5007
- sinon.testCase = testCase;
5008
- }
5009
- }(typeof sinon == "object" && sinon || null));
5010
-
5011
- /**
5012
- * @depend ../sinon.js
5013
- * @depend stub.js
5014
- */
5015
- /*jslint eqeqeq: false, onevar: false, nomen: false, plusplus: false*/
5016
- /*global module, require, sinon*/
5017
- /**
5018
- * Assertions matching the test spy retrieval interface.
5019
- *
5020
- * @author Christian Johansen (christian@cjohansen.no)
5021
- * @license BSD
5022
- *
5023
- * Copyright (c) 2010-2013 Christian Johansen
5024
- */
5025
-
5026
- (function (sinon, global) {
5027
- var commonJSModule = typeof module == "object" && typeof require == "function";
5028
- var slice = Array.prototype.slice;
5029
- var assert;
5030
-
5031
- if (!sinon && commonJSModule) {
5032
- sinon = require("../sinon");
5033
- }
5034
-
5035
- if (!sinon) {
5036
- return;
5037
- }
5038
-
5039
- function verifyIsStub() {
5040
- var method;
5041
-
5042
- for (var i = 0, l = arguments.length; i < l; ++i) {
5043
- method = arguments[i];
5044
-
5045
- if (!method) {
5046
- assert.fail("fake is not a spy");
5047
- }
5048
-
5049
- if (typeof method != "function") {
5050
- assert.fail(method + " is not a function");
5051
- }
5052
-
5053
- if (typeof method.getCall != "function") {
5054
- assert.fail(method + " is not stubbed");
5055
- }
5056
- }
5057
- }
5058
-
5059
- function failAssertion(object, msg) {
5060
- object = object || global;
5061
- var failMethod = object.fail || assert.fail;
5062
- failMethod.call(object, msg);
5063
- }
5064
-
5065
- function mirrorPropAsAssertion(name, method, message) {
5066
- if (arguments.length == 2) {
5067
- message = method;
5068
- method = name;
5069
- }
5070
-
5071
- assert[name] = function (fake) {
5072
- verifyIsStub(fake);
5073
-
5074
- var args = slice.call(arguments, 1);
5075
- var failed = false;
5076
-
5077
- if (typeof method == "function") {
5078
- failed = !method(fake);
5079
- } else {
5080
- failed = typeof fake[method] == "function" ?
5081
- !fake[method].apply(fake, args) : !fake[method];
5082
- }
5083
-
5084
- if (failed) {
5085
- failAssertion(this, fake.printf.apply(fake, [message].concat(args)));
5086
- } else {
5087
- assert.pass(name);
5088
- }
5089
- };
5090
- }
5091
-
5092
- function exposedName(prefix, prop) {
5093
- return !prefix || /^fail/.test(prop) ? prop :
5094
- prefix + prop.slice(0, 1).toUpperCase() + prop.slice(1);
5095
- }
5096
-
5097
- ;
5098
-
5099
- assert = {
5100
- failException: "AssertError",
5101
-
5102
- fail: function fail(message) {
5103
- var error = new Error(message);
5104
- error.name = this.failException || assert.failException;
5105
-
5106
- throw error;
5107
- },
5108
-
5109
- pass: function pass(assertion) {
5110
- },
5111
-
5112
- callOrder: function assertCallOrder() {
5113
- verifyIsStub.apply(null, arguments);
5114
- var expected = "", actual = "";
5115
-
5116
- if (!sinon.calledInOrder(arguments)) {
5117
- try {
5118
- expected = [].join.call(arguments, ", ");
5119
- actual = sinon.orderByFirstCall(slice.call(arguments)).join(", ");
5120
- } catch (e) {
5121
- // If this fails, we'll just fall back to the blank string
5122
- }
5123
-
5124
- failAssertion(this, "expected " + expected + " to be " +
5125
- "called in order but were called as " + actual);
5126
- } else {
5127
- assert.pass("callOrder");
5128
- }
5129
- },
5130
-
5131
- callCount: function assertCallCount(method, count) {
5132
- verifyIsStub(method);
5133
-
5134
- if (method.callCount != count) {
5135
- var msg = "expected %n to be called " + sinon.timesInWords(count) +
5136
- " but was called %c%C";
5137
- failAssertion(this, method.printf(msg));
5138
- } else {
5139
- assert.pass("callCount");
5140
- }
5141
- },
5142
-
5143
- expose: function expose(target, options) {
5144
- if (!target) {
5145
- throw new TypeError("target is null or undefined");
5146
- }
5147
-
5148
- var o = options || {};
5149
- var prefix = typeof o.prefix == "undefined" && "assert" || o.prefix;
5150
- var includeFail = typeof o.includeFail == "undefined" || !!o.includeFail;
5151
-
5152
- for (var method in this) {
5153
- if (method != "export" && (includeFail || !/^(fail)/.test(method))) {
5154
- target[exposedName(prefix, method)] = this[method];
5155
- }
5156
- }
5157
-
5158
- return target;
5159
- }
5160
- };
5161
-
5162
- mirrorPropAsAssertion("called", "expected %n to have been called at least once but was never called");
5163
- mirrorPropAsAssertion("notCalled", function (spy) {
5164
- return !spy.called;
5165
- },
5166
- "expected %n to not have been called but was called %c%C");
5167
- mirrorPropAsAssertion("calledOnce", "expected %n to be called once but was called %c%C");
5168
- mirrorPropAsAssertion("calledTwice", "expected %n to be called twice but was called %c%C");
5169
- mirrorPropAsAssertion("calledThrice", "expected %n to be called thrice but was called %c%C");
5170
- mirrorPropAsAssertion("calledOn", "expected %n to be called with %1 as this but was called with %t");
5171
- mirrorPropAsAssertion("alwaysCalledOn", "expected %n to always be called with %1 as this but was called with %t");
5172
- mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
5173
- mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
5174
- mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
5175
- mirrorPropAsAssertion("calledWithMatch", "expected %n to be called with match %*%C");
5176
- mirrorPropAsAssertion("alwaysCalledWith", "expected %n to always be called with arguments %*%C");
5177
- mirrorPropAsAssertion("alwaysCalledWithMatch", "expected %n to always be called with match %*%C");
5178
- mirrorPropAsAssertion("calledWithExactly", "expected %n to be called with exact arguments %*%C");
5179
- mirrorPropAsAssertion("alwaysCalledWithExactly", "expected %n to always be called with exact arguments %*%C");
5180
- mirrorPropAsAssertion("neverCalledWith", "expected %n to never be called with arguments %*%C");
5181
- mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called with match %*%C");
5182
- mirrorPropAsAssertion("threw", "%n did not throw exception%C");
5183
- mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");
5184
-
5185
- if (commonJSModule) {
5186
- module.exports = assert;
5187
- } else {
5188
- sinon.assert = assert;
5189
- }
5190
- }(typeof sinon == "object" && sinon || null, typeof window != "undefined" ? window : global));
5191
-
5192
- return sinon;
5193
- }.call(typeof window != 'undefined' && window || {}));