massWrap.tap.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. var arrow = {
  14. in: counter,
  15. out: anticounter
  16. }
  17. var nester = {
  18. in: counter,
  19. out: anticounter
  20. }
  21. test('should wrap multiple functions safely', function (t) {
  22. t.plan(9)
  23. t.equal(counter, generator.inc, 'basic function equality testing should work')
  24. t.equal(anticounter, generator.dec, 'basic function equality testing should work')
  25. t.doesNotThrow(function () { generator.inc() })
  26. t.doesNotThrow(function () { generator.dec() })
  27. t.equal(0, outsider, 'calls have side effects')
  28. var count = 0
  29. function wrapper (original) {
  30. return function () {
  31. count++
  32. var returned = original.apply(this, arguments)
  33. count++
  34. return returned
  35. }
  36. }
  37. shimmer.massWrap(generator, ['inc', 'dec'], wrapper)
  38. t.doesNotThrow(function () { generator.inc() })
  39. t.doesNotThrow(function () { generator.dec() })
  40. t.equal(4, count, 'both pre and post increments should have happened')
  41. t.equal(0, outsider, 'original function has still been called')
  42. })
  43. test('should wrap multiple functions on multiple modules safely', function (t) {
  44. t.plan(15)
  45. t.equal(counter, arrow.in, 'basic function equality testing should work')
  46. t.equal(counter, nester.in, 'basic function equality testing should work')
  47. t.equal(anticounter, arrow.out, 'basic function equality testing should work')
  48. t.equal(anticounter, nester.out, 'basic function equality testing should work')
  49. t.doesNotThrow(function () { arrow.in() })
  50. t.doesNotThrow(function () { nester.in() })
  51. t.doesNotThrow(function () { arrow.out() })
  52. t.doesNotThrow(function () { nester.out() })
  53. t.equal(0, outsider, 'calls have side effects')
  54. var count = 0
  55. function wrapper (original) {
  56. return function () {
  57. count++
  58. var returned = original.apply(this, arguments)
  59. count++
  60. return returned
  61. }
  62. }
  63. shimmer.massWrap([arrow, nester], ['in', 'out'], wrapper)
  64. t.doesNotThrow(function () { arrow.in() })
  65. t.doesNotThrow(function () { arrow.out() })
  66. t.doesNotThrow(function () { nester.in() })
  67. t.doesNotThrow(function () { nester.out() })
  68. t.equal(8, count, 'both pre and post increments should have happened')
  69. t.equal(0, outsider, 'original function has still been called')
  70. })
  71. test('wrap 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 () {
  78. shimmer.massWrap()
  79. }, "wrapping with no arguments doesn't throw")
  80. t.doesNotThrow(function () {
  81. mock.verify()
  82. }, 'logger was called with the expected message')
  83. })
  84. test('wrap called with module but nothing else', function (t) {
  85. t.plan(2)
  86. var mock = sinon.expectation
  87. .create('logger')
  88. .withExactArgs('must provide one or more functions to wrap on modules')
  89. .once()
  90. shimmer({ logger: mock })
  91. t.doesNotThrow(function () {
  92. shimmer.massWrap(generator)
  93. }, "wrapping with only 1 argument doesn't throw")
  94. t.doesNotThrow(function () {
  95. mock.verify()
  96. }, 'logger was called with the expected message')
  97. })
  98. test('wrap called with original but no wrapper', function (t) {
  99. t.plan(2)
  100. var mock = sinon.expectation
  101. .create('logger')
  102. .twice()
  103. shimmer({ logger: mock })
  104. t.doesNotThrow(function () {
  105. shimmer.massWrap(generator, ['inc'])
  106. }, "wrapping with only original function doesn't throw")
  107. t.doesNotThrow(function () {
  108. mock.verify()
  109. }, 'logger was called with the expected message')
  110. })
  111. test('wrap called with non-function original', function (t) {
  112. t.plan(2)
  113. var mock = sinon.expectation
  114. .create('logger')
  115. .withExactArgs('must provide one or more functions to wrap on modules')
  116. .once()
  117. shimmer({ logger: mock })
  118. t.doesNotThrow(function () {
  119. shimmer.massWrap({ orange: 'slices' }, 'orange', function () {})
  120. }, "wrapping non-function original doesn't throw")
  121. t.doesNotThrow(function () {
  122. mock.verify()
  123. }, 'logger was called with the expected message')
  124. })
  125. test('wrap called with non-function wrapper', function (t) {
  126. t.plan(2)
  127. var mock = sinon.expectation
  128. .create('logger')
  129. .withArgs('must provide one or more functions to wrap on modules')
  130. .once()
  131. shimmer({ logger: mock })
  132. t.doesNotThrow(function () {
  133. shimmer.massWrap({ orange: function () {} }, 'orange', 'hamchunx')
  134. }, "wrapping with non-function wrapper doesn't throw")
  135. t.doesNotThrow(function () {
  136. mock.verify()
  137. }, 'logger was called with the expected message')
  138. })