From 14c3d8f270de953044c37af5db358a78a61b3678 Mon Sep 17 00:00:00 2001 From: Yelaman Yelmuratov Date: Sat, 18 May 2024 15:54:53 +0500 Subject: [PATCH] . t added tests for scrubbers --- lib/src/core/comparator.dart | 2 -- lib/src/reporters/diff_tool/diff_tools.dart | 4 +-- lib/src/scrubbers/date_scrubber.dart | 27 ++++++++++++------- test/approval_test.dart | 21 +++++++++++++++ ...oval_test.verify_custom_scrub.approved.txt | 1 + ...proval_test.verify_date_scrub.approved.txt | 2 ++ test/utils/helper.dart | 11 +++++--- 7 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 test/approved_files/approval_test.verify_custom_scrub.approved.txt create mode 100644 test/approved_files/approval_test.verify_date_scrub.approved.txt diff --git a/lib/src/core/comparator.dart b/lib/src/core/comparator.dart index 887c4e5..0a8d8b1 100644 --- a/lib/src/core/comparator.dart +++ b/lib/src/core/comparator.dart @@ -2,8 +2,6 @@ part of '../../approval_tests.dart'; /// `Comparator` is an abstract class for comparing files. abstract interface class Comparator { - const Comparator(); - /// A method named `compare` for comparing two files. bool compare({ required String approvedPath, diff --git a/lib/src/reporters/diff_tool/diff_tools.dart b/lib/src/reporters/diff_tool/diff_tools.dart index 1d1386e..2f7c653 100644 --- a/lib/src/reporters/diff_tool/diff_tools.dart +++ b/lib/src/reporters/diff_tool/diff_tools.dart @@ -3,8 +3,7 @@ part of '../../../../approval_tests.dart'; /// `MacDiffTools` contains diff tools available on macOS. final class MacDiffTools { static const DiffInfo visualStudioCode = DiffInfo( - command: - '/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code', + command: '/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code', arg: '-d', ); @@ -16,7 +15,6 @@ final class MacDiffTools { /// `WindowsDiffTools` contains diff tools available on Windows. final class WindowsDiffTools { - // TODO: check correct path for Visual Studio Code on Windows static const DiffInfo visualStudioCode = DiffInfo( command: 'C:\\Program Files\\Microsoft VS Code\\Code.exe', arg: '-d', diff --git a/lib/src/scrubbers/date_scrubber.dart b/lib/src/scrubbers/date_scrubber.dart index f440151..7c9f48c 100644 --- a/lib/src/scrubbers/date_scrubber.dart +++ b/lib/src/scrubbers/date_scrubber.dart @@ -3,16 +3,23 @@ part of '../../approval_tests.dart'; /// A class named `ScrubDates` that extends `ScrubWithRegEx`. /// `ScrubDates` uses a regular expression to scrub date strings in a specific format. class ScrubDates extends ScrubWithRegEx { - static int _index = 0; + /// Constant pattern to match date strings + static const String _datePattern = r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+'; /// Creates a `ScrubDates` instance with a predefined pattern to match date strings. - /// Replaces matched date strings with a placeholder containing a unique index. - ScrubDates() - : super.custom( - pattern: r'\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}:\d{2}Z', - replacementFunction: (match) { - _index++; - return ''; - }, - ); + /// Replaces matched date strings with a fixed placeholder ``. + const ScrubDates(); + + static int _index = 0; + + @override + String scrub(String input) => input + .replacingOccurrences( + matchingPattern: _datePattern, + replacementProvider: (match) { + _index++; + return ''; + }, + ) + .trim(); } diff --git a/test/approval_test.dart b/test/approval_test.dart index f50aee8..eff4d19 100644 --- a/test/approval_test.dart +++ b/test/approval_test.dart @@ -18,6 +18,7 @@ void main() { const dbQuery = DatabaseRequestQuery("1"); const lines25 = _Lines.lines25; const lines30 = _Lines.lines30; + final dateTime = DateTime(2021, 10, 10, 10, 10, 10); /// ================== Set up ================== @@ -205,6 +206,26 @@ void main() { scrubber: const ScrubWithRegEx(), ); }); + + test('Verify string with custom scrubber', () { + helper.verify( + ' Hello World \t\n ', + 'verify_custom_scrub', + scrubber: ScrubWithRegEx.custom( + pattern: r'\s+', + replacementFunction: (match) => '-', + ), + ); + }); + + test('Verify string with date scrubber', () { + helper.verifyAll( + [dateTime, DateTime.now()], + 'verify_date_scrub', + scrubber: const ScrubDates(), + deleteReceivedFile: false, + ); + }); }); /// ================== Tear down ================== diff --git a/test/approved_files/approval_test.verify_custom_scrub.approved.txt b/test/approved_files/approval_test.verify_custom_scrub.approved.txt new file mode 100644 index 0000000..9d3d6ea --- /dev/null +++ b/test/approved_files/approval_test.verify_custom_scrub.approved.txt @@ -0,0 +1 @@ +-Hello-World- \ No newline at end of file diff --git a/test/approved_files/approval_test.verify_date_scrub.approved.txt b/test/approved_files/approval_test.verify_date_scrub.approved.txt new file mode 100644 index 0000000..1c5624c --- /dev/null +++ b/test/approved_files/approval_test.verify_date_scrub.approved.txt @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/test/utils/helper.dart b/test/utils/helper.dart index ef25266..6356627 100644 --- a/test/utils/helper.dart +++ b/test/utils/helper.dart @@ -45,21 +45,24 @@ class ApprovalTestHelper { } void verifyAll( - List contents, + List contents, String testName, { bool expectException = false, bool approveResult = false, bool deleteReceivedFile = true, + ApprovalScrubber scrubber = const ScrubNothing(), + Reporter reporter = const CommandLineReporter(), }) { Approvals.verifyAll( contents, - processor: (item) => - item, // Simple processor function that returns the item itself. + processor: (item) => item.toString(), // Simple processor function that returns the item itself. options: _getOptions( testName, expectException: expectException, approveResult: approveResult, deleteReceivedFile: deleteReceivedFile, + scrubber: scrubber, + reporter: reporter, ), ); } @@ -102,7 +105,7 @@ class ApprovalTestHelper { } void verifySequence( - List sequence, + List sequence, String testName, { bool expectException = false, bool approveResult = false,