Skip to content

Commit

Permalink
Tests checking correctness of drop UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Coolthulhu committed Oct 9, 2022
1 parent 514bbd5 commit 496f852
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
16 changes: 7 additions & 9 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,9 @@ std::vector<inventory_entry *> inventory_column::get_entries(
{
std::vector<inventory_entry *> res;

if( allows_selecting() ) {
for( const auto &elem : entries ) {
if( filter_func( elem ) ) {
res.push_back( const_cast<inventory_entry *>( &elem ) );
}
for( const auto &elem : entries ) {
if( filter_func( elem ) ) {
res.push_back( const_cast<inventory_entry *>( &elem ) );
}
}

Expand Down Expand Up @@ -1007,7 +1005,7 @@ void selection_column_base::reset_width( const std::vector<inventory_column *> &
};

for( const inventory_column *const col : all_columns ) {
if( col && !dynamic_cast<const selection_column_base *>( col ) ) {
if( col && col->allows_selecting() && !dynamic_cast<const selection_column_base *>( col ) ) {
for( const inventory_entry *const ent : col->get_entries( always_yes ) ) {
if( ent ) {
expand_to_fit( *ent );
Expand Down Expand Up @@ -2523,7 +2521,7 @@ void inventory_drop_selector::rebuild()
iter_dropping->second :
0;

size_t implied_drop_count = total_count - selected_count;
size_t implied_drop_count = matching_count - selected_count;

if( selected_count + implied_drop_count > total_count ) {
debugmsg( "Dropped item count > total item count" );
Expand Down Expand Up @@ -2565,10 +2563,10 @@ void inventory_drop_selector::rebuild()
only_implied.chosen_count = implied_count.selected;
sel_col.on_change( only_implied );
}
refresh_active_column();


refresh_active_column();
}

// TODO: Refresh only those which changed status
for( inventory_column *col : get_all_columns() ) {
col->clear_cell_cache();
Expand Down
43 changes: 22 additions & 21 deletions tests/inventory_ui_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ const auto is_item = []( const inventory_entry &ie )
return ie.is_item();
};

static void verify_has_entries( debug_inventory_selector &ui, const item &filler )
static void verify_has_entries( const debug_inventory_selector &ui, const item &filler )
{
const itype_id filler_id = filler.typeId();
size_t total_item_entry_count = 0;
for( const inventory_column *col : ui.get_all_columns() ) {
auto entries = col->get_entries( is_item );
auto entries = col->get_entries( true_fun );
for( auto &entry : entries ) {
if( entry->any_item()->typeId() == filler_id ) {
total_item_entry_count += entry->get_total_charges();
if( entry->is_item() && entry->any_item()->typeId() == filler_id ) {
total_item_entry_count += entry->get_stack_size();
}
}
}
Expand Down Expand Up @@ -153,39 +153,30 @@ static void test_drop_implications( player &u,

ui.set_filter( "" );
ui.select_position( ui.get_selection_position() );
THEN( "Expected number of bottles is predicted to be dropped" ) {
for( const inventory_column *col : ui.get_all_columns() ) {
const_cast<inventory_column *>( col )->prepare_paging();
printf( "Col:\n" );
for( auto entry : col->get_entries( true_fun ) ) {
printf( "%s, ", entry->cached_name.c_str() );
}
printf( "\n" );
}

THEN( "Expected number of bottles is predicted to be dropped" ) {
const selection_column_base *selection = nullptr;
for( const inventory_column *col : ui.get_all_columns() ) {
// Horrible!
const selection_column_base *cast_col = dynamic_cast<const selection_column_base *>( col );
if( cast_col != nullptr && cast_col->visible() ) {
if( cast_col != nullptr ) {
REQUIRE( selection == nullptr );
selection = cast_col;
break;
}
}

REQUIRE( selection != nullptr );
auto entries = selection->get_entries( is_item );

CAPTURE( selection->get_entries( true_fun ) );
REQUIRE( entries.size() > 0 );

size_t actual_filler_count = std::accumulate( entries.begin(), entries.end(), 0,
size_t actual_implied_filler_count = std::accumulate( entries.begin(), entries.end(), 0,
[&filler]( int acc, const inventory_entry * ie ) {
return acc + ( ie->any_item()->typeId() == filler.typeId() ? ie->get_stack_size() : 0 );
return acc + ( ie->any_item()->typeId() == filler.typeId() ? ie->chosen_count : 0 );
} );

CHECK( actual_filler_count >= expected_filler_count_min );
CHECK( actual_filler_count <= expected_filler_count_max );
CHECK( actual_implied_filler_count >= expected_filler_count_min );
CHECK( actual_implied_filler_count <= expected_filler_count_max );
}
}

Expand All @@ -211,6 +202,16 @@ TEST_CASE( "when dropping bags, the ui adds implied drops to 'selection' column"
REQUIRE( filler_count > 0 );
CAPTURE( filler_count );

size_t filler_count_in_inv = 0;
for( const auto &elem : u.inv.slice() ) {
for( auto iter = elem->begin(); iter != elem->end(); iter++ ) {
if( iter->typeId() == filler.typeId() ) {
filler_count_in_inv++;
}
}
}
REQUIRE( filler_count == filler_count_in_inv );

ui.add_character_items( u );

WHEN( "The character wants to drop a backpack" ) {
Expand Down Expand Up @@ -259,7 +260,7 @@ static void test_drop_colors( player &u,
}

TEST_CASE( "when dropping bags, the ui colors implied drops in 'TODO: find good way to reference color here'",
"[ui][drop_token]" )
"[ui][drop_token][.]" )
{
return;

Expand Down

0 comments on commit 496f852

Please sign in to comment.