summaryrefslogtreecommitdiff
path: root/node_modules/streamsearch/test/test.js
blob: 39a04d7f834bea5e8da37c4cf56973a7b3217dd5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';

const assert = require('assert');

const StreamSearch = require('../lib/sbmh.js');

[
  {
    needle: '\r\n',
    chunks: [
      'foo',
      ' bar',
      '\r',
      '\n',
      'baz, hello\r',
      '\n world.',
      '\r\n Node.JS rules!!\r\n\r\n',
    ],
    expect: [
      [false, 'foo'],
      [false, ' bar'],
      [ true, null],
      [false, 'baz, hello'],
      [ true, null],
      [false, ' world.'],
      [ true, null],
      [ true, ' Node.JS rules!!'],
      [ true, ''],
    ],
  },
  {
    needle: '---foobarbaz',
    chunks: [
      '---foobarbaz',
      'asdf',
      '\r\n',
      '---foobarba',
      '---foobar',
      'ba',
      '\r\n---foobarbaz--\r\n',
    ],
    expect: [
      [ true, null],
      [false, 'asdf'],
      [false, '\r\n'],
      [false, '---foobarba'],
      [false, '---foobarba'],
      [ true, '\r\n'],
      [false, '--\r\n'],
    ],
  },
].forEach((test, i) => {
  console.log(`Running test #${i + 1}`);
  const { needle, chunks, expect } = test;

  const results = [];
  const ss = new StreamSearch(Buffer.from(needle),
                              (isMatch, data, start, end) => {
    if (data)
      data = data.toString('latin1', start, end);
    else
      data = null;
    results.push([isMatch, data]);
  });

  for (const chunk of chunks)
    ss.push(Buffer.from(chunk));

  assert.deepStrictEqual(results, expect);
});