Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaboc committed Dec 23, 2023
1 parent b9a057f commit 414ef8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion test/matchers/email_matcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void main() {
final pattern = const EmailMatcher().pattern;
final regExp = RegExp(pattern);

test('matches email address despite no letters around it', () {
test('matches email address when there are no letters around it', () {
const address = '[email protected]';
final matches = regExp.allMatches(address);
expect(matches, hasLength(1));
Expand Down
2 changes: 1 addition & 1 deletion test/matchers/url_matcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void main() {
final pattern = const UrlMatcher().pattern;
final regExp = RegExp(pattern);

test('matches URL with no letters around it', () {
test('matches URL when there are no letters around it', () {
const url = 'https://example.com/';
final matches = regExp.allMatches(url).toList();
expect(matches, hasLength(1));
Expand Down
26 changes: 22 additions & 4 deletions test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ void main() {
expect(stopwatch.elapsedMicroseconds, lessThan(1000));
});

test('parsed correctly with default matchers', () async {
test('parsed correctly with preset matchers', () async {
final elements = await parser.parse(
// "john.doe" in the email address is parsed as URL
// mistakenly if UrlMatcher is specified before
// EmailMatcher in the list of default matchers.
'abc https://example.com/sample.jpg. def\n'
'[email protected] 911',
);
Expand Down Expand Up @@ -64,6 +61,27 @@ void main() {
expect(elements[5].matcherType, TelMatcher);
});

test('earlier matcher is used if multiple ones match a string', () async {
final elements1 = await TextParser(
matchers: const [EmailMatcher(), UrlLikeMatcher()],
).parse('abc [email protected]');

expect(elements1, hasLength(2));
expect(elements1[1].matcherType, EmailMatcher);

final elements2 = await TextParser(
matchers: const [UrlLikeMatcher(), EmailMatcher()],
).parse('abc [email protected]');

expect(elements2, hasLength(4));
expect(elements2[1].text, 'john.doe');
expect(elements2[1].matcherType, UrlLikeMatcher);
expect(elements2[2].text, '@');
expect(elements2[2].matcherType, TextMatcher);
expect(elements2[3].text, 'example.com');
expect(elements2[3].matcherType, UrlLikeMatcher);
});

test('parsed into a single element if there is no match', () async {
final elements = await parser.parse('abcde');

Expand Down

0 comments on commit 414ef8b

Please sign in to comment.