From 015be230f32b0271ad09645e3bd3c35fbfa67ecf Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Sat, 19 Aug 2023 17:45:58 +0200 Subject: [PATCH] [MIRROR] Adds a unit test to check items are dropped when the arm holding them is dismembered. [MDB IGNORE] (#23201) * Adds a unit test to check items are dropped when the arm holding them is dismembered. (#77646) ## About The Pull Request What it says on the tin. First time ever making a unit test, so critiques and feedback are very much appreciated. If anyone thinks of other relevant edge cases, please do bring them up. ## Why It's Good For The Game Requested in #77613 (71fe46511afcbe7ce86754bda09baf2386f6ed2b), as the fact it broke again is a regression. Will fail until that PR is merged, as you might expect. * Adds a unit test to check items are dropped when the arm holding them is dismembered. --------- Co-authored-by: A miscellaneous Fern <80640114+FernandoJ8@users.noreply.github.com> --- code/modules/unit_tests/_unit_tests.dm | 1 + code/modules/unit_tests/dismemberment.dm | 38 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 code/modules/unit_tests/dismemberment.dm diff --git a/code/modules/unit_tests/_unit_tests.dm b/code/modules/unit_tests/_unit_tests.dm index 1ec215745cc..4d44b9ae0f0 100644 --- a/code/modules/unit_tests/_unit_tests.dm +++ b/code/modules/unit_tests/_unit_tests.dm @@ -127,6 +127,7 @@ #include "dcs_check_list_arguments.dm" #include "dcs_get_id_from_elements.dm" #include "designs.dm" +#include "dismemberment.dm" #include "door_access.dm" #include "dragon_expiration.dm" #include "drink_icons.dm" diff --git a/code/modules/unit_tests/dismemberment.dm b/code/modules/unit_tests/dismemberment.dm new file mode 100644 index 00000000000..d8ce43b960e --- /dev/null +++ b/code/modules/unit_tests/dismemberment.dm @@ -0,0 +1,38 @@ +/** + * Unit test to check that held items are dropped correctly when we are dismembered. + * + * Also tests for edge cases such as undroppable items. + */ +/datum/unit_test/dismemberment/Run() + var/mob/living/carbon/human/dummy = allocate(/mob/living/carbon/human/consistent) + + var/obj/item/testing_item = allocate(/obj/item/analyzer) + testing_item.name = "testing item" + + // Standard situation: We're holding a normal item and get dismembered. + test_item(dummy, testing_item) + + // Abnormal situation: We're holding an undroppable item and get dismembered. + ADD_TRAIT(testing_item, TRAIT_NODROP, TRAIT_GENERIC) + test_item(dummy, testing_item, status_text = "after applying TRAIT_NODROP to the testing item") + + +/datum/unit_test/dismemberment/proc/test_item(mob/living/carbon/human/dummy, obj/item/testing_item, status_text = "") + //Check both to make sure being the active hand doesn't make a difference. + dummy.put_in_l_hand(testing_item) + check_dismember(dummy, BODY_ZONE_L_ARM, status_text) + + dummy.put_in_r_hand(testing_item) + check_dismember(dummy, BODY_ZONE_R_ARM, status_text) + + +/datum/unit_test/dismemberment/proc/check_dismember(mob/living/carbon/human/dummy, which_arm, status_text) + var/obj/item/bodypart/dismembered_limb = dummy.get_bodypart(which_arm) + var/obj/item/held_item = dummy.get_item_for_held_index(dismembered_limb.held_index) + + dismembered_limb.dismember() + TEST_ASSERT(held_item in dummy.loc, "Dummy did not drop [held_item] when [dismembered_limb] was dismembered [status_text].") + // Clean up after ourselves + qdel(dismembered_limb) + dummy.fully_heal(HEAL_ALL) +