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

@@ -1,11 +1,10 @@
idf_build_get_property(target IDF_TARGET)
set(srcs "esp_timer_cxx.cpp" "esp_exception.cpp" "gpio_cxx.cpp" "spi_cxx.cpp" "spi_host_cxx.cpp")
set(srcs "esp_timer_cxx.cpp" "esp_exception.cpp" "gpio_cxx.cpp" "i2c_cxx.cpp" "spi_cxx.cpp" "spi_host_cxx.cpp")
set(requires "esp_timer" "driver")
if(NOT ${target} STREQUAL "linux")
list(APPEND srcs
"i2c_cxx.cpp"
"esp_event_api.cpp"
"esp_event_cxx.cpp")
list(APPEND requires "esp_event")

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

View File

@@ -1,16 +1,8 @@
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "unity.h"
#include "unity_cxx.hpp"
@@ -59,16 +51,6 @@ struct MasterFixture {
vector<uint8_t> data;
};
TEST_CASE("I2CMaster GPIO out of range", "[cxx i2c][leaks=300]")
{
TEST_THROW(I2CMaster(0, 255, 255, 400000), I2CException);
}
TEST_CASE("I2CMaster SDA and SCL equal", "[cxx i2c][leaks=300]")
{
TEST_THROW(I2CMaster(0, 0, 0, 400000), I2CException);
}
TEST_CASE("I2Transfer timeout", "[cxx i2c][leaks=300]")
{
std::vector<uint8_t> data = {MAGIC_TEST_NUMBER};