loop_scan.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var binary = require('../');
  2. var test = require('tap').test;
  3. var EventEmitter = require('events').EventEmitter;
  4. test('loop scan', function (t) {
  5. t.plan(8 + 6 + 2);
  6. var em = new EventEmitter;
  7. binary.stream(em)
  8. .loop(function (end) {
  9. var vars_ = this.vars;
  10. this
  11. .scan('filler', 'BEGINMSG')
  12. .buffer('cmd', 3)
  13. .word8('num')
  14. .tap(function (vars) {
  15. t.strictEqual(vars, vars_);
  16. if (vars.num != 0x02 && vars.num != 0x06) {
  17. t.same(vars.filler.length, 0);
  18. }
  19. if (vars.cmd.toString() == 'end') end();
  20. })
  21. ;
  22. })
  23. .tap(function (vars) {
  24. t.same(vars.cmd.toString(), 'end');
  25. t.same(vars.num, 0x08);
  26. })
  27. ;
  28. setTimeout(function () {
  29. em.emit('data', new Buffer(
  30. 'BEGINMSGcmd\x01'
  31. + 'GARBAGEDATAXXXX'
  32. + 'BEGINMSGcmd\x02'
  33. + 'BEGINMSGcmd\x03'
  34. ));
  35. }, 10);
  36. setTimeout(function () {
  37. em.emit('data', new Buffer(
  38. 'BEGINMSGcmd\x04'
  39. + 'BEGINMSGcmd\x05'
  40. + 'GARBAGEDATAXXXX'
  41. + 'BEGINMSGcmd\x06'
  42. ));
  43. em.emit('data', new Buffer('BEGINMSGcmd\x07'));
  44. }, 20);
  45. setTimeout(function () {
  46. em.emit('data', new Buffer('BEGINMSGend\x08'));
  47. }, 30);
  48. });