Skip to content

Commit

Permalink
Remove assertion that threw when any matcher had empty pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
kaboc committed Dec 23, 2023
1 parent 06271e7 commit b9a057f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
23 changes: 12 additions & 11 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@ class Parser {
_matcherGroupRanges.clear();

var groupIndexStart = 2;
final patterns = <String>[];

for (var i = 0; i < matchers.length; i++) {
assert(
matchers[i].pattern.isNotEmpty,
'All matchers must have a non-empty pattern.',
);
final groupName = '$_kMatcherGroupPrefix$i';
_matcherGroupNames.add(groupName);

_matcherGroupNames.add('$_kMatcherGroupPrefix$i');
var pattern = matchers[i].pattern;
if (pattern.isEmpty) {
// Expression that does not match anything.
pattern = '(?!)';
}
patterns.add('(?<$groupName>$pattern)');

final regExp = RegExp(
'${matchers[i].pattern}|.*',
'$pattern|.*',
multiLine: multiLine,
caseSensitive: caseSensitive,
unicode: unicode,
Expand All @@ -57,14 +62,10 @@ class Parser {
_matcherGroupRanges.add([
for (var i = 0; i < groupCount; i++) groupIndexStart + i,
]);

groupIndexStart += groupCount + 1;
}

_pattern = {
for (var i = 0; i < matchers.length; i++)
'(?<${_matcherGroupNames[i]}>${matchers[i].pattern})',
}.join('|');
_pattern = patterns.join('|');
}

List<TextElement> parse(String text, {required bool onlyMatches}) {
Expand Down
37 changes: 27 additions & 10 deletions test/matchers/matcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,34 @@ void main() {
);
});

test('Having matcher containing empty pattern throws AssertionError', () {
expect(
() => TextParser(matchers: const [EmailMatcher(), PatternMatcher('')]),
throwsA(
isA<AssertionError>().having(
(e) => e.message,
'message',
contains('must have a non-empty pattern'),
),
),
test('correctly parsed with matchers containing empty pattern', () async {
final parser = TextParser(
matchers: const [
PatternMatcher('(bbb)'),
PatternMatcher(''),
PatternMatcher('(ddd)'),
],
);
final elements = await parser.parse('aaabbbcccdddeee');

expect(elements, hasLength(5));
expect(elements[0], const TextElement('aaa'));
expect(elements[0].matcherType, TextMatcher);
expect(elements[0].matcherIndex, null);
expect(elements[1].text, 'bbb');
expect(elements[1].matcherType, PatternMatcher);
expect(elements[1].matcherIndex, 0);
expect(elements[1].groups, ['bbb']);
expect(elements[2].text, 'ccc');
expect(elements[2].matcherType, TextMatcher);
expect(elements[2].matcherIndex, null);
expect(elements[3].text, 'ddd');
expect(elements[3].matcherType, PatternMatcher);
expect(elements[3].matcherIndex, 2);
expect(elements[3].groups, ['ddd']);
expect(elements[4].text, 'eee');
expect(elements[4].matcherType, TextMatcher);
expect(elements[4].matcherIndex, null);
});
});
}

0 comments on commit b9a057f

Please sign in to comment.