summaryrefslogtreecommitdiff
path: root/src/mongo/unittest
diff options
context:
space:
mode:
authorBilly Donahue <billy.donahue@mongodb.com>2020-01-06 22:26:02 +0000
committerevergreen <evergreen@mongodb.com>2020-01-06 22:26:02 +0000
commit4d662310ee40288ee63f0bc761ab49539a86fe9b (patch)
treecf972008cb26f273e2afde0c79a191cde67cd56e /src/mongo/unittest
parent12d44ec1d9a03ed3d0828096bb32de925235c447 (diff)
downloadmongo-4d662310ee40288ee63f0bc761ab49539a86fe9b.tar.gz
SERVER-43371 StringData for unit test registration
Diffstat (limited to 'src/mongo/unittest')
-rw-r--r--src/mongo/unittest/unittest.cpp13
-rw-r--r--src/mongo/unittest/unittest.h33
2 files changed, 28 insertions, 18 deletions
diff --git a/src/mongo/unittest/unittest.cpp b/src/mongo/unittest/unittest.cpp
index 03c00dfd75f..279b2d07c07 100644
--- a/src/mongo/unittest/unittest.cpp
+++ b/src/mongo/unittest/unittest.cpp
@@ -67,8 +67,9 @@ logger::MessageLogDomain* unittestOutput() {
return p;
}
+/** Each map key is owned by its corresponding Suite object. */
auto& suitesMap() {
- static std::map<std::string, std::shared_ptr<Suite>> m;
+ static std::map<StringData, std::shared_ptr<Suite>> m;
return m;
}
@@ -370,7 +371,7 @@ int Suite::run(const std::vector<std::string>& suites,
if (torun.empty()) {
for (const auto& kv : suitesMap()) {
- torun.push_back(kv.first);
+ torun.push_back(std::string{kv.first});
}
}
@@ -433,13 +434,13 @@ int Suite::run(const std::vector<std::string>& suites,
return rc;
}
-Suite& Suite::getSuite(const std::string& name) {
+Suite& Suite::getSuite(StringData name) {
auto& map = suitesMap();
if (auto found = map.find(name); found != map.end()) {
return *found->second;
}
- auto sp = std::make_shared<Suite>(ConstructorEnable{}, name);
- auto [it, noCollision] = map.try_emplace(name, sp->shared_from_this());
+ auto sp = std::make_shared<Suite>(ConstructorEnable{}, std::string{name});
+ auto [it, noCollision] = map.try_emplace(sp->key(), sp->shared_from_this());
fassert(10162, noCollision);
return *sp;
}
@@ -496,7 +497,7 @@ std::ostream& TestAssertionFailure::stream() {
std::vector<std::string> getAllSuiteNames() {
std::vector<std::string> result;
for (const auto& kv : suitesMap()) {
- result.push_back(kv.first);
+ result.push_back(std::string{kv.first});
}
return result;
}
diff --git a/src/mongo/unittest/unittest.h b/src/mongo/unittest/unittest.h
index ef53b66412c..ebd63872978 100644
--- a/src/mongo/unittest/unittest.h
+++ b/src/mongo/unittest/unittest.h
@@ -382,9 +382,14 @@ public:
*
* Safe to call during static initialization.
*/
- static Suite& getSuite(const std::string& name);
+ static Suite& getSuite(StringData name);
private:
+ /** Points to the string data of the _name field. */
+ StringData key() const {
+ return _name;
+ }
+
std::string _name;
std::vector<SuiteTest> _tests;
};
@@ -510,29 +515,33 @@ protected:
template <typename T>
class RegistrationAgent {
public:
- RegistrationAgent(std::string suiteName, std::string testName, std::string fileName)
- : _suiteName(std::move(suiteName)),
- _testName(std::move(testName)),
- _fileName(std::move(fileName)) {
- Suite::getSuite(_suiteName).add(_testName, _fileName, [] { T{}.run(); });
+ /**
+ * These StringData must point to data that outlives this RegistrationAgent.
+ * In the case of TEST/TEST_F, these are string literals.
+ */
+ RegistrationAgent(StringData suiteName, StringData testName, StringData fileName)
+ : _suiteName{suiteName}, _testName{testName}, _fileName{fileName} {
+ Suite::getSuite(_suiteName).add(std::string{_testName}, std::string{_fileName}, [] {
+ T{}.run();
+ });
}
- const std::string& getSuiteName() const {
+ StringData getSuiteName() const {
return _suiteName;
}
- const std::string& getTestName() const {
+ StringData getTestName() const {
return _testName;
}
- const std::string& getFileName() const {
+ StringData getFileName() const {
return _fileName;
}
private:
- std::string _suiteName;
- std::string _testName;
- std::string _fileName;
+ StringData _suiteName;
+ StringData _testName;
+ StringData _fileName;
};
/**