1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HCTEST_H
17 #define HCTEST_H
18 #ifndef HCTEST_REPEAT_TIMES
19 #define HCTEST_REPEAT_TIMES 1
20 #endif
21 #include <ohos_init.h>
22 #include "unity.h"
23 #include "hctest_internal.h"
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 #define CONST_EMPTY_STRING ""
32 #define CONST_STRING_SPACE " "
33 #define CONST_DOT_STRING ","
34 
35 #define TEST_INIT(func) LAYER_INITCALL_DEF(func, test, "test")
36 #define TEST_INIT_PRI(func, priority) LAYER_INITCALL(func, test, "test", priority)
37 typedef struct TestSuiteManager {
38     /**
39      * @brief get the test suite by suite name
40      * @param the test suite name
41      * @return the TestSuite point
42      * */
43     CTestSuite *(*GetTestSuite)(const char *test_suite);
44     BOOL (*RegisterTestSuite)(CTestSuite *testSuite);
45 
46     /**
47      * @brief remove the test suite.
48      * @param the test case addr.
49      * @return TRUE success
50      * */
51     BOOL (*RemoveTestSuite)(CTestSuite *testSuite);
52 
53     /**
54      * @brief remove the test suite.
55      * @param the test case addr.
56      * @return TRUE success
57      * */
58     void (*AddTestCase)(CTestCase *testCase);
59 
60     /**
61      * @brief remove special test suite.
62      * @param caseName
63      */
64     void (*RunSpecialTestSuite)(
65         const char *subSystemName, const char *moduleName, const char *suiteName, const char *caseName, int caseLevel);
66 
67     void (*RunTestSuite)(const char *suite_name);
68 
69     Vector test_suites;
70 } TestSuiteManager;
71 TestSuiteManager *GetTestMgrInstance(void);
72 
73 #define LITE_TEST_SUIT(subsystem, module, test_suite)                  \
74     static CTestSuite suite_object##test_suite;                        \
75     static void initSuite##test_suite(void)                            \
76     {                                                                  \
77         suite_object##test_suite.subsystem_name = #subsystem;          \
78         suite_object##test_suite.module_name = #module;                \
79         suite_object##test_suite.suite_name = #test_suite;             \
80         suite_object##test_suite.file = __FILE__;                      \
81         suite_object##test_suite.times = HCTEST_REPEAT_TIMES;          \
82         suite_object##test_suite.test_cases = VECTOR_Make(NULL, NULL); \
83         TestSuiteManager *testMgr = GetTestMgrInstance();              \
84         testMgr->RegisterTestSuite(&(suite_object##test_suite));       \
85     }                                                                  \
86     SYS_SERVICE_INIT(initSuite##test_suite);
87 
88 #define LITE_TEST_CASE(test_suite, case_object, test_flag)        \
89     static void case_object##_runTest(void);                      \
90     static CTestCase create##case_object;                         \
91     static void initCase##case_object(void)                       \
92     {                                                             \
93         create##case_object.suite_name = #test_suite;             \
94         create##case_object.case_name = #case_object;             \
95         create##case_object.flag = test_flag;                     \
96         create##case_object.line_num = __LINE__;                  \
97         create##case_object.lite_setup = test_suite##SetUp;       \
98         create##case_object.lite_teardown = test_suite##TearDown; \
99         create##case_object.execute_func = case_object##_runTest; \
100         TestSuiteManager *testMgr = GetTestMgrInstance();         \
101         testMgr->AddTestCase(&(create##case_object));             \
102     }                                                             \
103     SYS_RUN(initCase##case_object);                               \
104     static void case_object##_runTest(void)
105 
106 #define RUN_TEST_SUITE(test_suite)                        \
107     static void runSuite##test_suite(void)                \
108     {                                                     \
109         TestSuiteManager *testMgr = GetTestMgrInstance(); \
110         testMgr->RunTestSuite(#test_suite);               \
111     }                                                     \
112     TEST_INIT(runSuite##test_suite);
113 
114 void LiteTestPrint(const char *fmt, ...);
115 
116 #ifdef __cplusplus
117 #if __cplusplus
118 }
119 #endif
120 #endif
121 
122 #endif /* HCTEST_H */