diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dbbff3..7ec0b22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The issue #12 removed public class I2cMasterInterface. But it is referred intern - [Issue #10](https://github.com/suikan4github/rpp_driver/issues/10) Cover APIs of the Raspberry Pi Pico SDK. - [Issue #15](https://github.com/suikan4github/rpp_driver/issues/15) Remove the redundant parameter check code from ConfigureSRC() - [Issue #16](https://github.com/suikan4github/rpp_driver/issues/16) Change the member function name from ConfigureSRC() to ConfigureSrc(). +- [Issue #22](https://github.com/suikan4github/rpp_driver/issues/22) Refactor the test_i2cmaster.cpp to use the fixture. ### Deprecated - [Issue #12](https://github.com/suikan4github/rpp_driver/issues/12) Is I2cMasterInterface needed? ### Removed diff --git a/test/test_i2cmaster.cpp b/test/test_i2cmaster.cpp index e64d792..2f0ed19 100644 --- a/test/test_i2cmaster.cpp +++ b/test/test_i2cmaster.cpp @@ -3,8 +3,33 @@ #include "i2c/i2cmaster.hpp" +using testing::_; +using testing::InSequence; using testing::Return; +class I2cMasterTest : public ::testing::Test { + protected: + ::rpp_driver::MockSdkWrapper sdk_; + i2c_inst_t i2c_inst_ = 0; + ::rpp_driver::I2cMaster *i2c_; + + virtual void SetUp() { + // Constructor will call these functiions. + EXPECT_CALL(sdk_, i2c_init(&i2c_inst_, _)); + EXPECT_CALL(sdk_, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2); + EXPECT_CALL(sdk_, gpio_pull_up(_)).Times(2); + + i2c_ = new ::rpp_driver::I2cMaster(sdk_, i2c_inst_, 100 * 1000, 17, 23); + } + + virtual void TearDown() { + // We can ignore these call inside destructor + EXPECT_CALL(sdk_, i2c_deinit(_)); + + delete i2c_; + } +}; // I2cMasterTest + // Constructor test TEST(I2cMaster, Constructor) { ::rpp_driver::MockSdkWrapper sdk; @@ -52,94 +77,62 @@ TEST(I2cMaster, Destructor) { } // Destructor -TEST(I2cMaster, ReadBlocking) { - ::rpp_driver::MockSdkWrapper sdk; - i2c_inst_t i2c_inst; - +TEST_F(I2cMasterTest, ReadBlocking) { uint8_t addr = 3; uint8_t buf[5]; bool nostop = true; int return_value = 11; - using ::testing::_; - - // We can ignore these call inside constructor - EXPECT_CALL(sdk, i2c_init(&i2c_inst, _)); - EXPECT_CALL(sdk, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2); - EXPECT_CALL(sdk, gpio_pull_up(_)).Times(2); - - ::rpp_driver::I2cMaster i2c(sdk, i2c_inst, 100 * 1000, 17, 23); - - EXPECT_CALL(sdk, i2c_read_blocking(&i2c_inst, addr, buf, sizeof(buf), nostop)) - .WillOnce(Return(return_value)); + { + InSequence dummy; - EXPECT_CALL(sdk, - i2c_read_blocking(&i2c_inst, addr, buf, sizeof(buf), !nostop)) - .WillOnce(Return(return_value)); + EXPECT_CALL(sdk_, + i2c_read_blocking(&i2c_inst_, addr, buf, sizeof(buf), nostop)) + .WillOnce(Return(return_value)); - EXPECT_EQ(i2c.ReadBlocking(addr, buf, sizeof(buf), nostop), return_value); - EXPECT_EQ(i2c.ReadBlocking(addr, buf, sizeof(buf), !nostop), return_value); - // We can ignore these call inside destructor - EXPECT_CALL(sdk, i2c_deinit(_)); + EXPECT_CALL(sdk_, + i2c_read_blocking(&i2c_inst_, addr, buf, sizeof(buf), !nostop)) + .WillOnce(Return(return_value)); + } + EXPECT_EQ(i2c_->ReadBlocking(addr, buf, sizeof(buf), nostop), return_value); + EXPECT_EQ(i2c_->ReadBlocking(addr, buf, sizeof(buf), !nostop), return_value); } // ReadBlocking -TEST(I2cMaster, WriteBlocking) { - ::rpp_driver::MockSdkWrapper sdk; - i2c_inst_t i2c_inst; - +TEST_F(I2cMasterTest, WriteBlocking) { uint8_t addr = 3; uint8_t buf[5]; bool nostop = true; int return_value = 11; - using ::testing::_; - - // We can ignore these call inside constructor - EXPECT_CALL(sdk, i2c_init(&i2c_inst, _)); - EXPECT_CALL(sdk, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2); - EXPECT_CALL(sdk, gpio_pull_up(_)).Times(2); - - ::rpp_driver::I2cMaster i2c(sdk, i2c_inst, 100 * 1000, 17, 23); + { + InSequence dummy; - EXPECT_CALL(sdk, - i2c_write_blocking(&i2c_inst, addr, buf, sizeof(buf), nostop)) - .WillOnce(Return(return_value)); - EXPECT_CALL(sdk, - i2c_write_blocking(&i2c_inst, addr, buf, sizeof(buf), !nostop)) - .WillOnce(Return(return_value)); + EXPECT_CALL(sdk_, + i2c_write_blocking(&i2c_inst_, addr, buf, sizeof(buf), nostop)) + .WillOnce(Return(return_value)); + EXPECT_CALL(sdk_, + i2c_write_blocking(&i2c_inst_, addr, buf, sizeof(buf), !nostop)) + .WillOnce(Return(return_value)); + } + EXPECT_EQ(i2c_->WriteBlocking(addr, buf, sizeof(buf), nostop), return_value); + EXPECT_EQ(i2c_->WriteBlocking(addr, buf, sizeof(buf), !nostop), return_value); - EXPECT_EQ(i2c.WriteBlocking(addr, buf, sizeof(buf), nostop), return_value); - EXPECT_EQ(i2c.WriteBlocking(addr, buf, sizeof(buf), !nostop), return_value); - // We can ignore these call inside destructor - EXPECT_CALL(sdk, i2c_deinit(_)); } // WriteBlocking -TEST(I2cMaster, IsDeviceExisting) { - ::rpp_driver::MockSdkWrapper sdk; - i2c_inst_t i2c_inst; - +TEST_F(I2cMasterTest, IsDeviceExisting) { uint8_t addr[] = {17, 23}; bool nostop = false; int return_value[] = {-1, 1}; - using ::testing::_; - - // We can ignore these call inside constructor - EXPECT_CALL(sdk, i2c_init(&i2c_inst, _)); - EXPECT_CALL(sdk, gpio_set_function(_, GPIO_FUNC_I2C)).Times(2); - EXPECT_CALL(sdk, gpio_pull_up(_)).Times(2); - - ::rpp_driver::I2cMaster i2c(sdk, i2c_inst, 100 * 1000, 17, 23); + { + InSequence dummy; - EXPECT_CALL(sdk, i2c_read_blocking(_, addr[0], _, 1, nostop)) - .WillOnce(Return(return_value[0])); + EXPECT_CALL(sdk_, i2c_read_blocking(_, addr[0], _, 1, nostop)) + .WillOnce(Return(return_value[0])); - EXPECT_CALL(sdk, i2c_read_blocking(_, addr[1], _, 1, nostop)) - .WillOnce(Return(return_value[1])); - - EXPECT_FALSE(i2c.IsDeviceExisting(addr[0])); - EXPECT_TRUE(i2c.IsDeviceExisting(addr[1])); - - // We can ignore these call inside destructor - EXPECT_CALL(sdk, i2c_deinit(_)); + EXPECT_CALL(sdk_, i2c_read_blocking(_, addr[1], _, 1, nostop)) + .WillOnce(Return(return_value[1])); + } + EXPECT_FALSE(i2c_->IsDeviceExisting(addr[0])); + EXPECT_TRUE(i2c_->IsDeviceExisting(addr[1])); }