Skip to content

Add googletest example #32

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions drivers/gpio_singleton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef GPIO_H
#define GPIO_H

#include <cstddef>
#include <cstdint>
#include <iostream>
using namespace std;

class Gpio {
public:
// Get the singleton instance of Gpio
static Gpio& getInstance() {
static Gpio instance;
return instance;
}

/** API to initialize GPIO.
*
* @retval 0 on success
* @retval -1 on failure
*/
virtual int init()
{
cout << "Virtual Init" << endl;
return 0;
}

/* API that accepts multiple parameters and returns through reference.
* @param[out] name Name
* @param[in] name_len Name length in bytes
* @param[in] level Level
* @param[in] drive Drive
*
* @retval 0 on success
* @retval -1 on failure
*/
virtual int config(char* name, uint8_t name_len, int level, int drive)
{
return 0;
}


// Deleted copy constructor and assignment operator
Gpio(const Gpio&) = delete;
void operator=(const Gpio&) = delete;

protected:
Gpio() {}

private:
// Internal private methods for the actual implementation
int gpio_init_impl();
int gpio_config_impl(char* name, uint8_t name_len, int level, int drive);
};

#endif // GPIO_H
39 changes: 39 additions & 0 deletions examples/led_driver_test_gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.20.0)
project(led_driver_test_gtest)

add_executable(led_driver_test_gtest led_test.cpp)

set(WORKSPACE_ROOT ${PROJECT_SOURCE_DIR}/../../)

project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

target_link_libraries(
led_driver_test_gtest
GTest::gtest_main
GTest::gmock_main
)

target_sources(led_driver_test_gtest PRIVATE ${WORKSPACE_ROOT}/uut_cpp/src/led.cpp)

target_include_directories(led_driver_test_gtest PRIVATE ${WORKSPACE_ROOT}/drivers)
target_include_directories(led_driver_test_gtest PRIVATE ${WORKSPACE_ROOT}/uut_cpp/include)

include(GoogleTest)
gtest_discover_tests(led_driver_test_gtest)

add_compile_options(-Werror -Wall -g -O0 --coverage)
39 changes: 39 additions & 0 deletions examples/led_driver_test_gtest/led_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "gpio_singleton.h"
#include "led.h"

using ::testing::Return;

class MockGpio : public Gpio {
public:
MOCK_METHOD(int, init, (), (override));
MOCK_METHOD(int, config, (char* name, uint8_t name_len, int level, int drive), (override));
};

// Declare external mock function
extern Gpio& getInstance();

// Create a fake version of getInstance() that returns the mock singleton
Gpio& getInstance() {
static MockGpio mockInstance;
return mockInstance;
}

TEST(test_when_led_init_is_called_it_calls_gpio_init_and_returns_success, GpioInit) {
// Cast the singleton instance from Gpio& to MockGpio& to access mock-specific methods
MockGpio& mockgpio = dynamic_cast<MockGpio&>(getInstance());

EXPECT_CALL(mockgpio, init()).Times(1);

Led led;

EXPECT_EQ(0, led.init());
}


int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
::testing::GTEST_FLAG(throw_on_failure) = true; // Fail on unmet expectations
return RUN_ALL_TESTS();
}
15 changes: 15 additions & 0 deletions uut_cpp/include/led.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef LED_H
#define LED_H

#include <cstddef>
#include <cstdint>

class Led {
public:
// Public methods for LED functionality
int init();
void light_up();
int config();
};

#endif // LED_H
28 changes: 28 additions & 0 deletions uut_cpp/src/led.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iostream>
using namespace std;
#include "gpio_singleton.h"
#include "variadic_module.h"
#include "get_inline.h"
#include "led.h"

// Get the singleton instance of Gpio
Gpio& gpio = Gpio::getInstance();

int Led::init() {
cout << "Init" << endl;
return gpio.init(); // Use the singleton Gpio class for initialization
}

int Led::config() {
const int NAME_LEN = 10;
char name[NAME_LEN];

// Use the singleton Gpio class for configuration
int result = gpio.config(name, sizeof(name), 1, 2);

// Return 0 if name matches "MagicName", otherwise return -1
return (std::strcmp(name, "MagicName") == 0) ? 0 : -1;
}
Loading