#include #include #include #include #include #include "avatar.h" #include "cata_catch.h" #include "cata_utility.h" #include "coordinates.h" #include "inventory_ui.h" #include "item.h" #include "item_location.h" #include "map.h" #include "map_helpers.h" #include "map_selector.h" #include "player_helpers.h" #include "point.h" #include "pocket_type.h" #include "ret_val.h" #include "type_id.h" class Character; static const itype_id itype_backpack( "backpack" ); static const itype_id itype_bag_plastic( "bag_plastic" ); static const itype_id itype_hammer( "hammer" ); static const itype_id itype_knife_hunting( "knife_hunting" ); namespace { class counting_preset : public inventory_selector_preset { public: explicit counting_preset( std::function shows ) : shows( std::move( shows ) ) {} bool is_shown( const item_location &loc ) const override { ++asked[loc.get_item()]; return shows( *loc ); } mutable std::map asked; private: std::function shows; }; class layout_selector : public inventory_selector { public: using inventory_selector::inventory_selector; using inventory_selector::get_all_columns; }; std::vector layout_of( const layout_selector &selector ) { std::vector out; for( const inventory_column *col : selector.get_all_columns() ) { for( const inventory_entry *e : col->get_entries( []( const inventory_entry & en ) { return en.is_item(); }, true ) ) { out.push_back( e->any_item()->typeId().str() + " top=" + ( e->topmost_parent ? e->topmost_parent->typeId().str() : "none" ) + " indent=" + std::to_string( e->indent ) + " chevron=" + ( e->chevron ? "yes" : "no" ) ); } } return out; } void check_asked_once( const counting_preset &preset ) { for( const auto &[it, n] : preset.asked ) { CAPTURE( it->typeId() ); CHECK( n == 1 ); } } class haul_selector_probe : public inventory_haul_selector { public: using inventory_haul_selector::inventory_haul_selector; using inventory_multiselector::to_use; }; } // namespace TEST_CASE( "inventory_selector_asks_the_preset_once_per_item", "[inventory][ui]" ) { clear_avatar(); clear_map(); avatar &u = get_avatar(); map &here = get_map(); const tripoint_bub_ms pos = u.pos_bub(); GIVEN( "backpack with a hammer and a knife" ) { item pack( itype_backpack ); REQUIRE( pack.put_in( item( itype_hammer ), pocket_type::CONTAINER ).success() ); REQUIRE( pack.put_in( item( itype_knife_hunting ), pocket_type::CONTAINER ).success() ); REQUIRE_FALSE( here.add_item( pos, pack ).is_null() ); WHEN( "every item shown" ) { counting_preset preset( return_true ); layout_selector selector( u, preset ); selector.add_map_items( pos ); THEN( "all three listed, each asked once" ) { CHECK( selector.item_entry_count() == 3 ); CHECK( preset.asked.size() == 3 ); check_asked_once( preset ); CHECK( layout_of( selector ) == std::vector { "backpack top=none indent=0 chevron=yes", "knife_hunting top=backpack indent=2 chevron=no", "hammer top=backpack indent=2 chevron=no" } ); } } WHEN( "backpack is hidden" ) { counting_preset preset( []( const item & it ) { return it.typeId() != itype_backpack; } ); layout_selector selector( u, preset ); selector.add_map_items( pos ); THEN( "only contents listed, each item asked once" ) { CHECK( selector.item_entry_count() == 2 ); check_asked_once( preset ); CHECK( layout_of( selector ) == std::vector { "knife_hunting top=none indent=0 chevron=no", "hammer top=none indent=0 chevron=no" } ); } } } GIVEN( "backpack with plastic bag with hammer" ) { item bag( itype_bag_plastic ); REQUIRE( bag.put_in( item( itype_hammer ), pocket_type::CONTAINER ).success() ); item pack( itype_backpack ); REQUIRE( pack.put_in( bag, pocket_type::CONTAINER ).success() ); REQUIRE_FALSE( here.add_item( pos, pack ).is_null() ); WHEN( "plastic bag in the middle hidden" ) { counting_preset preset( []( const item & it ) { return it.typeId() != itype_bag_plastic; } ); layout_selector selector( u, preset ); selector.add_map_items( pos ); THEN( "backpack and hammer listed, each item asked once" ) { CHECK( selector.item_entry_count() == 2 ); check_asked_once( preset ); CHECK( layout_of( selector ) == std::vector { "backpack top=none indent=0 chevron=yes", "hammer top=none indent=2 chevron=no" } ); } } } } TEST_CASE( "haul_selector_skips_items_not_on_the_tile", "[inventory][ui]" ) { clear_avatar(); clear_map(); avatar &u = get_avatar(); map &here = get_map(); const tripoint_bub_ms pos = u.pos_bub(); const tripoint_bub_ms elsewhere = pos + tripoint_rel_ms( 1, 0, 0 ); GIVEN( "haul list with a hammer here and a knife on the next tile" ) { item &hammer = here.add_item( pos, item( itype_hammer ) ); item &knife = here.add_item( elsewhere, item( itype_knife_hunting ) ); REQUIRE_FALSE( hammer.is_null() ); REQUIRE_FALSE( knife.is_null() ); std::vector haul_list { item_location( map_cursor( &here, pos ), &hammer ), item_location( map_cursor( &here, elsewhere ), &knife ) }; WHEN( "haul selector applies the list" ) { haul_selector_probe selector( u ); selector.add_map_items( pos ); selector.apply_selection( haul_list ); THEN( "only hammer is selected" ) { REQUIRE( selector.to_use.size() == 1 ); CHECK( selector.to_use.front().first.get_item() == &hammer ); CHECK( selector.to_use.front().second == 1 ); } } } }