unwrap.tap.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. var generator = {
  9. inc: counter
  10. }
  11. test('should unwrap safely', function (t) {
  12. t.plan(9)
  13. t.equal(counter, generator.inc, 'basic function equality testing should work')
  14. t.doesNotThrow(function () { generator.inc() })
  15. t.equal(1, outsider, 'calls have side effects')
  16. function wrapper (original) {
  17. return function () {
  18. return original.apply(this, arguments)
  19. }
  20. }
  21. shimmer.wrap(generator, 'inc', wrapper)
  22. t.doesNotEqual(counter, generator.inc, 'function should be wrapped')
  23. t.doesNotThrow(function () { generator.inc() })
  24. t.equal(2, outsider, 'original function has still been called')
  25. shimmer.unwrap(generator, 'inc')
  26. t.equal(counter, generator.inc, 'basic function equality testing should work')
  27. t.doesNotThrow(function () { generator.inc() })
  28. t.equal(3, outsider, 'original function has still been called')
  29. })
  30. test("shouldn't throw on double unwrapping", function (t) {
  31. t.plan(6)
  32. t.equal(counter, generator.inc, 'basic function equality testing should work')
  33. var mock = sinon.expectation
  34. .create('logger')
  35. .withArgs('no original to unwrap to -- ' +
  36. 'has inc already been unwrapped?')
  37. .once()
  38. shimmer({ logger: mock })
  39. function wrapper (original) {
  40. return function () {
  41. return original.apply(this, arguments)
  42. }
  43. }
  44. shimmer.wrap(generator, 'inc', wrapper)
  45. t.doesNotEqual(counter, generator.inc, 'function should be wrapped')
  46. shimmer.unwrap(generator, 'inc')
  47. t.equal(counter, generator.inc, 'basic function equality testing should work')
  48. t.doesNotThrow(function () { shimmer.unwrap(generator, 'inc') },
  49. 'should double unwrap without issue')
  50. t.equal(counter, generator.inc, 'function is unchanged after unwrapping')
  51. t.doesNotThrow(function () {
  52. mock.verify()
  53. }, 'logger was called with the expected message')
  54. })
  55. test('unwrap called with no arguments', function (t) {
  56. t.plan(2)
  57. var mock = sinon.expectation
  58. .create('logger')
  59. .twice()
  60. shimmer({ logger: mock })
  61. t.doesNotThrow(function () { shimmer.unwrap() }, 'should log instead of throwing')
  62. t.doesNotThrow(function () {
  63. mock.verify()
  64. }, 'logger was called with the expected message')
  65. })
  66. test('unwrap called with module but no name', function (t) {
  67. t.plan(2)
  68. var mock = sinon.expectation
  69. .create('logger')
  70. .twice()
  71. shimmer({ logger: mock })
  72. t.doesNotThrow(function () { shimmer.unwrap({}) }, 'should log instead of throwing')
  73. t.doesNotThrow(function () {
  74. mock.verify()
  75. }, 'logger was called with the expected message')
  76. })