Skip to content

Commit cf14563

Browse files
✨ feat: Test empty string or pattern and allow to skip those cases.
1 parent 4a0affc commit cf14563

File tree

6 files changed

+89
-10
lines changed

6 files changed

+89
-10
lines changed

src/data.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
const data = [
2+
{
3+
string: '',
4+
patterns: [
5+
{
6+
pattern: '',
7+
hits: [0],
8+
},
9+
{
10+
pattern: 'abcd',
11+
hits: [],
12+
},
13+
],
14+
},
215
{
316
string: 'abcd',
417
patterns: [
18+
{
19+
pattern: '',
20+
hits: [0, 1, 2, 3, 4],
21+
},
522
{
623
pattern: 'abcde',
724
hits: [],
@@ -34,6 +51,43 @@ const data = [
3451
},
3552
],
3653
},
54+
{
55+
string:
56+
'Some books are to be tasted, others to be swallowed, and some few to be chewed and digested.',
57+
patterns: [
58+
{
59+
pattern: 'to',
60+
hits: [15, 36, 66],
61+
},
62+
],
63+
},
64+
{
65+
string: 'aaaaaaaaab',
66+
patterns: [
67+
{
68+
pattern: 'aaaab',
69+
hits: [5],
70+
},
71+
],
72+
},
73+
{
74+
string: 'tototo',
75+
patterns: [
76+
{
77+
pattern: 'to',
78+
hits: [0, 2, 4],
79+
},
80+
],
81+
},
82+
{
83+
string: 'aaaaa',
84+
patterns: [
85+
{
86+
pattern: 'aa',
87+
hits: [0, 1, 2, 3],
88+
},
89+
],
90+
},
3791
];
3892

3993
export default data;

src/mock.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export default function* mock(string, patterns) {
1919
while (true) {
2020
const i = s.indexOf(p, fromIndex);
2121
if (i === -1) break;
22-
yield new Hit(i, pattern, pattern.i);
22+
yield new Hit(string.i + i, pattern, pattern.i);
23+
if (fromIndex >= s.length) break;
2324
fromIndex = i + 1;
2425
}
2526
}

src/single.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
const single = ({test, algorithms, data}) => {
1+
const single = ({
2+
test,
3+
algorithms,
4+
data,
5+
skipEmptyString,
6+
skipEmptyPattern,
7+
}) => {
28
for (const algorithm of algorithms) {
39
for (const {string, patterns} of data) {
10+
if (string === '' && skipEmptyString) continue;
411
for (const {pattern, hits} of patterns) {
12+
if (pattern === '' && skipEmptyPattern) continue;
513
test(macro, algorithm, string, pattern, hits);
614
}
715
}

test/src/_fixtures.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {map} from '@iterable-iterator/map';
2+
3+
import {mock, StringSlice} from '../../src/index.js';
4+
5+
export const mockedSingle = (s, si, sj, p, pi, pj) =>
6+
map(
7+
(hit) => hit.si,
8+
mock(new StringSlice(s, si, sj), [new StringSlice(p, pi, pj)]),
9+
);

test/src/single.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import test from 'ava';
22

3-
import {map} from '@iterable-iterator/map';
3+
import {single, data} from '../../src/index.js';
44

5-
import {mock, single, data, StringSlice} from '../../src/index.js';
6-
7-
const mockedSingle = (s, si, sj, p, pi, pj) =>
8-
map(
9-
(hit) => hit.si,
10-
mock(new StringSlice(s, si, sj), [new StringSlice(p, pi, pj)]),
11-
);
5+
import {mockedSingle} from './_fixtures.js';
126

137
single({
148
test,

test/src/skip.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import test from 'ava';
2+
3+
import {single, data} from '../../src/index.js';
4+
5+
import {mockedSingle} from './_fixtures.js';
6+
7+
single({
8+
test,
9+
algorithms: [mockedSingle],
10+
data,
11+
skipEmptyString: true,
12+
skipEmptyPattern: true,
13+
});

0 commit comments

Comments
 (0)