Skip to content

Commit

Permalink
item/tests: update tname tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei8l committed Jan 13, 2024
1 parent 5695b71 commit 3ba1213
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 13 deletions.
12 changes: 11 additions & 1 deletion data/mods/TEST_DATA/items.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,17 @@
"to_hit": -2,
"effects": [ "NEVER_MISFIRES", "NON_FOULING", "RECOVER_80" ],
"qualities": [ [ "HAMMER", 1 ] ],
"melee_damage": { "bash": 7 }
"melee_damage": { "bash": 7 },
"variant_type": "generic",
"variants": [
{ "id": "test_rock_blue", "name": { "str": "blue test_rock" }, "description": "It's a blue test rock", "append": true },
{
"id": "test_rock_green",
"name": { "str": "green test_rock" },
"description": "It's a green test rock",
"append": true
}
]
},
{
"type": "TOOL_ARMOR",
Expand Down
28 changes: 22 additions & 6 deletions tests/item_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static const item_category_id item_category_clothing( "clothing" );
static const item_category_id item_category_container( "container" );
static const item_category_id item_category_food( "food" );
static const item_category_id item_category_guns( "guns" );
static const item_category_id item_category_spare_parts( "spare_parts" );
static const item_category_id item_category_tools( "tools" );

static const itype_id itype_test_backpack( "test_backpack" );
Expand Down Expand Up @@ -905,21 +906,36 @@ TEST_CASE( "rigid_splint_compliance", "[item][armor]" )

TEST_CASE( "item_single_type_contents", "[item]" )
{
item rock( "test_rock" );
std::array<std::string, 2> const variants = { "test_rock_blue", "test_rock_green" };
item walnut( "walnut" );
item nail( "nail" );
item bag( "bag_plastic" );
REQUIRE( bag.get_category_of_contents().id == item_category_container );
int const num = GENERATE( 1, 2 );
bool ret = true;
for( int i = 0; i < num; i++ ) {
ret &= bag.put_in( walnut, pocket_type::CONTAINER ).success();
rock.set_itype_variant( variants[i] );
ret &= bag.put_in( rock, pocket_type::CONTAINER ).success();
}
REQUIRE( ret );
CAPTURE( num, bag.display_name() );
CHECK( bag.get_category_of_contents() == *item_category_food );
REQUIRE( nail.get_category_of_contents().id != walnut.get_category_of_contents().id );
REQUIRE( bag.put_in( nail, pocket_type::CONTAINER ).success() );
CHECK( bag.get_category_of_contents().id == item_category_container );
CHECK( bag.get_category_of_contents() == *item_category_spare_parts );
REQUIRE( walnut.get_category_of_contents().id != rock.get_category_of_contents().id );
REQUIRE( bag.put_in( walnut, pocket_type::CONTAINER ).success() );
if( num == 1 ) {
// 1 rock and 1 walnut - nothing dominates
CHECK( bag.get_category_of_contents().id == item_category_container );
} else {
// 2 rock and 1 walnuts - rocks dominate
CHECK( bag.get_category_of_contents().id == item_category_spare_parts );
REQUIRE( bag.put_in( walnut, pocket_type::CONTAINER ).success() );
item hammer( "hammer" );
REQUIRE( hammer.get_category_of_contents().id != rock.get_category_of_contents().id );
REQUIRE( hammer.get_category_of_contents().id != walnut.get_category_of_contents().id );
REQUIRE( bag.put_in( hammer, pocket_type::CONTAINER ).success() );
// no dominant category anymore - revert to container
CHECK( bag.get_category_of_contents().id == item_category_container );
}

SECTION( "clothing" ) {
item jeans( "jeans" );
Expand Down
204 changes: 201 additions & 3 deletions tests/item_tname_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
#include <set>
#include <string>

#include "avatar.h"
#include "calendar.h"
#include "cata_catch.h"
#include "character.h"
#include "flag.h"
#include "item.h"
#include "item_category.h"
#include "item_pocket.h"
#include "item_tname.h"
#include "itype.h"
#include "options_helpers.h"
#include "pocket_type.h"
Expand All @@ -19,9 +23,12 @@ static const fault_id fault_gun_dirt( "fault_gun_dirt" );

static const item_category_id item_category_veh_parts( "veh_parts" );

static const itype_id itype_bag_plastic( "bag_plastic" );
static const itype_id itype_bag_garbage( "bag_garbage" );
static const itype_id itype_backpack_hiking( "backpack_hiking" );
static const itype_id itype_purse( "purse" );
static const itype_id itype_rock( "rock" );
static const itype_id itype_hammer( "hammer" );
static const itype_id itype_test_rock( "test_rock" );

static const skill_id skill_survival( "survival" );
Expand Down Expand Up @@ -663,6 +670,10 @@ TEST_CASE( "nested_items_tname", "[item][tname]" )
item backpack_hiking( itype_backpack_hiking );
item purse( itype_purse );
item rock( itype_test_rock );
item bag( itype_bag_plastic );
item bag_garbage( itype_bag_garbage );
item hammer( itype_hammer );
rock.clear_itype_variant();
item rock2( itype_rock );
const std::string color_pref =
"<color_c_green>++</color>\u00A0";
Expand All @@ -674,6 +685,7 @@ TEST_CASE( "nested_items_tname", "[item][tname]" )
backpack_hiking.put_in( rock, pocket_type::CONTAINER );

std::string const rock_nested_tname = colorize( rock.tname(), rock.color_in_inventory() );
std::string const hammers_nested_tname = colorize( hammer.tname( 2 ), rock.color_in_inventory() );
std::string const rocks_nested_tname = colorize( rock.tname( 2 ), rock.color_in_inventory() );
REQUIRE( rock_nested_tname == "<color_c_light_gray>TEST rock</color>" );
SECTION( "single rock" ) {
Expand All @@ -683,13 +695,52 @@ TEST_CASE( "nested_items_tname", "[item][tname]" )
SECTION( "several rocks" ) {
backpack_hiking.put_in( rock, pocket_type::CONTAINER );
CHECK( backpack_hiking.tname( 1 ) == color_pref + "hiking backpack " + nesting_sym +
" " + rocks_nested_tname + " (2)" );
" 2 " + rocks_nested_tname );
}
SECTION( "several stacks" ) {
REQUIRE( rock.get_category_shallow().id != purse.get_category_shallow().id );
backpack_hiking.put_in( rock, pocket_type::CONTAINER );
backpack_hiking.put_in( rock2, pocket_type::CONTAINER );
backpack_hiking.put_in( purse, pocket_type::CONTAINER );
CHECK( backpack_hiking.tname( 1 ) == color_pref + "hiking backpack " + nesting_sym + " 2 items" );
}
SECTION( "several stacks of same category" ) {
REQUIRE( rock.get_category_shallow().id == rock2.get_category_shallow().id );
backpack_hiking.put_in( rock, pocket_type::CONTAINER );
backpack_hiking.put_in( rock2, pocket_type::CONTAINER );
CHECK( backpack_hiking.tname( 1 ) ==
color_pref + "hiking backpack " + nesting_sym + " 2 " +
colorize( rock.get_category_shallow().name(), c_magenta ) );
}
SECTION( "several stacks of variants" ) {
item rock_blue( itype_test_rock );
item rock_green( itype_test_rock );
rock_blue.set_itype_variant( "test_rock_blue" );
rock_green.set_itype_variant( "test_rock_green" );
backpack_hiking.put_in( rock_blue, pocket_type::CONTAINER );
backpack_hiking.put_in( rock_green, pocket_type::CONTAINER );
CHECK( backpack_hiking.tname( 1 ) == color_pref + "hiking backpack " + nesting_sym +
" 3 " + rocks_nested_tname );
}
SECTION( "dominant type" ) {
backpack_hiking.clear_items();
REQUIRE( backpack_hiking.put_in( hammer, pocket_type::CONTAINER ).success() );
REQUIRE( backpack_hiking.put_in( hammer, pocket_type::CONTAINER ).success() );
REQUIRE( backpack_hiking.put_in( bag, pocket_type::CONTAINER ).success() );
CHECK( backpack_hiking.tname( 1 ) == color_pref + "hiking backpack " + nesting_sym + " 2 " +
hammers_nested_tname + " / 3 items" );
}
SECTION( "dominant category" ) {
backpack_hiking.clear_items();
REQUIRE( rock.get_category_shallow().id == rock2.get_category_shallow().id );
REQUIRE( backpack_hiking.put_in( rock, pocket_type::CONTAINER ).success() );
REQUIRE( backpack_hiking.put_in( rock2, pocket_type::CONTAINER ).success() );
REQUIRE( rock.get_category_of_contents().id == rock2.get_category_of_contents().id );
REQUIRE( rock.get_category_of_contents().id != purse.get_category_of_contents().id );
REQUIRE( backpack_hiking.put_in( purse, pocket_type::CONTAINER ).success() );
CHECK( backpack_hiking.tname( 1 ) ==
color_pref + "hiking backpack " + nesting_sym + " 2 " +
colorize( rock.get_category_shallow().name(), c_magenta ) + " / 3 items" );
}
SECTION( "container has whitelist" ) {
std::string const wlmark = "";
REQUIRE( backpack_hiking.get_all_contained_pockets().size() >= 2 );
Expand Down Expand Up @@ -738,7 +789,22 @@ TEST_CASE( "nested_items_tname", "[item][tname]" )
backpack_hiking.put_in( purse, pocket_type::CONTAINER );

CHECK( backpack_hiking.tname( 1 ) == color_pref + "hiking backpack " + nesting_sym +
" " + purse_color + color_pref + "purses" + color_end_tag + " (2)" );
" 2 " + purse_color + color_pref + "purses" + color_end_tag );
}
SECTION( "dominant nested category" ) {
REQUIRE( bag.put_in( rock, pocket_type::CONTAINER ).success() );
REQUIRE( bag_garbage.put_in( rock, pocket_type::CONTAINER ).success() );
REQUIRE( bag.get_category_of_contents().id == rock.get_category_of_contents().id );
REQUIRE( bag_garbage.get_category_of_contents().id == rock.get_category_of_contents().id );
REQUIRE( backpack_hiking.put_in( bag_garbage, pocket_type::CONTAINER ).success() );
REQUIRE( backpack_hiking.put_in( bag, pocket_type::CONTAINER ).success() );
item bag2( itype_bag_plastic );
REQUIRE( bag2.put_in( hammer, pocket_type::CONTAINER ).success() );
REQUIRE( bag.get_category_of_contents().id != bag2.get_category_of_contents().id );
REQUIRE( backpack_hiking.put_in( bag2, pocket_type::CONTAINER ).success() );
CHECK( backpack_hiking.tname( 1 ) ==
color_pref + "hiking backpack " + nesting_sym + " 2 " +
colorize( rock.get_category_shallow().name(), c_magenta ) + " / 3 items" );
}
}

Expand All @@ -753,4 +819,136 @@ TEST_CASE( "nested_items_tname", "[item][tname]" )
usb_drive.put_in( medisoft, pocket_type::SOFTWARE );
CHECK( usb_drive.tname( 1 ) == "USB drive " + nesting_sym + " " + medisoft_nested_tname );
}

