Skip to content

Commit

Permalink
@ d t added tests for comparators.
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed May 11, 2024
1 parent e808e9c commit c6f6a72
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 63 deletions.
56 changes: 28 additions & 28 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
## 0.4.2

- Autopublish to pub.dev if the version in pubspec.yaml has been changed by auto adding a new tag to the repositories.
- Codecov added to github actions. Codecov badge, graph was added to README.md.
- Added mdsnippets to github actions. And example snippets added to README.md.
- Added `ApprovalTests.Dart.StarterProject'. You can find it in the README.md file.
- Autopublish to `pub.dev` if the version in `pubspec.yaml` has been changed by auto adding a new tag to the repositories.
- Codecov added to `github` actions. Codecov badge, graph was added to `README.md`.
- Added `mdsnippets` to `github` actions. And example snippets added to `README.md`.
- Added `ApprovalTests.Dart.StarterProject`. You can find it in the `README.md` file.

## 0.3.9

- Fix with default file path if package from pub.dev
- Gilded rose moved to starter project
- Code formatting
- Fix with default file path if package from `pub.dev`.
- Gilded rose moved to starter project.
- Code formatted.

## 0.3.8

- Test publish action with new version: 0.3.8.
- Test publish action with new version: `v0.3.8`.

## 0.3.6-dev

- Updated pubspec.yaml file: changed homepage.
- Updated `pubspec.yaml` file: changed homepage.

## 0.3.5-dev

- The repository has been moved to Approvals.
- Fixed bug with checking of strings received from txt file.
- Added Github Actions.
- Added dependabot.
- Updated README file.
- Updated TODO file.
- Fixed bug with checking of strings received from `txt` file.
- Added `Github` Actions.
- Added `dependabot`.
- Updated `README.md` file.
- Updated `TODO.md` file.


## 0.3.3-dev

- Updated README file.
- Updated `README.md` file.

## 0.3.2-dev

- Default `lints` replaced by `sizzle_lints`.
- Updated `analysis_options` file.
- Updated README file.
- Updated `README.md` file.

## 0.3.1-dev

- Updated README file.
- Updated `README.md` file.

## 0.3.0-dev

- Approval was refactored
- Added tests
- Code formatted
- Approval was refactored.
- Added tests.
- Code formatted.
- `deleteReceivedFile` field was added to the `ApprovalTests` class. If it is set to `true`, the received file will be deleted after test. By default, it is set to `false`.
- `logErrors` field was added to the `ApprovalTests` class. If it is set to `true`, the errors will be logged. By default, it is set to `true`.
- `logResults` field was added to the `ApprovalTests` class. If it is set to `true`, the success results will be logged. By default, it is set to `true`.

## 0.2.1-dev

- Fix: `approval_dart` changed to `approval_tests`.
- Code formatted
- Code formatted.

## 0.2.0-dev

- I rewrote the main functions of the class. Now you can use several comparison options.
- Added methods for comparing JSON strings.
- Added methods for comparing `JSON` strings.
- Added methods for array comparison.
- Improved documentation.
- Added more examples: each method has its own small example.

## 0.1.0-dev

- Added `verifyAll` method to verify array of items *(or use array as inputs)*.
- Added `verifyAllCombinationsAsJson` method to verify all combinations of items as JSON.
- Updated README file.
- Added `verifyAllCombinationsAsJson` method to verify all combinations of items as `JSON`.
- Updated `README.md` file.

## 0.0.9-dev

- Updated README file.
- Updated `README.md` file.

## 0.0.8-dev

- Updated README file: added examples, more info.
- Updated `README.md` file: added examples, more info.

## 0.0.7-dev

- Comparator completed, some refactoring. Updated README file.
- Comparator completed, some refactoring. Updated `README.md` file.

## 0.0.6-dev

- Some updates with README file.
- Some updates with `README.md` file.

## 0.0.5-dev

Expand Down
2 changes: 2 additions & 0 deletions lib/approval_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ part 'src/core/utils/extensions.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';
16 changes: 7 additions & 9 deletions lib/src/comparator/command_line_comparator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ part of '../../approval_tests.dart';
/// `CommandLineComparator` it is for comparing files via Command Line.
///
/// This method compares the content of two files line by line and prints the differences in the console.
class CommandLineComparator extends ComparatorImp {
class CommandLineComparator extends Comparator {
const CommandLineComparator();

@override
Expand Down Expand Up @@ -39,16 +39,14 @@ class CommandLineComparator extends ComparatorImp {

if (buffer.isNotEmpty && isLogError) {
final String message = buffer.toString();
logError(exception: message.trim());
ApprovalLogger.exception(message);
}
} catch (e, st) {
if (isLogError) {
logError(
exception: 'Error during comparison via Command Line. Error: $e',
stackTrace: st,
);
}
rethrow;
throw CommandLineComparatorException(
message: 'Error during comparison via Command Line.',
exception: e,
stackTrace: st,
);
}
}
}
13 changes: 0 additions & 13 deletions lib/src/comparator/comparator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,3 @@ abstract interface class Comparator {
bool isLogError = true,
});
}

/// `ComparatorImp` is an abstract class for implementing the `Comparator` interface.
abstract class ComparatorImp implements Comparator {
const ComparatorImp();

/// A method named `logError` for logging errors.
void logError({
required Object exception,
StackTrace? stackTrace,
}) {
ApprovalLogger.exception(exception, stackTrace: stackTrace);
}
}
34 changes: 25 additions & 9 deletions lib/src/comparator/ide_comparator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ part of '../../approval_tests.dart';
/// - `Visual Studio Code`
/// - `IntelliJ IDEA`
/// - `Android Studio`
final class IDEComparator extends ComparatorImp {
final class IDEComparator extends Comparator {
final ComparatorIDE ide;

const IDEComparator({
Expand All @@ -20,19 +20,35 @@ final class IDEComparator extends ComparatorImp {
bool isLogError = true,
}) async {
try {
final File approvedFile = File(approvedPath);
final File receivedFile = File(receivedPath);

if (!_fileExists(approvedFile) || !_fileExists(receivedFile)) {
_throwFileException(
'Files not found for comparison. Please check the paths: \n\n Approved file path: $approvedPath, \n\n Received file path: $receivedPath.',
);
}

await Process.run(
ide.command,
[ide.argument, approvedPath, receivedPath],
);
} catch (e, st) {
if (isLogError) {
logError(
exception:
'Error during comparison via ${ide.name}. Please restart your IDE. Error: $e',
stackTrace: st,
);
}
rethrow;
throw IDEComparatorException(
message: 'Error during comparison via ${ide.name}. Please try restart your IDE.',
exception: e,
stackTrace: st,
);
}
}

bool _fileExists(File file) => file.existsSync();

void _throwFileException(String message) {
throw IDEComparatorException(
message: message,
exception: null,
stackTrace: null,
);
}
}
14 changes: 14 additions & 0 deletions lib/src/reporter/command_line_comparator_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
part of '../../approval_tests.dart';

/// Exception thrown when an error occurs during comparison via Command Line.
final class CommandLineComparatorException implements Exception {
final String message;
final Object? exception;
final StackTrace? stackTrace;

const CommandLineComparatorException({
required this.message,
required this.exception,
required this.stackTrace,
});
}
14 changes: 14 additions & 0 deletions lib/src/reporter/ide_comparator_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
part of '../../approval_tests.dart';

/// Exception thrown when an error occurs during comparison via IDE.
final class IDEComparatorException implements Exception {
final String message;
final Object? exception;
final StackTrace? stackTrace;

const IDEComparatorException({
required this.message,
required this.exception,
required this.stackTrace,
});
}
32 changes: 28 additions & 4 deletions test/approval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ void main() {
);
});

test("Method «verify» must throw DoesntMatchException with error handling",
() {
test("Method «verify» must throw DoesntMatchException with error handling", () {
expect(
() => helper.verify(
'Hello W0rld',
Expand Down Expand Up @@ -235,7 +234,7 @@ void main() {
);
});

test('Simulate file not found error during comparison', () async {
test('Simulate file not found error during comparison. Must throw CommandLineComparatorException.', () async {
const comparator = CommandLineComparator();

// Setup: paths to non-existent files
Expand All @@ -248,7 +247,32 @@ void main() {
approvedPath: nonExistentApprovedPath,
receivedPath: nonExistentReceivedPath,
),
throwsA(isA<FileSystemException>()),
throwsA(isA<CommandLineComparatorException>()),
);

ApprovalLogger.success(
"Test Passed: Successfully handled a file not found error during comparison.",
);
});

test('Simulate file not found error during comparison. Must throw IDEComparatorException.', () async {
const comparator = IDEComparator(ide: ComparatorIDE.visualStudioCode);

// Setup: paths to non-existent files
const nonExistentApprovedPath = 'path/to/nonexistent/approved.txt';
const nonExistentReceivedPath = 'path/to/nonexistent/received.txt';

// Expect an exception to be thrown
expect(
() => comparator.compare(
approvedPath: nonExistentApprovedPath,
receivedPath: nonExistentReceivedPath,
),
throwsA(isA<IDEComparatorException>()),
);

ApprovalLogger.success(
"Test Passed: Successfully handled a file not found error during comparison.",
);
});
});
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit c6f6a72

Please sign in to comment.