feat: included board tests

pull/47/head
alestiago 4 years ago
parent 4621f871bb
commit bafa686a28

@ -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<Flipper>(
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<Flipper>(
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<Baseboard>();
expect(leftFlippers.length, equals(2));
},
);
});
});
}

@ -1,3 +1,4 @@
import 'package:flame/components.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_theme/pinball_theme.dart'; import 'package:pinball_theme/pinball_theme.dart';
@ -20,3 +21,41 @@ extension DebugPinballGameTest on DebugPinballGame {
), ),
); );
} }
extension ComponentX on Component {
T findNestedChild<T extends Component>({
bool Function(T)? condition,
}) {
T? nestedChild;
propagateToChildren<T>((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<T> findNestedChildren<T extends Component>({
bool Function(T)? condition,
}) {
final nestedChildren = <T>[];
propagateToChildren<T>((child) {
final foundChild = (condition ?? (_) => true)(child);
if (foundChild) {
nestedChildren.add(child);
}
return true;
});
return nestedChildren;
}
}

Loading…
Cancel
Save