test: coverage

pull/313/head
alestiago 3 years ago
parent 5ee0496afd
commit a70f9a9cc0

@ -35,9 +35,8 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
/// ///
/// This can be used for testing [Ball]'s behaviors in isolation. /// This can be used for testing [Ball]'s behaviors in isolation.
@visibleForTesting @visibleForTesting
Ball.test() Ball.test({required this.baseColor})
: baseColor = const Color(0xFFFFFFFF), : super(
super(
children: [_BallSpriteComponent()], children: [_BallSpriteComponent()],
); );

@ -15,10 +15,19 @@ void main() {
final flameTester = FlameTester(TestGame.new); final flameTester = FlameTester(TestGame.new);
group('Ball', () { group('Ball', () {
const baseColor = Color(0xFFFFFFFF);
test(
'can be instantiated',
() {
expect(Ball(baseColor: baseColor), isA<Ball>());
expect(Ball.test(baseColor: baseColor), isA<Ball>());
},
);
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ready(); await game.ready();
await game.ensureAdd(ball); await game.ensureAdd(ball);
@ -27,7 +36,7 @@ void main() {
); );
flameTester.test('add a BallScalingBehavior', (game) async { flameTester.test('add a BallScalingBehavior', (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
expect( expect(
ball.descendants().whereType<BallScalingBehavior>().length, ball.descendants().whereType<BallScalingBehavior>().length,
@ -39,7 +48,7 @@ void main() {
flameTester.test( flameTester.test(
'is dynamic', 'is dynamic',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
expect(ball.body.bodyType, equals(BodyType.dynamic)); expect(ball.body.bodyType, equals(BodyType.dynamic));
@ -48,7 +57,7 @@ void main() {
group('can be moved', () { group('can be moved', () {
flameTester.test('by its weight', (game) async { flameTester.test('by its weight', (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
game.update(1); game.update(1);
@ -56,7 +65,7 @@ void main() {
}); });
flameTester.test('by applying velocity', (game) async { flameTester.test('by applying velocity', (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
ball.body.gravityScale = Vector2.zero(); ball.body.gravityScale = Vector2.zero();
@ -71,7 +80,7 @@ void main() {
flameTester.test( flameTester.test(
'exists', 'exists',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
expect(ball.body.fixtures[0], isA<Fixture>()); expect(ball.body.fixtures[0], isA<Fixture>());
@ -81,7 +90,7 @@ void main() {
flameTester.test( flameTester.test(
'is dense', 'is dense',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
final fixture = ball.body.fixtures[0]; final fixture = ball.body.fixtures[0];
@ -92,7 +101,7 @@ void main() {
flameTester.test( flameTester.test(
'shape is circular', 'shape is circular',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
final fixture = ball.body.fixtures[0]; final fixture = ball.body.fixtures[0];
@ -104,7 +113,7 @@ void main() {
flameTester.test( flameTester.test(
'has Layer.all as default filter maskBits', 'has Layer.all as default filter maskBits',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ready(); await game.ready();
await game.ensureAdd(ball); await game.ensureAdd(ball);
await game.ready(); await game.ready();
@ -118,7 +127,7 @@ void main() {
group('stop', () { group('stop', () {
group("can't be moved", () { group("can't be moved", () {
flameTester.test('by its weight', (game) async { flameTester.test('by its weight', (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
ball.stop(); ball.stop();
@ -133,7 +142,7 @@ void main() {
flameTester.test( flameTester.test(
'by its weight when previously stopped', 'by its weight when previously stopped',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
ball.stop(); ball.stop();
ball.resume(); ball.resume();
@ -146,7 +155,7 @@ void main() {
flameTester.test( flameTester.test(
'by applying velocity when previously stopped', 'by applying velocity when previously stopped',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
ball.stop(); ball.stop();
ball.resume(); ball.resume();
@ -162,7 +171,7 @@ void main() {
group('boost', () { group('boost', () {
flameTester.test('applies an impulse to the ball', (game) async { flameTester.test('applies an impulse to the ball', (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
expect(ball.body.linearVelocity, equals(Vector2.zero())); expect(ball.body.linearVelocity, equals(Vector2.zero()));
@ -173,7 +182,7 @@ void main() {
}); });
flameTester.test('adds TurboChargeSpriteAnimation', (game) async { flameTester.test('adds TurboChargeSpriteAnimation', (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
await ball.boost(Vector2.all(10)); await ball.boost(Vector2.all(10));
@ -187,7 +196,7 @@ void main() {
flameTester.test('removes TurboChargeSpriteAnimation after it finishes', flameTester.test('removes TurboChargeSpriteAnimation after it finishes',
(game) async { (game) async {
final ball = Ball(baseColor: Colors.blue); final ball = Ball(baseColor: baseColor);
await game.ensureAdd(ball); await game.ensureAdd(ball);
await ball.boost(Vector2.all(10)); await ball.boost(Vector2.all(10));

@ -0,0 +1,100 @@
// ignore_for_file: cascade_invocations
import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/ball/behaviors/behaviors.dart';
import '../../../../helpers/helpers.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final asset = Assets.images.ball.ball.keyName;
final flameTester = FlameTester(() => TestGame([asset]));
group('BallScalingBehavior', () {
const baseColor = Color(0xFFFFFFFF);
test('can be instantiated', () {
expect(
BallScalingBehavior(),
isA<BallScalingBehavior>(),
);
});
flameTester.test('can be loaded', (game) async {
final ball = Ball.test(baseColor: baseColor);
final behavior = BallScalingBehavior();
await ball.add(behavior);
await game.ensureAdd(ball);
expect(
ball.firstChild<BallScalingBehavior>(),
equals(behavior),
);
});
flameTester.test('can be loaded', (game) async {
final ball = Ball.test(baseColor: baseColor);
final behavior = BallScalingBehavior();
await ball.add(behavior);
await game.ensureAdd(ball);
expect(
ball.firstChild<BallScalingBehavior>(),
equals(behavior),
);
});
flameTester.test('scales the shape radius', (game) async {
final ball1 = Ball.test(baseColor: baseColor)
..initialPosition = Vector2(0, 10);
await ball1.add(BallScalingBehavior());
final ball2 = Ball.test(baseColor: baseColor)
..initialPosition = Vector2(0, -10);
await ball2.add(BallScalingBehavior());
await game.ensureAddAll([ball1, ball2]);
game.update(1);
final shape1 = ball1.body.fixtures.first.shape;
final shape2 = ball2.body.fixtures.first.shape;
expect(
shape1.radius,
greaterThan(shape2.radius),
);
});
flameTester.testGameWidget(
'scales the sprite',
setUp: (game, tester) async {
final ball1 = Ball.test(baseColor: baseColor)
..initialPosition = Vector2(0, 10);
await ball1.add(BallScalingBehavior());
final ball2 = Ball.test(baseColor: baseColor)
..initialPosition = Vector2(0, -10);
await ball2.add(BallScalingBehavior());
await game.ensureAddAll([ball1, ball2]);
game.update(1);
await tester.pump();
await game.ready();
final sprite1 = ball1.firstChild<SpriteComponent>()!;
final sprite2 = ball2.firstChild<SpriteComponent>()!;
expect(
sprite1.scale.x,
greaterThan(sprite2.scale.x),
);
expect(
sprite1.scale.y,
greaterThan(sprite2.scale.y),
);
},
);
});
}
Loading…
Cancel
Save