Added Unit tests for data classes
This commit is contained in:
@@ -52,8 +52,8 @@
|
|||||||
</TestAction>
|
</TestAction>
|
||||||
<LaunchAction
|
<LaunchAction
|
||||||
buildConfiguration = "Release"
|
buildConfiguration = "Release"
|
||||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
selectedDebuggerIdentifier = ""
|
||||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
|
||||||
launchStyle = "0"
|
launchStyle = "0"
|
||||||
useCustomWorkingDirectory = "NO"
|
useCustomWorkingDirectory = "NO"
|
||||||
ignoresPersistentStateOnLaunch = "NO"
|
ignoresPersistentStateOnLaunch = "NO"
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:cabo_counter/data/round.dart';
|
import 'package:cabo_counter/data/round.dart';
|
||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
|
||||||
/// This class represents a game session for Cabo game.
|
/// This class represents a game session for Cabo game.
|
||||||
/// [createdAt] is the timestamp of when the game session was created.
|
/// [createdAt] is the timestamp of when the game session was created.
|
||||||
@@ -139,6 +140,11 @@ class GameSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The _getLowestScoreIndex method but forwarded for testing purposes.
|
||||||
|
@visibleForTesting
|
||||||
|
List<int> testingGetLowestScoreIndex(List<int> roundScores) =>
|
||||||
|
_getLowestScoreIndex(roundScores);
|
||||||
|
|
||||||
/// Returns the index of the player with the lowest score. If there are
|
/// Returns the index of the player with the lowest score. If there are
|
||||||
/// multiple players with the same lowest score, all of them are returned.
|
/// multiple players with the same lowest score, all of them are returned.
|
||||||
/// [roundScores] is a list of the scores of all players in the current round.
|
/// [roundScores] is a list of the scores of all players in the current round.
|
||||||
@@ -157,6 +163,12 @@ class GameSession {
|
|||||||
return lowestScoreIndex;
|
return lowestScoreIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
void testingAssignPoints(int roundNum, List<int> roundScores,
|
||||||
|
int caboPlayerIndex, List<int> winnerIndex, [int? loserIndex]) =>
|
||||||
|
_assignPoints(
|
||||||
|
roundNum, roundScores, caboPlayerIndex, winnerIndex, loserIndex);
|
||||||
|
|
||||||
/// Assigns points to the players based on the scores of the current round.
|
/// Assigns points to the players based on the scores of the current round.
|
||||||
/// [roundNum] is the number of the current round.
|
/// [roundNum] is the number of the current round.
|
||||||
/// [roundScores] is the raw list of the scores of all players in the current round.
|
/// [roundScores] is the raw list of the scores of all players in the current round.
|
||||||
@@ -238,6 +250,9 @@ class GameSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@visibleForTesting
|
||||||
|
void testingSumPoints() => _sumPoints();
|
||||||
|
|
||||||
/// Sums up the points of all players and stores the result in the
|
/// Sums up the points of all players and stores the result in the
|
||||||
/// playerScores list.
|
/// playerScores list.
|
||||||
void _sumPoints() {
|
void _sumPoints() {
|
||||||
|
|||||||
164
test/data/game_session_test.dart
Normal file
164
test/data/game_session_test.dart
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
import 'package:cabo_counter/data/game_session.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late GameSession session;
|
||||||
|
final testPlayers = ['Alice', 'Bob', 'Charlie'];
|
||||||
|
final testDate = DateTime(2023, 1, 1);
|
||||||
|
const testTitle = 'Test Game';
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
session = GameSession(
|
||||||
|
createdAt: testDate,
|
||||||
|
gameTitle: testTitle,
|
||||||
|
players: testPlayers,
|
||||||
|
pointLimit: 100,
|
||||||
|
caboPenalty: 5,
|
||||||
|
isPointsLimitEnabled: true,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('Initialization & JSON', () {
|
||||||
|
test('Initialization', () {
|
||||||
|
expect(session.gameTitle, testTitle);
|
||||||
|
expect(session.players, testPlayers);
|
||||||
|
expect(session.playerScores, [0, 0, 0]);
|
||||||
|
expect(session.roundNumber, 1);
|
||||||
|
expect(session.isGameFinished, isFalse);
|
||||||
|
expect(session.winner, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('toJson and fromJson', () {
|
||||||
|
// Add some rounds to test serialization
|
||||||
|
session.addRoundScoresToList(1, [10, 20, 30], [10, 20, 30], 0);
|
||||||
|
session.addRoundScoresToList(2, [15, 25, 35], [5, 5, 5], 1);
|
||||||
|
|
||||||
|
final json = session.toJson();
|
||||||
|
final fromJsonSession = GameSession.fromJson(json);
|
||||||
|
|
||||||
|
expect(fromJsonSession.gameTitle, testTitle);
|
||||||
|
expect(fromJsonSession.players, testPlayers);
|
||||||
|
expect(fromJsonSession.roundList.length, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('null values in JSON', () {
|
||||||
|
expect(
|
||||||
|
() => GameSession.fromJson({
|
||||||
|
'createdAt': testDate.toIso8601String(),
|
||||||
|
'gameTitle': null, // Invalid
|
||||||
|
'players': testPlayers,
|
||||||
|
'pointLimit': 100,
|
||||||
|
'caboPenalty': 50,
|
||||||
|
'isPointsLimitEnabled': true,
|
||||||
|
'isGameFinished': false,
|
||||||
|
'winner': '',
|
||||||
|
'roundNumber': 1,
|
||||||
|
'playerScores': [0, 0, 0],
|
||||||
|
'roundList': [],
|
||||||
|
}),
|
||||||
|
throwsA(isA<TypeError>()));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('Helper Functions', () {
|
||||||
|
test('getLengthOfPlayerNames', () {
|
||||||
|
expect(session.getLengthOfPlayerNames(),
|
||||||
|
equals(15)); // Alice(5) + Bob(3) + Charlie(7)
|
||||||
|
});
|
||||||
|
|
||||||
|
test('increaseRound', () {
|
||||||
|
expect(session.roundNumber, 1);
|
||||||
|
session.increaseRound();
|
||||||
|
expect(session.roundNumber, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getLowestScoreIndex', () {
|
||||||
|
List<int> lowestScoreIndex;
|
||||||
|
|
||||||
|
lowestScoreIndex = session.testingGetLowestScoreIndex([5, 10, 15]);
|
||||||
|
expect(lowestScoreIndex, [0]);
|
||||||
|
|
||||||
|
lowestScoreIndex = session.testingGetLowestScoreIndex([5, 5, 15]);
|
||||||
|
expect(lowestScoreIndex, [0, 1]);
|
||||||
|
|
||||||
|
lowestScoreIndex = session.testingGetLowestScoreIndex([5, 5, 5]);
|
||||||
|
expect(lowestScoreIndex, [0, 1, 2]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('Game Functions', () {
|
||||||
|
test('applyKamikaze', () {
|
||||||
|
session.applyKamikaze(1, 0); // Alice has kamikaze
|
||||||
|
expect(session.roundList[0].scoreUpdates, [0, 50, 50]);
|
||||||
|
expect(session.roundList[0].scores, [0, 0, 0]);
|
||||||
|
expect(session.roundList[0].kamikazePlayerIndex, 0);
|
||||||
|
expect(session.roundList[0].caboPlayerIndex, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('calculateScoredPoints - CABO player has lowest', () {
|
||||||
|
session.calculateScoredPoints(1, [3, 5, 8], 0); // Alice has lowest
|
||||||
|
expect(session.roundList[0].scoreUpdates, equals([0, 5, 8]));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('calculateScoredPoints - CABO player not lowest', () {
|
||||||
|
session.calculateScoredPoints(1, [5, 3, 8], 0); // Bob has lowest
|
||||||
|
expect(session.roundList[0].scoreUpdates, [10, 0, 8]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('addRoundScoresToList', () {
|
||||||
|
session.addRoundScoresToList(1, [3, 5, 8], [0, 5, 8], 0);
|
||||||
|
expect(session.roundList.length, 1);
|
||||||
|
expect(session.roundList[0].roundNum, 1);
|
||||||
|
expect(session.roundList[0].scoreUpdates, [0, 5, 8]);
|
||||||
|
expect(session.roundList[0].scores, [3, 5, 8]);
|
||||||
|
expect(session.roundList[0].kamikazePlayerIndex, isNull);
|
||||||
|
expect(session.roundList[0].caboPlayerIndex, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('updatePoints - game not finished', () async {
|
||||||
|
session.addRoundScoresToList(1, [10, 20, 30], [10, 20, 30], 0);
|
||||||
|
await session.updatePoints();
|
||||||
|
expect(session.isGameFinished, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('updatePoints - game finished', () async {
|
||||||
|
session.addRoundScoresToList(1, [101, 20, 30], [101, 20, 30], 0);
|
||||||
|
await session.updatePoints();
|
||||||
|
expect(session.isGameFinished, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('_assignPoints', () {
|
||||||
|
// Alice said Cabo and has the lowest score
|
||||||
|
session.testingAssignPoints(1, [5, 10, 15], 0, [0]);
|
||||||
|
expect(session.roundList[0].scoreUpdates, [0, 10, 15]);
|
||||||
|
|
||||||
|
// Alice said Cabo and has not the lowest score
|
||||||
|
session.testingAssignPoints(1, [5, 10, 15], 0, [1], 0);
|
||||||
|
expect(session.roundList[0].scoreUpdates, [10, 0, 15]);
|
||||||
|
|
||||||
|
// Bob and Charlie have the lowest score, Alice said Cabo
|
||||||
|
session.testingAssignPoints(1, [15, 5, 5], 0, [1, 2], 0);
|
||||||
|
expect(session.roundList[0].scoreUpdates, [20, 0, 0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('_sumPoints', () async {
|
||||||
|
session.addRoundScoresToList(1, [10, 20, 30], [10, 20, 30], 0);
|
||||||
|
session.addRoundScoresToList(2, [5, 5, 5], [5, 5, 5], 1);
|
||||||
|
session.testingSumPoints();
|
||||||
|
expect(session.playerScores, [15, 25, 35]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('_checkHundredPointsReached via updatePoints', () {
|
||||||
|
session.addRoundScoresToList(1, [50, 5, 15], [50, 0, 15], 1);
|
||||||
|
session.addRoundScoresToList(2, [50, 5, 15], [50, 0, 15], 1);
|
||||||
|
session.updatePoints();
|
||||||
|
expect(session.playerScores, equals([50, 0, 30]));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('_setWinner via updatePoints', () async {
|
||||||
|
session.addRoundScoresToList(1, [101, 20, 30], [101, 0, 30], 1);
|
||||||
|
await session.updatePoints();
|
||||||
|
expect(session.winner, 'Bob'); // Bob has lowest score (20)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
117
test/data/round_test.dart
Normal file
117
test/data/round_test.dart
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
import 'package:cabo_counter/data/round.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Round round;
|
||||||
|
const testRoundNum = 1;
|
||||||
|
const testCaboPlayerIndex = 0;
|
||||||
|
const testKamikazePlayerIndex = 1;
|
||||||
|
const testScores = [10, 20, 30];
|
||||||
|
const testScoreUpdates = [5, 15, 25];
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
round = Round(
|
||||||
|
roundNum: testRoundNum,
|
||||||
|
caboPlayerIndex: testCaboPlayerIndex,
|
||||||
|
kamikazePlayerIndex: testKamikazePlayerIndex,
|
||||||
|
scores: testScores,
|
||||||
|
scoreUpdates: testScoreUpdates,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('Constructor Tests', () {
|
||||||
|
test('Constructor sets correct values', () {
|
||||||
|
expect(round.roundNum, testRoundNum);
|
||||||
|
expect(round.caboPlayerIndex, testCaboPlayerIndex);
|
||||||
|
expect(round.kamikazePlayerIndex, testKamikazePlayerIndex);
|
||||||
|
expect(round.scores, testScores);
|
||||||
|
expect(round.scoreUpdates, testScoreUpdates);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Constructor with null kamikazePlayerIndex', () {
|
||||||
|
final roundWithoutKamikaze = Round(
|
||||||
|
roundNum: testRoundNum,
|
||||||
|
caboPlayerIndex: testCaboPlayerIndex,
|
||||||
|
kamikazePlayerIndex: null,
|
||||||
|
scores: testScores,
|
||||||
|
scoreUpdates: testScoreUpdates,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(roundWithoutKamikaze.kamikazePlayerIndex, isNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('JSON Methods', () {
|
||||||
|
test('toJson() returns correct map', () {
|
||||||
|
final jsonMap = round.toJson();
|
||||||
|
|
||||||
|
expect(jsonMap['roundNum'], equals(testRoundNum));
|
||||||
|
expect(jsonMap['caboPlayerIndex'], equals(testCaboPlayerIndex));
|
||||||
|
expect(jsonMap['kamikazePlayerIndex'], equals(testKamikazePlayerIndex));
|
||||||
|
expect(jsonMap['scores'], equals(testScores));
|
||||||
|
expect(jsonMap['scoreUpdates'], equals(testScoreUpdates));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('fromJson() creates correct Round object', () {
|
||||||
|
final jsonMap = {
|
||||||
|
'roundNum': testRoundNum,
|
||||||
|
'caboPlayerIndex': testCaboPlayerIndex,
|
||||||
|
'kamikazePlayerIndex': testKamikazePlayerIndex,
|
||||||
|
'scores': testScores,
|
||||||
|
'scoreUpdates': testScoreUpdates,
|
||||||
|
};
|
||||||
|
|
||||||
|
final fromJsonRound = Round.fromJson(jsonMap);
|
||||||
|
|
||||||
|
expect(fromJsonRound.roundNum, testRoundNum);
|
||||||
|
expect(fromJsonRound.caboPlayerIndex, testCaboPlayerIndex);
|
||||||
|
expect(fromJsonRound.kamikazePlayerIndex, testKamikazePlayerIndex);
|
||||||
|
expect(fromJsonRound.scores, testScores);
|
||||||
|
expect(fromJsonRound.scoreUpdates, testScoreUpdates);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('fromJson() with null kamikazePlayerIndex', () {
|
||||||
|
final jsonMap = {
|
||||||
|
'roundNum': testRoundNum,
|
||||||
|
'caboPlayerIndex': testCaboPlayerIndex,
|
||||||
|
'kamikazePlayerIndex': null,
|
||||||
|
'scores': testScores,
|
||||||
|
'scoreUpdates': testScoreUpdates,
|
||||||
|
};
|
||||||
|
|
||||||
|
final fromJsonRound = Round.fromJson(jsonMap);
|
||||||
|
|
||||||
|
expect(fromJsonRound.kamikazePlayerIndex, isNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('toString()', () {
|
||||||
|
test('toString() returns correct string representation', () {
|
||||||
|
final expectedString = 'Round $testRoundNum, '
|
||||||
|
'caboPlayerIndex: $testCaboPlayerIndex, '
|
||||||
|
'kamikazePlayerIndex: $testKamikazePlayerIndex, '
|
||||||
|
'scores: $testScores, '
|
||||||
|
'scoreUpdates: $testScoreUpdates, ';
|
||||||
|
|
||||||
|
expect(round.toString(), equals(expectedString));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('toString() with null kamikazePlayerIndex', () {
|
||||||
|
final roundWithoutKamikaze = Round(
|
||||||
|
roundNum: testRoundNum,
|
||||||
|
caboPlayerIndex: testCaboPlayerIndex,
|
||||||
|
kamikazePlayerIndex: null,
|
||||||
|
scores: testScores,
|
||||||
|
scoreUpdates: testScoreUpdates,
|
||||||
|
);
|
||||||
|
|
||||||
|
final expectedString = 'Round $testRoundNum, '
|
||||||
|
'caboPlayerIndex: $testCaboPlayerIndex, '
|
||||||
|
'kamikazePlayerIndex: null, '
|
||||||
|
'scores: $testScores, '
|
||||||
|
'scoreUpdates: $testScoreUpdates, ';
|
||||||
|
|
||||||
|
expect(roundWithoutKamikaze.toString(), expectedString);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user