Skip to content

Commit

Permalink
Improve code coverage for searchstream (#30)
Browse files Browse the repository at this point in the history
* improve description of tests

* increase more
  • Loading branch information
Uzlopak authored Nov 29, 2021
1 parent e293d89 commit 441d3fd
Showing 1 changed file with 198 additions and 6 deletions.
204 changes: 198 additions & 6 deletions test/streamsearch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,53 @@ const { expect } = require('chai');
const Streamsearch = require('../deps/streamsearch/sbmh');

describe('streamsearch', () => {
it('begin pos', (done) => {
it('should process a Buffer without a needle', (done) => {
const expected = [
[false, Buffer.from("bar hello"), 0, 9],
]
const needle = "\r\n";
const s = new Streamsearch(needle);
const chunks = [
Buffer.from('bar hello'),
];
let i = 0;
s.on('info', (isMatched, data, start, end) => {
expect(isMatched).to.be.eql(expected[i][0]);
expect(data).to.be.eql(expected[i][1]);
expect(start).to.be.eql(expected[i][2]);
expect(end).to.be.eql(expected[i][3]);
i++;
if (i === 1) {
done();
}
});

s.push(chunks[0]);
});
it('should cast a string without a needle', (done) => {
const expected = [
[false, Buffer.from("bar hello"), 0, 9],
]
const needle = "\r\n";
const s = new Streamsearch(needle);
const chunks = [
'bar hello',
];
let i = 0;
s.on('info', (isMatched, data, start, end) => {
expect(isMatched).to.be.eql(expected[i][0]);
expect(data).to.be.eql(expected[i][1]);
expect(start).to.be.eql(expected[i][2]);
expect(end).to.be.eql(expected[i][3]);
i++;
if (i === 1) {
done();
}
});

s.push(chunks[0]);
});
it('should process a chunk with a needle at the beginning', (done) => {
const expected = [
[true, undefined, undefined, undefined],
[false, Buffer.from("\r\nbar hello"), 2, 11],
Expand All @@ -27,7 +73,7 @@ describe('streamsearch', () => {
s.push(chunks[0]);
});

it('middle pos', (done) => {
it('should process a chunk with a needle in the middle', (done) => {
const expected = [
[true, Buffer.from("bar\r\n hello"), 0, 3],
[false, Buffer.from("bar\r\n hello"), 5, 11],
Expand All @@ -52,7 +98,7 @@ describe('streamsearch', () => {
s.push(chunks[0]);
});

it('end pos', (done) => {
it('should process a chunk with a needle at the end', (done) => {
const expected = [
[true, Buffer.from("bar hello\r\n"), 0, 9],
]
Expand All @@ -76,7 +122,7 @@ describe('streamsearch', () => {
s.push(chunks[0]);
});

it('multiple end pos', (done) => {
it('should process a chunk with multiple needle at the end', (done) => {
const expected = [
[true, Buffer.from("bar hello\r\n\r\n"), 0, 9],
[true, Buffer.from("bar hello\r\n\r\n"), 11, 11],
Expand All @@ -101,7 +147,7 @@ describe('streamsearch', () => {
s.push(chunks[0]);
});

it('two chunks without needle', (done) => {
it('should process two chunks without a needle', (done) => {
const expected = [
[false, Buffer.from("bar"), 0, 3],
[false, Buffer.from("hello"), 0, 5],
Expand All @@ -128,7 +174,7 @@ describe('streamsearch', () => {
s.push(chunks[1]);
});

it('two chunks with overflowing needle', (done) => {
it('should process two chunks with an overflowing needle', (done) => {
const expected = [
[false, Buffer.from("bar\r"), 0, 3],
[true, undefined, undefined, undefined],
Expand All @@ -155,4 +201,150 @@ describe('streamsearch', () => {
s.push(chunks[0]);
s.push(chunks[1]);
});

it('should process two chunks with a potentially overflowing needle', (done) => {
const expected = [
[false, Buffer.from("bar\r"), 0, 3],
[false, Buffer.from("\r\0\0"), 0, 1],
[false, Buffer.from("\n\r\nhello"), 0, 8],
]
const needle = "\r\n\n";
const s = new Streamsearch(needle);
const chunks = [
Buffer.from('bar\r'),
Buffer.from('\n\r\nhello'),
];
let i = 0;
s.on('info', (isMatched, data, start, end) => {
expect(isMatched).to.be.eql(expected[i][0]);
expect(data).to.be.eql(expected[i][1]);
expect(start).to.be.eql(expected[i][2]);
expect(end).to.be.eql(expected[i][3]);
i++;
if (i === 3) {
done();
}
});

s.push(chunks[0]);
s.push(chunks[1]);
});

it('should process three chunks with a overflowing needle', (done) => {
const expected = [
[false, Buffer.from("bar\r"), 0, 3],
[true, undefined, undefined, undefined],
[false, Buffer.from("\nhello"), 1, 6],
]
const needle = "\r\n\n";
const s = new Streamsearch(needle);
const chunks = [
Buffer.from('bar\r'),
Buffer.from('\n'),
Buffer.from('\nhello'),
];
let i = 0;
s.on('info', (isMatched, data, start, end) => {
expect(isMatched).to.be.eql(expected[i][0]);
expect(data).to.be.eql(expected[i][1]);
expect(start).to.be.eql(expected[i][2]);
expect(end).to.be.eql(expected[i][3]);
i++;
if (i === 3) {
done();
}
});

s.push(chunks[0]);
s.push(chunks[1]);
s.push(chunks[2]);
});

it('should process four chunks with a overflowing needle', (done) => {
const expected = [
[false, Buffer.from("bar\r"), 0, 3],
[true, undefined, undefined, undefined],
[false, Buffer.from("hello"), 0, 5],
]
const needle = "\r\n\n";
const s = new Streamsearch(needle);
const chunks = [
Buffer.from('bar\r'),
Buffer.from('\n'),
Buffer.from('\n'),
Buffer.from('hello'),
];
let i = 0;
s.on('info', (isMatched, data, start, end) => {
expect(isMatched).to.be.eql(expected[i][0]);
expect(data).to.be.eql(expected[i][1]);
expect(start).to.be.eql(expected[i][2]);
expect(end).to.be.eql(expected[i][3]);
i++;
if (i === 3) {
done();
}
});

s.push(chunks[0]);
s.push(chunks[1]);
s.push(chunks[2]);
s.push(chunks[3]);
});

it('should process four chunks with a potentially overflowing needle', (done) => {
const expected = [
[false, Buffer.from("bar\r"), 0, 3],
[false, Buffer.from("\r\n\0"), 0, 2],
[false, Buffer.from("\r\n\0"), 0, 1],
[false, Buffer.from("hello"), 0, 5],
]
const needle = "\r\n\n";
const s = new Streamsearch(needle);
const chunks = [
Buffer.from('bar\r'),
Buffer.from('\n'),
Buffer.from('\r'),
Buffer.from('hello'),
];
let i = 0;
s.on('info', (isMatched, data, start, end) => {
expect(isMatched).to.be.eql(expected[i][0]);
expect(data).to.be.eql(expected[i][1]);
expect(start).to.be.eql(expected[i][2]);
expect(end).to.be.eql(expected[i][3]);
i++;
if (i === 4) {
done();
}
});

s.push(chunks[0]);
s.push(chunks[1]);
s.push(chunks[2]);
s.push(chunks[3]);
});

it('should reset the internal values if .reset() is called', () => {

const s = new Streamsearch("test");

expect(s._lookbehind_size).to.be.eql(0);
expect(s.matches).to.be.eql(0);
expect(s._bufpos).to.be.eql(0);

s._lookbehind_size = 1;
s._bufpos = 1;
s.matches = 1;

expect(s._lookbehind_size).to.be.eql(1);
expect(s.matches).to.be.eql(1);
expect(s._bufpos).to.be.eql(1);

s.reset();

expect(s._lookbehind_size).to.be.eql(0);
expect(s.matches).to.be.eql(0);
expect(s._bufpos).to.be.eql(0);
});
})

0 comments on commit 441d3fd

Please sign in to comment.