Skip to content

Commit

Permalink
. t added tests for scrubbers
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed May 18, 2024
1 parent 8df5745 commit 14c3d8f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 19 deletions.
2 changes: 0 additions & 2 deletions lib/src/core/comparator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions lib/src/reporters/diff_tool/diff_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);

Expand All @@ -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',
Expand Down
27 changes: 17 additions & 10 deletions lib/src/scrubbers/date_scrubber.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<date$_index>';
},
);
/// Replaces matched date strings with a fixed placeholder `<date>`.
const ScrubDates();

static int _index = 0;

@override
String scrub(String input) => input
.replacingOccurrences(
matchingPattern: _datePattern,
replacementProvider: (match) {
_index++;
return '<date$_index>';
},
)
.trim();
}
21 changes: 21 additions & 0 deletions test/approval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==================
Expand Down Expand 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 ==================
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Hello-World-
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<date1>
<date2>
11 changes: 7 additions & 4 deletions test/utils/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,24 @@ class ApprovalTestHelper {
}

void verifyAll(
List<String> contents,
List<dynamic> 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,
),
);
}
Expand Down Expand Up @@ -102,7 +105,7 @@ class ApprovalTestHelper {
}

void verifySequence(
List<int> sequence,
List<dynamic> sequence,
String testName, {
bool expectException = false,
bool approveResult = false,
Expand Down

0 comments on commit 14c3d8f

Please sign in to comment.