Compare commits

...

3 Commits

Author SHA1 Message Date
Manikandan K S
4f2473cbe0
Merge 653aec68afdcadaae9913d7baf79f738fac538d2 into a0f06a70e3da7afa88da9527c43951bca1f7cef2 2026-07-29 15:06:03 -04:00
Abseil Team
a0f06a70e3 Make GoogleTest handle std::monostate and valueless_by_exception() for std::variant similarly to how it handles std::nullopt
Fixes: #2066
PiperOrigin-RevId: 955981465
Change-Id: I6b34dc2e634d4b727a50fb3e21e5d8e25c39a13b
2026-07-29 11:08:13 -07:00
Manikandan K. S.
653aec68af fix: prevent hidden file creation when base_name is empty in MakeFileName
When base_name is empty, MakeFileName would produce filenames
starting with '.' (e.g., '.xml'), creating hidden files on Unix.
Fall back to 'file' as the base name to avoid this.

Fixes #3815
2026-07-26 17:06:07 +05:30
4 changed files with 64 additions and 21 deletions

View File

@ -945,13 +945,13 @@ template <typename T>
class [[nodiscard]] UniversalPrinter<std::optional<T>> {
public:
static void Print(const std::optional<T>& value, ::std::ostream* os) {
*os << '(';
if (!value) {
*os << "nullopt";
UniversalPrint(std::nullopt, os);
} else {
*os << '(';
UniversalPrint(*value, os);
*os << ')';
}
*os << ')';
}
};
@ -961,27 +961,38 @@ class [[nodiscard]] UniversalPrinter<std::nullopt_t> {
static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
};
struct UniversalPrinterVisitor {
template <typename T>
void operator()(const T& arg) const {
*os << "'" << GetTypeName<T>() << "(index = " << index << ")' with value ";
UniversalPrint(arg, os);
}
::std::ostream* os;
std::size_t index;
};
// Printer for std::variant
template <typename... T>
class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
public:
static void Print(const std::variant<T...>& value, ::std::ostream* os) {
*os << '(';
std::visit(Visitor{os, value.index()}, value);
*os << ')';
}
private:
struct Visitor {
template <typename U>
void operator()(const U& u) const {
*os << "'" << GetTypeName<U>() << "(index = " << index
<< ")' with value ";
UniversalPrint(u, os);
if (value.valueless_by_exception()) {
*os << "(valueless)";
} else {
*os << '(';
std::visit(UniversalPrinterVisitor{os, value.index()}, value);
*os << ')';
}
::std::ostream* os;
std::size_t index;
};
}
};
// Printer for std::monostate
template <>
class [[nodiscard]] UniversalPrinter<std::monostate> {
public:
static void Print(std::monostate, ::std::ostream* os) {
*os << "(monostate)";
}
};
// UniversalPrintArray(begin, len, os) prints an array of 'len'

View File

@ -227,12 +227,12 @@ FilePath FilePath::RemoveFileName() const {
FilePath FilePath::MakeFileName(const FilePath& directory,
const FilePath& base_name, int number,
const char* extension) {
std::string base = base_name.IsEmpty() ? "file" : base_name.string();
std::string file;
if (number == 0) {
file = base_name.string() + "." + extension;
file = base + "." + extension;
} else {
file =
base_name.string() + "_" + StreamableToString(number) + "." + extension;
file = base + "_" + StreamableToString(number) + "." + extension;
}
return ConcatPaths(directory, FilePath(file));
}

View File

@ -274,6 +274,18 @@ TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
EXPECT_EQ("bar_14.xml", actual.string());
}
TEST(MakeFileNameTest, GenerateWhenBaseNameEmpty) {
FilePath actual =
FilePath::MakeFileName(FilePath("foo"), FilePath(""), 0, "xml");
EXPECT_EQ("foo" GTEST_PATH_SEP_ "file.xml", actual.string());
}
TEST(MakeFileNameTest, GenerateWhenBaseNameEmptyNumberNonZero) {
FilePath actual =
FilePath::MakeFileName(FilePath("foo"), FilePath(""), 42, "xml");
EXPECT_EQ("foo" GTEST_PATH_SEP_ "file_42.xml", actual.string());
}
TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath("bar.xml"));
EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());

View File

@ -2030,6 +2030,26 @@ TEST(PrintOneofTest, Basic) {
PrintToString(Type(NonPrintable{})));
}
TEST(PrintVariantTest, Monostate) {
EXPECT_EQ("(monostate)", PrintToString(std::monostate()));
#if GTEST_HAS_EXCEPTIONS
struct ThrowOnMove {
ThrowOnMove() = default;
ThrowOnMove(ThrowOnMove&& other) { *this = std::move(other); }
ThrowOnMove& operator=(ThrowOnMove&&) {
(void)std::vector<bool>().at(0);
return *this;
}
};
std::variant<std::monostate, ThrowOnMove> v = std::monostate();
EXPECT_EQ("('std::monostate(index = 0)' with value (monostate))",
PrintToString(v));
EXPECT_THROW(v = ThrowOnMove(), std::out_of_range);
EXPECT_EQ("(valueless)", PrintToString(v));
#endif
}
#if GTEST_INTERNAL_HAS_COMPARE_LIB
TEST(PrintOrderingTest, Basic) {
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));