massUnwrap.tap.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict'
  2. var tap = require('tap')
  3. var test = tap.test
  4. var sinon = require('sinon')
  5. var shimmer = require('../index.js')
  6. var outsider = 0
  7. function counter () { return ++outsider }
  8. function anticounter () { return --outsider }
  9. var generator = {
  10. inc: counter,
  11. dec: anticounter
  12. }
  13. test('should unwrap safely', function (t) {
  14. t.plan(18)
  15. t.equal(counter, generator.inc, 'basic function equality testing should work')
  16. t.equal(anticounter, generator.dec, 'basic function equality testing should work')
  17. t.doesNotThrow(function () { generator.inc() })
  18. t.equal(1, outsider, 'calls have side effects')
  19. t.doesNotThrow(function () { generator.dec() })
  20. t.equal(0, outsider, 'calls have side effects')
  21. function wrapper (original) {
  22. return function () {
  23. return original.apply(this, arguments)
  24. }
  25. }
  26. shimmer.massWrap(generator, ['inc', 'dec'], wrapper)
  27. t.doesNotEqual(counter, generator.inc, 'function should be wrapped')
  28. t.doesNotEqual(anticounter, generator.dec, 'function should be wrapped')
  29. t.doesNotThrow(function () { generator.inc() })
  30. t.equal(1, outsider, 'original function has still been called')
  31. t.doesNotThrow(function () { generator.dec() })
  32. t.equal(0, outsider, 'original function has still been called')
  33. shimmer.massUnwrap(generator, ['inc', 'dec'])
  34. t.equal(counter, generator.inc, 'basic function equality testing should work')
  35. t.equal(anticounter, generator.dec, 'basic function equality testing should work')
  36. t.doesNotThrow(function () { generator.inc() })
  37. t.equal(1, outsider, 'original function has still been called')
  38. t.doesNotThrow(function () { generator.dec() })
  39. t.equal(0, outsider, 'original function has still been called')
  40. })
  41. test("shouldn't throw on double unwrapping", function (t) {
  42. t.plan(10)
  43. t.equal(counter, generator.inc, 'basic function equality testing should work')
  44. t.equal(anticounter, generator.dec, 'basic function equality testing should work')
  45. var mock = sinon.stub()
  46. shimmer({ logger: mock })
  47. function wrapper (original) {
  48. return function () {
  49. return original.apply(this, arguments)
  50. }
  51. }
  52. shimmer.wrap(generator, 'inc', wrapper)
  53. shimmer.wrap(generator, 'dec', wrapper)
  54. t.doesNotEqual(counter, generator.inc, 'function should be wrapped')
  55. t.doesNotEqual(anticounter, generator.dec, 'function should be wrapped')
  56. shimmer.massUnwrap(generator, ['inc', 'dec'])
  57. t.equal(counter, generator.inc, 'basic function equality testing should work')
  58. t.equal(anticounter, generator.dec, 'basic function equality testing should work')
  59. t.doesNotThrow(function () { shimmer.massUnwrap(generator, ['inc', 'dec']) },
  60. 'should double unwrap without issue')
  61. t.equal(counter, generator.inc, 'function is unchanged after unwrapping')
  62. t.equal(anticounter, generator.dec, 'function is unchanged after unwrapping')
  63. t.doesNotThrow(function () {
  64. sinon.assert.calledWith(mock, 'no original to unwrap to -- ' +
  65. 'has inc already been unwrapped?')
  66. sinon.assert.calledWith(mock, 'no original to unwrap to -- ' +
  67. 'has dec already been unwrapped?')
  68. sinon.assert.calledTwice(mock)
  69. }, 'logger was called with the expected message')
  70. })
  71. test('massUnwrap called with no arguments', function (t) {
  72. t.plan(2)
  73. var mock = sinon.expectation
  74. .create('logger')
  75. .twice()
  76. shimmer({ logger: mock })
  77. t.doesNotThrow(function () { shimmer.massUnwrap() }, 'should log instead of throwing')
  78. t.doesNotThrow(function () {
  79. mock.verify()
  80. }, 'logger was called with the expected message')
  81. })
  82. test('massUnwrap called with module but nothing else', function (t) {
  83. t.plan(2)
  84. var mock = sinon.expectation
  85. .create('logger')
  86. .withExactArgs('must provide one or more functions to unwrap on modules')
  87. .once()
  88. shimmer({ logger: mock })
  89. t.doesNotThrow(function () {
  90. shimmer.massUnwrap(generator)
  91. }, "wrapping with only 1 argument doesn't throw")
  92. t.doesNotThrow(function () {
  93. mock.verify()
  94. }, 'logger was called with the expected message')
  95. })