8
0

utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals describe, it, expect */
  6. 'use strict';
  7. bender.amd.require( 'utils' );
  8. describe( 'extend()', function( utils ) {
  9. it( 'should extend and override', function() {
  10. var target = {
  11. a: 1,
  12. b: 2
  13. };
  14. var extensions = {
  15. a: 'A',
  16. c: 3,
  17. // Extend by reference (no deep-copy).
  18. obj: {
  19. a: 1
  20. },
  21. // Extend by reference (no deep-copy).
  22. arr: [ 1, 2 ],
  23. // Extend by reference.
  24. fn: function() {}
  25. };
  26. var ret = utils.extend( target, extensions );
  27. expect( target.a ).to.equal( 'A' );
  28. expect( target.b ).to.equal( 2 );
  29. expect( target ).to.have.property( 'c' ).to.equal( 3 );
  30. expect( target ).to.have.property( 'obj' ).to.equal( extensions.obj );
  31. expect( target ).to.have.property( 'arr' ).to.equal( extensions.arr );
  32. expect( target ).to.have.property( 'fn' ).to.equal( extensions.fn );
  33. // "target" should be the return value.
  34. expect( ret ).to.equal( target );
  35. } );
  36. it( 'should not be touched by non-objects', function() {
  37. var target = {
  38. a: 1
  39. };
  40. expect( utils.extend( target, function() {} ) ).to.equal( target );
  41. expect( utils.extend( target, 1 ) ).to.equal( target );
  42. expect( utils.extend( target, 'a' ) ).to.equal( target );
  43. expect( utils.extend( target, true ) ).to.equal( target );
  44. expect( utils.extend( target, undefined ) ).to.equal( target );
  45. expect( utils.extend( target, [] ) ).to.equal( target );
  46. expect( utils.extend( target, Date.now() ) ).to.equal( target );
  47. expect( utils.extend( target ) ).to.equal( target );
  48. // None of the above calls should have touched "target".
  49. expect( target ).to.have.property( 'a' ).to.equal( 1 );
  50. expect( Object.getOwnPropertyNames( target ).length ).to.equal( 1 );
  51. } );
  52. it( 'should extend by several params in order', function() {
  53. var target = {
  54. a: 0,
  55. b: 0
  56. };
  57. var ext1 = {
  58. b: 1,
  59. c: 1
  60. };
  61. var ext2 = {
  62. c: 2,
  63. d: 2
  64. };
  65. utils.extend( target, ext1, ext2 );
  66. expect( target ).to.have.property( 'a' ).to.equal( 0 );
  67. expect( target ).to.have.property( 'b' ).to.equal( 1 );
  68. expect( target ).to.have.property( 'c' ).to.equal( 2 );
  69. expect( target ).to.have.property( 'd' ).to.equal( 2 );
  70. } );
  71. } );
  72. describe( 'isFunction()', function( utils ) {
  73. it( 'should be true for functions only', function() {
  74. var f1 = function() {
  75. };
  76. /* jshint -W054 */ // The Function constructor is a form of eval
  77. var f2 = new Function( '' );
  78. /* jshint +W054 */
  79. expect( utils.isFunction( f1 ) ).to.be.true();
  80. expect( utils.isFunction( f2 ) ).to.be.true();
  81. expect( utils.isFunction( 1 ) ).to.be.false();
  82. expect( utils.isFunction( 'a' ) ).to.be.false();
  83. expect( utils.isFunction( true ) ).to.be.false();
  84. expect( utils.isFunction( undefined ) ).to.be.false();
  85. expect( utils.isFunction( [] ) ).to.be.false();
  86. expect( utils.isFunction( {} ) ).to.be.false();
  87. expect( utils.isFunction( Date.now() ) ).to.be.false();
  88. } );
  89. } );
  90. describe( 'isObject()', function( utils ) {
  91. it( 'should be true for pure objects only', function() {
  92. var f1 = function() {
  93. };
  94. /* jshint -W054 */ // The Function constructor is a form of eval
  95. var f2 = new Function( '' );
  96. /* jshint +W054 */
  97. expect( utils.isObject( {} ) ).to.be.true();
  98. expect( utils.isObject( [] ) ).to.be.true();
  99. expect( utils.isObject( 1 ) ).to.be.false();
  100. expect( utils.isObject( 'a' ) ).to.be.false();
  101. expect( utils.isObject( true ) ).to.be.false();
  102. expect( utils.isObject( undefined ) ).to.be.false();
  103. expect( utils.isObject( Date.now() ) ).to.be.false();
  104. expect( utils.isObject( f1 ) ).to.be.false();
  105. expect( utils.isObject( f2 ) ).to.be.false();
  106. expect( utils.isObject( null ) ).to.be.false();
  107. } );
  108. } );