Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* test dependencies */
  2. var clone = require('..');
  3. var expect = require('expect.js');
  4. /* tests */
  5. describe('clone', function(){
  6. it('date', function(){
  7. var obj = new Date;
  8. var cloned = clone(obj);
  9. expect(cloned.getTime()).to.be(obj.getTime());
  10. expect(cloned).not.to.be(obj);
  11. });
  12. it('regexp', function(){
  13. var obj = /hello/i;
  14. var cloned = clone(obj);
  15. expect(cloned.toString()).to.be(obj.toString());
  16. expect(cloned).not.to.be(obj);
  17. });
  18. it('array', function(){
  19. var obj = [1, 2, 3, '4'];
  20. var cloned = clone(obj);
  21. expect(cloned).to.eql(obj);
  22. expect(cloned).not.to.be(obj);
  23. });
  24. it('object', function(){
  25. var obj = {
  26. a: 1,
  27. b: 2,
  28. c: 3
  29. };
  30. var cloned = clone(obj);
  31. expect(cloned).to.eql(obj);
  32. expect(cloned).not.to.be(obj);
  33. });
  34. it('object combined', function(){
  35. var date = new Date;
  36. var obj = {
  37. a: {
  38. b: [1, 2, date, { hello: 'world' }]
  39. }
  40. };
  41. var cloned = clone(obj);
  42. expect(cloned).to.eql(obj);
  43. expect(cloned.a).not.to.be(obj.a);
  44. expect(cloned.a.b).not.to.be(obj.a.b);
  45. expect(cloned.a.b[2]).not.to.be(obj.a.b[2]);
  46. expect(cloned.a.b[2].getTime()).to.be(obj.a.b[2].getTime());
  47. expect(cloned.a.b[3]).to.eql(obj.a.b[3]);
  48. expect(cloned.a.b[3]).not.to.be(obj.a.b[3]);
  49. });
  50. it('object with functions', function() {
  51. var func = function () { return 'original'; };
  52. var host = { fluent: func };
  53. var cloned = clone(host);
  54. // cloned function matches original
  55. expect(cloned.fluent).to.be(func);
  56. // change cloned function (no longer matches original)
  57. cloned.fluent = function () { return 'updated'; };
  58. expect(cloned.fluent).not.to.be(func);
  59. expect(cloned.fluent()).to.be('updated');
  60. // original function is still in place
  61. expect(func()).to.be('original');
  62. });
  63. });