I am a freshman of TDD and C++, I write some code works but it seems ugly…
could you give me some tips on how to refactor these code?
I define testcases like this:
// define some test case
namespace test_case_planes {
const std::string t_filename = "planes.segy_first_trace";
boost::uintmax_t t_file_size = 5888;
boost::uintmax_t t_traces_size = 1;
boost::int16_t t_trace_samples_size = 512;
boost::int16_t t_sample_interval = 4000; // 2000us, 2ms
}
namespace test_case_ld0042_file_00018 {
const std::string t_filename = "ld0042_file_00018.sgy_first_trace";
boost::uintmax_t t_file_size = 12040;
boost::uintmax_t t_traces_size = 1;
boost::int16_t t_trace_samples_size = 2050;
boost::int16_t t_sample_interval = 2000; // 2000us, 2ms
}
and test like this(with google test)
TEST(Segy, constructWithNoParas){
using namespace test_case_planes;
segy::Segy* test_segy = new segy::Segy();
EXPECT_EQ(test_segy->getFilename(), t_filename);
}
TEST(Segy, constructWithFilename){
using namespace test_case_ld0042_file_00018;
segy::Segy* test_segy = new segy::Segy(t_filename);
EXPECT_EQ(test_segy->getFilename(), t_filename);
}
TEST(Segy, setFilename){
using namespace test_case_ld0042_file_00018;
segy::Segy* test_segy = new segy::Segy();
test_segy->setFilename(t_filename);
EXPECT_EQ(test_segy->getFilename(), t_filename);
}
I think what you are looking for is a fixture. I am not familiar with google test, but test fixtures should be available in most test frameworks. You should look into the documentation for that.