From 1019b669fa626924982174462843a9eef152b250 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 31 Mar 2022 22:52:17 +0100 Subject: [PATCH] feat: disallowed adding components to a ComponentController --- lib/flame/component_controller.dart | 5 +++++ test/flame/component_controller_test.dart | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/flame/component_controller.dart b/lib/flame/component_controller.dart index 2bbf5ca9..bc88094c 100644 --- a/lib/flame/component_controller.dart +++ b/lib/flame/component_controller.dart @@ -22,6 +22,11 @@ abstract class ComponentController extends Component { ); await super.addToParent(parent); } + + @override + Future add(Component component) { + throw Exception('ComponentController should not add other components.'); + } } /// Mixin that attaches a single [ComponentController] to a [Component]. diff --git a/test/flame/component_controller_test.dart b/test/flame/component_controller_test.dart index 4e5da210..0df9719b 100644 --- a/test/flame/component_controller_test.dart +++ b/test/flame/component_controller_test.dart @@ -31,6 +31,7 @@ void main() { ); }, ); + flameTester.test( 'throws AssertionError when not attached to controlled component', (game) async { @@ -44,6 +45,20 @@ void main() { ); }, ); + + flameTester.test( + 'throws Exception when adding other component', + (game) async { + final component = ControlledComponent(); + final controller = TestComponentController(component); + + final anotherComponet = Component(); + await expectLater( + () async => controller.add(anotherComponet), + throwsException, + ); + }, + ); }); group('Controls', () {