Skip to content

Commit 03273a8

Browse files
committed
Merge pull request #2254 from chaoran:master
PiperOrigin-RevId: 248759825
2 parents 9d4cde4 + 5b4a135 commit 03273a8

File tree

3 files changed

+63
-75
lines changed

3 files changed

+63
-75
lines changed

googletest/docs/advanced.md

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,78 +1817,6 @@ For technical reasons, there are some caveats:
18171817
1. `statement` in `EXPECT_FATAL_FAILURE{_ON_ALL_THREADS}()()` cannot return a
18181818
value.
18191819
1820-
## Registering tests programmatically
1821-
1822-
The `TEST` macros handle the vast majority of all use cases, but there are few
1823-
were runtime registration logic is required. For those cases, the framework
1824-
provides the `::testing::RegisterTest` that allows callers to register arbitrary
1825-
tests dynamically.
1826-
1827-
This is an advanced API only to be used when the `TEST` macros are insufficient.
1828-
The macros should be preferred when possible, as they avoid most of the
1829-
complexity of calling this function.
1830-
1831-
It provides the following signature:
1832-
1833-
```c++
1834-
template <typename Factory>
1835-
TestInfo* RegisterTest(const char* test_case_name, const char* test_name,
1836-
const char* type_param, const char* value_param,
1837-
const char* file, int line, Factory factory);
1838-
```
1839-
1840-
The `factory` argument is a factory callable (move-constructible) object or
1841-
function pointer that creates a new instance of the Test object. It handles
1842-
ownership to the caller. The signature of the callable is `Fixture*()`, where
1843-
`Fixture` is the test fixture class for the test. All tests registered with the
1844-
same `test_case_name` must return the same fixture type. This is checked at
1845-
runtime.
1846-
1847-
The framework will infer the fixture class from the factory and will call the
1848-
`SetUpTestCase` and `TearDownTestCase` for it.
1849-
1850-
Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is
1851-
undefined.
1852-
1853-
Use case example:
1854-
1855-
```c++
1856-
class MyFixture : public ::testing::Test {
1857-
public:
1858-
// All of these optional, just like in regular macro usage.
1859-
static void SetUpTestCase() { ... }
1860-
static void TearDownTestCase() { ... }
1861-
void SetUp() override { ... }
1862-
void TearDown() override { ... }
1863-
};
1864-
1865-
class MyTest : public MyFixture {
1866-
public:
1867-
explicit MyTest(int data) : data_(data) {}
1868-
void TestBody() override { ... }
1869-
1870-
private:
1871-
int data_;
1872-
};
1873-
1874-
void RegisterMyTests(const std::vector<int>& values) {
1875-
for (int v : values) {
1876-
::testing::RegisterTest(
1877-
"MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr,
1878-
std::to_string(v).c_str(),
1879-
__FILE__, __LINE__,
1880-
// Important to use the fixture type as the return type here.
1881-
[=]() -> MyFixture* { return new MyTest(v); });
1882-
}
1883-
}
1884-
...
1885-
int main(int argc, char** argv) {
1886-
std::vector<int> values_to_test = LoadValuesFromConfig();
1887-
RegisterMyTests(values_to_test);
1888-
...
1889-
return RUN_ALL_TESTS();
1890-
}
1891-
```
18921820
18931821
## Getting the Current Test's Name
18941822

googletest/include/gtest/gtest-printers.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ class TypeWithoutFormatter {
151151
public:
152152
// This default version is called when kTypeKind is kOtherType.
153153
static void PrintValue(const T& value, ::std::ostream* os) {
154-
PrintBytesInObjectTo(static_cast<const unsigned char*>(
155-
reinterpret_cast<const void*>(&value)),
156-
sizeof(value), os);
154+
PrintBytesInObjectTo(
155+
static_cast<const unsigned char*>(
156+
reinterpret_cast<const void*>(std::addressof(value))),
157+
sizeof(value), os);
157158
}
158159
};
159160

googletest/test/googletest-printers-test.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,65 @@ TEST(PrintOneofTest, Basic) {
15561556
PrintToString(Type(NonPrintable{})));
15571557
}
15581558
#endif // GTEST_HAS_ABSL
1559+
namespace {
1560+
class string_ref;
1561+
1562+
/**
1563+
* This is a synthetic pointer to a fixed size string.
1564+
*/
1565+
class string_ptr {
1566+
public:
1567+
string_ptr(const char* data, size_t size) : data_(data), size_(size) {}
1568+
1569+
string_ptr& operator++() noexcept {
1570+
data_ += size_;
1571+
return *this;
1572+
}
1573+
1574+
string_ref operator*() const noexcept;
1575+
1576+
private:
1577+
const char* data_;
1578+
size_t size_;
1579+
};
1580+
1581+
/**
1582+
* This is a synthetic reference of a fixed size string.
1583+
*/
1584+
class string_ref {
1585+
public:
1586+
string_ref(const char* data, size_t size) : data_(data), size_(size) {}
1587+
1588+
string_ptr operator&() const noexcept { return {data_, size_}; } // NOLINT
1589+
1590+
bool operator==(const char* s) const noexcept {
1591+
if (size_ > 0 && data_[size_ - 1] != 0) {
1592+
return std::string(data_, size_) == std::string(s);
1593+
} else {
1594+
return std::string(data_) == std::string(s);
1595+
}
1596+
}
1597+
1598+
private:
1599+
const char* data_;
1600+
size_t size_;
1601+
};
1602+
1603+
string_ref string_ptr::operator*() const noexcept { return {data_, size_}; }
1604+
1605+
TEST(string_ref, compare) {
1606+
const char* s = "alex\0davidjohn\0";
1607+
string_ptr ptr(s, 5);
1608+
EXPECT_EQ(*ptr, "alex");
1609+
EXPECT_TRUE(*ptr == "alex");
1610+
++ptr;
1611+
EXPECT_EQ(*ptr, "david");
1612+
EXPECT_TRUE(*ptr == "david");
1613+
++ptr;
1614+
EXPECT_EQ(*ptr, "john");
1615+
}
1616+
1617+
} // namespace
15591618

15601619
} // namespace gtest_printers_test
15611620
} // namespace testing

0 commit comments

Comments
 (0)