From cb03f174a01fc171f65ba0bd6790e58856dddba1 Mon Sep 17 00:00:00 2001 From: Zantox Date: Sun, 14 Apr 2024 13:35:48 +0200 Subject: [PATCH] Add unit-tests for card decks (#24791) * Add unit-tests for card decks * Fix linter CI comments * Rename unit-test file due to duplicate filename * Fix review comments * Fix unit-test broken after review comment fixing * Remove blank unit-tests for card-decks * Use TRUE instead of magic-value 1 in cardshuffle sound call Co-authored-by: Burzah <116982774+Burzah@users.noreply.github.com> --------- Co-authored-by: Burzah <116982774+Burzah@users.noreply.github.com> --- code/modules/games/cards.dm | 14 +++++++--- code/modules/unit_tests/_unit_tests.dm | 1 + code/modules/unit_tests/games/test_cards.dm | 30 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 code/modules/unit_tests/games/test_cards.dm diff --git a/code/modules/games/cards.dm b/code/modules/games/cards.dm index ad5e29ab160..83d32b64ac4 100644 --- a/code/modules/games/cards.dm +++ b/code/modules/games/cards.dm @@ -50,10 +50,16 @@ /obj/item/deck/Initialize(mapload) . = ..() + build_decks() + update_icon(UPDATE_ICON_STATE) + +/obj/item/deck/proc/build_decks() + if(length(cards)) + // prevent building decks more than once + return for(var/deck in 1 to deck_size) build_deck() deck_total = length(cards) - update_icon(UPDATE_ICON_STATE) /obj/item/deck/proc/build_deck() return @@ -227,8 +233,10 @@ var/mob/living/user = usr if(cooldown < world.time - 1 SECONDS) cards = shuffle(cards) - user.visible_message("[user] shuffles [src].") - playsound(user, 'sound/items/cardshuffle.ogg', 50, 1) + + if(user) + user.visible_message("[user] shuffles [src].") + playsound(user, 'sound/items/cardshuffle.ogg', 50, TRUE) cooldown = world.time diff --git a/code/modules/unit_tests/_unit_tests.dm b/code/modules/unit_tests/_unit_tests.dm index 7f234307d41..76f2504ef03 100644 --- a/code/modules/unit_tests/_unit_tests.dm +++ b/code/modules/unit_tests/_unit_tests.dm @@ -2,6 +2,7 @@ //Keep this sorted alphabetically #ifdef UNIT_TESTS +#include "games\test_cards.dm" #include "jobs\test_job_globals.dm" #include "aicard_icons.dm" #include "announcements.dm" diff --git a/code/modules/unit_tests/games/test_cards.dm b/code/modules/unit_tests/games/test_cards.dm new file mode 100644 index 00000000000..bde0b9f1f8b --- /dev/null +++ b/code/modules/unit_tests/games/test_cards.dm @@ -0,0 +1,30 @@ +/datum/unit_test/card_deck/proc/validate_deck(obj/item/deck/deck) + var/list/card_count = list() + for(var/datum/playingcard/card in deck.cards) + if(card_count[card.name] == null) + card_count[card.name] = 1 + else if(card.name == "Joker") + card_count[card.name]++ + else + // duplicate card in deck + return FALSE + + if(length(card_count) != 53) // 2 Jokers, so 53 unique cards + return FALSE + return TRUE + + +/datum/unit_test/card_deck/Run() + // setup + var/loc = pick(block(run_loc_bottom_left, run_loc_top_right)) + var/obj/item/deck/cards/cards = new /obj/item/deck/cards(loc) + cards.build_decks() + + // is deck proper upon spawning + if(!validate_deck(cards)) + Fail("52 card deck not initialized correctly.") + + // is deck proper after shuffling + cards.deckshuffle() + if(!validate_deck(cards)) + Fail("52 card deck broken after shuffling.")