-
-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
ParentIsA
to force parent child relations (#1566)
- Loading branch information
1 parent
666a2b1
commit 2cdf386
Showing
4 changed files
with
68 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import '../../../components.dart'; | ||
|
||
/// A mixin that ensures a parent is of the given type [T]. | ||
mixin ParentIsA<T extends Component> on Component { | ||
@override | ||
T get parent => super.parent! as T; | ||
|
||
@override | ||
void onMount() { | ||
assert(super.parent is T, 'Parent must be of type $T'); | ||
super.onMount(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/flame/test/components/mixins/parent_is_a_test.dart
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
class ParentComponent extends Component {} | ||
|
||
class DifferentComponent extends Component {} | ||
|
||
class TestComponent extends Component with ParentIsA<ParentComponent> {} | ||
|
||
void main() { | ||
group('ParentIsA', () { | ||
testWithFlameGame('successfully sets the parent link', (game) async { | ||
final parent = ParentComponent(); | ||
final component = TestComponent(); | ||
|
||
await parent.add(component); | ||
await game.add(parent); | ||
|
||
expect(component.parent, isA<ParentComponent>()); | ||
}); | ||
|
||
test('throws assertion error when the wrong parent is used', () { | ||
final parent = DifferentComponent(); | ||
final component = TestComponent(); | ||
|
||
parent.add(component); | ||
|
||
expect( | ||
component.onMount, | ||
failsAssert('Parent must be of type ParentComponent'), | ||
); | ||
}); | ||
}); | ||
} |