From baab7f65c4eee7e18190fafbe3bdd2e575c4c27f Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Wed, 23 Mar 2022 12:10:01 +0000 Subject: [PATCH] refactor: removed findNested extensions (#77) --- test/game/components/board_test.dart | 19 +++++++------- test/helpers/extensions.dart | 39 ---------------------------- 2 files changed, 9 insertions(+), 49 deletions(-) diff --git a/test/game/components/board_test.dart b/test/game/components/board_test.dart index 7791d891..f0cd0e16 100644 --- a/test/game/components/board_test.dart +++ b/test/game/components/board_test.dart @@ -30,9 +30,9 @@ void main() { await game.ready(); await game.ensureAdd(board); - final leftFlippers = board.findNestedChildren( - condition: (flipper) => flipper.side.isLeft, - ); + final leftFlippers = board.descendants().whereType().where( + (flipper) => flipper.side.isLeft, + ); expect(leftFlippers.length, equals(1)); }, ); @@ -43,10 +43,9 @@ void main() { final board = Board(); await game.ready(); await game.ensureAdd(board); - - final rightFlippers = board.findNestedChildren( - condition: (flipper) => flipper.side.isRight, - ); + final rightFlippers = board.descendants().whereType().where( + (flipper) => flipper.side.isRight, + ); expect(rightFlippers.length, equals(1)); }, ); @@ -58,7 +57,7 @@ void main() { await game.ready(); await game.ensureAdd(board); - final baseboards = board.findNestedChildren(); + final baseboards = board.descendants().whereType(); expect(baseboards.length, equals(2)); }, ); @@ -70,7 +69,7 @@ void main() { await game.ready(); await game.ensureAdd(board); - final kickers = board.findNestedChildren(); + final kickers = board.descendants().whereType(); expect(kickers.length, equals(2)); }, ); @@ -83,7 +82,7 @@ void main() { await game.ready(); await game.ensureAdd(board); - final roundBumpers = board.findNestedChildren(); + final roundBumpers = board.descendants().whereType(); expect(roundBumpers.length, equals(3)); }, ); diff --git a/test/helpers/extensions.dart b/test/helpers/extensions.dart index abea191d..b3c4c6f8 100644 --- a/test/helpers/extensions.dart +++ b/test/helpers/extensions.dart @@ -1,4 +1,3 @@ -import 'package:flame/components.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_theme/pinball_theme.dart'; @@ -21,41 +20,3 @@ extension DebugPinballGameTest on DebugPinballGame { ), ); } - -extension ComponentX on Component { - T findNestedChild({ - bool Function(T)? condition, - }) { - T? nestedChild; - propagateToChildren((child) { - final foundChild = (condition ?? (_) => true)(child); - if (foundChild) { - nestedChild = child; - } - - return !foundChild; - }); - - if (nestedChild == null) { - throw Exception('No child of type $T found.'); - } else { - return nestedChild!; - } - } - - List findNestedChildren({ - bool Function(T)? condition, - }) { - final nestedChildren = []; - propagateToChildren((child) { - final foundChild = (condition ?? (_) => true)(child); - if (foundChild) { - nestedChildren.add(child); - } - - return true; - }); - - return nestedChildren; - } -}