Skip to content

Commit

Permalink
. r d F updated TODO, tests, examples, comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed May 17, 2024
1 parent 364a1dd commit 4004baa
Show file tree
Hide file tree
Showing 37 changed files with 78 additions and 167 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ void main() {
<!-- endSnippet -->

this will result in the following file
`example_test.approved.txt`
<!-- snippet: example_test.approved.txt -->
<a id='snippet-example_test.approved.txt'></a>
`example_test.test_JSON_object.approved`
<!-- snippet: example_test.test_JSON_object.approved -->
<a id='snippet-example_test.test_JSON_object.approved'></a>
```txt
{
"name": "JsonTest",
Expand All @@ -112,7 +112,7 @@ this will result in the following file
"version": 0.1
}
```
<sup><a href='/test/example/example_test.approved.txt#L1-L8' title='Snippet source file'>snippet source</a> | <a href='#snippet-example_test.approved.txt' title='Start of snippet'>anchor</a></sup>
<sup><a href='/test/example/example_test.test_JSON_object.approved#L1-L8' title='Snippet source file'>snippet source</a> | <a href='#snippet-example_test.test_JSON_object.approved' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

#### • Via file rename
Expand Down Expand Up @@ -203,10 +203,10 @@ void main() {
<!-- endSnippet -->

this will result in the following file
`verify_as_json_test.approved.txt`
`verify_as_json_test.verify_model.approved.txt`

<!-- snippet: verify_as_json_test.approved.txt -->
<a id='snippet-verify_as_json_test.approved.txt'></a>
<!-- snippet: verify_as_json_test.verify_model.approved.txt -->
<a id='snippet-verify_as_json_test.verify_model.approved.txt'></a>
```txt
{
"jsonItem": {
Expand All @@ -233,7 +233,7 @@ this will result in the following file
}
}
```
<sup><a href='/example/verify_methods/verify_as_json/verify_as_json_test.approved.txt#L1-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-verify_as_json_test.approved.txt' title='Start of snippet'>anchor</a></sup>
<sup><a href='/example/verify_methods/verify_as_json/verify_as_json_test.verify_model.approved.txt#L1-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-verify_as_json_test.verify_model.approved.txt' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

<img src="https://github.com/yelmuratoff/packages_assets/blob/main/assets/approval_tests/passed.png?raw=true" alt="Passed test example" title="ApprovalTests" style="max-width: 800px;">
Expand Down
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ Tasks for the project are listed below:
* [x] Add codecov and coverage badge
* [x] Set up autopublishing github action with PAT token and new tags
* [x] Cover the project completely with tests
* [x] Add name of current test to the file name
* [x] Rewrite comparators and reporters
* [x] Revise the implementation with getting the default file path. Now it works with primary delay. Read more: https://github.com/approvals/ApprovalTests.Dart/issues/3
2 changes: 1 addition & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:test/test.dart';

void main() {
group('Fizz Buzz', () {
test("Verify all combinations", () {
test("verify combinations", () {
Approvals.verifyAll(
[3, 5, 15],
options: const Options(
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions example/verify_methods/verify/verify_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import 'package:approval_tests/approval_tests.dart';
import 'package:test/test.dart';

void main() {
test('Verify method', () {
const String response =
'{"result": "success", "data": {"id": 1, "name": "Item"}}';
test('verify method', () {
const String response = '{"result": "success", "data": {"id": 1, "name": "Item"}}';

Approvals.verify(
response,
Expand Down
2 changes: 1 addition & 1 deletion example/verify_methods/verify_all/verify_all_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:approval_tests/approval_tests.dart';
import 'package:test/test.dart';

void main() {
test('Verify all items', () {
test('verify all items', () {
const List<String> items = ['apple', 'banana', 'cherry'];

Approvals.verifyAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:approval_tests/approval_tests.dart';
import 'package:test/test.dart';

void main() {
test('Verify all combinations of input values', () {
test('verify combinations', () {
const List<List<int>> inputs = [
[1, 2],
[3, 4],
Expand Down
2 changes: 1 addition & 1 deletion example/verify_methods/verify_as_json/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:test/test.dart';

void main() {
group('Approval Tests for Complex Objects', () {
test('test complex JSON object', () {
test('test JSON object', () {
const complexObject = {
'name': 'JsonTest',
'features': ['Testing', 'JSON'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ void main() {
),
);

test('Verify JSON output of an object', () {
test('verify model', () {
Approvals.verifyAsJson(
jsonItem,
options: const Options(
deleteReceivedFile:
true, // Automatically delete the received file after the test.
approveResult:
true, // Approve the result automatically. You can remove this property after the approved file is created.
deleteReceivedFile: true, // Automatically delete the received file after the test.
approveResult: true, // Approve the result automatically. You can remove this property after the approved file is created.
),
);
});
Expand Down
11 changes: 7 additions & 4 deletions example/verify_methods/verify_query/verify_db_query_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:approval_tests/approval_tests.dart';
import 'package:test/test.dart';

import '../../../test/queries/db_request_query.dart';

void main() async {
const dbQuery = DatabaseRequestQuery("1");
await Approvals.verifyQuery(
dbQuery,
options: const Options(deleteReceivedFile: true),
);
test('verify db query', () async {
await Approvals.verifyQuery(
dbQuery,
options: const Options(deleteReceivedFile: true),
);
});
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'package:approval_tests/approval_tests.dart';
import 'package:test/test.dart';

import '../../../test/queries/network_request_query.dart';

void main() async {
final query = NetworkRequestQuery(
Uri.parse('https://jsonplaceholder.typicode.com/todos/1'),
);
await Approvals.verifyQuery(
query,
options: const Options(deleteReceivedFile: true),
);
test('verify network query', () async {
await Approvals.verifyQuery(
query,
options: const Options(deleteReceivedFile: true),
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:approval_tests/approval_tests.dart';
import 'package:test/test.dart';

void main() {
test('Verify sequence of numbers', () {
test('verify sequence', () {
const List<int> sequence = [1, 2, 3, 4, 5];

Approvals.verifySequence(
Expand Down
10 changes: 5 additions & 5 deletions lib/approval_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ part 'src/namer/namer.dart';
part 'src/core/approval_number.dart';
part 'src/namer/file_namer_options.dart';

part 'src/comparator/comparator.dart';
part 'src/comparator/command_line_comparator.dart';
part 'src/comparator/ide_comparator.dart';
part 'src/comparator/diff_info.dart';
part 'src/comparator/diff_tools.dart';
part 'src/reporters/comparator.dart';
part 'src/reporters/command_line_comparator.dart';
part 'src/reporters/ide_comparator.dart';
part 'src/reporters/diff_info.dart';
part 'src/reporters/diff_tools.dart';

part 'src/exceptions/doesnt_match_exception.dart';
part 'src/exceptions/command_line_comparator_exception.dart';
Expand Down
9 changes: 6 additions & 3 deletions lib/src/approvals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Approvals {
}) {
try {
// Get the file path without extension or use the provided file path
final completedPath = options.namer?.filePath ?? ApprovalUtils.filePath.split('.dart').first;
final completedPath = options.namer?.filePath ??
ApprovalUtils.filePath.split('.dart').first;

// Create namer object with given or computed file name
final namer = options.namer ?? makeNamer(completedPath);
Expand All @@ -27,12 +28,14 @@ class Approvals {
// Write the content to a file whose path is specified in namer.received
writer.writeToFile(namer.received);

if (options.approveResult || !ApprovalUtils.isFileExists(namer.approved)) {
if (options.approveResult ||
!ApprovalUtils.isFileExists(namer.approved)) {
writer.writeToFile(namer.approved);
}

// Check if received file matches the approved file
final bool isFilesMatch = ApprovalUtils.filesMatch(namer.approved, namer.received);
final bool isFilesMatch =
ApprovalUtils.filesMatch(namer.approved, namer.received);

// Log results and throw exception if files do not match
if (!isFilesMatch) {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/core/extensions/approval_string_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extension StringExtensions on String {
required String Function(String) replacementProvider,
}) {
final regExp = RegExp(matchingPattern);
return replaceAllMapped(regExp, (match) => replacementProvider(match.group(0)!));
return replaceAllMapped(
regExp, (match) => replacementProvider(match.group(0)!),);
}
}
11 changes: 8 additions & 3 deletions lib/src/core/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ final class ApprovalUtils {
final int green = int.parse(hex.substring(2, 4), radix: 16);
final int blue = int.parse(hex.substring(4, 6), radix: 16);

final AnsiPen pen = AnsiPen()..rgb(r: red / 255, g: green / 255, b: blue / 255);
final AnsiPen pen = AnsiPen()
..rgb(r: red / 255, g: green / 255, b: blue / 255);
return pen;
}

Expand Down Expand Up @@ -57,8 +58,12 @@ final class ApprovalUtils {
static bool filesMatch(String approvedPath, String receivedPath) {
try {
// Read contents of the approved and received files
final approved = ApprovalUtils.readFile(path: approvedPath).replaceAll('\r\n', '\n').trim();
final received = ApprovalUtils.readFile(path: receivedPath).replaceAll('\r\n', '\n').trim();
final approved = ApprovalUtils.readFile(path: approvedPath)
.replaceAll('\r\n', '\n')
.trim();
final received = ApprovalUtils.readFile(path: receivedPath)
.replaceAll('\r\n', '\n')
.trim();

// Return true if contents of both files match exactly
return approved.compareTo(received) == 0;
Expand Down
16 changes: 12 additions & 4 deletions lib/src/namer/namer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,39 @@ final class Namer implements ApprovalNamer {
if (options != null) {
return options!.approved;
}
return addTestName ? '$filePath.$currentTestName.$approvedExtension' : '$filePath.$approvedExtension';
return addTestName
? '$filePath.$currentTestName.$approvedExtension'
: '$filePath.$approvedExtension';
}

@override
String get approvedFileName {
if (options != null) {
return options!.approvedFileName;
}
return addTestName ? '$_fileName.$currentTestName.$approvedExtension' : '$_fileName.$approvedExtension';
return addTestName
? '$_fileName.$currentTestName.$approvedExtension'
: '$_fileName.$approvedExtension';
}

@override
String get received {
if (options != null) {
return options!.received;
}
return addTestName ? '$filePath.$currentTestName.$receivedExtension' : '$filePath.$receivedExtension';
return addTestName
? '$filePath.$currentTestName.$receivedExtension'
: '$filePath.$receivedExtension';
}

@override
String get receivedFileName {
if (options != null) {
return options!.receivedFileName;
}
return addTestName ? '$_fileName.$currentTestName.$receivedExtension' : '$_fileName.$receivedExtension';
return addTestName
? '$_fileName.$currentTestName.$receivedExtension'
: '$_fileName.$receivedExtension';
}

@override
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ final class IDEComparator extends Comparator {
);
} catch (e, st) {
throw IDEComparatorException(
message: 'Error during comparison via ${ide.name}. Please try restart your IDE.',
message:
'Error during comparison via ${ide.name}. Please try restart your IDE.',
exception: e,
stackTrace: st,
);
Expand Down
5 changes: 4 additions & 1 deletion lib/src/scrubbers/reg_exp_scrubber.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class ScrubWithRegEx implements ApprovalScrubber {
replacementFunction = null;

/// Creates a `ScrubWithRegEx` with a custom regular expression pattern and replacement function.
const ScrubWithRegEx.custom({required this.pattern, required this.replacementFunction});
const ScrubWithRegEx.custom({
required this.pattern,
required this.replacementFunction,
});

@override
String scrub(String input) => input
Expand Down
Loading

0 comments on commit 4004baa

Please sign in to comment.