Skip to content

Commit

Permalink
@ F added header to generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed Jul 2, 2024
1 parent 50f46cd commit 81a1ae7
Show file tree
Hide file tree
Showing 40 changed files with 87 additions and 30 deletions.
2 changes: 1 addition & 1 deletion examples/dart_example/test/dart_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void main() {
[3, 5, 15],
options: const Options(
reporter: DiffReporter(),
deleteApprovedFile: true,
deleteReceivedFile: false,
),
processor: (items) => fizzBuzz(items).toString(),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

[1, 2, Fizz]
[1, 2, Fizz, 4, Buzz]
[1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

MyApp: {count: 1}
MyHomePage: {count: 1}
Text: {text: 'You have pushed the button this many times:', count: 1}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# This file was generated by approval_tests. Please do not edit.

Text: {key: 'Counter', text: '2', count: 1}
Text: {key: 'Counter', text: '0', count: 0}
3 changes: 3 additions & 0 deletions packages/approval_tests/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Added new reporter: `GitReporter`. It allows you to use `git` to view the differences between the received and approved files.
- Added support to approve files using CLI. Now you can approve files using the command line: `dart run approval_tests:review`
- Added support to use ApprovalTests during widget tests.
- Added header to generated files. For resolved issues you can add this to approved files:
'# This file was generated by approval_tests. Please do not edit.\n'
- Some minor changes and code improvements.
Thanks to [Richard Coutts](https://github.com/buttonsrtoys)

## 1.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

[1, 2, Fizz]
[1, 2, Fizz, 4, Buzz]
[1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

{"result": "success", "data": {"id": 1, "name": "Item"}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

Item: apple
Item: banana
Item: cherry
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

Combination: [1, 3], [1, 4], [2, 3], [2, 4]
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

{
"name": "JsonTest",
"features": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

{
"jsonItem": {
"id": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

{"id": "1", "name": "John Doe", "email": "[email protected]"}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

{
"userId": 1,
"id": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

1
2
3
Expand Down
1 change: 1 addition & 0 deletions packages/approval_tests/lib/approval_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:approval_tests/src/core/constants/constants.dart';
import 'package:approval_tests/src/core/enums/file_type.dart';
import 'package:diff_match_patch2/diff_match_patch.dart';
import 'package:talker/talker.dart';
Expand Down
3 changes: 0 additions & 3 deletions packages/approval_tests/lib/src/approvals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ class Approvals {
if (options.deleteReceivedFile) {
_deleteFileAfterTest(namer: namer, fileType: FileType.received);
}
if (options.deleteApprovedFile) {
_deleteFileAfterTest(namer: namer, fileType: FileType.approved);
}
}
}
} catch (e, st) {
Expand Down
10 changes: 3 additions & 7 deletions packages/approval_tests/lib/src/core/approval_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ part of '../../approval_tests.dart';

/// `ApprovalWriter` is an abstract class for writing approvals.
abstract interface class ApprovalWriter {
/// A method named `approvalFilename` that returns name of approved file.
String approvalFilename(String base);
const ApprovalWriter();

/// A method named `receivedFilename` that returns name of received file.
String receivedFilename(String base);

/// `writeReceivedFile` method writes the received file.
void writeReceivedFile(String received);
/// `writeToFile` method writes the received file.
void writeToFile(String path);
}
8 changes: 8 additions & 0 deletions packages/approval_tests/lib/src/core/constants/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
final class ApprovalTestsConstants {
static const String widgetHeader = '''
# This file was autogenerated by package:approval_tests_flutter. Please do not edit.
# Below is a list of class found in the project /lib folder.''';

static const String baseHeader = '''
# This file was generated by approval_tests. Please do not edit.\n''';
}
6 changes: 0 additions & 6 deletions packages/approval_tests/lib/src/core/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class Options {
/// A final bool variable `deleteReceivedFile` used to determine if the received file should be deleted after passed test.
final bool deleteReceivedFile;

/// A final bool variable `deleteApprovedFile` used to determine if the approved file should be deleted after passed test.
final bool deleteApprovedFile;

/// A final variable `namer` of type `Namer` used to set the name and path of the file.
final Namer? namer;

Expand All @@ -55,7 +52,6 @@ class Options {
this.comparator = const FileComparator(),
this.reporter = const CommandLineReporter(),
this.deleteReceivedFile = true,
this.deleteApprovedFile = false,
this.namer,
this.logErrors = true,
this.logResults = true,
Expand All @@ -68,7 +64,6 @@ class Options {
Comparator? comparator,
Reporter? reporter,
bool? deleteReceivedFile,
bool? deleteApprovedFile,
Namer? namer,
bool? logErrors,
bool? logResults,
Expand All @@ -80,7 +75,6 @@ class Options {
comparator: comparator ?? this.comparator,
reporter: reporter ?? this.reporter,
deleteReceivedFile: deleteReceivedFile ?? this.deleteReceivedFile,
deleteApprovedFile: deleteApprovedFile ?? this.deleteApprovedFile,
namer: namer ?? this.namer,
logErrors: logErrors ?? this.logErrors,
logResults: logResults ?? this.logResults,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@
part of '../../approval_tests.dart';

/// `ApprovalTextWriter` is a class that writes the content to a file at the specified path.
class ApprovalTextWriter {
class ApprovalTextWriter extends ApprovalWriter {
// The two instance variables content and fileExtension of type String
final String content;

// Constructor for the class ApprovalTextWriter that takes in two parameters: content and fileExtension
const ApprovalTextWriter(this.content);

// A method that writes the given content to the file at the specified path
@override
void writeToFile(String path) {
// File instance is created with the given path
final File file = File(path);

final StringBuffer buffer = StringBuffer();
buffer.writeln(ApprovalTestsConstants.baseHeader);
buffer.write(content);

// Check if the file already exists at the specific path
if (!file.existsSync()) {
// If the file does not exist, then it is created
file.createSync(recursive: true);

// After creating the file, the content is written to it
file.writeAsStringSync(content);
file.writeAsStringSync(buffer.toString());
} else {
// If the file already exists, then the content is simply overwritten
file.writeAsStringSync(content);
file.writeAsStringSync(buffer.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

Hello World
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

Hello World
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# This file was generated by approval_tests. Please do not edit.

Hello World
Hello World
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

Combination: [1, 3], [1, 4], [2, 3], [2, 4]
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

Combination: [1, 3], [1, 4], [2, 3], [2, 4]
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

{
"message": "Hello World"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

{
"message": "Hello World"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

{
"jsonItem": {
"id": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

-Hello-World-
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# This file was generated by approval_tests. Please do not edit.

<date1>
<date2>
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

Hello World
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

{"id": "1", "name": "John Doe", "email": "[email protected]"}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

{"id": "1", "name": "John Doe", "email": "[email protected]"}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

Hello World
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

1
2
3
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

1
2
3
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This file was generated by approval_tests. Please do not edit.

{
"id": 1,
"name": "JsonItem",
Expand Down
9 changes: 0 additions & 9 deletions packages/approval_tests/test/groups/minor_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,6 @@ void main() {
);
});

test('Verify without namer', () {
Approvals.verify(
'Hello World',
options: const Options(
deleteApprovedFile: true,
),
);
});

test('Verify model without class name', () {
helper.verifyAsJson(
ApprovalTestHelper.jsonItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file was generated by approval_tests. Please do not edit.

Hello World
1 change: 0 additions & 1 deletion packages/approval_tests_flutter/lib/src/src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Future<void> Function(String?, String, Options?) _globalApprovalTest =
comparator: options.comparator,
reporter: options.reporter,
deleteReceivedFile: options.deleteReceivedFile,
deleteApprovedFile: options.deleteApprovedFile,
namer: Namer(
filePath: options.namer?.filePath,
options: options.namer?.options,
Expand Down

0 comments on commit 81a1ae7

Please sign in to comment.