Inititial stub for cmock tests (#297)

This commit is contained in:
David Chalco 2020-09-24 17:06:28 -07:00 committed by GitHub
parent 1fc1bd4321
commit 190c9e780d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 907 additions and 0 deletions

View file

@ -0,0 +1,118 @@
# ============================= Queue unit tests ===========================
project( "queue" )
set(project_name "queue")
set(utest_name "${project_name}_utest")
set(utest_source "${project_name}_utest.c")
set(utest_yml "${project_name}.yml")
# ===================== Create your mock here (edit) ========================
# clear the original variables
set( mock_list "" )
set( mock_include_list "" )
set( mock_define_list "" )
set( real_source_files "" )
set( real_include_directories "" )
set( test_include_directories "" )
set( utest_link_list "" )
set( utest_dep_list "" )
set(mock_dir "wrapper_mocks")
# list of files to preprocess
list(APPEND preprocessed_mock_list
"${MODULE_ROOT_DIR}/FreeRTOS/Source/include/task.h"
#"${MODULE_ROOT_DIR}/FreeRTOS/Source/include/portable.h"
)
set(preprocess_commands "-I ${MODULE_ROOT_DIR}/FreeRTOS/Source/Include -I ${MODULE_ROOT_DIR}/FreeRTOS/Test/CMock/config" )
# list the files to mock here
list(APPEND mock_list
"${MODULE_ROOT_DIR}/FreeRTOS/Source/include/task.h"
"${MODULE_ROOT_DIR}/FreeRTOS/Source/include/list.h"
#"${MODULE_ROOT_DIR}/FreeRTOS/Source/include/portable.h"
)
# list the directories your mocks need
list(APPEND mock_include_list
"config"
"${MODULE_ROOT_DIR}/FreeRTOS/Source/include"
#"${CMAKE_CURRENT_LIST_DIR}/${mock_dir}"
)
#list the definitions of your mocks to control what to be included
list(APPEND mock_define_list
portUSING_MPU_WRAPPERS=0
)
# ================= Create the library under test here (edit) ==================
# list the files you would like to test here
list(APPEND real_source_files
"${MODULE_ROOT_DIR}/FreeRTOS/Source/queue.c"
)
# list the directories the module under test includes
list(APPEND real_include_directories
"config"
"${MODULE_ROOT_DIR}/FreeRTOS/Source/include"
#"${CMAKE_CURRENT_BINARY_DIR}/${mock_dir}"
)
# ===================== Create UnitTest Code here (edit) =====================
# list the directories your test needs to include
list(APPEND test_include_directories
"config"
"${MODULE_ROOT_DIR}/FreeRTOS/Source/include"
"${CMAKE_CURRENT_BINARY_DIR}/${mock_dir}"
)
# ============================= (end edit) ===================================
set(mock_name "${project_name}_mock")
set(real_name "${project_name}_real")
set(pre_mock_name "pre_${mock_name}")
create_mock_list("${mock_name}"
"${mock_list}"
"${CMAKE_CURRENT_LIST_DIR}/${utest_yml}"
"${mock_include_list}"
"${mock_define_list}"
"${mock_dir}"
)
separate_arguments(unix_flags UNIX_COMMAND "${preprocess_commands}")
message(STATUS "Unix Flags: ${unix_flags}")
#preprocess_mock_list(${mock_name}
# ${preprocessed_mock_list}
# "${unix_flags}"
# )
#add_dependencies(${mock_name} ${pre_mock_name} )
create_real_library(${real_name}
"${real_source_files}"
"${real_include_directories}"
"${mock_name}"
)
list(APPEND utest_link_list
-l${mock_name}
lib${real_name}.a
)
list(APPEND utest_dep_list
${real_name}
)
create_test(${utest_name}
"${CMAKE_CURRENT_LIST_DIR}/${utest_source}"
"${utest_link_list}"
"${utest_dep_list}"
"${test_include_directories}"
"${CMAKE_CURRENT_LIST_DIR}/${utest_yml}"
"${mock_dir}"
)

View file

@ -0,0 +1,33 @@
:cmock:
:mock_prefix: mock_
:mock_path: wrapper_mocks
:when_no_prototypes: :warn
:treat_externs: :include
:enforce_strict_ordering: TRUE
:plugins:
- :ignore
- :ignore_arg
- :expect_any_args
- :array
- :callback
- :return_thru_ptr
:callback_include_count: true # include a count arg when calling the callback
:callback_after_arg_check: false # check arguments before calling the callback
:treat_as:
uint8: HEX8
uint16: HEX16
uint32: UINT32
int8: INT8
bool: UINT8
:includes: # This will add these includes to each mock.
- <stdbool.h>
- "FreeRTOS.h"
:treat_externs: :exclude # Now the extern-ed functions will be mocked.
:weak: __attribute__((weak))
:verbosity: 3
:attributes:
- PRIVILEGED_FUNCTION
:strippables:
- PRIVILEGED_FUNCTION
- portDONT_DISCARD
:treat_externs: :include

View file

@ -0,0 +1,88 @@
/*
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* C runtime includes. */
#include <stdlib.h>
#include <stdbool.h>
/* Queue includes */
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "queue.h"
/* Test includes. */
#include "unity.h"
/* Mock includes. */
#include "mock_task.h"
/* ============================ GLOBAL VARIABLES =========================== */
static uint16_t usMallocFreeCalls = 0;
/* ========================== CALLBACK FUNCTIONS =========================== */
void * pvPortMalloc( size_t xSize )
{
return malloc(xSize);
}
void vPortFree( void * pv )
{
return free(pv);
}
/*******************************************************************************
* Unity fixtures
******************************************************************************/
void setUp( void )
{
}
/* called before each testcase */
void tearDown( void )
{
TEST_ASSERT_EQUAL_INT_MESSAGE( 0, usMallocFreeCalls,
"free is not called the same number of times as malloc,"
"you might have a memory leak!!" );
usMallocFreeCalls = 0;
}
/* called at the beginning of the whole suite */
void suiteSetUp()
{
}
/* called at the end of the whole suite */
int suiteTearDown( int numFailures )
{
return numFailures;
}
/*!
* @brief xQueueCreate happy path.
*
*/
void test_xQueueCreate_Success( void )
{
QueueHandle_t xQueue = xQueueCreate(1 , 1);
TEST_ASSERT_NOT_EQUAL( NULL, xQueue );
}