-
Notifications
You must be signed in to change notification settings - Fork 58
feat: Support diffThreshold to handle comparison failures that depend on the image generation environment #176
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
be3d6be
Support diffThreshold to handle comparison failures that depend on th…
mataku f8c1079
Run dart format
mataku 8ec0c5c
Fix baseDir usage
mataku 2357227
fix: correct expected error type in golden test runner test
mataku 14547f6
Run dart analyzer issues
mataku 6f65b0a
Add missing alchemist_file_comparator method tests
mataku 6d827bc
fix: move comparator setup into try block and restrict to exact Local…
mataku 9ad8504
docs: remove incorrect "warning is printed" from diffThreshold docume…
mataku File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| /// A [LocalFileComparator] that passes golden tests within a configurable | ||
| /// pixel-diff diffThreshold. | ||
| /// | ||
| /// When [diffThreshold] is greater than 0 and the diff percentage is within | ||
| /// diffThreshold, the test passes. This is useful for handling minor | ||
| /// cross-platform or cross-architecture rendering differences. | ||
| class AlchemistFileComparator extends LocalFileComparator { | ||
| /// Creates an [AlchemistFileComparator] with the given [testUri] and | ||
| /// [diffThreshold]. | ||
| /// | ||
| /// The [diffThreshold] must be between 0.0 (inclusive) and 1.0 (exclusive). | ||
| AlchemistFileComparator(super.testUri, this.diffThreshold) | ||
| : assert( | ||
| diffThreshold >= 0.0 && diffThreshold < 1.0, | ||
| 'diffThreshold must be between 0.0 (inclusive) and 1.0 (exclusive)', | ||
| ); | ||
|
|
||
| /// Creates an [AlchemistFileComparator] from an [existing] | ||
| /// [LocalFileComparator], preserving its base directory. | ||
| factory AlchemistFileComparator.fromExisting( | ||
| LocalFileComparator existing, | ||
| double diffThreshold, | ||
| ) { | ||
| return AlchemistFileComparator( | ||
| existing.basedir.resolve('_alchemist.dart'), | ||
| diffThreshold, | ||
| ); | ||
| } | ||
|
|
||
| /// The maximum fraction of differing pixels that is still considered a | ||
| /// passing test. | ||
| /// | ||
| /// Must be between 0.0 (inclusive) and 1.0 (exclusive). When the diff | ||
| /// percentage exceeds 0 but is within this threshold, the test passes. | ||
| /// A value of 0.0 means no threshold is applied. | ||
| final double diffThreshold; | ||
|
|
||
| /// Compares the given [imageBytes] to the [goldenBytes] and returns the | ||
| /// [ComparisonResult]. | ||
| /// | ||
| /// Exposed for testing. In production, [compare] calls this method | ||
| /// internally. | ||
| @protected | ||
| @visibleForTesting | ||
| Future<ComparisonResult> compareImageBytes( | ||
| Uint8List imageBytes, | ||
| Uint8List goldenBytes, | ||
| ) { | ||
| return GoldenFileComparator.compareLists(imageBytes, goldenBytes); | ||
| } | ||
|
|
||
| /// Compares [imageBytes] to the golden file identified by [golden]. | ||
| /// | ||
| /// Returns `true` if the images match, or if the diff percentage is within | ||
| /// [diffThreshold]. Returns `false` and generates failure output otherwise. | ||
| @override | ||
| Future<bool> compare(Uint8List imageBytes, Uri golden) async { | ||
| final goldenBytes = await getGoldenBytes(golden); | ||
| final result = await compareImageBytes( | ||
| imageBytes, | ||
| Uint8List.fromList(goldenBytes), | ||
| ); | ||
|
|
||
| if (result.passed) return true; | ||
|
|
||
| if (diffThreshold > 0 && result.diffPercent <= diffThreshold) { | ||
| return true; | ||
| } | ||
|
|
||
| await generateFailureOutput(result, golden, basedir); | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.