From 414ef8bae6317d458708058e29e331bb39eac1a9 Mon Sep 17 00:00:00 2001 From: Kabo <20254485+kaboc@users.noreply.github.com> Date: Sat, 23 Dec 2023 15:12:43 +0900 Subject: [PATCH] Improve tests --- test/matchers/email_matcher_test.dart | 2 +- test/matchers/url_matcher_test.dart | 2 +- test/parser_test.dart | 26 ++++++++++++++++++++++---- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/test/matchers/email_matcher_test.dart b/test/matchers/email_matcher_test.dart index c134fbf..05b25d4 100644 --- a/test/matchers/email_matcher_test.dart +++ b/test/matchers/email_matcher_test.dart @@ -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 = 'foo@example.com'; final matches = regExp.allMatches(address); expect(matches, hasLength(1)); diff --git a/test/matchers/url_matcher_test.dart b/test/matchers/url_matcher_test.dart index d7513c4..9c2887c 100644 --- a/test/matchers/url_matcher_test.dart +++ b/test/matchers/url_matcher_test.dart @@ -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)); diff --git a/test/parser_test.dart b/test/parser_test.dart index f4ac729..8099540 100644 --- a/test/parser_test.dart +++ b/test/parser_test.dart @@ -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' 'john.doe@example.com 911', ); @@ -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 john.doe@example.com'); + + expect(elements1, hasLength(2)); + expect(elements1[1].matcherType, EmailMatcher); + + final elements2 = await TextParser( + matchers: const [UrlLikeMatcher(), EmailMatcher()], + ).parse('abc john.doe@example.com'); + + 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');