Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option override support for tests #38051

Merged
merged 3 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions tests/invlet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "catch/catch.hpp"
#include "game.h"
#include "map.h"
#include "options_helpers.h"
#include "options.h"
#include "player.h"
#include "map_helpers.h"
Expand Down Expand Up @@ -724,25 +725,25 @@ static void merge_invlet_test( player &dummy, inventory_location from )

#define invlet_test_autoletter_off( name, dummy, from, to ) \
SECTION( std::string( name ) + " (auto letter off)" ) { \
get_options().get_option( "AUTO_INV_ASSIGN" ).setValue( "disabled" ); \
override_option opt( "AUTO_INV_ASSIGN", "disabled" ); \
invlet_test( dummy, from, to ); \
}

#define stack_invlet_test_autoletter_off( name, dummy, from, to ) \
SECTION( std::string( name ) + " (auto letter off)" ) { \
get_options().get_option( "AUTO_INV_ASSIGN" ).setValue( "disabled" ); \
override_option opt( "AUTO_INV_ASSIGN", "disabled" ); \
stack_invlet_test( dummy, from, to ); \
}

#define swap_invlet_test_autoletter_off( name, dummy, loc ) \
SECTION( std::string( name ) + " (auto letter off)" ) { \
get_options().get_option( "AUTO_INV_ASSIGN" ).setValue( "disabled" ); \
override_option opt( "AUTO_INV_ASSIGN", "disabled" ); \
swap_invlet_test( dummy, loc ); \
}

#define merge_invlet_test_autoletter_off( name, dummy, from ) \
SECTION( std::string( name ) + " (auto letter off)" ) { \
get_options().get_option( "AUTO_INV_ASSIGN" ).setValue( "disabled" ); \
override_option opt( "AUTO_INV_ASSIGN", "disabled" ); \
merge_invlet_test( dummy, from ); \
}

Expand Down
2 changes: 2 additions & 0 deletions tests/iteminfo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "item.h"
#include "iteminfo_query.h"
#include "itype.h"
#include "options_helpers.h"
#include "recipe_dictionary.h"

static void iteminfo_test( const item &i, const iteminfo_query &q, const std::string &reference )
Expand All @@ -25,6 +26,7 @@ TEST_CASE( "item description and physical attributes", "[item][iteminfo]" )
iteminfo_parts::BASE_PRICE, iteminfo_parts::BASE_VOLUME,
iteminfo_parts::BASE_WEIGHT, iteminfo_parts::BASE_MATERIAL
} );
override_option opt( "USE_METRIC_WEIGHTS", "lbs" );
iteminfo_test(
item( "jug_plastic" ), q,
"A standard plastic jug used for milk and household cleaning chemicals.\n"
Expand Down
9 changes: 5 additions & 4 deletions tests/monster_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "map.h"
#include "map_helpers.h"
#include "monster.h"
#include "options_helpers.h"
#include "options.h"
#include "player.h"
#include "test_statistics.h"
Expand Down Expand Up @@ -296,7 +297,7 @@ static void monster_check()
TEST_CASE( "write_slope_to_speed_map_trig", "[.]" )
{
clear_map_and_put_player_underground();
get_options().get_option( "CIRCLEDIST" ).setValue( "true" );
override_option opt( "CIRCLEDIST", "true" );
trigdist = true;
test_moves_to_squares( "mon_zombie_dog", true );
test_moves_to_squares( "mon_pig", true );
Expand All @@ -305,7 +306,7 @@ TEST_CASE( "write_slope_to_speed_map_trig", "[.]" )
TEST_CASE( "write_slope_to_speed_map_square", "[.]" )
{
clear_map_and_put_player_underground();
get_options().get_option( "CIRCLEDIST" ).setValue( "false" );
override_option opt( "CIRCLEDIST", "false" );
trigdist = false;
test_moves_to_squares( "mon_zombie_dog", true );
test_moves_to_squares( "mon_pig", true );
Expand All @@ -316,15 +317,15 @@ TEST_CASE( "write_slope_to_speed_map_square", "[.]" )
TEST_CASE( "monster_speed_square", "[speed]" )
{
clear_map_and_put_player_underground();
get_options().get_option( "CIRCLEDIST" ).setValue( "false" );
override_option opt( "CIRCLEDIST", "false" );
trigdist = false;
monster_check();
}

TEST_CASE( "monster_speed_trig", "[speed]" )
{
clear_map_and_put_player_underground();
get_options().get_option( "CIRCLEDIST" ).setValue( "true" );
override_option opt( "CIRCLEDIST", "true" );
trigdist = true;
monster_check();
}
17 changes: 17 additions & 0 deletions tests/options_helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "options_helpers.h"

#include "debug.h"
#include "options.h"

override_option::override_option( const std::string &option, const std::string &value ) :
option_( option )
{
options_manager::cOpt &opt = get_options().get_option( option_ );
old_value_ = opt.getValue( true );
opt.setValue( value );
}

override_option::~override_option()
{
get_options().get_option( option_ ).setValue( old_value_ );
}
21 changes: 21 additions & 0 deletions tests/options_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#ifndef CATA_TEST_OPTIONS_HELPERS_H
#define CATA_TEST_OPTIONS_HELPERS_H

#include <string>

// RAII class to temporarily override a particular option value
// The previous value will be restored in the destructor
class override_option
{
public:
override_option( const std::string &option, const std::string &value );
override_option( const override_option & ) = delete;
override_option &operator=( const override_option & ) = delete;
~override_option();
private:
std::string option_;
std::string old_value_;
};

#endif