Skip to content

Commit

Permalink
Add unit tests for limb scores and character modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
dseguin committed Dec 1, 2021
1 parent 65912d9 commit 0551dbe
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
37 changes: 37 additions & 0 deletions data/mods/TEST_DATA/body_parts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"type": "limb_score",
"id": "test",
"name": "Testing",
"affected_by_wounds": true,
"affected_by_encumb": true
},
{
"id": "test_tail",
"type": "body_part",
"name": "test tail",
"name_multiple": "test tails",
"heading": "TST Tail",
"heading_multiple": "Tails",
"encumbrance_text": "Your testing tail feels heavy",
"hit_size": 1,
"hit_size_relative": [ 0, 0, 0 ],
"drench_capacity": 4,
"main_part": "test_tail",
"hit_difficulty": 0.8,
"limb_type": "tail",
"opposite_part": "test_tail",
"connected_to": "torso",
"side": "both",
"accusative": { "ctxt": "bodypart_accusative", "str": "test tail" },
"hp_bar_ui_text": "TST TAIL",
"base_hp": 20,
"limb_scores": [ [ "test", 0.8 ] ]
},
{
"type": "enchantment",
"id": "ENCH_TEST_TAIL",
"condition": "ALWAYS",
"modified_bodyparts": [ { "gain": "test_tail" } ]
}
]
9 changes: 9 additions & 0 deletions data/mods/TEST_DATA/character_modifiers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"type": "character_mod",
"id": "test_char_cost_mod",
"description": "Test character cost modifier",
"mod_type": "x",
"value": { "limb_score": "test", "limb_type": "tail", "max": 1000.0, "nominator": 30.0, "subtract": 30.0 }
}
]
17 changes: 17 additions & 0 deletions data/mods/TEST_DATA/items.json
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,23 @@
"flags": [ "DEAF", "OVERSIZE", "POWERARMOR_COMPATIBLE" ],
"armor": [ { "coverage": 1 } ]
},
{
"id": "test_tail_encumber",
"type": "ARMOR",
"name": "TEST tail encumbrance",
"description": "Tests the effects of encumbrance on the test body part.",
"weight": "500 g",
"volume": "3 L",
"price": 117500,
"material": [ "plastic" ],
"symbol": "[",
"color": "yellow",
"warmth": 40,
"material_thickness": 2,
"environmental_protection": 20,
"flags": [ "VARSIZE", "WATERPROOF", "RAINPROOF", "GAS_PROOF", "RAD_PROOF", "ELECTRIC_IMMUNE", "OUTER" ],
"armor": [ { "encumbrance": 40, "coverage": 100, "covers": [ "test_tail" ] } ]
},
{
"id": "test_hazmat_suit",
"repairs_like": "fsurvivor_suit",
Expand Down
105 changes: 105 additions & 0 deletions tests/character_modifier_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "bodypart.h"
#include "cata_catch.h"
#include "character_modifier.h"
#include "magic_enchantment.h"
#include "player_helpers.h"

static const bodypart_str_id bodypart_test_tail( "test_tail" );
static const character_modifier_id character_modifier_test_char_cost_mod( "test_char_cost_mod" );
static const enchantment_id enchantment_ENCH_TEST_TAIL( "ENCH_TEST_TAIL" );
static const itype_id itype_test_tail_encumber( "test_tail_encumber" );
static const limb_score_id limb_score_test( "test" );

static void create_char( Character &dude )
{
clear_character( dude, true );

dude.enchantment_cache->force_add( *enchantment_ENCH_TEST_TAIL );
dude.recalculate_bodyparts();
REQUIRE( dude.has_part( bodypart_test_tail ) );
}

TEST_CASE( "Basic limb score test", "[character][encumbrance]" )
{
standard_npc dude( "Test NPC" );
create_char( dude );
const bodypart *test_bp = dude.get_part( bodypart_test_tail );
REQUIRE( test_bp != nullptr );

GIVEN( "limb is not encumbered" ) {
WHEN( "limb is not wounded" ) {
THEN( "modified limb score equals unmodified limb score" ) {
CHECK( test_bp->get_id()->get_limb_score( limb_score_test ) == Approx( 0.8 ).epsilon( 0.001 ) );
CHECK( dude.get_limb_score( limb_score_test ) == Approx( 0.8 ).epsilon( 0.001 ) );
}
}
WHEN( "limb is wounded" ) {
dude.apply_damage( nullptr, test_bp->get_id(), test_bp->get_hp_max() / 2, true );
THEN( "modified limb score is less than unmodified limb score" ) {
CHECK( test_bp->get_id()->get_limb_score( limb_score_test ) == Approx( 0.8 ).epsilon( 0.001 ) );
CHECK( dude.get_limb_score( limb_score_test ) == Approx( 0.53333 ).epsilon( 0.001 ) );
}
}
}

GIVEN( "limb is encumbered" ) {
item it( itype_test_tail_encumber );
dude.wear_item( it, false );
WHEN( "limb is not wounded" ) {
THEN( "modified limb score is less than unmodified limb score" ) {
CHECK( test_bp->get_id()->get_limb_score( limb_score_test ) == Approx( 0.8 ).epsilon( 0.001 ) );
CHECK( dude.get_limb_score( limb_score_test ) == Approx( 0.4701 ).epsilon( 0.001 ) );
}
}
WHEN( "limb is wounded" ) {
dude.apply_damage( nullptr, test_bp->get_id(), test_bp->get_hp_max() / 2, true );
THEN( "modified limb score is less than unmodified limb score" ) {
CHECK( test_bp->get_id()->get_limb_score( limb_score_test ) == Approx( 0.8 ).epsilon( 0.001 ) );
CHECK( dude.get_limb_score( limb_score_test ) == Approx( 0.3134 ).epsilon( 0.001 ) );
}
}
}
}

TEST_CASE( "Basic character modifier test", "[character][encumbrance]" )
{
standard_npc dude( "Test NPC" );
create_char( dude );
const bodypart *test_bp = dude.get_part( bodypart_test_tail );
REQUIRE( test_bp != nullptr );
REQUIRE( character_modifier_test_char_cost_mod->use_limb_score() == limb_score_test );

GIVEN( "limb is not encumbered" ) {
WHEN( "limb is not wounded" ) {
THEN( "modified limb score equals unmodified limb score" ) {
CHECK( dude.get_modifier( character_modifier_test_char_cost_mod ) == Approx( 7.5 ).epsilon(
0.001 ) );
}
}
WHEN( "limb is wounded" ) {
dude.apply_damage( nullptr, test_bp->get_id(), test_bp->get_hp_max() / 2, true );
THEN( "modified limb score is less than unmodified limb score" ) {
CHECK( dude.get_modifier( character_modifier_test_char_cost_mod ) == Approx( 26.25 ).epsilon(
0.001 ) );
}
}
}

GIVEN( "limb is encumbered" ) {
item it( itype_test_tail_encumber );
dude.wear_item( it, false );
WHEN( "limb is not wounded" ) {
THEN( "modified limb score is less than unmodified limb score" ) {
CHECK( dude.get_modifier( character_modifier_test_char_cost_mod ) == Approx( 33.81579 ).epsilon(
0.001 ) );
}
}
WHEN( "limb is wounded" ) {
dude.apply_damage( nullptr, test_bp->get_id(), test_bp->get_hp_max() / 2, true );
THEN( "modified limb score is less than unmodified limb score" ) {
CHECK( dude.get_modifier( character_modifier_test_char_cost_mod ) == Approx( 65.72368 ).epsilon(
0.001 ) );
}
}
}
}

0 comments on commit 0551dbe

Please sign in to comment.