Merge pull request #216 from Excello-Recherche-Education/unit-test-minigame-history

Add Unit tests for UserMinigameHistory
This commit is contained in:
Adrien Ufferte
2026-02-03 16:07:54 +01:00
committed by GitHub
4 changed files with 107 additions and 6 deletions
+3 -5
View File
@@ -6,7 +6,7 @@ const MAX_DIFFICULTY: int = 4
const CONSECUTIVE_WINS_TO_PROMOTE: int = 2
const CONSECUTIVE_LOSSES_TO_DEMOTE: int = 2
@export var difficulty: int = 0
@export var difficulty: int = MIN_DIFFICULTY
@export var consecutives_losses: int = 0
@export var consecutives_wins: int = 0
@export var history: Array[bool] = []
@@ -19,12 +19,10 @@ func add_game(is_won: bool) -> void:
consecutives_wins += 1
if consecutives_wins >= CONSECUTIVE_WINS_TO_PROMOTE:
consecutives_wins = 0
if difficulty != MAX_DIFFICULTY:
difficulty += 1
difficulty = mini(MAX_DIFFICULTY, difficulty + 1)
else:
consecutives_wins = 0
consecutives_losses += 1
if consecutives_losses >= CONSECUTIVE_LOSSES_TO_DEMOTE:
consecutives_losses = 0
if difficulty != MIN_DIFFICULTY:
difficulty -= 1
difficulty = maxi(MIN_DIFFICULTY, difficulty - 1)
-1
View File
@@ -1,4 +1,3 @@
class_name TestBezier
extends GutTest
const EPSILON: float = 0.00001
@@ -0,0 +1,103 @@
extends GutTest
func test_defaults() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
assert_eq(history.difficulty, UserMinigameHistory.MIN_DIFFICULTY)
assert_eq(history.consecutives_losses, 0)
assert_eq(history.consecutives_wins, 0)
assert_eq(history.history.size(), 0)
func test_add_game_win_updates_history_and_counters() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
history.consecutives_losses = 1
history.add_game(true)
assert_eq(history.history, [true])
assert_eq(history.consecutives_losses, 0)
assert_eq(history.consecutives_wins, 1)
assert_eq(history.difficulty, UserMinigameHistory.MIN_DIFFICULTY)
func test_add_game_loss_updates_history_and_counters() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
history.consecutives_wins = 1
history.add_game(false)
assert_eq(history.history, [false])
assert_eq(history.consecutives_wins, 0)
assert_eq(history.consecutives_losses, 1)
assert_eq(history.difficulty, UserMinigameHistory.MIN_DIFFICULTY)
func test_wins_promote_and_reset_wins() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
for _win: int in history.CONSECUTIVE_WINS_TO_PROMOTE:
history.add_game(true)
assert_eq(history.difficulty, UserMinigameHistory.MIN_DIFFICULTY + 1)
assert_eq(history.consecutives_wins, 0)
assert_eq(history.consecutives_losses, 0)
assert_eq(history.history, [true, true])
func test_two_losses_demote_and_reset_losses() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
history.difficulty = UserMinigameHistory.MIN_DIFFICULTY + 2
for _loss: int in history.CONSECUTIVE_LOSSES_TO_DEMOTE:
history.add_game(false)
assert_eq(history.difficulty, UserMinigameHistory.MIN_DIFFICULTY + 1)
assert_eq(history.consecutives_losses, 0)
assert_eq(history.consecutives_wins, 0)
assert_eq(history.history, [false, false])
func test_wins_do_not_exceed_max_difficulty() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
history.difficulty = UserMinigameHistory.MAX_DIFFICULTY
history.consecutives_wins = UserMinigameHistory.CONSECUTIVE_WINS_TO_PROMOTE - 1
history.add_game(true)
assert_eq(history.difficulty, UserMinigameHistory.MAX_DIFFICULTY)
assert_eq(history.consecutives_wins, 0)
assert_eq(history.consecutives_losses, 0)
assert_eq(history.history, [true])
func test_losses_do_not_go_below_min_difficulty() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
history.difficulty = UserMinigameHistory.MIN_DIFFICULTY
history.consecutives_losses = UserMinigameHistory.CONSECUTIVE_LOSSES_TO_DEMOTE - 1
history.add_game(false)
assert_eq(history.difficulty, UserMinigameHistory.MIN_DIFFICULTY)
assert_eq(history.consecutives_losses, 0)
assert_eq(history.consecutives_wins, 0)
assert_eq(history.history, [false])
func test_mixed_results_track_consecutive_counters() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
history.add_game(true)
for _loss: int in history.CONSECUTIVE_LOSSES_TO_DEMOTE:
history.add_game(false)
assert_eq(history.history, [true, false, false])
assert_eq(history.consecutives_wins, 0)
assert_eq(history.consecutives_losses, 0)
assert_eq(history.difficulty, UserMinigameHistory.MIN_DIFFICULTY)
history.add_game(false)
for _win: int in history.CONSECUTIVE_WINS_TO_PROMOTE:
history.add_game(true)
assert_eq(history.consecutives_wins, 0)
assert_eq(history.consecutives_losses, 0)
assert_eq(history.difficulty, UserMinigameHistory.MIN_DIFFICULTY + 1)
func test_extended_history() -> void:
var history: UserMinigameHistory = UserMinigameHistory.new()
history.add_game(true)
history.add_game(false)
history.add_game(false)
history.add_game(true)
history.add_game(true)
history.add_game(false)
history.add_game(true)
history.add_game(false)
history.add_game(true)
assert_eq(history.history, [true, false, false, true, true, false, true, false, true])
@@ -0,0 +1 @@
uid://bcws3l2np60mh