Skip to content

Commit c5cdf0e

Browse files
committed
Add GoogleTest stub and test cases
1 parent db37944 commit c5cdf0e

File tree

6 files changed

+81
-3
lines changed

6 files changed

+81
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
| test.cpp:71:5:71:16 | ANestedClass | Special member function ANestedClass is never called. |
22
| test.cpp:82:5:82:22 | AnotherNestedClass | Special member function AnotherNestedClass is never called from a main function or entry point. |
3+
| test.cpp:155:1:157:37 | ~sample_test_called_from_google_test_function_Test | Special member function sample_test_called_from_google_test_function_Test::~sample_test_called_from_google_test_function_Test is never called. |

cpp/autosar/test/rules/M0-1-10/test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,22 @@ class M {
142142
public:
143143
M(const M &) = delete; // COMPLIANT - ignore if deleted
144144
};
145+
146+
#include <gtest/gtest.h>
147+
int called_from_google_test_function(
148+
int a_param) // COMPLIANT - called from TEST
149+
{
150+
int something = a_param;
151+
something++;
152+
return something;
153+
}
154+
155+
TEST(
156+
sample_test,
157+
called_from_google_test_function) // COMPLIANT - False positive!
158+
// ~sample_test_called_from_google_test_function_Test
159+
{
160+
bool pass = false;
161+
if (called_from_google_test_function(0) >= 10)
162+
pass = true;
163+
}

cpp/common/src/codingstandards/cpp/EncapsulatingFunctions.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ class GoogleTestFunction extends MainLikeFunction {
3939
base.getNamespace().hasName("testing")
4040
)
4141
or
42-
// or at a location in a file called "gtest.h".
43-
base.getDefinitionLocation().getFile().getBaseName() = "gtest.h"
42+
// or at a location in a file called gtest.h (or gtest-internal.h,
43+
// gtest-typed-test.h etc).
44+
base.getDefinitionLocation().getFile().getBaseName().regexpMatch("gtest*.h")
4445
)
4546
}
4647
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
2+
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
3+
4+
#define GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
5+
test_suite_name##_##test_name##_Test
6+
7+
#define GTEST_TEST_(test_suite_name, test_name, parent_class) \
8+
class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
9+
: public parent_class { \
10+
public: \
11+
GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() = default; \
12+
~GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() override = default; \
13+
GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
14+
(const GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) &) = delete; \
15+
GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) & operator=( \
16+
const GTEST_TEST_CLASS_NAME_(test_suite_name, \
17+
test_name) &) = delete; /* NOLINT */ \
18+
GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
19+
(GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) &&) noexcept = delete; \
20+
GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) & operator=( \
21+
GTEST_TEST_CLASS_NAME_(test_suite_name, \
22+
test_name) &&) noexcept = delete; /* NOLINT */ \
23+
\
24+
private: \
25+
void TestBody() override; \
26+
}; \
27+
void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody() \
28+
29+
#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_
2+
#define GOOGLETEST_INCLUDE_GTEST_GTEST_H_
3+
4+
#include "gtest/gtest-internal.h"
5+
6+
namespace testing {
7+
8+
class Test
9+
{
10+
public:
11+
virtual ~Test();
12+
protected:
13+
// Creates a Test object.
14+
Test();
15+
private:
16+
virtual void TestBody() = 0;
17+
Test(const Test&) = delete;
18+
Test& operator=(const Test&) = delete;
19+
};
20+
21+
#define GTEST_TEST(test_suite_name, test_name) \
22+
GTEST_TEST_(test_suite_name, test_name, ::testing::Test)
23+
24+
#define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name)
25+
26+
} // namespace testing
27+
28+
#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_H_

cpp/options

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
semmle-extractor-options:--clang -std=c++14 -nostdinc++ -I../../../../common/test/includes/standard-library -I../../../../common/test/includes/custom-library
1+
semmle-extractor-options:--clang -std=c++14 -nostdinc++ -I../../../../common/test/includes/standard-library -I../../../../common/test/includes/custom-library

0 commit comments

Comments
 (0)