From: Yedidya Feldblum Date: Mon, 17 Jul 2017 04:23:33 +0000 (-0700) Subject: CodeMod: Prefer ADD_FAILURE() over EXPECT_TRUE(false), et cetera X-Git-Tag: v2017.07.17.00~1 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b15db0d83e11c8863ef6f57f1bac5dec846eaa00;p=folly.git CodeMod: Prefer ADD_FAILURE() over EXPECT_TRUE(false), et cetera Summary: CodeMod: Prefer `ADD_FAILURE()` over `EXPECT_TRUE(false)`, et cetera. The tautologically-conditioned and tautologically-contradicted boolean expectations/assertions have better alternatives: unconditional passes and failures. Reviewed By: Orvid Differential Revision: D5432398 Tags: codemod, codemod-opensource fbshipit-source-id: d16b447e8696a6feaa94b41199f5052226ef6914 --- diff --git a/folly/futures/test/HeaderCompileTest.cpp b/folly/futures/test/HeaderCompileTest.cpp index 927905fd..6916c032 100644 --- a/folly/futures/test/HeaderCompileTest.cpp +++ b/folly/futures/test/HeaderCompileTest.cpp @@ -22,5 +22,5 @@ #include TEST(Basic, compiles) { - EXPECT_TRUE(true); + SUCCEED(); } diff --git a/folly/io/async/test/HHWheelTimerSlowTests.cpp b/folly/io/async/test/HHWheelTimerSlowTests.cpp index 4fc1f083..be4e64ed 100644 --- a/folly/io/async/test/HHWheelTimerSlowTests.cpp +++ b/folly/io/async/test/HHWheelTimerSlowTests.cpp @@ -296,7 +296,7 @@ TEST_F(HHWheelTimerTest, Stress) { timeout - 256); timeouts[i].fn = [&, i, timeout]() { LOG(INFO) << "FAIL:timer " << i << " still fired in " << timeout; - EXPECT_FALSE(true); + ADD_FAILURE(); }; } else { t.scheduleTimeout(&timeouts[i], std::chrono::milliseconds(timeout)); diff --git a/folly/io/test/IOBufCursorTest.cpp b/folly/io/test/IOBufCursorTest.cpp index 22c2a76d..60ac46ae 100644 --- a/folly/io/test/IOBufCursorTest.cpp +++ b/folly/io/test/IOBufCursorTest.cpp @@ -142,7 +142,7 @@ TEST(IOBuf, Cursor) { c.write((uint8_t)40); // OK try { c.write((uint8_t)10); // Bad write, checked should except. - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (...) { } } diff --git a/folly/test/ConvTest.cpp b/folly/test/ConvTest.cpp index 3d5d0f63..77e77d42 100644 --- a/folly/test/ConvTest.cpp +++ b/folly/test/ConvTest.cpp @@ -147,7 +147,7 @@ TEST(Conv, Floating2Floating) { EXPECT_TRUE(shouldWork == std::numeric_limits::min() || shouldWork == 0.f); } catch (...) { - EXPECT_TRUE(false); + ADD_FAILURE(); } } @@ -601,7 +601,7 @@ TEST(Conv, StringPieceToDouble) { // Test NaN conversion try { to("not a number"); - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (const std::range_error &) { } @@ -638,7 +638,7 @@ TEST(Conv, EmptyStringToInt) { try { to(pc); - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (const std::range_error &) { } } @@ -649,7 +649,7 @@ TEST(Conv, CorruptedStringToInt) { try { to(&pc); - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (const std::range_error &) { } } @@ -660,7 +660,7 @@ TEST(Conv, EmptyStringToDouble) { try { to(pc); - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (const std::range_error &) { } } @@ -671,7 +671,7 @@ TEST(Conv, IntToDouble) { /* This seems not work in ubuntu11.10, gcc 4.6.1 try { auto f = to(957837589847); - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (std::range_error& e) { //LOG(INFO) << e.what(); } @@ -684,7 +684,7 @@ TEST(Conv, DoubleToInt) { try { auto i2 = to(42.1); LOG(ERROR) << "to returned " << i2 << " instead of throwing"; - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (std::range_error&) { //LOG(INFO) << e.what(); } @@ -701,7 +701,7 @@ TEST(Conv, EnumToInt) { LOG(ERROR) << "to returned " << static_cast(i2) << " instead of throwing"; - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (std::range_error&) { //LOG(INFO) << e.what(); } @@ -726,7 +726,7 @@ TEST(Conv, IntToEnum) { LOG(ERROR) << "to returned " << static_cast(i2) << " instead of throwing"; - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (std::range_error&) { //LOG(INFO) << e.what(); } @@ -743,7 +743,7 @@ TEST(Conv, UnsignedEnum) { try { auto i = to(x); LOG(ERROR) << "to returned " << i << " instead of throwing"; - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (std::range_error&) { } } @@ -915,7 +915,7 @@ void testConvError( std::string where = to(__FILE__, "(", line, "): "); try { auto res = expr(); - EXPECT_TRUE(false) << where << exprStr << " -> " << res; + ADD_FAILURE() << where << exprStr << " -> " << res; } catch (const ConversionError& e) { EXPECT_EQ(code, e.errorCode()) << where << exprStr; std::string str(e.what()); diff --git a/folly/test/EnumerateTest.cpp b/folly/test/EnumerateTest.cpp index 82852497..67ffe31a 100644 --- a/folly/test/EnumerateTest.cpp +++ b/folly/test/EnumerateTest.cpp @@ -127,7 +127,7 @@ TEST(Enumerate, EmptyRange) { std::vector v; for (auto it : folly::enumerate(v)) { (void)it; // Silence warnings. - EXPECT_TRUE(false); + ADD_FAILURE(); } } diff --git a/folly/test/ExceptionWrapperTest.cpp b/folly/test/ExceptionWrapperTest.cpp index 9d7db865..f6f00f38 100644 --- a/folly/test/ExceptionWrapperTest.cpp +++ b/folly/test/ExceptionWrapperTest.cpp @@ -508,10 +508,10 @@ TEST(ExceptionWrapper, handle_std_exception) { bool handled = false; auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) { ew.handle( - [](const std::logic_error&) { EXPECT_TRUE(false); }, + [](const std::logic_error&) { ADD_FAILURE(); }, [&](const std::runtime_error&) { handled = true; }, - [](const std::exception&) { EXPECT_TRUE(false); }, - [](...) { EXPECT_TRUE(false); }); + [](const std::exception&) { ADD_FAILURE(); }, + [](...) { ADD_FAILURE(); }); }; expect_runtime_error_yes_catch_all(ew_eptr); @@ -526,9 +526,9 @@ TEST(ExceptionWrapper, handle_std_exception) { auto expect_runtime_error_no_catch_all = [&](const exception_wrapper& ew) { ew.handle( - [](const std::logic_error&) { EXPECT_TRUE(false); }, + [](const std::logic_error&) { ADD_FAILURE(); }, [&](const std::runtime_error&) { handled = true; }, - [](const std::exception&) { EXPECT_TRUE(false); }); + [](const std::exception&) { ADD_FAILURE(); }); }; expect_runtime_error_no_catch_all(ew_eptr); @@ -543,10 +543,10 @@ TEST(ExceptionWrapper, handle_std_exception) { auto expect_runtime_error_catch_non_std = [&](const exception_wrapper& ew) { ew.handle( - [](const std::logic_error&) { EXPECT_TRUE(false); }, + [](const std::logic_error&) { ADD_FAILURE(); }, [&](const std::runtime_error&) { handled = true; }, - [](const std::exception&) { EXPECT_TRUE(false); }, - [](const int&) { EXPECT_TRUE(false); }); + [](const std::exception&) { ADD_FAILURE(); }, + [](const int&) { ADD_FAILURE(); }); }; expect_runtime_error_catch_non_std(ew_eptr); @@ -563,12 +563,12 @@ TEST(ExceptionWrapper, handle_std_exception) { // outer handler: auto expect_runtime_error_rethrow = [&](const exception_wrapper& ew) { ew.handle( - [](const std::logic_error&) { EXPECT_TRUE(false); }, + [](const std::logic_error&) { ADD_FAILURE(); }, [&](const std::runtime_error& e) { handled = true; throw e; }, - [](const std::exception&) { EXPECT_TRUE(false); }); + [](const std::exception&) { ADD_FAILURE(); }); }; EXPECT_THROW(expect_runtime_error_rethrow(ew_eptr), std::runtime_error); @@ -589,8 +589,8 @@ TEST(ExceptionWrapper, handle_std_exception_unhandled) { bool handled = false; auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) { ew.handle( - [](const std::logic_error&) { EXPECT_TRUE(false); }, - [](const std::runtime_error&) { EXPECT_TRUE(false); }, + [](const std::logic_error&) { ADD_FAILURE(); }, + [](const std::runtime_error&) { ADD_FAILURE(); }, [&](...) { handled = true; }); }; @@ -610,7 +610,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) { auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) { ew.handle( - [](const std::exception&) { EXPECT_TRUE(false); }, + [](const std::exception&) { ADD_FAILURE(); }, [&](...) { handled = true; }); }; @@ -626,7 +626,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) { auto expect_int_no_catch_all = [&](const exception_wrapper& ew) { ew.handle( - [](const std::exception&) { EXPECT_TRUE(false); }, + [](const std::exception&) { ADD_FAILURE(); }, [&](const int&) { handled = true; }); }; @@ -643,7 +643,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) { auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) { ew.handle( [&](const int&) { handled = true; }, - [](const std::exception&) { EXPECT_TRUE(false); }); + [](const std::exception&) { ADD_FAILURE(); }); }; expect_int_no_catch_all_2(ew_eptr1); @@ -665,7 +665,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) { auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) { ew.handle( - [](const std::exception&) { EXPECT_TRUE(false); }, + [](const std::exception&) { ADD_FAILURE(); }, [&](...) { handled = true; }); }; @@ -681,7 +681,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) { auto expect_int_no_catch_all = [&](const exception_wrapper& ew) { ew.handle( - [](const std::exception&) { EXPECT_TRUE(false); }, + [](const std::exception&) { ADD_FAILURE(); }, [&](const BigNonStdError&) { handled = true; }); }; @@ -698,7 +698,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) { auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) { ew.handle( [&](const BigNonStdError&) { handled = true; }, - [](const std::exception&) { EXPECT_TRUE(false); }); + [](const std::exception&) { ADD_FAILURE(); }); }; expect_int_no_catch_all_2(ew_eptr1); @@ -724,7 +724,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) { handled = true; throw e; }, - [](const BaseException&) { EXPECT_TRUE(false); }), + [](const BaseException&) { ADD_FAILURE(); }), DerivedException); EXPECT_TRUE(handled); handled = false; @@ -734,7 +734,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) { handled = true; throw e; }, - [](...) { EXPECT_TRUE(false); }), + [](...) { ADD_FAILURE(); }), DerivedException); EXPECT_TRUE(handled); } diff --git a/folly/test/ExpectedTest.cpp b/folly/test/ExpectedTest.cpp index d8291d0f..fe24055d 100644 --- a/folly/test/ExpectedTest.cpp +++ b/folly/test/ExpectedTest.cpp @@ -676,7 +676,7 @@ TEST(Expected, Then) { // Error case: Expected ex = Expected, E>{ unexpected, E::E1}.then([](std::unique_ptr p) -> int { - EXPECT_TRUE(false); + ADD_FAILURE(); return *p; }); EXPECT_FALSE(bool(ex)); diff --git a/folly/test/FileTest.cpp b/folly/test/FileTest.cpp index 7aec67ba..24342671 100644 --- a/folly/test/FileTest.cpp +++ b/folly/test/FileTest.cpp @@ -118,21 +118,21 @@ TEST(File, Truthy) { if (temp) { ; } else { - EXPECT_FALSE(true); + ADD_FAILURE(); } if (File file = File::temporary()) { ; } else { - EXPECT_FALSE(true); + ADD_FAILURE(); } EXPECT_FALSE(bool(File())); if (File()) { - EXPECT_TRUE(false); + ADD_FAILURE(); } if (File notOpened = File()) { - EXPECT_TRUE(false); + ADD_FAILURE(); } } diff --git a/folly/test/LifoSemTests.cpp b/folly/test/LifoSemTests.cpp index 02c112b2..c6c9b45f 100644 --- a/folly/test/LifoSemTests.cpp +++ b/folly/test/LifoSemTests.cpp @@ -227,7 +227,7 @@ TEST(LifoSem, shutdown_multi) { threads.push_back(DSched::thread([&]{ try { a.wait(); - EXPECT_TRUE(false); + ADD_FAILURE(); } catch (ShutdownSemError&) { // expected EXPECT_TRUE(a.isShutdown()); diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index a3898856..8070b801 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -1009,7 +1009,7 @@ void createProtectedBuf(StringPiece& contents, char** buf) { const size_t kSuccess = 0; char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize); if (pageAlignedBuf == nullptr) { - ASSERT_FALSE(true); + FAIL(); } // Protect the page after the first full page-aligned region of the // malloc'ed buffer diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index 31c6e4ad..9446a4aa 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -420,7 +420,7 @@ TEST(PrettyToDouble, Basic) { try{ recoveredX = prettyToDouble(testString, formatType); } catch (const std::range_error& ex) { - EXPECT_TRUE(false) << testCase.prettyString << " -> " << ex.what(); + ADD_FAILURE() << testCase.prettyString << " -> " << ex.what(); } double relativeError = fabs(x) < 1e-5 ? (x-recoveredX) : (x - recoveredX) / x; @@ -438,7 +438,7 @@ TEST(PrettyToDouble, Basic) { recoveredX = prettyToDouble(prettyPrint(x, formatType, addSpace), formatType); } catch (std::range_error&) { - EXPECT_TRUE(false); + ADD_FAILURE(); } double relativeError = (x - recoveredX) / x; EXPECT_NEAR(0, relativeError, 1e-3); diff --git a/folly/test/ThreadLocalTest.cpp b/folly/test/ThreadLocalTest.cpp index 2aeb2054..fc9bdefb 100644 --- a/folly/test/ThreadLocalTest.cpp +++ b/folly/test/ThreadLocalTest.cpp @@ -530,7 +530,7 @@ TEST(ThreadLocal, Fork) { EXPECT_TRUE(WIFEXITED(status)); EXPECT_EQ(0, WEXITSTATUS(status)); } else { - EXPECT_TRUE(false) << "fork failed"; + ADD_FAILURE() << "fork failed"; } EXPECT_EQ(2, totalValue()); @@ -573,7 +573,7 @@ TEST(ThreadLocal, Fork2) { EXPECT_TRUE(WIFEXITED(status)); EXPECT_EQ(0, WEXITSTATUS(status)); } else { - EXPECT_TRUE(false) << "fork failed"; + ADD_FAILURE() << "fork failed"; } }