Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

test-utils.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. var utils = require('../lib/utils');
  2. var path = require('path');
  3. var assert = require('assert');
  4. var t = -1;
  5. var group = path.basename(__filename, '.js') + '/';
  6. var tests = [
  7. { run: function() {
  8. var what = this.what;
  9. var r;
  10. assert.strictEqual(r = utils.readInt(Buffer.from([0,0,0]), 0),
  11. false,
  12. makeMsg(what, 'Wrong result: ' + r));
  13. next();
  14. },
  15. what: 'readInt - without stream callback - failure #1'
  16. },
  17. { run: function() {
  18. var what = this.what;
  19. var r;
  20. assert.strictEqual(r = utils.readInt(Buffer.from([]), 0),
  21. false,
  22. makeMsg(what, 'Wrong result: ' + r));
  23. next();
  24. },
  25. what: 'readInt - without stream callback - failure #2'
  26. },
  27. { run: function() {
  28. var what = this.what;
  29. var r;
  30. assert.strictEqual(r = utils.readInt(Buffer.from([0,0,0,5]), 0),
  31. 5,
  32. makeMsg(what, 'Wrong result: ' + r));
  33. next();
  34. },
  35. what: 'readInt - without stream callback - success'
  36. },
  37. { run: function() {
  38. var what = this.what;
  39. var callback = function() {};
  40. var stream = {
  41. _cleanup: function(cb) {
  42. cleanupCalled = true;
  43. assert(cb === callback, makeMsg(what, 'Wrong callback'));
  44. }
  45. };
  46. var cleanupCalled = false;
  47. var r = utils.readInt(Buffer.from([]), 0, stream, callback);
  48. assert.strictEqual(r,
  49. false,
  50. makeMsg(what, 'Wrong result: ' + r));
  51. assert(cleanupCalled, makeMsg(what, 'Cleanup not called'));
  52. next();
  53. },
  54. what: 'readInt - with stream callback'
  55. },
  56. { run: function() {
  57. var what = this.what;
  58. var r;
  59. assert.strictEqual(r = utils.readString(Buffer.from([0,0,0]), 0),
  60. false,
  61. makeMsg(what, 'Wrong result: ' + r));
  62. next();
  63. },
  64. what: 'readString - without stream callback - bad length #1'
  65. },
  66. { run: function() {
  67. var what = this.what;
  68. var r;
  69. assert.strictEqual(r = utils.readString(Buffer.from([]), 0),
  70. false,
  71. makeMsg(what, 'Wrong result: ' + r));
  72. next();
  73. },
  74. what: 'readString - without stream callback - bad length #2'
  75. },
  76. { run: function() {
  77. var what = this.what;
  78. var r;
  79. assert.deepEqual(r = utils.readString(Buffer.from([0,0,0,1,5]), 0),
  80. Buffer.from([5]),
  81. makeMsg(what, 'Wrong result: ' + r));
  82. next();
  83. },
  84. what: 'readString - without stream callback - success'
  85. },
  86. { run: function() {
  87. var what = this.what;
  88. var r = utils.readString(Buffer.from([0,0,0,1,33]), 0, 'ascii');
  89. assert.deepEqual(r,
  90. '!',
  91. makeMsg(what, 'Wrong result: ' + r));
  92. next();
  93. },
  94. what: 'readString - without stream callback - encoding'
  95. },
  96. { run: function() {
  97. var what = this.what;
  98. var callback = function() {};
  99. var stream = {
  100. _cleanup: function(cb) {
  101. cleanupCalled = true;
  102. assert(cb === callback, makeMsg(what, 'Wrong callback'));
  103. }
  104. };
  105. var cleanupCalled = false;
  106. var r;
  107. assert.deepEqual(r = utils.readString(Buffer.from([0,0,0,1]),
  108. 0,
  109. stream,
  110. callback),
  111. false,
  112. makeMsg(what, 'Wrong result: ' + r));
  113. assert(cleanupCalled, makeMsg(what, 'Cleanup not called'));
  114. next();
  115. },
  116. what: 'readString - with stream callback - no encoding'
  117. },
  118. { run: function() {
  119. var what = this.what;
  120. var callback = function() {};
  121. var stream = {
  122. _cleanup: function(cb) {
  123. cleanupCalled = true;
  124. assert(cb === callback, makeMsg(what, 'Wrong callback'));
  125. }
  126. };
  127. var cleanupCalled = false;
  128. var r;
  129. assert.deepEqual(r = utils.readString(Buffer.from([0,0,0,1]),
  130. 0,
  131. 'ascii',
  132. stream,
  133. callback),
  134. false,
  135. makeMsg(what, 'Wrong result: ' + r));
  136. assert(cleanupCalled, makeMsg(what, 'Cleanup not called'));
  137. next();
  138. },
  139. what: 'readString - with stream callback - encoding'
  140. },
  141. ];
  142. function next() {
  143. if (Array.isArray(process._events.exit))
  144. process._events.exit = process._events.exit[1];
  145. if (++t === tests.length)
  146. return;
  147. var v = tests[t];
  148. process.nextTick(function() {
  149. v.run.call(v);
  150. });
  151. }
  152. function makeMsg(what, msg) {
  153. return '[' + group + what + ']: ' + msg;
  154. }
  155. process.once('exit', function() {
  156. assert(t === tests.length,
  157. makeMsg('_exit',
  158. 'Only finished ' + t + '/' + tests.length + ' tests'));
  159. });
  160. next();