From f2e432821b7246aef441ed823a93491758aa181e Mon Sep 17 00:00:00 2001 From: alestiago Date: Fri, 16 Feb 2024 06:18:26 +0000 Subject: [PATCH] ci: add trashy_road workflow --- .github/workflows/trashy_road.yaml | 26 ++++++++++++ .../behaviors/player_moving_behavior.dart | 2 +- .../lib/src/game/entities/player/player.dart | 2 +- .../player_moving_behavior_test.dart | 41 +++++++++++++------ .../player_moving_keyboard_behavior_test.dart | 36 +++++++++++----- 5 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/trashy_road.yaml diff --git a/.github/workflows/trashy_road.yaml b/.github/workflows/trashy_road.yaml new file mode 100644 index 00000000..859fc372 --- /dev/null +++ b/.github/workflows/trashy_road.yaml @@ -0,0 +1,26 @@ +name: trashy_road + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + push: + paths: + - .github/workflows/trashy_road.yaml + - "packages/trashy_road/**" + branches: + - main + pull_request: + paths: + - .github/workflows/trashy_road.yaml + - "packages/trashy_road/**" + branches: + - main + +jobs: + build: + uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/flutter_package.yml@v1 + with: + coverage_excludes: "*.gen.dart" + working_directory: "packages/trashy_road" diff --git a/packages/trashy_road/lib/src/game/entities/player/behaviors/player_moving_behavior.dart b/packages/trashy_road/lib/src/game/entities/player/behaviors/player_moving_behavior.dart index b65a9fc9..a5ce3287 100644 --- a/packages/trashy_road/lib/src/game/entities/player/behaviors/player_moving_behavior.dart +++ b/packages/trashy_road/lib/src/game/entities/player/behaviors/player_moving_behavior.dart @@ -7,7 +7,7 @@ import 'package:trashy_road/src/game/game.dart'; enum Direction { up, down, left, right } /// A behavior that allows the player to move around the game. -class PlayerMovingBehavior extends Behavior +final class PlayerMovingBehavior extends Behavior with FlameBlocReader { /// The position the player is trying to move to. /// diff --git a/packages/trashy_road/lib/src/game/entities/player/player.dart b/packages/trashy_road/lib/src/game/entities/player/player.dart index 5998e240..a3a658cb 100644 --- a/packages/trashy_road/lib/src/game/entities/player/player.dart +++ b/packages/trashy_road/lib/src/game/entities/player/player.dart @@ -35,7 +35,7 @@ class Player extends PositionedEntity { ); @visibleForTesting - Player.test({super.behaviors}); + Player.empty(); /// Derives a [Player] from a [TiledObject]. /// diff --git a/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_behavior_test.dart b/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_behavior_test.dart index 8ba9611c..7ebdb981 100644 --- a/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_behavior_test.dart +++ b/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_behavior_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flame/game.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_test/flame_test.dart'; @@ -8,6 +10,17 @@ import 'package:trashy_road/src/game/game.dart'; class _MockTiledMap extends Mock implements TiledMap {} +class _MockObjectGroup extends Mock implements ObjectGroup {} + +class _MockTiledObject extends Mock implements TiledObject {} + +class _TestPlayer extends Player { + _TestPlayer() : super.empty(); + + @override + FutureOr onLoad() {} +} + class _TestGame extends FlameGame { _TestGame({ required GameBloc gameBloc, @@ -22,9 +35,7 @@ class _TestGame extends FlameGame { FlameBlocProvider( create: () => _gameBloc, children: [ - Player.test( - behaviors: [behavior], - ), + _TestPlayer()..add(behavior), ], ), ); @@ -39,9 +50,13 @@ void main() { late GameBloc gameBloc; setUp(() { - gameBloc = GameBloc( - map: _MockTiledMap(), - ); + final objectGroup = _MockObjectGroup(); + when(() => objectGroup.objects).thenReturn([_MockTiledObject()]); + + final map = _MockTiledMap(); + when(() => map.layerByName('TrashLayer')).thenReturn(objectGroup); + + gameBloc = GameBloc(map: map); game = _TestGame(gameBloc: gameBloc); }); @@ -58,11 +73,12 @@ void main() { final behavior = PlayerMovingBehavior(); await game.pump(behavior); - final previousVerticalPosition = behavior.parent.position.y; + final player = behavior.parent; + final previousVerticalPosition = player.position.y; + behavior.move(direction); - game.update(0); + game.update(1); - final player = behavior.parent; final currentVerticalPosition = player.position.y; expect( previousVerticalPosition, @@ -72,6 +88,7 @@ void main() { ); }, ); + testWithGame<_TestGame>( 'moves downwards with $Direction.down', () => game, @@ -82,7 +99,7 @@ void main() { final previousVerticalPosition = behavior.parent.position.y; behavior.move(direction); - game.update(0); + game.update(10); final player = behavior.parent; final currentVerticalPosition = player.position.y; @@ -104,7 +121,7 @@ void main() { final previousVerticalPosition = behavior.parent.position.x; behavior.move(direction); - game.update(0); + game.update(10); final player = behavior.parent; final currentVerticalPosition = player.position.x; @@ -126,7 +143,7 @@ void main() { final previousVerticalPosition = behavior.parent.position.x; behavior.move(direction); - game.update(0); + game.update(10); final player = behavior.parent; final currentVerticalPosition = player.position.x; diff --git a/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_keyboard_behavior_test.dart b/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_keyboard_behavior_test.dart index 4641f7c0..d5a53f1d 100644 --- a/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_keyboard_behavior_test.dart +++ b/packages/trashy_road/test/src/game/entities/player/behaviors/player_moving_keyboard_behavior_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flame/game.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_test/flame_test.dart'; @@ -9,12 +11,23 @@ import 'package:trashy_road/src/game/game.dart'; class _MockTiledMap extends Mock implements TiledMap {} +class _MockObjectGroup extends Mock implements ObjectGroup {} + +class _MockTiledObject extends Mock implements TiledObject {} + class _MockRawKeyEventData extends Mock implements RawKeyEventData { @override String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) => super.toString(); } +class _TestPlayer extends Player { + _TestPlayer() : super.empty(); + + @override + FutureOr onLoad() {} +} + class _TestGame extends FlameGame { _TestGame({ required GameBloc gameBloc, @@ -25,9 +38,7 @@ class _TestGame extends FlameGame { Future pump( PlayerKeyboardMovingBehavior behavior, ) async { - final player = Player.test( - behaviors: [PlayerMovingBehavior()], - ); + final player = _TestPlayer(); await ensureAdd( FlameBlocProvider( @@ -36,6 +47,7 @@ class _TestGame extends FlameGame { ), ); + await player.ensureAdd(PlayerMovingBehavior()); await player.ensureAdd(behavior); } } @@ -48,9 +60,13 @@ void main() { late GameBloc gameBloc; setUp(() { - gameBloc = GameBloc( - map: _MockTiledMap(), - ); + final objectGroup = _MockObjectGroup(); + when(() => objectGroup.objects).thenReturn([_MockTiledObject()]); + + final map = _MockTiledMap(); + when(() => map.layerByName('TrashLayer')).thenReturn(objectGroup); + + gameBloc = GameBloc(map: map); game = _TestGame(gameBloc: gameBloc); }); @@ -80,7 +96,7 @@ void main() { behavior.onKeyEvent(event, {upKey}); - game.update(0); + game.update(1); final player = behavior.parent; final currentVerticalPosition = player.position.y; @@ -108,7 +124,7 @@ void main() { behavior.onKeyEvent(event, {downKey}); - game.update(0); + game.update(1); final player = behavior.parent; final currentVerticalPosition = player.position.y; @@ -136,7 +152,7 @@ void main() { behavior.onKeyEvent(event, {leftKey}); - game.update(0); + game.update(1); final player = behavior.parent; final currentHorizontalPosition = player.position.x; @@ -164,7 +180,7 @@ void main() { behavior.onKeyEvent(event, {rightKey}); - game.update(0); + game.update(1); final player = behavior.parent; final currentHorizontalPosition = player.position.x;