testGame<T extends Game<Event>> function

  1. @isTest
void testGame<T extends Game<Event>>(
  1. String testName, {
  2. required GameConfig config,
  3. required List<Player> players,
  4. required Future<void> test(
    1. GameTester<T>
    ),
})

Tests the game with T type in a group with name and test named name + '_Tests'

All parameters are required

  • config specifies the GameConfig
  • players specifies the list of players
  • test gives you access to a GameTester which allows you to test the game

Uses the OnDevice clients TODO: Maybe try to use only Backend Providers as long as nothing need to be async (which it shouldn't since the game method are all non-async)?

Implementation

@isTest
void testGame<T extends Game>(
  String testName, {
  required GameConfig config,
  required List<Player> players,
  required Future<void> Function(GameTester<T>) test,
}) {
  darttest.test(testName, () async {
    final root = ProviderContainer();
    final readers = <PlayerID, Reader>{};

    root.read(GameProviders.clientType.notifier).state = OnDeviceClient;

    for (final p in players) {
      readers[p.id] = ProviderContainer(
          parent: root,
          overrides: [GameProviders.playerID.overrideWithValue(p.id)]).read;
    }
    readers[players.first.id]!(GameProviders.config.notifier).state = config;
    final code =
        await readers[players.first.id]!(GameProviders.createGame.future);
    for (final p in players) {
      readers[p.id]!(GameProviders.code.notifier).state = code;
      await readers[p.id]!(GameProviders.joinGame.future);
    }

    if (readers[players.first.id]!(GameProviders.status) !=
        GameStatus.Started) {
      await readers[players.first.id]!(GameProviders.startGame.future);
    }

    await test(GameTester<T>(root.read, readers, players, code));
  });
}