feat (driver)!: Added mocking for i2c.h

BREAKING CHANGE: removed ringbuf.h from public i2c.h
This commit is contained in:
Jakob Hasse
2021-10-22 10:35:46 +08:00
parent 13ab2cd9f9
commit 1eb96cfc79
13 changed files with 173 additions and 28 deletions

View File

@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main)
idf_build_set_property(COMPILE_DEFINITIONS "-DNO_DEBUG_STORAGE" APPEND)
# Overriding components which should be mocked
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/driver/")
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/esp_timer/")
# Including experimental component here because it's outside IDF's main component directory
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/examples/cxx/experimental/experimental_cpp_component/")
project(test_i2c_cxx_host)

View File

@@ -0,0 +1,8 @@
| Supported Targets | Linux |
| ----------------- | ----- |
# Build
`idf.py build` (sdkconfig.defaults sets the linux target by default)
# Run
`build/host_i2c_cxx_test.elf`

View File

@@ -0,0 +1,9 @@
idf_component_get_property(cpp_component experimental_cpp_component COMPONENT_DIR)
idf_component_register(SRCS "i2c_cxx_test.cpp"
INCLUDE_DIRS
"."
"${cpp_component}/host_test/fixtures"
"${cpp_component}/private_include"
$ENV{IDF_PATH}/tools/catch
REQUIRES cmock driver experimental_cpp_component)

View File

@@ -0,0 +1,56 @@
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0
*
* I2C C++ unit tests
*
* This example code is in the Public Domain (or CC0 licensed, at your option.)
*
* Unless required by applicable law or agreed to in writing, this
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
*/
#define CATCH_CONFIG_MAIN
#include <stdio.h>
#include "unity.h"
#include "freertos/portmacro.h"
#include "i2c_cxx.hpp"
#include "system_cxx.hpp"
#include "test_fixtures.hpp"
#include "catch.hpp"
extern "C" {
#include "Mocki2c.h"
}
// TODO: IDF-2693, function definition just to satisfy linker, mock esp_common instead
const char *esp_err_to_name(esp_err_t code) {
return "host_test error";
}
using namespace std;
using namespace idf;
TEST_CASE("I2CBus")
{
I2CBus bus(static_cast<i2c_port_t>(0));
}
TEST_CASE("I2CMaster parameter configuration fails")
{
CMockFixture fix;
i2c_param_config_ExpectAnyArgsAndReturn(ESP_FAIL);
CHECK_THROWS_AS(I2CMaster(0, 1, 2, 400000), I2CException&);
}
TEST_CASE("I2CMaster driver install failure")
{
CMockFixture fix;
i2c_param_config_ExpectAnyArgsAndReturn(ESP_OK);
i2c_driver_install_ExpectAnyArgsAndReturn(ESP_FAIL);
CHECK_THROWS_AS(I2CMaster(0, 1, 2, 400000), I2CException&);
}

View File

@@ -0,0 +1,3 @@
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n
CONFIG_IDF_TARGET="linux"
CONFIG_CXX_EXCEPTIONS=y