google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.53k stars 10.09k forks source link

[FR]: Request for googletest to support non-compile tests in C++ #4430

Closed phaedon closed 10 months ago

phaedon commented 10 months ago

Does the feature exist in the most recent commit?

No

Why do we need this feature?

Modern idioms for C++ library design often rely on compile-time invariants (static_assert, constexpr, enable_if, etc) It is useful to be able to write unit tests which can run and successfully verify compilation failures.

Describe the proposal.

I don't have a detailed design, however I have previously seen (and personally used) something along these lines.

// Code
template <int Dim>
struct Polygon {
  Polygon() { static_assert(Dim >= 3); }
};

// Test
TEST_NON_COMPILE(PolygonTest, OneDimension) {
  Polygon<1> not_a_polygon;
}

// Build
cc_test_non_compile(
  name = "polygon_test",
  // etc.
)

And then test output would be successful on the basis of the compilation failure triggered by the static assert.

Is the feature specific to an operating system, compiler, or build system version?

No

derekmauro commented 10 months ago

To figure out if code successfully compiles, we would have to implement a compiler. We are not going to do that. That job is best left to an actual compiler. GoogleTest is designed to test runtime bugs.

Some ways you could do this: