test.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var tape = require('tape')
  2. var from = require('from2')
  3. var iterate = require('./')
  4. tape('merge sort', function (t) {
  5. var a = from.obj(['a', 'b', 'd', 'e', 'g', 'h'])
  6. var b = from.obj(['b', 'c', 'f'])
  7. var output = []
  8. var readA = iterate(a)
  9. var readB = iterate(b)
  10. var loop = function () {
  11. readA(function (err, dataA, nextA) {
  12. if (err) throw err
  13. readB(function (err, dataB, nextB) {
  14. if (err) throw err
  15. if (!dataA && !dataB) {
  16. t.same(output, ['a', 'b', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], 'sorts list')
  17. t.end()
  18. return
  19. }
  20. if (!dataB || dataA < dataB) {
  21. output.push(dataA)
  22. nextA()
  23. return loop()
  24. }
  25. if (!dataA || dataA > dataB) {
  26. output.push(dataB)
  27. nextB()
  28. return loop()
  29. }
  30. output.push(dataA)
  31. output.push(dataB)
  32. nextA()
  33. nextB()
  34. loop()
  35. })
  36. })
  37. }
  38. loop()
  39. })
  40. tape('error handling', function (t) {
  41. var a = from.obj(['a', 'b', 'd', 'e', 'g', 'h'])
  42. var read = iterate(a)
  43. a.destroy(new Error('oh no'))
  44. read(function (err) {
  45. t.ok(err, 'had error')
  46. t.same(err.message, 'oh no')
  47. t.end()
  48. })
  49. })