From 8641461c9838530d97e00247f889072e1edd341a Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 27 Apr 2014 06:56:12 -0700 Subject: [PATCH] More tests. --- CMakeLists.txt | 10 ++++++++++ format-test.cc | 46 ++++++++++++++++++++++++++++++++++++++++++--- test/CMakeLists.txt | 3 +++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4915ec4e..3d19a082 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,16 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1) endif () +include(CheckSymbolExists) +if (WIN32) + check_symbol_exists(dup io.h HAVE_DUP) +else () + check_symbol_exists(dup unistd.h HAVE_DUP) +endif () +if (HAVE_DUP) + add_definitions(-DFMT_USE_DUP=1) +endif () + add_library(gtest gtest/gtest-all.cc) find_package(Threads) diff --git a/format-test.cc b/format-test.cc index 817befe4..9ad672c4 100644 --- a/format-test.cc +++ b/format-test.cc @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,17 @@ # include #endif +#if FMT_USE_DUP +# ifdef _WIN32 +# include +# else +# include +# include +# include +# include +# endif +#endif + #include "format.h" #include @@ -1341,7 +1353,7 @@ TEST(FormatterTest, FormatChar) { TEST(FormatterTest, FormatWChar) { EXPECT_EQ(L"a", str(Format(L"{0}") << L'a')); // This shouldn't compile: - //Format("{0}") << L'a'; + //Format("{}") << L'a'; } TEST(FormatterTest, FormatCString) { @@ -1490,7 +1502,7 @@ TEST(FormatterTest, ActionNotCalledOnError) { // The test doesn't compile on older compilers which follow C++03 and // require an accessible copy constructor when binding a temporary to // a const reference. -#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 7 +#if FMT_GCC_VERSION >= 407 TEST(FormatterTest, ArgLifetime) { // The following code is for testing purposes only. It is a definite abuse // of the API and shouldn't be used in real applications. @@ -1609,11 +1621,39 @@ TEST(FormatIntTest, FormatDec) { EXPECT_EQ("42", FormatDec(42ull)); } +#ifdef FMT_USE_DUP + +class File { + private: + int fd_; + File(const File &); + void operator=(const File &); + public: + File(int fd) : fd_(fd) {} + ~File() { close(fd_); } + int fd() const { return fd_; } +}; + TEST(ColorTest, PrintColored) { + std::fflush(stdout); + File saved_stdio(dup(1)); + EXPECT_NE(-1, saved_stdio.fd()); + { + File out(open("out", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); + EXPECT_NE(-1, out.fd()); + EXPECT_NE(-1, dup2(out.fd(), 1)); + } fmt::PrintColored(fmt::RED, "Hello, {}!\n") << "world"; - // TODO + std::fflush(stdout); + EXPECT_NE(-1, dup2(saved_stdio.fd(), 1)); + std::ifstream out("out"); + std::stringstream content; + content << out.rdbuf(); + EXPECT_EQ("\x1b[31mHello, world!\n\x1b[0m", content.str()); } +#endif + template std::string str(const T &value) { return fmt::str(fmt::Format("{0}") << value); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c8acaa43..051b11df 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -33,3 +33,6 @@ expect_compile_error("fmt::Writer a, b; b = a;") expect_compile_error("fmt::Writer() << L'a';") expect_compile_error("fmt::Writer() << fmt::pad(\"abc\", 5, L' ');") expect_compile_error("fmt::Writer() << fmt::pad(42, 5, L' ');") + +# Formatting a wide character with a narrow format string is forbidden. +expect_compile_error("Format(\"{}\") << L'a';")