test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. var test = require('tape');
  3. var noms = require('./');
  4. function countObj(num) {
  5. var i = 20;
  6. return noms.obj(function(next) {
  7. if (++i < num) {
  8. this.push({
  9. num: i
  10. });
  11. } else {
  12. this.push(null);
  13. }
  14. process.nextTick(function () {
  15. next();
  16. });
  17. }, function (next){
  18. this.push({
  19. num: 0
  20. });
  21. i = 1;
  22. next(null, {num: 1});
  23. });
  24. }
  25. function countObjWithNext(num) {
  26. var i = -1;
  27. return noms.obj(function(next) {
  28. if (++i < num) {
  29. process.nextTick(function () {
  30. next(null, {
  31. num: i
  32. });
  33. });
  34. } else {
  35. process.nextTick(function () {
  36. next(null, null);
  37. });
  38. }
  39. });
  40. }
  41. function dripWordAsync(string, opts) {
  42. // from from2's tests
  43. return noms(opts||{}, function(size, next) {
  44. if (string.length <= 0) {
  45. return next(null, null);
  46. }
  47. var chunk = string.slice(0, size);
  48. string = string.slice(size);
  49. process.nextTick(function () {
  50. next(null, chunk);
  51. });
  52. });
  53. }
  54. function dripWord(string, opts) {
  55. // from from2's tests
  56. return noms(opts||{}, function(size, next) {
  57. if (string.length <= 0) {
  58. return next(null, null);
  59. }
  60. var chunk = string.slice(0, size);
  61. string = string.slice(size);
  62. next(null, chunk);
  63. });
  64. }
  65. test('works', function (t) {
  66. t.plan(10);
  67. countObj(10).on('data', function (d) {
  68. t.ok(true, d.num);
  69. });
  70. });
  71. test('works with next', function (t) {
  72. t.plan(10);
  73. countObjWithNext(10).on('data', function (d) {
  74. t.ok(true, d.num);
  75. });
  76. });
  77. test('works with size 1', function (t) {
  78. t.plan(3);
  79. var stream = dripWord('abc');
  80. t.equals(stream.read(1).toString(), 'a');
  81. t.equals(stream.read(1).toString(), 'b');
  82. t.equals(stream.read(1).toString(), 'c');
  83. });
  84. test('works with size 2', function (t) {
  85. t.plan(3);
  86. dripWord('abcde', {highWaterMark: 2}).on('data', function (d) {
  87. t.ok(true, d.toString());
  88. });
  89. });
  90. test('works with size async 1', function (t) {
  91. t.plan(3);
  92. var stream = dripWordAsync('abc');
  93. stream.on('readable', function () {
  94. t.equals(stream.read(1).toString(), 'a');
  95. t.equals(stream.read(1).toString(), 'b');
  96. t.equals(stream.read(1).toString(), 'c');
  97. });
  98. });
  99. test('works with size async 2', function (t) {
  100. t.plan(3);
  101. dripWordAsync('abcde', {highWaterMark: 2}).on('data', function (d) {
  102. t.ok(true, d.toString());
  103. });
  104. });