Skip to content

Refactoring common architecture #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 5.0.0

- Updated documentation.
- Updated dependencies.
- Updated `README.md` file.
- Updated `TODO.md` file.
- Rewrited comparators. Now you can use `IDEComparator` without adding to global path. Also you can add custom diff tool.
- Rewrited Scrubbers: regex scrubber, date scrubber.
- Now, first run automatically create approved snapshot. You can approve another snapshot by setting `approveResult` to `true` and using Diff Tool.

## 0.4.6

- Updated dependencies. Removed `dcli` package.
Expand Down
108 changes: 6 additions & 102 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,11 @@ How to use diff tools is just below, there is a `Comparator` class for that.
#### • Via approveResult property
If you want the result to be automatically saved after running the test, you need to use the `approveResult` property in `Options`:

<!-- snippet: sample_verify_as_json_test -->
<a id='snippet-sample_verify_as_json_test'></a>
```dart
void main() {
test('test complex JSON object', () {
final complexObject = {
'name': 'JsonTest',
'features': ['Testing', 'JSON'],
'version': 0.1,
};

Approvals.verifyAsJson(
complexObject,
options: const Options(
approveResult: true,
),
);
});
}
```
<sup><a href='/test/example/example_test.dart#L4-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_verify_as_json_test' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
snippet: sample_verify_as_json_test

this will result in the following file
`example_test.approved.txt`
<!-- snippet: example_test.approved.txt -->
<a id='snippet-example_test.approved.txt'></a>
```txt
{
"name": "JsonTest",
"features": [
"Testing",
"JSON"
],
"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>
<!-- endSnippet -->
`example_test.test_JSON_object.approved.txt`
snippet: example_test.test_JSON_object.approved.txt

#### • Via file rename
You can just rename the `.received` file to `.approved`.
Expand Down Expand Up @@ -168,73 +134,11 @@ And the `verify_methods` folder has small examples of using different `ApprovalT

### JSON example

<!-- snippet: same_verify_as_json_test_with_model -->
<a id='snippet-same_verify_as_json_test_with_model'></a>
```dart
void main() {
const jsonItem = JsonItem(
id: 1,
name: "JsonItem",
anotherItem: AnotherItem(id: 1, name: "AnotherItem"),
subItem: SubItem(
id: 1,
name: "SubItem",
anotherItems: [
AnotherItem(id: 1, name: "AnotherItem 1"),
AnotherItem(id: 2, name: "AnotherItem 2"),
],
),
);

test('Verify JSON output of an object', () {
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.
),
);
});
}
```
<sup><a href='/example/verify_methods/verify_as_json/verify_as_json_test.dart#L6-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-same_verify_as_json_test_with_model' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
snippet: same_verify_as_json_test_with_model

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

<!-- snippet: verify_as_json_test.approved.txt -->
<a id='snippet-verify_as_json_test.approved.txt'></a>
```txt
{
"jsonItem": {
"id": 1,
"name": "JsonItem",
"subItem": {
"id": 1,
"name": "SubItem",
"anotherItems": [
{
"id": 1,
"name": "AnotherItem 1"
},
{
"id": 2,
"name": "AnotherItem 2"
}
]
},
"anotherItem": {
"id": 1,
"name": "AnotherItem"
}
}
}
```
<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>
<!-- endSnippet -->
`verify_as_json_test.verify_model.approved.txt`
snippet: verify_as_json_test.verify_model.approved.txt

<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
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Tasks for the project are listed below:

* [ ] Refactor the code based on ApprovalTests architectural rules: check reporters and comparators
* [ ] Add more detailed documentation to the project
* [ ] Test new comparators on Windows
* [ ] Test new comparators on Linux

# DONE

Expand All @@ -25,4 +27,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
6 changes: 2 additions & 4 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ 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(
comparator: IDEComparator(
ide: ComparatorIDE.visualStudioCode,
),
comparator: IDEComparator(),
deleteReceivedFile: true,
),
processor: (items) => fizzBuzz(items).toString(),
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
29 changes: 21 additions & 8 deletions lib/approval_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@ import 'package:test_api/src/backend/invoker.dart' show Invoker;

