Skip to content

Commit

Permalink
fix: 🐛 unit test for Forge2dGame
Browse files Browse the repository at this point in the history
  - Working on unit test
  • Loading branch information
shwetachauhan-simform committed Oct 12, 2022
1 parent 202d809 commit 601cca7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
69 changes: 67 additions & 2 deletions packages/flame_forge2d/test/body_component_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'dart:ui';

import 'package:flame/extensions.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import 'helpers/mocks.dart';

class _TestBodyComponent extends BodyComponent {
@override
Body createBody() => body;
Expand Down Expand Up @@ -52,6 +53,9 @@ void main() {
await game.add(component);

game.camera.followVector2(Vector2.zero());

// a CircleShape contains point
expect(component.containsPoint(Vector2.all(1.5)), isTrue);
},
verify: (game, tester) async {
await expectLater(
Expand Down Expand Up @@ -107,6 +111,9 @@ void main() {
await game.add(component);

game.camera.followVector2(Vector2.zero());

// a PolygonShape contains point
expect(component.containsPoint(Vector2.all(10)), isTrue);
},
verify: (game, tester) async {
await expectLater(
Expand Down Expand Up @@ -258,5 +265,63 @@ void main() {
});
});
});

group('Add component to parent', () {
final flameTester = FlameTester(Forge2DGame.new);
final testPaint = Paint()..color = const Color(0xffff0000);

flameTester.testGameWidget(
'add and remove child to BodyComponent',
setUp: (game, tester) async {
final worldCenter =
game.screenToWorld(game.size * game.camera.zoom / 2);

final bodyDef = BodyDef(position: worldCenter.clone());
final body = game.world.createBody(bodyDef);
final shape = PolygonShape()
..set(
[
Vector2.zero(),
Vector2.all(10),
Vector2(0, 10),
],
);
body.createFixture(FixtureDef(shape));

final component = _TestBodyComponent()
..body = body
..paint = testPaint;

component.addToParent(game);
await game.ready();

expect(game.contains(component), true);
expect(component.isMounted, true);
expect(game.children.length, 1);
component.removeFromParent();
await game.ready();

expect(component.isMounted, false);
expect(component.isLoaded, true);
expect(game.children.length, 0);
},
);
});

group('BodyComponent contact events', () {
test('beginContact called', () {
final contactCallback = MockContactCallback();
final contact = MockContact();
final bodyA = MockBody()..angularDamping = 1.0;
final fixtureA = MockFixture();
when(() => bodyA.userData).thenReturn(contactCallback);
when(() => fixtureA.userData).thenReturn(Object());
contactCallback.beginContact(fixtureA.userData!, contact);

verify(
() => contactCallback.beginContact(fixtureA.userData!, contact),
).called(1);
});
});
});
}
38 changes: 38 additions & 0 deletions packages/flame_forge2d/test/forge2d_game_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:test/test.dart';

class _TestForge2dGame extends Forge2DGame {
_TestForge2dGame() : super(zoom: 4.0, gravity: Vector2(0, -10.0));
}

void main() {
group(
'Test corresponding position on screen and in the Forge2D world',
() {
test('Zero positioned camera should be zero in world', () {
expect(
_TestForge2dGame().screenToWorld(Vector2.zero()),
Vector2.zero(),
);
});

test('Zero positioned camera should be zero in FlameWorld', () {
expect(
_TestForge2dGame().screenToFlameWorld(Vector2.zero()),
Vector2.zero(),
);
});
},
);

group(
'Test input vector does not get modified while function call',
() {
test('Camera should not modify the input vector while projecting it', () {
final vec = Vector2(5, 6);
_TestForge2dGame().camera.projectVector(vec);
expect(vec, Vector2(5, 6));
});
},
);
}
4 changes: 4 additions & 0 deletions packages/flame_forge2d/test/helpers/mocks.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui';

import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:mocktail/mocktail.dart';

Expand All @@ -12,3 +14,5 @@ class MockFixture extends Mock implements Fixture {}
class MockManifold extends Mock implements Manifold {}

class MockContactImpulse extends Mock implements ContactImpulse {}

class MockCanvas extends Mock implements Canvas {}

0 comments on commit 601cca7

Please sign in to comment.