Skip to content

Commit 12fc8ed

Browse files
committed
tests: unity: Separate example test and wrap tests
The python scripts to prefix functions with __wrap was tested using the example_test. This test is mean as an example of how to do unit testing. Moved the existing wrap tests and some new ones to a new test suite to avoid adding more irrelevant tests to the example test. Signed-off-by: Gregers Gram Rygg <[email protected]>
1 parent d3fdbd0 commit 12fc8ed

File tree

16 files changed

+301
-55
lines changed

16 files changed

+301
-55
lines changed

scripts/unity/func_name_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def func_names_from_header(in_file, out_file):
1414

1515
with open(out_file, 'w') as f_out:
1616
# Regex match all function names in the header file
17+
# Tests for validating the regex in tests/unity/wrap
1718
x = re.findall(r"^\s*(?:\w+[*\s]+)+(\w+?)\s*\([\w\s,*\.\[\]]*?\)\s*;",
1819
content, re.M | re.S)
1920
for item in x:

scripts/unity/header_prepare.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import argparse
99

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

1112
def header_prepare(in_file, out_file, out_wrap_file):
1213
with open(in_file) as f_in:

tests/unity/example_test/src/example_test.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ void test_uut_init(void)
3030
int err;
3131

3232
__wrap_foo_init_ExpectAndReturn(NULL, 0);
33-
__wrap_foo_syscall_Expect();
3433
__wrap_foo_execute_ExpectAndReturn(0);
35-
__wrap_foo_execute2_ExpectAndReturn(0);
36-
__wrap_foo_execute3_ExpectAndReturn(0);
3734

3835
err = uut_init(NULL);
3936
TEST_ASSERT_EQUAL(0, err);

tests/unity/example_test/src/foo/foo.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ int foo_init(void *handle)
1111
/* This implementation will be wrapped and mocked. */
1212
return 0;
1313
}
14+
15+
int foo_execute(void)
16+
{
17+
return 0;
18+
}

tests/unity/example_test/src/foo/foo.h

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,8 @@
99

1010
#include <toolchain/common.h>
1111

12-
#include "foo_internal.h"
13-
// c++ comment
14-
/* c comment */
15-
/* c
16-
* multi line comment
17-
*/
18-
#define FOO_ADDR "https://my.page.com/somewhere"
19-
2012
int foo_init(void *handle);
2113

22-
static const char test_string[] = "test string";
23-
24-
#define TEST_MACRO(xx) do { \
25-
const STRUCT_SECTION_ITERABLE(settings_handler_static, \
26-
name) \
27-
} while (0)
28-
29-
static inline int foo_execute(void)
30-
{
31-
foo_t val = 0;
32-
33-
return (int)val;
34-
}
35-
36-
static ALWAYS_INLINE int foo_execute2(void)
37-
{
38-
return 0;
39-
}
40-
41-
inline static int foo_execute3(void)
42-
{
43-
return 0;
44-
}
45-
46-
__syscall void foo_syscall(void);
47-
48-
static inline void z_impl_foo_syscall(void)
49-
{
50-
/* empty */
51-
}
14+
int foo_execute(void);
5215

5316
#endif /* __FOO_H */

tests/unity/example_test/src/foo/foo_internal.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/unity/example_test/src/uut/uut.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,5 @@ int uut_init(void *handle)
2424
return err;
2525
}
2626

27-
foo_syscall();
28-
29-
return foo_execute() + foo_execute2() + foo_execute3();
27+
return foo_execute();
3028
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# Copyright (c) 2022 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
project(wrap_test)
11+
12+
# generate runner for the test
13+
test_runner_generate(src/wrap_test.c)
14+
15+
# create mocks for test_code functions
16+
cmock_handle(src/test_code/test_code.h test_code)
17+
18+
# add module test_code
19+
target_sources(app PRIVATE src/test_code/test_code.c)
20+
target_include_directories(app PRIVATE src)
21+
22+
# add module call
23+
target_sources(app PRIVATE src/call/call.c)
24+
target_include_directories(app PRIVATE ./src/call)
25+
26+
# add test file
27+
target_sources(app PRIVATE src/wrap_test.c)
28+
target_include_directories(app PRIVATE .)
29+
target_include_directories(app PRIVATE src/test_code)

tests/unity/wrap_test/prj.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# Copyright (c) 2022 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
CONFIG_UNITY=y
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
#include <sys/util.h>
7+
#include <stddef.h>
8+
9+
#include "call.h"
10+
#include "test_code/test_code.h"
11+
12+
void call_syscall(void)
13+
{
14+
syscall();
15+
}
16+
17+
int call_static_inline_fn(void)
18+
{
19+
return static_inline_fn();
20+
}
21+
22+
int call_inline_static_fn(void)
23+
{
24+
return inline_static_fn();
25+
}
26+
27+
int call_always_inline_fn(void)
28+
{
29+
return always_inline_fn();
30+
}
31+
32+
int call_after_macro(void)
33+
{
34+
return after_macro();
35+
}
36+
37+
int call_extra_whitespace(void)
38+
{
39+
return extra_whitespace();
40+
}
41+
42+
int call_multiline_def(int arg, int len)
43+
{
44+
return multiline_def(arg, len);
45+
}

0 commit comments

Comments
 (0)