Guest User

Untitled

a guest
Jun 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. const PRESTART = 0
  2. , STARTED = 1
  3. , STARTDONE = 2
  4. , STARTERRORED = 3
  5. ;
  6. /* convenience function for wrapping a suite with setup and teardown
  7. * functions. this takes an object which has three properties:
  8. *
  9. * (by the way, I'm looking for a better name for this function)
  10. *
  11. * suite: the test suite object, required
  12. * setup: a function that should be run before the test
  13. * teardown: a function that should be run after the test
  14. * teardown: a function that should be run after the test
  15. * suiteSetup: a function run before any tests in a suite are ran
  16. */
  17. exports.wrap = function(obj) {
  18. var suite = obj.suite
  19. , setup = obj.setup
  20. , teardown = obj.teardown
  21. , suiteSetup = obj.suiteSetup
  22. ;
  23.  
  24. if (!suite) {
  25. throw new Error('Cannot wrap suite -- no suite provided');
  26. }
  27.  
  28. for(var key in suite) {
  29. if (typeof suite[key] == 'function') {
  30. suite[key] = wrapper(suite[key]);
  31. }
  32. else {
  33. exports.wrap({suite: suite[key], setup: setup, teardown: teardown});
  34. }
  35. }
  36.  
  37. var state = 0
  38. , pendingTests = []
  39. , controlError
  40. ;
  41.  
  42. function wrapper(func) {
  43. var newFunc = function(test) {
  44. if (teardown) {
  45. var finish = test.finish;
  46. test.finish = function() {
  47. teardown(test, finish);
  48. }
  49. }
  50.  
  51. if (setup) {
  52. setup(test, function() { func(test); });
  53. }
  54. else {
  55. func(test);
  56. }
  57. }
  58.  
  59. if (suiteSetup) {
  60. // for async suiteStartup
  61. var setupDone = function() {
  62. state = STARTDONE;
  63. process.removeListener('uncaughtException', setupErrored);
  64. for (var i = 0; i < pendingTests.length; i++) {
  65. pendingTests[i][0](pendingTests[i][1]);
  66. }
  67. }
  68.  
  69. var setupErrored = function(err) {
  70. state = STARTERRORED;
  71. controlError = err;
  72.  
  73. if (pendingTests.length > 1) {
  74. pendingTests.pop();
  75. process.removeListener('uncaughtException', setupErrored);
  76. process.nextTick(function() {
  77. throw err;
  78. });
  79. }
  80. }
  81.  
  82. // wrapping newFunc
  83. var f = newFunc;
  84. newFunc = function(test) {
  85. switch(state) {
  86. case PRESTART: // suiteSetup hasn't been ran
  87. state = STARTED;
  88. process.on('uncaughtException', setupErrored);
  89. pendingTests.push([f, test]);
  90.  
  91. try {
  92. suiteSetup(setupDone);
  93. }
  94. catch(e) {
  95. setupErrored(e);
  96. }
  97. break;
  98. case STARTED: // suiteSetup is running
  99. pendingTests.push([f, test]);
  100. break;
  101. case STARTDONE: // suiteSetup is done
  102. f(test);
  103. break;
  104. case STARTERRORED: // need to error the test
  105. (function() {
  106. throw controlError;
  107. })();
  108. break;
  109. }
  110. }
  111. }
  112.  
  113. newFunc.toString = function() {
  114. return (suiteSetup ? suiteSetup+'\n' : '') +
  115. (setup ? setup+'\n' : '') +
  116. func +
  117. (teardown ? '\n'+teardown : '')
  118. ;
  119. }
  120.  
  121. return newFunc;
  122. }
  123.  
  124. return suite;
  125. }
Add Comment
Please, Sign In to add comment