Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve tests #1609

Merged
merged 9 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/flame/lib/src/components/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ class _LifecycleManager {
void _processChildrenQueue() {
while (_children.isNotEmpty) {
final child = _children.first;
assert(child.parent!.isMounted);
assert(child._parent!.isMounted);
if (child.isLoaded) {
child._mount();
_children.removeFirst();
Expand Down
22 changes: 11 additions & 11 deletions packages/flame/test/assets/images_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockImage extends Mock implements Image {
int disposedCount = 0;

@override
void dispose() {
disposedCount++;
}
}

void main() {
// A simple 1x1 pixel encoded as base64 - just so that we have something to
// load into the Images cache.
Expand Down Expand Up @@ -54,7 +45,7 @@ void main() {

test('clear', () {
final cache = Images();
final image = MockImage();
final image = _MockImage();
cache.add('test', image);
expect(image.disposedCount, 0);
cache.clear('test');
Expand All @@ -63,7 +54,7 @@ void main() {

test('clearCache', () {
final cache = Images();
final images = List.generate(10, (_) => MockImage());
final images = List.generate(10, (_) => _MockImage());
for (var i = 0; i < images.length; i++) {
cache.add(i.toString(), images[i]);
}
Expand Down Expand Up @@ -106,3 +97,12 @@ void main() {
});
});
}

class _MockImage extends Mock implements Image {
int disposedCount = 0;

@override
void dispose() {
disposedCount++;
}
}
6 changes: 4 additions & 2 deletions packages/flame/test/cache/memory_cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import 'package:test/test.dart';

void main() {
group('MemoryCache', () {
test('basic cache addition', () {
test('basic cache access', () {
final cache = MemoryCache<int, String>();
cache.setValue(0, 'bla');
expect(cache.getValue(0), 'bla');
});

test('contains key', () {
test('containsKey', () {
final cache = MemoryCache<int, String>();
expect(cache.keys.length, 0);
cache.setValue(0, 'bla');
expect(cache.containsKey(0), true);
expect(cache.containsKey(1), false);
expect(cache.keys.length, 1);
});

test('cache size', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class _TestBlock extends PositionComponent with CollisionCallbacks {

void main() {
group('Benchmark collision detection', () {
withCollidables.test('collidable callbacks are called', (game) async {
testCollidableGame('collidable callbacks are called', (game) async {
final rng = Random(0);
final blocks = List.generate(
100,
Expand Down
20 changes: 10 additions & 10 deletions packages/flame/test/collisions/collision_callback_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'collision_test_helpers.dart';

void main() {
group('Collision callbacks', () {
withCollidables.test('collidable callbacks are called', (game) async {
testCollidableGame('collidable callbacks are called', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand Down Expand Up @@ -48,7 +48,7 @@ void main() {
expect(blockB.endCounter, 1);
});

withCollidables.test(
testCollidableGame(
'collidable callbacks are called when removing a Collidable',
(game) async {
final blockA = TestBlock(
Expand Down Expand Up @@ -76,7 +76,7 @@ void main() {
},
);

withCollidables.test('hitbox callbacks are called', (game) async {
testCollidableGame('hitbox callbacks are called', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand Down Expand Up @@ -115,7 +115,7 @@ void main() {
});
});

withCollidables.test(
testCollidableGame(
'hitbox callbacks are called when Collidable is removed',
(game) async {
final blockA = TestBlock(
Expand Down Expand Up @@ -145,7 +145,7 @@ void main() {
},
);

withCollidables.test(
testCollidableGame(
'hitbox end callbacks are called when hitbox is moved away fast',
(game) async {
final blockA = TestBlock(
Expand Down Expand Up @@ -175,7 +175,7 @@ void main() {
},
);

withCollidables.test(
testCollidableGame(
'onCollisionEnd is only called when there previously was a collision',
(game) async {
final blockA = TestBlock(
Expand All @@ -199,7 +199,7 @@ void main() {
},
);

withCollidables.test(
testCollidableGame(
'callbacks are only called once for hitboxes on each other',
(game) async {
final blockA = TestBlock(
Expand Down Expand Up @@ -257,7 +257,7 @@ void main() {
},
);

withCollidables.test(
testCollidableGame(
'end and start callbacks are only called once for hitboxes sharing a side',
(game) async {
final blockA = TestBlock(
Expand Down Expand Up @@ -316,7 +316,7 @@ void main() {
);

// Reproduced #1478
withCollidables.test(
testCollidableGame(
'collision callbacks with many hitboxes added',
(game) async {
const side = 10.0;
Expand Down Expand Up @@ -379,7 +379,7 @@ void main() {
);

// Reproduced #1478
withCollidables.test(
testCollidableGame(
'collision callbacks with changed game size',
(game) async {
final block = TestBlock(Vector2.all(20), Vector2.all(10))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Passthrough extends TestBlock with CollisionPassthrough {

void main() {
group('CollisionPassthrough', () {
withCollidables.test('Passing collisions to parent', (game) async {
testCollidableGame('Passing collisions to parent', (game) async {
final passthrough = Passthrough();
final hitboxParent =
TestBlock(Vector2.zero(), Vector2.all(10), addTestHitbox: false)
Expand Down
9 changes: 8 additions & 1 deletion packages/flame/test/collisions/collision_test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame_test/flame_test.dart';
import 'package:meta/meta.dart';

class HasCollidablesGame extends FlameGame with HasCollisionDetection {}

final withCollidables = FlameTester(() => HasCollidablesGame());
@isTest
Future<void> testCollidableGame(
String testName,
Future Function(HasCollidablesGame) testBody,
) {
return testWithGame(testName, () => HasCollidablesGame(), testBody);
}

class TestHitbox extends RectangleHitbox {
int startCounter = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import 'package:test/test.dart';
import 'collision_test_helpers.dart';

void main() {
group('Varying CollisionType', () {
withCollidables.test('actives do collide', (game) async {
group('CollisionType', () {
testCollidableGame('actives do collide', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -27,7 +27,7 @@ void main() {
expect(blockB.activeCollisions.length, 1);
});

withCollidables.test('passives do not collide', (game) async {
testCollidableGame('passives do not collide', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -44,7 +44,7 @@ void main() {
expect(blockB.activeCollisions.isEmpty, true);
});

withCollidables.test('inactives do not collide', (game) async {
testCollidableGame('inactives do not collide', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -61,7 +61,7 @@ void main() {
expect(blockB.activeCollisions.isEmpty, true);
});

withCollidables.test('active collides with static', (game) async {
testCollidableGame('active collides with static', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -79,7 +79,7 @@ void main() {
expect(blockB.activeCollisions.length, 1);
});

withCollidables.test('passive collides with active', (game) async {
testCollidableGame('passive collides with active', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -97,8 +97,7 @@ void main() {
expect(blockB.activeCollisions.length, 1);
});

withCollidables.test('passive does not collide with inactive',
(game) async {
testCollidableGame('passive does not collide with inactive', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -115,7 +114,7 @@ void main() {
expect(blockB.activeCollisions.length, 0);
});

withCollidables.test('inactive does not collide with static', (game) async {
testCollidableGame('inactive does not collide with static', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -132,7 +131,7 @@ void main() {
expect(blockB.activeCollisions.length, 0);
});

withCollidables.test('active does not collide with inactive', (game) async {
testCollidableGame('active does not collide with inactive', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -148,7 +147,7 @@ void main() {
expect(blockB.activeCollisions.length, 0);
});

withCollidables.test('inactive does not collide with active', (game) async {
testCollidableGame('inactive does not collide with active', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -164,7 +163,7 @@ void main() {
expect(blockB.activeCollisions.length, 0);
});

withCollidables.test(
testCollidableGame(
'correct collisions with many involved collidables',
(game) async {
final rng = Random(0);
Expand Down Expand Up @@ -193,7 +192,7 @@ void main() {
},
);

withCollidables.test('detects collision after scale', (game) async {
testCollidableGame('detects collision after scale', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -218,7 +217,7 @@ void main() {
expect(blockB.activeCollisions.length, 1);
});

withCollidables.test('detects collision after flip', (game) async {
testCollidableGame('detects collision after flip', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -243,7 +242,7 @@ void main() {
expect(blockB.activeCollisions.length, 1);
});

withCollidables.test('detects collision after scale', (game) async {
testCollidableGame('detects collision after scale', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -268,7 +267,7 @@ void main() {
expect(blockB.activeCollisions.length, 1);
});

withCollidables.test('testPoint detects point after flip', (game) async {
testCollidableGame('testPoint detects point after flip', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -281,7 +280,7 @@ void main() {
expect(blockA.containsPoint(Vector2(-1, 1)), true);
});

withCollidables.test('testPoint detects point after scale', (game) async {
testCollidableGame('testPoint detects point after scale', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand All @@ -294,7 +293,7 @@ void main() {
expect(blockA.containsPoint(Vector2.all(11)), true);
});

withCollidables.test('detects collision on child components', (game) async {
testCollidableGame('detects collision on child components', (game) async {
final blockA = TestBlock(
Vector2.zero(),
Vector2.all(10),
Expand Down
Loading