Skip to content

Commit

Permalink
Add test for list of nullables
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Aug 29, 2022
1 parent 9ab6a40 commit 9fd65e1
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 1 deletion.
67 changes: 66 additions & 1 deletion test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,70 @@ Future<void> main([List<String>? args]) async {
expect(result, [person]);
});

test('', () {});
test('List of nullables', () {
final config = Configuration.local([Player.schema, Game.schema]);
final realm = getRealm(config);

final game = Game();
final alice = Player('alice', game: game);
final bob = Player('bob', game: game);
final carol = Player('carol', game: game);
final players = [alice, bob, carol];

realm.write(() => realm.addAll(players));

void checkResult(List<Player> winnerByRound, Map<Player, List<int?>> scoresByPlayer) {
expect(game.winnerByRound, winnerByRound);
for (final p in players) {
expect(p.scoresByRound, scoresByPlayer[p] ?? []);
}
}

checkResult([], {});

int currentRound = 0;
void playRound(Map<Player, int> scores) {
realm.write(() {
for (final p in players) {
p.scoresByRound.add(scores[p]);
}
final bestResult =
scores.entries.fold<MapEntry<Player, int>?>(null, (bestResult, result) => result.value > (bestResult?.value ?? 0) ? result : bestResult);
game.winnerByRound[currentRound++] = bestResult!.key;
});
}

playRound({alice: 1, bob: 2});

checkResult([
bob
], {
alice: [1],
bob: [2],
carol: [null]
});

playRound({alice: 3, carol: 1});

checkResult([
bob,
alice
], {
alice: [1, 3],
bob: [2, null],
carol: [null, 1]
});

playRound({alice: 2, bob: 3, carol: 1});

checkResult([
bob,
alice,
bob
], {
alice: [1, 3, 2],
bob: [2, null, 3],
carol: [null, 1, 1]
});
});
}
14 changes: 14 additions & 0 deletions test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ class _Friend {
final friends = <_Friend>[];
}

@RealmModel()
class _Player {
@PrimaryKey()
late String name;
_Game? game;
final scoresByRound = <int?>[]; // null means player didn't finish
}

@RealmModel()
class _Game {
final winnerByRound = <_Player>[]; // null means no winner yet
int get rounds => winnerByRound.length;
}

String? testName;
Map<String, String?> arguments = {};
final baasApps = <String, BaasApp>{};
Expand Down
88 changes: 88 additions & 0 deletions test/test.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9fd65e1

Please sign in to comment.