Skip to content
Merged
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
3 changes: 2 additions & 1 deletion scripts/unity/func_name_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def func_names_from_header(in_file, out_file):

with open(out_file, 'w') as f_out:
# Regex match all function names in the header file
x = re.findall(r"^\s*(?:\w+[*\s]+)+(\w+?)\s*\([^\\]*?\)\s*;",
# Tests for validating the regex in tests/unity/wrap
x = re.findall(r"^\s*(?:\w+[*\s]+)+(\w+?)\s*\([\w\s,*\.\[\]]*?\)\s*;",
content, re.M | re.S)
for item in x:
f_out.write(item + "\n")
Expand Down
3 changes: 2 additions & 1 deletion scripts/unity/header_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import argparse

# Tests for validating the regexes are located tests/unity/wrap

def header_prepare(in_file, out_file, out_wrap_file):
with open(in_file) as f_in:
Expand Down Expand Up @@ -63,7 +64,7 @@ def header_prepare(in_file, out_file, out_wrap_file):
# Prepare file with functions prefixed with __wrap_ that will be used for
# mock generation.
func_pattern = re.compile(
r"^\s*((?:\w+[*\s]+)+)(\w+?\s*\([^\\{}#]*?\)\s*;)", re.M)
r"^\s*((?:\w+[*\s]+)+)(\w+?\s*\([\w\s,*\.\[\]]*?\)\s*;)", re.M)
content2 = func_pattern.sub(r"\n\1__wrap_\2", content)

with open(out_wrap_file, 'w') as f_wrap:
Expand Down
3 changes: 0 additions & 3 deletions tests/unity/example_test/src/example_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ void test_uut_init(void)
int err;

__wrap_foo_init_ExpectAndReturn(NULL, 0);
__wrap_foo_syscall_Expect();
__wrap_foo_execute_ExpectAndReturn(0);
__wrap_foo_execute2_ExpectAndReturn(0);
__wrap_foo_execute3_ExpectAndReturn(0);

err = uut_init(NULL);
TEST_ASSERT_EQUAL(0, err);
Expand Down
5 changes: 5 additions & 0 deletions tests/unity/example_test/src/foo/foo.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ int foo_init(void *handle)
/* This implementation will be wrapped and mocked. */
return 0;
}

int foo_execute(void)
{
return 0;
}
39 changes: 1 addition & 38 deletions tests/unity/example_test/src/foo/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,8 @@

#include <toolchain/common.h>

#include "foo_internal.h"
// c++ comment
/* c comment */
/* c
* multi line comment
*/
#define FOO_ADDR "https://my.page.com/somewhere"

int foo_init(void *handle);

static const char test_string[] = "test string";

#define TEST_MACRO(xx) do { \
const STRUCT_SECTION_ITERABLE(settings_handler_static, \
name) \
} while (0)

static inline int foo_execute(void)
{
foo_t val = 0;

return (int)val;
}

static ALWAYS_INLINE int foo_execute2(void)
{
return 0;
}

inline static int foo_execute3(void)
{
return 0;
}

__syscall void foo_syscall(void);

static inline void z_impl_foo_syscall(void)
{
/* empty */
}
int foo_execute(void);

#endif /* __FOO_H */
11 changes: 0 additions & 11 deletions tests/unity/example_test/src/foo/foo_internal.h

This file was deleted.

4 changes: 1 addition & 3 deletions tests/unity/example_test/src/uut/uut.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@ int uut_init(void *handle)
return err;
}

foo_syscall();

return foo_execute() + foo_execute2() + foo_execute3();
return foo_execute();
}
29 changes: 29 additions & 0 deletions tests/unity/wrap_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Copyright (c) 2022 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(wrap_test)

# generate runner for the test
test_runner_generate(src/wrap_test.c)

# create mocks for test_code functions
cmock_handle(src/test_code/test_code.h test_code)

# add module test_code
target_sources(app PRIVATE src/test_code/test_code.c)
target_include_directories(app PRIVATE src)

# add module call
target_sources(app PRIVATE src/call/call.c)
target_include_directories(app PRIVATE ./src/call)

