ContainerInfo: Move matcher regex construction to class ctor and add unit tests

This commit is contained in:
Alastair Robertson 2023-05-26 09:57:26 -07:00 committed by Alastair Robertson
parent a943f44b7a
commit a73cab758f
4 changed files with 56 additions and 4 deletions

View File

@ -172,6 +172,17 @@ const char* containerTypeEnumToStr(ContainerTypeEnum ty) {
});
}
namespace {
/*
* Create a regex to match a given type name.
*
* The type name "name" should match "name" and "name<xxx>".
*/
std::regex getMatcher(const std::string& typeName) {
return std::regex("^" + typeName + "$|^" + typeName + "<.*>$");
}
} // namespace
ContainerInfo::ContainerInfo(const fs::path& path) {
toml::table container;
try {
@ -197,6 +208,8 @@ ContainerInfo::ContainerInfo(const fs::path& path) {
throw std::runtime_error("`info.type_name` is a required field");
}
matcher = getMatcher(typeName);
if (std::optional<std::string> str = info["ctype"].value<std::string>()) {
ctype = containerTypeEnumFromStr(*str);
if (ctype == UNKNOWN_TYPE) {
@ -246,3 +259,12 @@ ContainerInfo::ContainerInfo(const fs::path& path) {
throw std::runtime_error("`codegen.decl` is a required field");
}
}
ContainerInfo::ContainerInfo(std::string typeName_,
ContainerTypeEnum ctype_,
std::string header_)
: typeName(std::move(typeName_)),
matcher(getMatcher(typeName)),
ctype(ctype_),
header(std::move(header_)) {
}

View File

@ -33,6 +33,11 @@ struct ContainerInfo {
};
explicit ContainerInfo(const std::filesystem::path& path); // Throws
ContainerInfo(std::string typeName,
ContainerTypeEnum ctype,
std::string header);
// Old ctors, remove with OICodeGen:
ContainerInfo() = default;
ContainerInfo(std::string typeName_,
std::regex matcher_,
@ -85,10 +90,6 @@ struct ContainerInfo {
bool operator<(const ContainerInfo& rhs) const {
return (typeName < rhs.typeName);
}
std::regex getMatcher() const {
return std::regex("^" + typeName + "<|^" + typeName + "$");
}
};
using ContainerInfoRefSet =

View File

@ -58,6 +58,12 @@ cpp_unittest(
DEPS oicore
)
cpp_unittest(
NAME test_container_info
SRCS test_container_info.cpp
DEPS oicore
)
# Integration tests
if (WITH_FLAKY_TESTS)
add_test(

View File

@ -0,0 +1,23 @@
#include <gtest/gtest.h>
#include "oi/ContainerInfo.h"
TEST(ContainerInfoTest, matcher) {
ContainerInfo info{"std::vector", SEQ_TYPE, "vector"};
EXPECT_TRUE(std::regex_search("std::vector<int>", info.matcher));
EXPECT_TRUE(std::regex_search("std::vector<std::list<int>>", info.matcher));
EXPECT_TRUE(std::regex_search("std::vector", info.matcher));
EXPECT_FALSE(std::regex_search("vector", info.matcher));
EXPECT_FALSE(std::regex_search("non_std::vector<int>", info.matcher));
EXPECT_FALSE(std::regex_search("std::vector_other<int>", info.matcher));
EXPECT_FALSE(std::regex_search("std::list<std::vector<int>>", info.matcher));
EXPECT_FALSE(std::regex_search("std::vector::value_type", info.matcher));
EXPECT_FALSE(std::regex_search("std::vector<int>::value_type", info.matcher));
EXPECT_FALSE(std::regex_search("std::vector<std::vector<int>>::value_type",
info.matcher));
// Uh-oh, here's a case that I don't think regexes are powerful enough to
// match: EXPECT_FALSE(std::regex_search("std::vector<int>::subtype<bool>",
// info.matcher));
}