part 'src/approvals.dart';
part 'src/writers/approval_text_writer.dart';

part 'src/core/logger/logger.dart';
part 'src/core/utils/utils.dart';
part 'src/core/options.dart';
part 'src/namer/namer.dart';
part 'src/comparator/comparator.dart';
part 'src/core/approval_writer.dart';
part 'src/core/enums/comporator_ide.dart';
part 'src/comparator/command_line_comparator.dart';
part 'src/comparator/ide_comparator.dart';
part 'src/reporter/doesnt_match_exception.dart';
part 'src/core/utils/converter.dart';
part 'src/core/utils/executable_query.dart';
part 'src/namer/approval_number.dart';
part 'src/reporter/command_line_comparator_exception.dart';
part 'src/reporter/ide_comparator_exception.dart';
part 'src/core/scrubber.dart';
part 'src/scrubbers/date_scrubber.dart';
part 'src/scrubbers/nothing_scrubber.dart';
part 'src/scrubbers/reg_exp_scrubber.dart';
part 'src/core/extensions/approval_string_extensions.dart';

part 'src/namer/namer.dart';
part 'src/core/approval_number.dart';
part 'src/namer/file_namer_options.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';
part 'src/exceptions/ide_comparator_exception.dart';
21 changes: 12 additions & 9 deletions lib/src/approvals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ part of '../approval_tests.dart';
/// `Approvals` is a class that provides methods to verify the content of a response.
class Approvals {
// Factory method to create an instance of ApprovalNamer with given file name
static ApprovalNamer makeNamer(String file) => Namer(file);
static ApprovalNamer makeNamer(String filePath) => Namer(filePath: filePath);

// ================== Verify methods ==================

Expand All @@ -14,19 +14,22 @@ class Approvals {
}) {
try {
// Get the file path without extension or use the provided file path
final completedPath =
options.filesPath ?? 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 = makeNamer(options.filesPath ?? completedPath);
final namer = options.namer ?? makeNamer(completedPath);

// Create writer object with scrubbed response and file extension retrieved from options
final writer = ApprovalTextWriter(options.scrub(response), "txt");
final writer = ApprovalTextWriter(
options.scrubber.scrub(response),
);

// Write the content to a file whose path is specified in namer.received
writer.writeToFile(namer.received);

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

Expand Down Expand Up @@ -65,11 +68,11 @@ class Approvals {
/// `_deleteFileAfterTest` method to delete the received file after the test.
static void _deleteFileAfterTest(Options options) {
if (options.deleteReceivedFile) {
if (options.filesPath != null) {
ApprovalUtils.deleteFile(Namer(options.filesPath!).received);
if (options.namer != null) {
ApprovalUtils.deleteFile(options.namer!.received);
} else {
ApprovalUtils.deleteFile(
Namer(ApprovalUtils.filePath.split('.dart').first).received,
Namer(filePath: ApprovalUtils.filePath.split('.dart').first).received,
);
}
}
Expand Down
19 changes: 19 additions & 0 deletions lib/src/core/approval_number.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
part of '../../approval_tests.dart';

/// `ApprovalNamer` is an abstract class that provides methods to generate the file names for the approved and received files.
abstract interface class ApprovalNamer {
/// A getter named `approved` that returns the string `'file_name.test_name.approved.txt'`.
String get approved;

/// A getter named `received` that returns the string `'file_name.test_name.received.txt'`.
String get received;

/// A getter named `approvedFileName` that returns the string `'file_name.approved.txt'`.
String get approvedFileName;

/// A getter named `receivedFileName` that returns the string `'file_name.received.txt'`.
String get receivedFileName;

/// A getter named `currentTestName` that returns the current test name.
String get currentTestName;
}
Loading
Loading