From bafa686a289816b8bd895bc1dcd1f67c31ee091b Mon Sep 17 00:00:00 2001 From: alestiago Date: Tue, 15 Mar 2022 10:16:51 +0000 Subject: [PATCH] feat: included board tests --- test/game/components/board_test.dart | 69 ++++++++++++++++++++++++++++ test/helpers/extensions.dart | 39 ++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 test/game/components/board_test.dart diff --git a/test/game/components/board_test.dart b/test/game/components/board_test.dart new file mode 100644 index 00000000..b52de89b --- /dev/null +++ b/test/game/components/board_test.dart @@ -0,0 +1,69 @@ +// ignore_for_file: cascade_invocations + +import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flame_test/flame_test.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pinball/game/game.dart'; + +import '../../helpers/helpers.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + final flameTester = FlameTester(Forge2DGame.new); + + group('BottomGroup', () { + flameTester.test( + 'loads correctly', + (game) async { + final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + await game.ready(); + await game.add(bottomGroup); + + expect(game.contains(bottomGroup), isTrue); + }, + ); + + group('children', () { + // TODO(alestiago): Make tests pass. + flameTester.test( + 'has one left flipper', + (game) async { + final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + await game.ready(); + await game.add(bottomGroup); + + final leftFlippers = bottomGroup.findNestedChildren( + condition: (flipper) => flipper.side.isLeft, + ); + expect(leftFlippers.length, equals(1)); + }, + ); + + flameTester.test( + 'has one right flipper', + (game) async { + final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + await game.ready(); + await game.add(bottomGroup); + + final leftFlippers = bottomGroup.findNestedChildren( + condition: (flipper) => flipper.side.isRight, + ); + expect(leftFlippers.length, equals(1)); + }, + ); + + flameTester.test( + 'has two Baseboards', + (game) async { + final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + await game.ready(); + await game.add(bottomGroup); + + final leftFlippers = bottomGroup.findNestedChildren(); + expect(leftFlippers.length, equals(2)); + }, + ); + }); + }); +} diff --git a/test/helpers/extensions.dart b/test/helpers/extensions.dart index 2a0a7e59..a5c56a86 100644 --- a/test/helpers/extensions.dart +++ b/test/helpers/extensions.dart @@ -1,3 +1,4 @@ +import 'package:flame/components.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_theme/pinball_theme.dart'; @@ -20,3 +21,41 @@ 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; + } +}