tname::segment_bitset type_only;
type_only.set( tname::segments::TYPE );
SECTION( "aggregated food stats" ) {
avatar &u = get_avatar();
item salt( "salt" );
std::string const cat_food_str = salt.get_category_shallow().name();
item pepper( "pepper" );
item juniper( "juniper" );
item ration( "protein_bar_evac" );
item carrot( "carrot" );
item sauerkraut( "sauerkraut" );
item bag( itype_bag_plastic );
std::string const carrots_tname = carrot.tname( 2, type_only );

SECTION( "inedible" ) {
REQUIRE( ( salt.is_food() && pepper.is_food() && juniper.is_food() && ration.is_food() &&
carrot.is_food() && sauerkraut.is_food() ) );

REQUIRE( ( u.will_eat( salt ).value() == edible_rating::INEDIBLE &&
u.will_eat( pepper ).value() == edible_rating::INEDIBLE ) );
REQUIRE( bag.put_in( salt, pocket_type::CONTAINER ).success() );
REQUIRE( bag.put_in( pepper, pocket_type::CONTAINER ).success() );
CHECK( bag.tname( 1 ) == "plastic bag > 2 " + colorize( cat_food_str, c_dark_gray ) );
}

SECTION( "non-perishable" ) {
REQUIRE( ( !juniper.goes_bad() && !ration.goes_bad() ) );
REQUIRE( ( u.will_eat( juniper ).value() == edible_rating::EDIBLE &&
u.will_eat( ration ).value() == edible_rating::EDIBLE ) );
REQUIRE( bag.put_in( juniper, pocket_type::CONTAINER ).success() );
REQUIRE( bag.put_in( ration, pocket_type::CONTAINER ).success() );
CHECK( bag.tname( 1 ) == "plastic bag > 2 " + colorize( cat_food_str, c_cyan ) );
}

SECTION( "perishable" ) {
bool same_item = GENERATE( true, false );
bool rotten = GENERATE( true, false );
bool both_rotten = rotten ? GENERATE( true, false ) : false;
bool frozen = GENERATE( true, false );
bool both_frozen = frozen ? GENERATE( true, false ) : false;
CAPTURE( same_item, rotten, both_rotten, frozen, both_frozen );

nc_color color = c_light_cyan;
item second_food( same_item ? carrot : sauerkraut );

std::string fresh_str( " (fresh)" );
std::string frozen_str;

REQUIRE( ( carrot.goes_bad() && second_food.goes_bad() ) );
REQUIRE( ( u.will_eat( carrot ).value() == edible_rating::EDIBLE &&
u.will_eat( second_food ).value() == edible_rating::EDIBLE ) );
REQUIRE( ( carrot.is_fresh() && second_food.is_fresh() ) );

if( rotten ) {
carrot.set_relative_rot( 99 );
REQUIRE_FALSE( carrot.is_fresh() );
fresh_str = std::string{};
if( both_rotten ) {
second_food.set_relative_rot( 99 );
fresh_str = " (rotten)";
color = c_brown;
}
}
if( frozen ) {
carrot.set_flag( flag_FROZEN );
if( both_frozen ) {
second_food.set_flag( flag_FROZEN );
frozen_str = " (frozen)";
color = c_dark_gray;
}
}
REQUIRE( bag.put_in( carrot, pocket_type::CONTAINER ).success() );
REQUIRE( bag.put_in( second_food, pocket_type::CONTAINER ).success() );
if( same_item ) {
CHECK( bag.tname( 1 ) ==
"plastic bag > 2 " +
colorize( carrots_tname + fresh_str + frozen_str, color ) );
} else {
CHECK( bag.tname( 1 ) == "plastic bag > 2 " +
colorize( cat_food_str, color ) + fresh_str +
frozen_str );
}
}
}

