Skip to content

Commit

Permalink
test(utils): filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sovrin committed Oct 19, 2022
1 parent 71126be commit 90fa6ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
56 changes: 43 additions & 13 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('onia', () => {
it('should parse string', () => {
const o = alpha('o');

assert(o.toString() === 'o');
assertSuccess(o({index: 0, text: 'o'}), 'o');
assertFailure(o({index: 1, text: 'o'}), 'o');
assertFailure(o({index: -1, text: 'o'}), 'o');
Expand All @@ -53,6 +54,7 @@ describe('onia', () => {
it('should parse regex', () => {
const number = regex(/[0-9]/g, 'number');

assert(number.toString() === (/[0-9]/g).toString());
assertSuccess(number({index: 0, text: '01'}), '0');
assertSuccess(number({index: 1, text: '01'}), '1');
assertFailure(number({index: 0, text: ''}), 'number');
Expand Down Expand Up @@ -128,6 +130,15 @@ describe('onia', () => {

assertSuccess(fn({index: 0, text: 'foo'}), 'foo');
});

it('should return number', () => {
const fn = optional(map(
regex(/\d/g, 'digit'),
int(),
));

assertSuccess(fn({index: 0, text: '0'}), 0);
});
});

describe('map', () => {
Expand Down Expand Up @@ -323,21 +334,40 @@ describe('onia', () => {
const open = alpha('{');
const close = alpha('}');
const unused = alpha('_');

const term = map(
sequence<any>([
open,
string,
close,
]),
filter([
'{',
close,
unused
]),
);
const hashtag = alpha('#');

it('should filter out braces', () => {
const term = map(
sequence<any>([
open,
string,
close,
]),
filter([
'{',
close,
unused
]),
);

assertSuccess(term({index: 0, text: '{foobar}'}), ['foobar']);
});

it('should filter out braces and ignore order', () => {
const term = map(
sequence<any>([
open,
optional(alpha('#')),
string,
close,
]),
filter([
'{',
close,
hashtag
]),
);

assertSuccess(term({index: 0, text: '{foobar}'}), ['foobar']);
});
});
Expand Down
6 changes: 5 additions & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import assert from 'assert';
import {Result} from '../src/types';

const serialize = (value) => JSON.stringify(value);
/**
*
* @param value
*/
const serialize = (value): string => JSON.stringify(value);

/**
*
Expand Down

0 comments on commit 90fa6ad

Please sign in to comment.