# add test file
target_sources(app PRIVATE src/wrap_test.c)
target_include_directories(app PRIVATE .)
target_include_directories(app PRIVATE src/test_code)
6 changes: 6 additions & 0 deletions tests/unity/wrap_test/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# Copyright (c) 2022 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
CONFIG_UNITY=y
45 changes: 45 additions & 0 deletions tests/unity/wrap_test/src/call/call.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <sys/util.h>
#include <stddef.h>

#include "call.h"
#include "test_code/test_code.h"

void call_syscall(void)
{
syscall();
}

int call_static_inline_fn(void)
{
return static_inline_fn();
}

int call_inline_static_fn(void)
{
return inline_static_fn();
}

int call_always_inline_fn(void)
{
return always_inline_fn();
}

int call_after_macro(void)
{
return after_macro();
}

int call_extra_whitespace(void)
{
return extra_whitespace();
}

int call_multiline_def(int arg, int len)
{
return multiline_def(arg, len);
}
17 changes: 17 additions & 0 deletions tests/unity/wrap_test/src/call/call.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#ifndef __CALL_H
#define __CALL_H

void call_syscall(void);
int call_static_inline_fn(void);
int call_inline_static_fn(void);
int call_always_inline_fn(void);
int call_after_macro(void);
int call_extra_whitespace(void);
int call_multiline_def(int arg, int len);

#endif /* __CALL_H */
3 changes: 3 additions & 0 deletions tests/unity/wrap_test/src/test_code/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"DisableFormat": true
}
7 changes: 7 additions & 0 deletions tests/unity/wrap_test/src/test_code/test_code.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include "test_code/test_code.h"
89 changes: 89 additions & 0 deletions tests/unity/wrap_test/src/test_code/test_code.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

/*
* DO NOT FORMAT THIS FILE!
*
* It contains various code blocks that is used to validate the test wrapper
* scripts (func_name_list.py and header_parse.py) which renames function
* declarations. The regular expression has had several bugs in the past, and
* the formating of the code here is used to validate that the fixes still work
* when modifying the regex.
*
* The code in this file ensure that the test wrapper scripts can handle the
* situations described in following list of issues:
* https://github.com/nrfconnect/sdk-nrf/pull/1413
* https://github.com/nrfconnect/sdk-nrf/pull/2182
* https://github.com/nrfconnect/sdk-nrf/pull/3432
* https://github.com/nrfconnect/sdk-nrf/pull/5069
* https://github.com/nrfconnect/sdk-nrf/pull/5814
* https://github.com/nrfconnect/sdk-nrf/pull/6478
* https://github.com/nrfconnect/sdk-nrf/pull/6547
*/

#ifndef __TEST_CODE_H
#define __TEST_CODE_H

// c++ comment
/* c comment */
/* c
* multi line comment
*/
#define SOME_ADDR "https://my.page.com/somewhere"

static const char test_string[] = "test string";

#define TEST_MACRO(xx) do { \
const STRUCT_SECTION_ITERABLE(settings_handler_static, \
name) \
} while (0)

static inline int inline_static_fn(void)
{
return 0;
}

inline static int static_inline_fn(void)
{
return 0;
}

static ALWAYS_INLINE int always_inline_fn(void)
{
return 0;
}

__syscall void syscall(void);

static inline void z_impl_syscall(void)
{
/* empty */
}

/* macro function that was parsed incorrectly (NCSDK-10918) */
#define SETTINGS_STATIC_HANDLER_DEFINE(_hname, _tree, _get, _set, _commit, \
_export) \
const Z_STRUCT_SECTION_ITERABLE(settings_handler_static, \
settings_handler_ ## _hname) = { \
.name = _tree, \
.h_get = _get, \
.h_set = _set, \
.h_commit = _commit, \
.h_export = _export, \
}
int after_macro(void);

int extra_whitespace (void) ;

/* typedef that should not be parsed as a function */
typedef struct __attribute__ ((__packed__)) {
} bar_t;

/* newline between arguments */
int multiline_def(int arg,
int len);

#endif /* __TEST_CODE_H */
Loading