2013-07-29 16:19:24 +08:00
|
|
|
#include "ClangTidyTest.h"
|
2014-06-18 17:33:46 +08:00
|
|
|
#include "google/ExplicitConstructorCheck.h"
|
2015-02-12 05:21:05 +08:00
|
|
|
#include "google/GlobalNamesInHeadersCheck.h"
|
2014-02-27 22:28:02 +08:00
|
|
|
#include "gtest/gtest.h"
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2015-03-05 21:46:14 +08:00
|
|
|
using namespace clang::tidy::google;
|
|
|
|
|
2013-07-29 16:19:24 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2014-02-27 22:28:02 +08:00
|
|
|
namespace test {
|
2013-07-29 16:19:24 +08:00
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
TEST(ExplicitConstructorCheckTest, SingleArgumentConstructorsOnly) {
|
|
|
|
EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(); };");
|
|
|
|
EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(int i, int j); };");
|
2014-03-20 17:39:36 +08:00
|
|
|
EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(const C&); };");
|
|
|
|
EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(C&&); };");
|
2014-03-19 20:48:22 +08:00
|
|
|
EXPECT_NO_CHANGES(ExplicitConstructorCheck,
|
|
|
|
"class C { C(const C&) = delete; };");
|
2014-03-20 17:39:36 +08:00
|
|
|
EXPECT_NO_CHANGES(ExplicitConstructorCheck,
|
|
|
|
"class C { C(int) = delete; };");
|
2013-07-29 16:19:24 +08:00
|
|
|
}
|
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
TEST(ExplicitConstructorCheckTest, Basic) {
|
2013-07-29 16:19:24 +08:00
|
|
|
EXPECT_EQ("class C { explicit C(int i); };",
|
2014-02-27 22:28:02 +08:00
|
|
|
runCheckOnCode<ExplicitConstructorCheck>("class C { C(int i); };"));
|
2013-07-29 16:19:24 +08:00
|
|
|
}
|
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
TEST(ExplicitConstructorCheckTest, DefaultParameters) {
|
2013-07-29 16:19:24 +08:00
|
|
|
EXPECT_EQ("class C { explicit C(int i, int j = 0); };",
|
2014-02-27 22:28:02 +08:00
|
|
|
runCheckOnCode<ExplicitConstructorCheck>(
|
|
|
|
"class C { C(int i, int j = 0); };"));
|
2013-07-29 16:19:24 +08:00
|
|
|
}
|
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
TEST(ExplicitConstructorCheckTest, OutOfLineDefinitions) {
|
2014-02-13 18:11:48 +08:00
|
|
|
EXPECT_EQ("class C { explicit C(int i); }; C::C(int i) {}",
|
2014-02-27 22:28:02 +08:00
|
|
|
runCheckOnCode<ExplicitConstructorCheck>(
|
|
|
|
"class C { C(int i); }; C::C(int i) {}"));
|
2014-02-13 18:11:48 +08:00
|
|
|
}
|
|
|
|
|
2014-04-29 23:07:08 +08:00
|
|
|
TEST(ExplicitConstructorCheckTest, RemoveExplicit) {
|
|
|
|
EXPECT_EQ("class A { A(const A&); };\n"
|
2014-04-29 23:50:14 +08:00
|
|
|
"class B { /*asdf*/ B(B&&); };\n"
|
|
|
|
"class C { /*asdf*/ C(const C&, int i = 0); };",
|
2014-04-29 23:07:08 +08:00
|
|
|
runCheckOnCode<ExplicitConstructorCheck>(
|
|
|
|
"class A { explicit A(const A&); };\n"
|
2014-04-29 23:50:14 +08:00
|
|
|
"class B { explicit /*asdf*/ B(B&&); };\n"
|
|
|
|
"class C { explicit/*asdf*/ C(const C&, int i = 0); };"));
|
2014-04-29 23:07:08 +08:00
|
|
|
}
|
|
|
|
|
2014-05-06 02:49:31 +08:00
|
|
|
TEST(ExplicitConstructorCheckTest, RemoveExplicitWithMacros) {
|
|
|
|
EXPECT_EQ(
|
|
|
|
"#define A(T) class T##Bar { explicit T##Bar(const T##Bar &b) {} };\n"
|
|
|
|
"A(Foo);",
|
|
|
|
runCheckOnCode<ExplicitConstructorCheck>(
|
|
|
|
"#define A(T) class T##Bar { explicit T##Bar(const T##Bar &b) {} };\n"
|
|
|
|
"A(Foo);"));
|
|
|
|
}
|
|
|
|
|
2015-02-12 05:21:05 +08:00
|
|
|
class GlobalNamesInHeadersCheckTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
bool runCheckOnCode(const std::string &Code, const std::string &Filename) {
|
|
|
|
static const char *const Header = "namespace std {\n"
|
|
|
|
"class string {};\n"
|
|
|
|
"} // namespace std\n"
|
|
|
|
"\n"
|
|
|
|
"#define SOME_MACRO(x) using x\n";
|
|
|
|
std::vector<ClangTidyError> Errors;
|
|
|
|
std::vector<std::string> Args;
|
|
|
|
if (!StringRef(Filename).endswith(".cpp")) {
|
|
|
|
Args.emplace_back("-xc++-header");
|
|
|
|
}
|
|
|
|
test::runCheckOnCode<readability::GlobalNamesInHeadersCheck>(
|
|
|
|
Header + Code, &Errors, Filename, Args);
|
|
|
|
if (Errors.empty())
|
|
|
|
return false;
|
|
|
|
assert(Errors.size() == 1);
|
|
|
|
assert(
|
|
|
|
Errors[0].Message.Message ==
|
|
|
|
"using declarations in the global namespace in headers are prohibited");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(GlobalNamesInHeadersCheckTest, UsingDeclarations) {
|
|
|
|
EXPECT_TRUE(runCheckOnCode("using std::string;", "foo.h"));
|
|
|
|
EXPECT_FALSE(runCheckOnCode("using std::string;", "foo.cpp"));
|
|
|
|
EXPECT_FALSE(runCheckOnCode("namespace my_namespace {\n"
|
|
|
|
"using std::string;\n"
|
|
|
|
"} // my_namespace\n",
|
|
|
|
"foo.h"));
|
|
|
|
EXPECT_FALSE(runCheckOnCode("SOME_MACRO(std::string);", "foo.h"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GlobalNamesInHeadersCheckTest, UsingDirectives) {
|
|
|
|
EXPECT_TRUE(runCheckOnCode("using namespace std;", "foo.h"));
|
|
|
|
EXPECT_FALSE(runCheckOnCode("using namespace std;", "foo.cpp"));
|
|
|
|
EXPECT_FALSE(runCheckOnCode("namespace my_namespace {\n"
|
|
|
|
"using namespace std;\n"
|
|
|
|
"} // my_namespace\n",
|
|
|
|
"foo.h"));
|
|
|
|
EXPECT_FALSE(runCheckOnCode("SOME_MACRO(namespace std);", "foo.h"));
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:21:45 +08:00
|
|
|
TEST_F(GlobalNamesInHeadersCheckTest, RegressionAnonymousNamespace) {
|
|
|
|
EXPECT_FALSE(runCheckOnCode("namespace {}", "foo.h"));
|
|
|
|
}
|
|
|
|
|
2014-02-27 22:28:02 +08:00
|
|
|
} // namespace test
|
2013-07-29 16:19:24 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|