SECTION( "aggregated clothing stats" ) {
item pants( "pants" );
pants.set_flag( flag_FIT );
bool same_item = GENERATE( true, false );
item second_item( same_item ? "pants" : "jeans" );
second_item.set_flag( flag_FIT );
REQUIRE( ( pants.has_flag( flag_VARSIZE ) && second_item.has_flag( flag_VARSIZE ) ) );

bool damaged = GENERATE( true, false );
bool both_damaged = damaged ? GENERATE( true, false ) : false;
bool unfit = GENERATE( true, false );
bool both_unfit = unfit ? GENERATE( true, false ) : false;
CAPTURE( same_item, damaged, both_damaged, unfit, both_unfit );

std::string const cat_cl_str = pants.get_category_shallow().name();
std::string const pants_tname = pants.tname( 2, type_only );
std::string dura_str = pants.durability_indicator();
std::string fit_str;

if( damaged ) {
pants.inc_damage();
dura_str = std::string{};
if( both_damaged ) {
second_item.inc_damage();
dura_str = pants.durability_indicator();
}
}
if( unfit ) {
pants.unset_flag( flag_FIT );
if( both_unfit ) {
second_item.unset_flag( flag_FIT );
fit_str = " (poor fit)";
}
}

REQUIRE( bag.put_in( pants, pocket_type::CONTAINER ).success() );
REQUIRE( bag.put_in( second_item, pocket_type::CONTAINER ).success() );

if( same_item ) {
CHECK( bag.tname( 1 ) == "plastic bag > 2 " +
colorize( dura_str + pants_tname + fit_str, c_light_gray ) );
} else {
CHECK( bag.tname( 1 ) == "plastic bag > 2 " + dura_str +
colorize( cat_cl_str, c_magenta ) + fit_str );
}
}
}
6 changes: 3 additions & 3 deletions tests/itemname_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ TEST_CASE( "display_name_rotten_food", "[item][display_name][contents]" )

REQUIRE( wrapper.put_in( butter_rot1, pocket_type::CONTAINER ).success() );
CHECK( wrapper.display_name() ==
"paper wrapper > " + butter_rot_tname + " hidden" );
"paper wrapper > " + butter_rot_tname );

REQUIRE( wrapper.put_in( butter_rot2, pocket_type::CONTAINER ).success() );
CHECK( wrapper.display_name() ==
"paper wrapper > " + butter_rot_tname + " (2) hidden" );
"paper wrapper > 2 " + butter_rot_tname );

REQUIRE( wrapper.put_in( butter_std, pocket_type::CONTAINER ).success() );
CHECK( wrapper.display_name() ==
"paper wrapper > 3 hidden items" );
"paper wrapper > 3 " + butter_std_tname );
}

0 comments on commit 3ba1213

Please sign in to comment.