diff options
author | Victor Zverovich <victor.zverovich@gmail.com> | 2019-11-15 05:41:14 -0800 |
---|---|---|
committer | Victor Zverovich <victor.zverovich@gmail.com> | 2019-11-15 07:28:02 -0800 |
commit | dcde089b4edaecb42c4bdd43c5b3674017a00d91 (patch) | |
tree | 834f7eb85247a0861bc63127cd5b0fb703ce759c /test | |
parent | 2145a7bdccdda0b39d96a99f943123317145b874 (diff) | |
download | fmtlib-dcde089b4edaecb42c4bdd43c5b3674017a00d91.tar.gz |
Improve POSIX API detection
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 4 | ||||
-rw-r--r-- | test/format-test.cc | 2 | ||||
-rw-r--r-- | test/gtest-extra-test.cc | 54 | ||||
-rw-r--r-- | test/gtest-extra.cc | 2 | ||||
-rw-r--r-- | test/gtest-extra.h | 17 | ||||
-rw-r--r-- | test/posix-mock-test.cc | 5 | ||||
-rw-r--r-- | test/posix-test.cc | 4 | ||||
-rw-r--r-- | test/printf-test.cc | 2 | ||||
-rw-r--r-- | test/util.cc | 6 |
9 files changed, 51 insertions, 45 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8a15135e..09645eb6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -47,8 +47,6 @@ target_compile_definitions(gmock set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc) add_library(test-main STATIC ${TEST_MAIN_SRC}) -target_compile_definitions(test-main PUBLIC - FMT_USE_FILE_DESCRIPTORS=$<BOOL:${HAVE_OPEN}>) target_include_directories(test-main SYSTEM PUBLIC gtest gmock) target_link_libraries(test-main gmock fmt) @@ -113,7 +111,7 @@ add_fmt_test(custom-formatter-test) add_fmt_test(ranges-test) add_fmt_test(scan-test) -if (HAVE_OPEN AND NOT MSVC_BUILD_STATIC) +if (NOT MSVC_BUILD_STATIC) add_fmt_executable(posix-mock-test posix-mock-test.cc ../src/format.cc ${TEST_MAIN_SRC}) target_include_directories( diff --git a/test/format-test.cc b/test/format-test.cc index 0dce6bad..af7ba528 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1819,7 +1819,7 @@ TEST(FormatIntTest, FormatInt) { } TEST(FormatTest, Print) { -#if FMT_USE_FILE_DESCRIPTORS +#if FMT_USE_FCNTL EXPECT_WRITE(stdout, fmt::print("Don't {}!", "panic"), "Don't panic!"); EXPECT_WRITE(stderr, fmt::print(stderr, "Don't {}!", "panic"), "Don't panic!"); diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 3f8fd5ea..99d1d03a 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -74,14 +74,6 @@ TEST_F(SingleEvaluationTest, FailedEXPECT_SYSTEM_ERROR) { EXPECT_EQ(s_ + 1, p_); } -// Tests that when EXPECT_WRITE fails, it evaluates its message argument -// exactly once. -TEST_F(SingleEvaluationTest, FailedEXPECT_WRITE) { - EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("test"), p_++), - "01234"); - EXPECT_EQ(s_ + 1, p_); -} - // Tests that assertion arguments are evaluated exactly once. TEST_F(SingleEvaluationTest, ExceptionTests) { // successful EXPECT_THROW_MSG @@ -163,6 +155,15 @@ TEST_F(SingleEvaluationTest, SystemErrorTests) { EXPECT_EQ(4, b_); } +#if FMT_USE_FCNTL +// Tests that when EXPECT_WRITE fails, it evaluates its message argument +// exactly once. +TEST_F(SingleEvaluationTest, FailedEXPECT_WRITE) { + EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("test"), p_++), + "01234"); + EXPECT_EQ(s_ + 1, p_); +} + // Tests that assertion arguments are evaluated exactly once. TEST_F(SingleEvaluationTest, WriteTests) { // successful EXPECT_WRITE @@ -187,6 +188,24 @@ TEST_F(SingleEvaluationTest, WriteTests) { EXPECT_EQ(2, b_); } +// Tests EXPECT_WRITE. +TEST(ExpectTest, EXPECT_WRITE) { + EXPECT_WRITE(stdout, do_nothing(), ""); + EXPECT_WRITE(stdout, std::printf("test"), "test"); + EXPECT_WRITE(stderr, std::fprintf(stderr, "test"), "test"); + EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("that"), "this"), + "Expected: this\n" + " Actual: that"); +} + +TEST(StreamingAssertionsTest, EXPECT_WRITE) { + EXPECT_WRITE(stdout, std::printf("test"), "test") << "unexpected failure"; + EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("test"), "other") + << "expected failure", + "expected failure"); +} +#endif // FMT_USE_FCNTL + // Tests that the compiler will not complain about unreachable code in the // EXPECT_THROW_MSG macro. TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) { @@ -280,16 +299,6 @@ TEST(ExpectTest, EXPECT_SYSTEM_ERROR) { format_system_error(EDOM, "test"))); } -// Tests EXPECT_WRITE. -TEST(ExpectTest, EXPECT_WRITE) { - EXPECT_WRITE(stdout, do_nothing(), ""); - EXPECT_WRITE(stdout, std::printf("test"), "test"); - EXPECT_WRITE(stderr, std::fprintf(stderr, "test"), "test"); - EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("that"), "this"), - "Expected: this\n" - " Actual: that"); -} - TEST(StreamingAssertionsTest, EXPECT_THROW_MSG) { EXPECT_THROW_MSG(throw_exception(), std::exception, "test") << "unexpected failure"; @@ -308,20 +317,13 @@ TEST(StreamingAssertionsTest, EXPECT_SYSTEM_ERROR) { "expected failure"); } -TEST(StreamingAssertionsTest, EXPECT_WRITE) { - EXPECT_WRITE(stdout, std::printf("test"), "test") << "unexpected failure"; - EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, std::printf("test"), "other") - << "expected failure", - "expected failure"); -} - TEST(UtilTest, FormatSystemError) { fmt::memory_buffer out; fmt::format_system_error(out, EDOM, "test message"); EXPECT_EQ(to_string(out), format_system_error(EDOM, "test message")); } -#if FMT_USE_FILE_DESCRIPTORS +#if FMT_USE_FCNTL using fmt::buffered_file; using fmt::error_code; diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index ae89e71d..2077c2c6 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -7,7 +7,7 @@ #include "gtest-extra.h" -#if FMT_USE_FILE_DESCRIPTORS +#if FMT_USE_FCNTL using fmt::file; diff --git a/test/gtest-extra.h b/test/gtest-extra.h index 4a693361..ee335615 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -10,16 +10,7 @@ #include <string> #include "gmock.h" - -#include "fmt/core.h" - -#ifndef FMT_USE_FILE_DESCRIPTORS -# define FMT_USE_FILE_DESCRIPTORS 0 -#endif - -#if FMT_USE_FILE_DESCRIPTORS -# include "fmt/posix.h" -#endif +#include "fmt/posix.h" #define FMT_TEST_THROW_(statement, expected_exception, expected_message, fail) \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ @@ -65,7 +56,7 @@ std::string format_system_error(int error_code, fmt::string_view message); EXPECT_THROW_MSG(statement, fmt::system_error, \ format_system_error(error_code, message)) -#if FMT_USE_FILE_DESCRIPTORS +#if FMT_USE_FCNTL // Captures file output by redirecting it to a pipe. // The output it can handle is limited by the pipe capacity. @@ -151,7 +142,9 @@ std::string read(fmt::file& f, std::size_t count); # define EXPECT_READ(file, expected_content) \ EXPECT_EQ(expected_content, read(file, std::strlen(expected_content))) -#endif // FMT_USE_FILE_DESCRIPTORS +#else +# define EXPECT_WRITE(file, statement, expected_output) SUCCEED() +#endif // FMT_USE_FCNTL template <typename Mock> struct ScopedMock : testing::StrictMock<Mock> { ScopedMock() { Mock::instance = this; } diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index 58e015aa..fbd4a687 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -30,7 +30,6 @@ using fmt::buffered_file; using fmt::error_code; -using fmt::file; using testing::_; using testing::Return; @@ -198,6 +197,9 @@ static void write_file(fmt::cstring_view filename, fmt::string_view content) { f.print("{}", content); } +#if FMT_USE_FCNTL +using fmt::file; + TEST(UtilTest, GetPageSize) { #ifdef _WIN32 SYSTEM_INFO si = {}; @@ -429,6 +431,7 @@ TEST(BufferedFileTest, FilenoNoRetry) { EXPECT_EQ(2, fileno_count); fileno_count = 0; } +#endif // FMT_USE_FCNTL struct TestMock { static TestMock* instance; diff --git a/test/posix-test.cc b/test/posix-test.cc index 9cf049b6..24d211d3 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -19,6 +19,9 @@ using fmt::buffered_file; using fmt::error_code; + +#if FMT_USE_FCNTL + using fmt::file; // Checks if the file is open by reading one character from it. @@ -376,3 +379,4 @@ TEST(LocaleTest, Strtod) { EXPECT_EQ(start + 3, ptr); } #endif +#endif // FMT_USE_FCNTL
\ No newline at end of file diff --git a/test/printf-test.cc b/test/printf-test.cc index 75101226..2d68fab4 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -477,7 +477,7 @@ enum E { A = 42 }; TEST(PrintfTest, Enum) { EXPECT_PRINTF("42", "%d", A); } -#if FMT_USE_FILE_DESCRIPTORS +#if FMT_USE_FCNTL TEST(PrintfTest, Examples) { const char* weekday = "Thursday"; const char* month = "August"; diff --git a/test/util.cc b/test/util.cc index 329f6543..e75bd450 100644 --- a/test/util.cc +++ b/test/util.cc @@ -33,11 +33,17 @@ std::string get_system_error(int error_code) { const char* const FILE_CONTENT = "Don't panic!"; fmt::buffered_file open_buffered_file(FILE** fp) { +#if FMT_USE_FCNTL fmt::file read_end, write_end; fmt::file::pipe(read_end, write_end); write_end.write(FILE_CONTENT, std::strlen(FILE_CONTENT)); write_end.close(); fmt::buffered_file f = read_end.fdopen("r"); if (fp) *fp = f.get(); +#else + fmt::buffered_file f("test-file", "w"); + fputs(FILE_CONTENT, f.get()); + if (fp) *fp = f.get(); +#endif return f; } |