tests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict';
  2. var hasSymbols = require('has-symbols/shams')();
  3. var forEach = require('for-each');
  4. module.exports = function (assign, t) {
  5. t.test('error cases', function (st) {
  6. st['throws'](function () { assign(null); }, TypeError, 'target must be an object');
  7. st['throws'](function () { assign(undefined); }, TypeError, 'target must be an object');
  8. st['throws'](function () { assign(null, {}); }, TypeError, 'target must be an object');
  9. st['throws'](function () { assign(undefined, {}); }, TypeError, 'target must be an object');
  10. st.end();
  11. });
  12. t.test('non-object target, no sources', function (st) {
  13. var bool = assign(true);
  14. st.equal(typeof bool, 'object', 'bool is object');
  15. st.equal(Boolean.prototype.valueOf.call(bool), true, 'bool coerces to `true`');
  16. var number = assign(1);
  17. st.equal(typeof number, 'object', 'number is object');
  18. st.equal(Number.prototype.valueOf.call(number), 1, 'number coerces to `1`');
  19. var string = assign('1');
  20. st.equal(typeof string, 'object', 'number is object');
  21. st.equal(String.prototype.valueOf.call(string), '1', 'number coerces to `"1"`');
  22. st.end();
  23. });
  24. t.test('non-object target, with sources', function (st) {
  25. var signal = {};
  26. st.test('boolean', function (st2) {
  27. var bool = assign(true, { a: signal });
  28. st2.equal(typeof bool, 'object', 'bool is object');
  29. st.equal(Boolean.prototype.valueOf.call(bool), true, 'bool coerces to `true`');
  30. st2.equal(bool.a, signal, 'source properties copied');
  31. st2.end();
  32. });
  33. st.test('number', function (st2) {
  34. var number = assign(1, { a: signal });
  35. st2.equal(typeof number, 'object', 'number is object');
  36. st2.equal(Number.prototype.valueOf.call(number), 1, 'number coerces to `1`');
  37. st2.equal(number.a, signal, 'source properties copied');
  38. st2.end();
  39. });
  40. st.test('string', function (st2) {
  41. var string = assign('1', { a: signal });
  42. st2.equal(typeof string, 'object', 'number is object');
  43. st2.equal(String.prototype.valueOf.call(string), '1', 'number coerces to `"1"`');
  44. st2.equal(string.a, signal, 'source properties copied');
  45. st2.end();
  46. });
  47. st.end();
  48. });
  49. t.test('non-object sources', function (st) {
  50. st.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 }, 'ignores null source');
  51. st.deepEqual(assign({ a: 1 }, { b: 2 }, undefined), { a: 1, b: 2 }, 'ignores undefined source');
  52. st.end();
  53. });
  54. t.test('returns the modified target object', function (st) {
  55. var target = {};
  56. var returned = assign(target, { a: 1 });
  57. st.equal(returned, target, 'returned object is the same reference as the target object');
  58. st.end();
  59. });
  60. t.test('has the right length', function (st) {
  61. st.equal(assign.length, 2, 'length is 2 => 2 required arguments');
  62. st.end();
  63. });
  64. t.test('merge two objects', function (st) {
  65. var target = { a: 1 };
  66. var returned = assign(target, { b: 2 });
  67. st.deepEqual(returned, { a: 1, b: 2 }, 'returned object has properties from both');
  68. st.end();
  69. });
  70. t.test('works with functions', function (st) {
  71. var target = function () {};
  72. target.a = 1;
  73. var returned = assign(target, { b: 2 });
  74. st.equal(target, returned, 'returned object is target');
  75. st.equal(returned.a, 1);
  76. st.equal(returned.b, 2);
  77. st.end();
  78. });
  79. t.test('works with primitives', function (st) {
  80. var target = 2;
  81. var source = { b: 42 };
  82. var returned = assign(target, source);
  83. st.equal(Object.prototype.toString.call(returned), '[object Number]', 'returned is object form of number primitive');
  84. st.equal(Number(returned), target, 'returned and target have same valueOf');
  85. st.equal(returned.b, source.b);
  86. st.end();
  87. });
  88. t.test('merge N objects', function (st) {
  89. var target = { a: 1 };
  90. var source1 = { b: 2 };
  91. var source2 = { c: 3 };
  92. var returned = assign(target, source1, source2);
  93. st.deepEqual(returned, { a: 1, b: 2, c: 3 }, 'returned object has properties from all sources');
  94. st.end();
  95. });
  96. t.test('only iterates over own keys', function (st) {
  97. var Foo = function () {};
  98. Foo.prototype.bar = true;
  99. var foo = new Foo();
  100. foo.baz = true;
  101. var target = { a: 1 };
  102. var returned = assign(target, foo);
  103. st.equal(returned, target, 'returned object is the same reference as the target object');
  104. st.deepEqual(target, { a: 1, baz: true }, 'returned object has only own properties from both');
  105. st.end();
  106. });
  107. t.test('includes enumerable symbols, after keys', { skip: !hasSymbols }, function (st) {
  108. var visited = [];
  109. var obj = {};
  110. Object.defineProperty(obj, 'a', { enumerable: true, get: function () { visited.push('a'); return 42; } });
  111. var symbol = Symbol('enumerable');
  112. Object.defineProperty(obj, symbol, {
  113. enumerable: true,
  114. get: function () { visited.push(symbol); return Infinity; }
  115. });
  116. var nonEnumSymbol = Symbol('non-enumerable');
  117. Object.defineProperty(obj, nonEnumSymbol, {
  118. enumerable: false,
  119. get: function () { visited.push(nonEnumSymbol); return -Infinity; }
  120. });
  121. var target = assign({}, obj);
  122. st.deepEqual(visited, ['a', symbol], 'key is visited first, then symbol');
  123. st.equal(target.a, 42, 'target.a is 42');
  124. st.equal(target[symbol], Infinity, 'target[symbol] is Infinity');
  125. st.notEqual(target[nonEnumSymbol], -Infinity, 'target[nonEnumSymbol] is not -Infinity');
  126. st.end();
  127. });
  128. t.test('does not fail when symbols are not present', function (st) {
  129. var getSyms;
  130. if (hasSymbols) {
  131. getSyms = Object.getOwnPropertySymbols;
  132. delete Object.getOwnPropertySymbols;
  133. }
  134. var visited = [];
  135. var obj = {};
  136. Object.defineProperty(obj, 'a', { enumerable: true, get: function () { visited.push('a'); return 42; } });
  137. var keys = ['a'];
  138. if (hasSymbols) {
  139. var symbol = Symbol('sym');
  140. Object.defineProperty(obj, symbol, {
  141. enumerable: true,
  142. get: function () { visited.push(symbol); return Infinity; }
  143. });
  144. keys.push(symbol);
  145. }
  146. var target = assign({}, obj);
  147. st.deepEqual(visited, keys, 'assign visits expected keys');
  148. st.equal(target.a, 42, 'target.a is 42');
  149. if (hasSymbols) {
  150. st.equal(target[symbol], Infinity);
  151. Object.getOwnPropertySymbols = getSyms;
  152. }
  153. st.end();
  154. });
  155. t.test('preserves correct property enumeration order', function (st) {
  156. var str = 'abcdefghijklmnopqrst';
  157. var letters = {};
  158. forEach(str.split(''), function (letter) {
  159. letters[letter] = letter;
  160. });
  161. var n = 5;
  162. st.comment('run the next test ' + n + ' times');
  163. var object = assign({}, letters);
  164. var actual = '';
  165. for (var k in object) {
  166. actual += k;
  167. }
  168. for (var i = 0; i < n; ++i) {
  169. st.equal(actual, str, 'property enumeration order should be followed');
  170. }
  171. st.end();
  172. });
  173. t.test('checks enumerability and existence, in case of modification during [[Get]]', { skip: !Object.defineProperty }, function (st) {
  174. var targetBvalue = {};
  175. var targetCvalue = {};
  176. var target = { b: targetBvalue, c: targetCvalue };
  177. var source = {};
  178. Object.defineProperty(source, 'a', {
  179. enumerable: true,
  180. get: function () {
  181. delete this.b;
  182. Object.defineProperty(this, 'c', { enumerable: false });
  183. return 'a';
  184. }
  185. });
  186. var sourceBvalue = {};
  187. var sourceCvalue = {};
  188. source.b = sourceBvalue;
  189. source.c = sourceCvalue;
  190. var result = assign(target, source);
  191. st.equal(result, target, 'sanity check: result is === target');
  192. st.equal(result.b, targetBvalue, 'target key not overwritten by deleted source key');
  193. st.equal(result.c, targetCvalue, 'target key not overwritten by non-enumerable source key');
  194. st.end();
  195. });
  196. };