Skip to content

Commit

Permalink
Fix crafting tests for incremental tool consumption
Browse files Browse the repository at this point in the history
Tool charges are now consumed incrementally and the relevant crafting
tests need to be updated to reflect that.

In particular, it is no longer sufficient to check tool charges after
merely starting the craft; the activity must be completed.
  • Loading branch information
ifreund committed Jun 25, 2019
1 parent 5ca6473 commit bd1d291
Showing 1 changed file with 50 additions and 60 deletions.
110 changes: 50 additions & 60 deletions tests/crafting_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,51 @@ static void prep_craft( const recipe_id &rid, const std::vector<item> &tools,
CHECK( can_craft == expect_craftable );
}

// This fakes a craft in a reasonable way which is fast
static void fake_test_craft( const recipe_id &rid, const std::vector<item> &tools,
bool expect_craftable )
static constexpr int midnight = HOURS( 0 );
static constexpr int midday = HOURS( 12 );

static void set_time( int time )
{
prep_craft( rid, tools, expect_craftable );
if( expect_craftable ) {
g->u.make_craft_with_command( rid, 1, false );
g->u.invalidate_crafting_inventory();
calendar::turn = time;
g->reset_light_level();
int z = g->u.posz();
g->m.update_visibility_cache( z );
g->m.invalidate_map_cache( z );
g->m.build_map_cache( z );
}

// This tries to actually run the whole craft activity, which is more thorough,
// but slow
static int actually_test_craft( const recipe_id &rid, const std::vector<item> &tools,
int interrupt_after_turns )
{
prep_craft( rid, tools, true );
set_time( midday ); // Ensure light for crafting
const recipe &rec = rid.obj();
REQUIRE( g->u.morale_crafting_speed_multiplier( rec ) == 1.0 );
REQUIRE( g->u.lighting_craft_speed_multiplier( rec ) == 1.0 );
REQUIRE( !g->u.activity );

// This really shouldn't be needed, but for some reason the tests fail for mingw builds without it
g->u.learn_recipe( &rec );
REQUIRE( g->u.has_recipe( &rec, g->u.crafting_inventory(), g->u.get_crafting_helpers() ) != -1 );

g->u.make_craft( rid, 1 );
CHECK( g->u.activity );
CHECK( g->u.activity.id() == activity_id( "ACT_CRAFT" ) );
int turns = 0;
while( g->u.activity.id() == activity_id( "ACT_CRAFT" ) ) {
if( turns >= interrupt_after_turns ) {
set_time( midnight ); // Kill light to interrupt crafting
}
++turns;
g->u.moves = 100;
g->u.activity.do_turn( g->u );
}
return turns;
}

TEST_CASE( "charge_handling" )
TEST_CASE( "charge_handling", "[crafting]" )
{
SECTION( "carver" ) {
std::vector<item> tools;
Expand All @@ -308,7 +341,7 @@ TEST_CASE( "charge_handling" )
tools.emplace_back( "power_supply" );
tools.emplace_back( "scrap" );

fake_test_craft( recipe_id( "carver_off" ), tools, true );
actually_test_craft( recipe_id( "carver_off" ), tools, INT_MAX );
CHECK( get_remaining_charges( "hotplate" ) == 10 );
CHECK( get_remaining_charges( "soldering_iron" ) == 10 );
}
Expand All @@ -328,7 +361,7 @@ TEST_CASE( "charge_handling" )
tools.emplace_back( "power_supply" );
tools.emplace_back( "scrap" );

fake_test_craft( recipe_id( "carver_off" ), tools, true );
actually_test_craft( recipe_id( "carver_off" ), tools, INT_MAX );
CHECK( get_remaining_charges( "hotplate" ) == 0 );
CHECK( get_remaining_charges( "soldering_iron" ) == 0 );
}
Expand All @@ -351,7 +384,7 @@ TEST_CASE( "charge_handling" )
tools.emplace_back( "scrap" );
tools.emplace_back( "UPS_off", -1, 500 );

fake_test_craft( recipe_id( "carver_off" ), tools, true );
actually_test_craft( recipe_id( "carver_off" ), tools, INT_MAX );
CHECK( get_remaining_charges( "hotplate" ) == 0 );
CHECK( get_remaining_charges( "soldering_iron" ) == 0 );
CHECK( get_remaining_charges( "UPS_off" ) == 480 );
Expand All @@ -375,11 +408,11 @@ TEST_CASE( "charge_handling" )
tools.emplace_back( "scrap" );
tools.emplace_back( "UPS_off", -1, 10 );

fake_test_craft( recipe_id( "carver_off" ), tools, false );
prep_craft( recipe_id( "carver_off" ), tools, false );
}
}

TEST_CASE( "tool_use" )
TEST_CASE( "tool_use", "[crafting]" )
{
SECTION( "clean_water" ) {
std::vector<item> tools;
Expand All @@ -389,7 +422,8 @@ TEST_CASE( "tool_use" )
tools.push_back( plastic_bottle );
tools.emplace_back( "pot" );

fake_test_craft( recipe_id( "water_clean" ), tools, true );
// Can't actually test crafting here since crafting a liquid currently causes a ui prompt
prep_craft( recipe_id( "water_clean" ), tools, true );
}
SECTION( "clean_water_in_occupied_cooking_vessel" ) {
std::vector<item> tools;
Expand All @@ -403,52 +437,8 @@ TEST_CASE( "tool_use" )
jar.contents.emplace_back( "water", -1, 2 );
tools.push_back( jar );

fake_test_craft( recipe_id( "water_clean" ), tools, false );
}
}

static constexpr int midnight = HOURS( 0 );
static constexpr int midday = HOURS( 12 );

static void set_time( int time )
{
calendar::turn = time;
g->reset_light_level();
int z = g->u.posz();
g->m.update_visibility_cache( z );
g->m.invalidate_map_cache( z );
g->m.build_map_cache( z );
}

// This tries to actually run the whole craft activity, which is more thorough,
// but slow
static int actually_test_craft( const recipe_id &rid, const std::vector<item> &tools,
int interrupt_after_turns )
{
prep_craft( rid, tools, true );
set_time( midday ); // Ensure light for crafting
const recipe &rec = rid.obj();
REQUIRE( g->u.morale_crafting_speed_multiplier( rec ) == 1.0 );
REQUIRE( g->u.lighting_craft_speed_multiplier( rec ) == 1.0 );
REQUIRE( !g->u.activity );

// This really shouldn't be needed, but for some reason the tests fail for mingw builds without it
g->u.learn_recipe( &rec );
REQUIRE( g->u.has_recipe( &rec, g->u.crafting_inventory(), g->u.get_crafting_helpers() ) != -1 );

g->u.make_craft( rid, 1 );
CHECK( g->u.activity );
CHECK( g->u.activity.id() == activity_id( "ACT_CRAFT" ) );
int turns = 0;
while( g->u.activity.id() == activity_id( "ACT_CRAFT" ) ) {
if( turns >= interrupt_after_turns ) {
set_time( midnight ); // Kill light to interrupt crafting
}
++turns;
g->u.moves = 100;
g->u.activity.do_turn( g->u );
prep_craft( recipe_id( "water_clean" ), tools, false );
}
return turns;
}

// Resume the first in progress craft found in the player's inventory
Expand Down Expand Up @@ -496,7 +486,7 @@ static void verify_inventory( const std::vector<std::string> &has,
}
}

TEST_CASE( "crafting_interruption" )
TEST_CASE( "crafting_interruption", "[crafting]" )
{
std::vector<item> tools;
tools.emplace_back( "hammer" );
Expand Down

0 comments on commit bd1d291

Please sign in to comment.