diff --git a/README.md b/README.md index f148128..92cea03 100644 --- a/README.md +++ b/README.md @@ -99,9 +99,9 @@ void main() { this will result in the following file -`example_test.approved.txt` - - +`example_test.test_JSON_object.approved` + + ```txt { "name": "JsonTest", @@ -112,7 +112,7 @@ this will result in the following file "version": 0.1 } ``` -snippet source | anchor +snippet source | anchor #### • Via file rename @@ -203,10 +203,10 @@ void main() { this will result in the following file -`verify_as_json_test.approved.txt` +`verify_as_json_test.verify_model.approved.txt` - - + + ```txt { "jsonItem": { @@ -233,7 +233,7 @@ this will result in the following file } } ``` -snippet source | anchor +snippet source | anchor Passed test example diff --git a/TODO.md b/TODO.md index 4fd4c38..7b88fad 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/example/main.dart b/example/main.dart index 2fb3b5b..1fc3a85 100644 --- a/example/main.dart +++ b/example/main.dart @@ -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( diff --git a/example/main.approved.txt b/example/main.verify_combinations.approved.txt similarity index 100% rename from example/main.approved.txt rename to example/main.verify_combinations.approved.txt diff --git a/example/verify_methods/verify/verify_test.dart b/example/verify_methods/verify/verify_test.dart index b4e362c..64cbd46 100644 --- a/example/verify_methods/verify/verify_test.dart +++ b/example/verify_methods/verify/verify_test.dart @@ -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, diff --git a/example/verify_methods/verify/verify_test.approved.txt b/example/verify_methods/verify/verify_test.verify_method.approved.txt similarity index 100% rename from example/verify_methods/verify/verify_test.approved.txt rename to example/verify_methods/verify/verify_test.verify_method.approved.txt diff --git a/example/verify_methods/verify_all/verify_all_test.dart b/example/verify_methods/verify_all/verify_all_test.dart index 0401481..4832904 100644 --- a/example/verify_methods/verify_all/verify_all_test.dart +++ b/example/verify_methods/verify_all/verify_all_test.dart @@ -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 items = ['apple', 'banana', 'cherry']; Approvals.verifyAll( diff --git a/example/verify_methods/verify_all/verify_all_test.approved.txt b/example/verify_methods/verify_all/verify_all_test.verify_all_items.approved.txt similarity index 100% rename from example/verify_methods/verify_all/verify_all_test.approved.txt rename to example/verify_methods/verify_all/verify_all_test.verify_all_items.approved.txt diff --git a/example/verify_methods/verify_all_combinations/verify_all_combinations_test.dart b/example/verify_methods/verify_all_combinations/verify_all_combinations_test.dart index 67938aa..e08a11f 100644 --- a/example/verify_methods/verify_all_combinations/verify_all_combinations_test.dart +++ b/example/verify_methods/verify_all_combinations/verify_all_combinations_test.dart @@ -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> inputs = [ [1, 2], [3, 4], diff --git a/example/verify_methods/verify_all_combinations/verify_all_combinations_test.approved.txt b/example/verify_methods/verify_all_combinations/verify_all_combinations_test.verify_combinations.approved.txt similarity index 100% rename from example/verify_methods/verify_all_combinations/verify_all_combinations_test.approved.txt rename to example/verify_methods/verify_all_combinations/verify_all_combinations_test.verify_combinations.approved.txt diff --git a/example/verify_methods/verify_as_json/json_test.dart b/example/verify_methods/verify_as_json/json_test.dart index 550751e..080299c 100644 --- a/example/verify_methods/verify_as_json/json_test.dart +++ b/example/verify_methods/verify_as_json/json_test.dart @@ -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'], diff --git a/example/verify_methods/verify_as_json/json_test.approved.txt b/example/verify_methods/verify_as_json/json_test.test_JSON_object.approved.txt similarity index 100% rename from example/verify_methods/verify_as_json/json_test.approved.txt rename to example/verify_methods/verify_as_json/json_test.test_JSON_object.approved.txt diff --git a/example/verify_methods/verify_as_json/verify_as_json_test.dart b/example/verify_methods/verify_as_json/verify_as_json_test.dart index 66e6d95..d59b82f 100644 --- a/example/verify_methods/verify_as_json/verify_as_json_test.dart +++ b/example/verify_methods/verify_as_json/verify_as_json_test.dart @@ -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. ), ); }); diff --git a/example/verify_methods/verify_as_json/verify_as_json_test.approved.txt b/example/verify_methods/verify_as_json/verify_as_json_test.verify_model.approved.txt similarity index 100% rename from example/verify_methods/verify_as_json/verify_as_json_test.approved.txt rename to example/verify_methods/verify_as_json/verify_as_json_test.verify_model.approved.txt diff --git a/example/verify_methods/verify_query/verify_db_query_test.dart b/example/verify_methods/verify_query/verify_db_query_test.dart index cbc8261..83f990a 100644 --- a/example/verify_methods/verify_query/verify_db_query_test.dart +++ b/example/verify_methods/verify_query/verify_db_query_test.dart @@ -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), + ); + }); } diff --git a/example/verify_methods/verify_query/verify_db_query_test.approved.txt b/example/verify_methods/verify_query/verify_db_query_test.verify_db_query.approved.txt similarity index 100% rename from example/verify_methods/verify_query/verify_db_query_test.approved.txt rename to example/verify_methods/verify_query/verify_db_query_test.verify_db_query.approved.txt diff --git a/example/verify_methods/verify_query/verify_network_query_test.dart b/example/verify_methods/verify_query/verify_network_query_test.dart index d581666..c02180d 100644 --- a/example/verify_methods/verify_query/verify_network_query_test.dart +++ b/example/verify_methods/verify_query/verify_network_query_test.dart @@ -1,4 +1,5 @@ import 'package:approval_tests/approval_tests.dart'; +import 'package:test/test.dart'; import '../../../test/queries/network_request_query.dart'; @@ -6,8 +7,10 @@ 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), + ); + }); } diff --git a/example/verify_methods/verify_query/verify_network_query_test.approved.txt b/example/verify_methods/verify_query/verify_network_query_test.verify_network_query.approved.txt similarity index 100% rename from example/verify_methods/verify_query/verify_network_query_test.approved.txt rename to example/verify_methods/verify_query/verify_network_query_test.verify_network_query.approved.txt diff --git a/example/verify_methods/verify_sequence/verify_sequence_test.dart b/example/verify_methods/verify_sequence/verify_sequence_test.dart index 31e5b39..9850547 100644 --- a/example/verify_methods/verify_sequence/verify_sequence_test.dart +++ b/example/verify_methods/verify_sequence/verify_sequence_test.dart @@ -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 sequence = [1, 2, 3, 4, 5]; Approvals.verifySequence( diff --git a/example/verify_methods/verify_sequence/verify_sequence_test.approved.txt b/example/verify_methods/verify_sequence/verify_sequence_test.verify_sequence.approved.txt similarity index 100% rename from example/verify_methods/verify_sequence/verify_sequence_test.approved.txt rename to example/verify_methods/verify_sequence/verify_sequence_test.verify_sequence.approved.txt diff --git a/lib/approval_tests.dart b/lib/approval_tests.dart index a9898f2..e4e11d9 100644 --- a/lib/approval_tests.dart +++ b/lib/approval_tests.dart @@ -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'; diff --git a/lib/src/approvals.dart b/lib/src/approvals.dart index 40e9978..b9e7d4f 100644 --- a/lib/src/approvals.dart +++ b/lib/src/approvals.dart @@ -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); @@ -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) { diff --git a/lib/src/core/extensions/approval_string_extensions.dart b/lib/src/core/extensions/approval_string_extensions.dart index 0ca39fa..b8687d1 100644 --- a/lib/src/core/extensions/approval_string_extensions.dart +++ b/lib/src/core/extensions/approval_string_extensions.dart @@ -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)!),); } } diff --git a/lib/src/core/utils/utils.dart b/lib/src/core/utils/utils.dart index e081194..ad61a28 100644 --- a/lib/src/core/utils/utils.dart +++ b/lib/src/core/utils/utils.dart @@ -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; } @@ -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; diff --git a/lib/src/namer/namer.dart b/lib/src/namer/namer.dart index 937ebb3..4cbbc1e 100644 --- a/lib/src/namer/namer.dart +++ b/lib/src/namer/namer.dart @@ -17,7 +17,9 @@ 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 @@ -25,7 +27,9 @@ final class Namer implements ApprovalNamer { if (options != null) { return options!.approvedFileName; } - return addTestName ? '$_fileName.$currentTestName.$approvedExtension' : '$_fileName.$approvedExtension'; + return addTestName + ? '$_fileName.$currentTestName.$approvedExtension' + : '$_fileName.$approvedExtension'; } @override @@ -33,7 +37,9 @@ final class Namer implements ApprovalNamer { if (options != null) { return options!.received; } - return addTestName ? '$filePath.$currentTestName.$receivedExtension' : '$filePath.$receivedExtension'; + return addTestName + ? '$filePath.$currentTestName.$receivedExtension' + : '$filePath.$receivedExtension'; } @override @@ -41,7 +47,9 @@ final class Namer implements ApprovalNamer { if (options != null) { return options!.receivedFileName; } - return addTestName ? '$_fileName.$currentTestName.$receivedExtension' : '$_fileName.$receivedExtension'; + return addTestName + ? '$_fileName.$currentTestName.$receivedExtension' + : '$_fileName.$receivedExtension'; } @override diff --git a/lib/src/comparator/command_line_comparator.dart b/lib/src/reporters/command_line_comparator.dart similarity index 100% rename from lib/src/comparator/command_line_comparator.dart rename to lib/src/reporters/command_line_comparator.dart diff --git a/lib/src/comparator/comparator.dart b/lib/src/reporters/comparator.dart similarity index 100% rename from lib/src/comparator/comparator.dart rename to lib/src/reporters/comparator.dart diff --git a/lib/src/comparator/diff_info.dart b/lib/src/reporters/diff_info.dart similarity index 100% rename from lib/src/comparator/diff_info.dart rename to lib/src/reporters/diff_info.dart diff --git a/lib/src/comparator/diff_tools.dart b/lib/src/reporters/diff_tools.dart similarity index 93% rename from lib/src/comparator/diff_tools.dart rename to lib/src/reporters/diff_tools.dart index 9b1e4e1..4ec223d 100644 --- a/lib/src/comparator/diff_tools.dart +++ b/lib/src/reporters/diff_tools.dart @@ -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', ); diff --git a/lib/src/comparator/ide_comparator.dart b/lib/src/reporters/ide_comparator.dart similarity index 95% rename from lib/src/comparator/ide_comparator.dart rename to lib/src/reporters/ide_comparator.dart index 1cd68f5..bd0dbfd 100644 --- a/lib/src/comparator/ide_comparator.dart +++ b/lib/src/reporters/ide_comparator.dart @@ -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, ); diff --git a/lib/src/scrubbers/reg_exp_scrubber.dart b/lib/src/scrubbers/reg_exp_scrubber.dart index 401f678..7a158c1 100644 --- a/lib/src/scrubbers/reg_exp_scrubber.dart +++ b/lib/src/scrubbers/reg_exp_scrubber.dart @@ -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 diff --git a/test/approval_test.dart b/test/approval_test.dart index 04a5f59..0eaaa9b 100644 --- a/test/approval_test.dart +++ b/test/approval_test.dart @@ -76,97 +76,6 @@ void main() { "$lines25 Group: Exception cases are starting $lines25", ); }); - // test('Method «verify» must throw PathNotFoundException', () { - // expect( - // () => helper.verify( - // 'Hello World', - // 'verify_exception', - // expectException: true, - // ), - // throwsA(isA()), - // ); - // ApprovalLogger.success( - // "Test Passed: Method «verify» correctly throws PathNotFoundException as expected.", - // ); - // }); - - // test('Method «verifyAll» must throw PathNotFoundException', () { - // expect( - // () => helper.verifyAll( - // ['Hello World', 'Hello World'], - // 'verify_exception', - // expectException: true, - // ), - // throwsA(isA()), - // ); - - // ApprovalLogger.success( - // "Test Passed: Method «verifyAll» correctly throws PathNotFoundException as expected.", - // ); - // }); - - // test('Method «verifyAsJson» must throw PathNotFoundException', () { - // expect( - // () => helper.verifyAsJson( - // {"message": "Hello World"}, - // 'verify_as_json_exception', - // expectException: true, - // ), - // throwsA(isA()), - // ); - - // ApprovalLogger.success( - // "Test Passed: Method «verifyAsJson» correctly throws PathNotFoundException as expected.", - // ); - // }); - - // test('Method «verifyAllCombinations» must throw PathNotFoundException', () { - // expect( - // () => helper.verifyAllCombinations( - // [ - // [1, 2], - // [3, 4], - // ], - // 'verify_all_combinations_exception', - // expectException: true, - // ), - // throwsA(isA()), - // ); - - // ApprovalLogger.success( - // "Test Passed: Method «verifyAllCombinations» correctly throws PathNotFoundException as expected.", - // ); - // }); - - // test("Method «verifySequence» must throw PathNotFoundException", () { - // expect( - // () => helper.verifySequence( - // [1, 2, 3], - // 'verify_sequence_exception', - // expectException: true, - // ), - // throwsA(isA()), - // ); - - // ApprovalLogger.success( - // "Test Passed: Method «verifySequence» correctly throws PathNotFoundException as expected.", - // ); - // }); - - // test("Method «verifyQuery» must throw PathNotFoundException", () async { - // expect( - // () => helper.verifyQuery( - // dbQuery, - // 'verify_query_exception', - // expectException: true, - // ), - // throwsA(isA()), - // ); - - // ApprovalLogger.success( - // "Test Passed: Method «verifyQuery» correctly throws PathNotFoundException as expected.", - // ); - // }); test("Method «verify» must throw DoesntMatchException", () { expect( @@ -203,20 +112,6 @@ void main() { setUpAll(() { ApprovalLogger.log("$lines25 Group: Minor cases are starting $lines25"); }); - // test('Verify method without initial path: PathNotFoundException', () { - // expect( - // () => helper.verify( - // 'Hello World', - // 'verify_exception', - // expectException: true, - // useDefaultPath: false, - // ), - // throwsA(isA()), - // ); - // ApprovalLogger.success( - // "Test Passed: The method was successfully verified for absence of an initial path — PathNotFoundException was thrown.", - // ); - // }); test('Simulate file not found error during comparison. Must throw CommandLineComparatorException.', () async { const comparator = CommandLineComparator(); diff --git a/test/example/example_test.dart b/test/example/example_test.dart index 3a50ae0..9284f8b 100644 --- a/test/example/example_test.dart +++ b/test/example/example_test.dart @@ -3,7 +3,7 @@ import 'package:test/test.dart'; // begin-snippet: sample_verify_as_json_test void main() { - test('test complex JSON object', () { + test('test JSON object', () { final complexObject = { 'name': 'JsonTest', 'features': ['Testing', 'JSON'], @@ -12,9 +12,6 @@ void main() { Approvals.verifyAsJson( complexObject, - options: const Options( - approveResult: true, - ), ); }); } diff --git a/test/example/example_test.approved.txt b/test/example/example_test.test_JSON_object.approved.txt similarity index 100% rename from test/example/example_test.approved.txt rename to test/example/example_test.test_JSON_object.approved.txt diff --git a/test/example/example_test.test_complex_JSON_object.approved.txt b/test/example/example_test.test_complex_JSON_object.approved.txt deleted file mode 100644 index 571b02c..0000000 --- a/test/example/example_test.test_complex_JSON_object.approved.txt +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "JsonTest", - "features": [ - "Testing", - "JSON" - ], - "version": 0.1 -} \ No newline at end of file diff --git a/test/utils/helper.Verify_method_without_initial_path:_PathNotFoundException.approved.txt b/test/utils/helper.Verify_method_without_initial_path:_PathNotFoundException.approved.txt deleted file mode 100644 index 5e1c309..0000000 --- a/test/utils/helper.Verify_method_without_initial_path:_PathNotFoundException.approved.txt +++ /dev/null @@ -1 +0,0 @@ -Hello World \ No newline at end of file diff --git a/test/utils/helper.dart b/test/utils/helper.dart index ff67473..832100d 100644 --- a/test/utils/helper.dart +++ b/test/utils/helper.dart @@ -53,7 +53,8 @@ class ApprovalTestHelper { }) { Approvals.verifyAll( contents, - processor: (item) => item, // Simple processor function that returns the item itself. + processor: (item) => + item, // Simple processor function that returns the item itself. options: _getOptions( testName, expectException: expectException,