From e6fd5f90fa084ffefd2a3936449b251e68b91a6f Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 1 Apr 2022 13:07:25 +0100 Subject: [PATCH] feat: disallowed adding components to `ComponentController` (#121) * feat: disallowed adding components to a ComponentController * docs: rephrased text * refactor: fixed variable typo * feat: included addAll test --- lib/flame/component_controller.dart | 5 ++++ test/flame/component_controller_test.dart | 30 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/flame/component_controller.dart b/lib/flame/component_controller.dart index 851028f0..1d6e0173 100644 --- a/lib/flame/component_controller.dart +++ b/lib/flame/component_controller.dart @@ -23,6 +23,11 @@ abstract class ComponentController extends Component { ); await super.addToParent(parent); } + + @override + Future add(Component component) { + throw Exception('ComponentController cannot 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..e1973274 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,35 @@ void main() { ); }, ); + + flameTester.test( + 'throws Exception when adding a component', + (game) async { + final component = ControlledComponent(); + final controller = TestComponentController(component); + + await expectLater( + () async => controller.add(Component()), + throwsException, + ); + }, + ); + + flameTester.test( + 'throws Exception when adding multiple components', + (game) async { + final component = ControlledComponent(); + final controller = TestComponentController(component); + + await expectLater( + () async => controller.addAll([ + Component(), + Component(), + ]), + throwsException, + ); + }, + ); }); group('Controls', () {