• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2018. Huawei Technologies Co., Ltd. All rights reserved.
2 
3 #ifndef GTEST_INCLUDE_GTEST_GTEST_EXT_H_
4 #define GTEST_INCLUDE_GTEST_GTEST_EXT_H_
5 
6 #include <vector>
7 #include "gtest/gtest.h"
8 
9 namespace testing {
10   namespace ext {
11 
12     // helper macro to create unique variable name
13     #define GTEST_TEST_UNIQUE_ID_(test_case_name, test_name, file_line) \
14     test_case_name##_##test_name##_##file_line
15 
16     // 1. define a extended TEST supporting test flags registing
17     #if !GTEST_DONT_DEFINE_TEST
18     # define HWTEST(test_case_name, test_name, test_flags) \
19     bool GTEST_TEST_UNIQUE_ID_(test_case_name, test_name, __LINE__) = testing::ext::TestDefManager::instance()->regist(#test_case_name, #test_name, test_flags, testing::ext::Plain);\
20     TEST(test_case_name, test_name)
21     #endif
22 
23     // 2. define a extended TEST_F supporting test flags registing
24     # define HWTEST_F(test_case_name, test_name, test_flags) \
25     bool GTEST_TEST_UNIQUE_ID_(test_case_name, test_name, __LINE__) = testing::ext::TestDefManager::instance()->regist(#test_case_name, #test_name, test_flags, testing::ext::Fixtured);\
26     TEST_F(test_case_name, test_name)
27 
28     // 3. define a extended TYPED_TEST supporting test flags registing
29     # define HWTYPED_TEST(test_case_name, test_name, test_flags) \
30     bool GTEST_TEST_UNIQUE_ID_(test_case_name, test_name, __LINE__) = testing::ext::TestDefManager::instance()->regist(#test_case_name, #test_name, test_flags, testing::ext::Typed);\
31     TYPED_TEST(test_case_name, test_name)
32 
33     // 4. define a extended TYPED_TEST_P supporting test flags registing
34     # define HWTYPED_TEST_P(test_case_name, test_name, test_flags) \
35     bool GTEST_TEST_UNIQUE_ID_(test_case_name, test_name, __LINE__) = testing::ext::TestDefManager::instance()->regist(#test_case_name, #test_name, test_flags, testing::ext::PatternTyped);\
36     TYPED_TEST_P(test_case_name, test_name)
37 
38     // 5. define a extended TEST_P supporting test flags registing
39     # define HWTEST_P(test_case_name, test_name, test_flags) \
40     bool GTEST_TEST_UNIQUE_ID_(test_case_name, test_name, __LINE__) = testing::ext::TestDefManager::instance()->regist(#test_case_name, #test_name, test_flags, testing::ext::Parameterized);\
41     TEST_P(test_case_name, test_name)
42 
43     // test definition types
44     enum TestDefType {
45         Plain, Fixtured, Typed, PatternTyped, Parameterized
46     };
47     // case_name/test_name matchNaming pattern
48     enum NamingMatchType {
49         AEqualsB, AStartsWithB, AContainsB, AEndsWithB
50     };
51 
52     // information of a test difinition
53     class TestDefInfo {
54     public:
55         friend class TestDefManager;
56     private:
57         const static  char kNamingSepchar = '/';
58         const char* const test_case_name;
59         const char* const name;
60         const int flags;
61         const TestDefType def_type;
62         TestDefInfo(const char* tcn, const char* n, int fs, TestDefType tdf);
63     };
64 
65     class TestDefManager {
66     private:
TestDefManager()67         TestDefManager() {};
68         std::vector<const TestDefInfo*> testDefInfos;
69         const TestDefInfo* findDefFor(const TestInfo* test) const;
70         bool matchNaming(const char* const a, const char* const b, NamingMatchType mt) const;
71     public:
72         static TestDefManager* instance();
73         static const TestDefManager* cinstance();
74         bool regist(const char* test_case_name, const char* test_name, int test_flags, TestDefType tdf);
75         int queryFlagsFor(const TestInfo* test, int def_value) const;
76         int getLevel(const std::string testcasename, const std::string testname) const;
77         int* getTestFlags(const std::string testcasename, const std::string testname) const;
78     };
79 
80   } // namespace ext
81 } // namespace testing
82 
83 #endif  // GTEST_INCLUDE_GTEST_GTEST_EXT_H_