-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Implement save and restore functions on mock canvas so their effects can be tested #3
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import 'dart:ui'; | |
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:test/fake.dart'; | ||
import 'package:vector_math/vector_math_64.dart'; | ||
|
||
import 'assertion_mode.dart'; | ||
import 'canvas_commands/cliprect_command.dart'; | ||
|
@@ -50,11 +51,14 @@ import 'canvas_commands/transform_command.dart'; | |
class MockCanvas extends Fake implements Canvas, Matcher { | ||
MockCanvas({this.mode = AssertionMode.matchExactly}) | ||
: _commands = [], | ||
_saveStack = [], | ||
_saveCount = 0; | ||
|
||
final AssertionMode mode; | ||
final List<CanvasCommand> _commands; | ||
|
||
int _saveCount; | ||
final List<Matrix4> _saveStack; | ||
|
||
/// The absolute tolerance used when comparing numeric quantities for | ||
/// equality. Two numeric variables `x` and `y` are considered equal if they | ||
|
@@ -78,7 +82,7 @@ class MockCanvas extends Fake implements Canvas, Matcher { | |
|
||
@override | ||
Description describe(Description description) { | ||
description.add('Canvas$_commands'); | ||
description.add('Canvas {\n${_commands.join('\n')}\n}'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just found that its much easier to read like this when there are a handful of commands |
||
return description; | ||
} | ||
|
||
|
@@ -100,7 +104,7 @@ class MockCanvas extends Fake implements Canvas, Matcher { | |
final n2 = other._commands.length; | ||
if (n1 != n2) { | ||
return _fail( | ||
'Canvas contains $n1 commands, but $n2 expected', | ||
'Canvas contains $n2 commands, but $n1 expected', | ||
matchState, | ||
); | ||
} | ||
|
@@ -146,8 +150,8 @@ class MockCanvas extends Fake implements Canvas, Matcher { | |
}); | ||
if (idx == -1) { | ||
return _fail( | ||
'Expected canvas command not found: $expectedCommand. ' | ||
'Actual commands: $_commands', | ||
'Expected canvas command not found: $expectedCommand}. ' | ||
'Actual commands:\n${_commands.join('\n')}', | ||
matchState, | ||
); | ||
} else { | ||
|
@@ -221,17 +225,35 @@ class MockCanvas extends Fake implements Canvas, Matcher { | |
int getSaveCount() => _saveCount; | ||
|
||
@override | ||
void restore() => _saveCount--; | ||
void restore() { | ||
_saveCount--; | ||
final savedTransform = _saveStack.removeLast(); | ||
if (savedTransform == _currentTransform.matrix) { | ||
return; | ||
} | ||
_lastTransform.setFrom(savedTransform); | ||
} | ||
|
||
@override | ||
void save() => _saveCount++; | ||
void save() { | ||
_saveCount++; | ||
_saveStack.add(_currentTransform.matrix.clone()); | ||
} | ||
|
||
//#endregion | ||
|
||
//#region Private helpers | ||
|
||
TransformCommand get _currentTransform { | ||
final transforms = _commands.whereType<TransformCommand>(); | ||
return transforms.isEmpty ? TransformCommand() : transforms.last; | ||
} | ||
|
||
bool get _isLastCommandTransform => | ||
_commands.isNotEmpty && _commands.last is TransformCommand; | ||
|
||
TransformCommand get _lastTransform { | ||
if (_commands.isNotEmpty && _commands.last is TransformCommand) { | ||
if (_isLastCommandTransform) { | ||
return _commands.last as TransformCommand; | ||
} | ||
final transform2d = TransformCommand(); | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a public
transform
conflicts with the method name so I changed tomatrix