utils.js 3.9 KB

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