CodeMod: Prefer ADD_FAILURE() over EXPECT_TRUE(false), et cetera
authorYedidya Feldblum <yfeldblum@fb.com>
Mon, 17 Jul 2017 04:23:33 +0000 (21:23 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 17 Jul 2017 04:37:27 +0000 (21:37 -0700)
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

12 files changed:
folly/futures/test/HeaderCompileTest.cpp
folly/io/async/test/HHWheelTimerSlowTests.cpp
folly/io/test/IOBufCursorTest.cpp
folly/test/ConvTest.cpp
folly/test/EnumerateTest.cpp
folly/test/ExceptionWrapperTest.cpp
folly/test/ExpectedTest.cpp
folly/test/FileTest.cpp
folly/test/LifoSemTests.cpp
folly/test/RangeTest.cpp
folly/test/StringTest.cpp
folly/test/ThreadLocalTest.cpp

index 927905fd005b76e374db744c71cd4d39e6bcf6d9..6916c032d8b26b737653cdc92cd26e11b21b46cc 100644 (file)
@@ -22,5 +22,5 @@
 #include <folly/portability/GTest.h>
 
 TEST(Basic, compiles) {
-  EXPECT_TRUE(true);
+  SUCCEED();
 }
index 4fc1f083f2e658219cd4156212fcac73cdcf260b..be4e64ede1f0eecaeb12582d90223bc1f1eaca7d 100644 (file)
@@ -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));
index 22c2a76d91013b5ed8d74e9d2251749e2351cd05..60ac46aeb076a45d10d55686e07516dad2d159e1 100644 (file)
@@ -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 (...) {
   }
 }
index 3d5d0f631b7573cddf1dcc408113537e43042293..77e77d423e25c86ee7d6494ac142fe947e477dfe 100644 (file)
@@ -147,7 +147,7 @@ TEST(Conv, Floating2Floating) {
     EXPECT_TRUE(shouldWork == std::numeric_limits<float>::min() ||
                 shouldWork == 0.f);
   } catch (...) {
-    EXPECT_TRUE(false);
+    ADD_FAILURE();
   }
 }
 
@@ -601,7 +601,7 @@ TEST(Conv, StringPieceToDouble) {
   // Test NaN conversion
   try {
     to<double>("not a number");
-    EXPECT_TRUE(false);
+    ADD_FAILURE();
   } catch (const std::range_error &) {
   }
 
@@ -638,7 +638,7 @@ TEST(Conv, EmptyStringToInt) {
 
   try {
     to<int>(pc);
-    EXPECT_TRUE(false);
+    ADD_FAILURE();
   } catch (const std::range_error &) {
   }
 }
@@ -649,7 +649,7 @@ TEST(Conv, CorruptedStringToInt) {
 
   try {
     to<int64_t>(&pc);
-    EXPECT_TRUE(false);
+    ADD_FAILURE();
   } catch (const std::range_error &) {
   }
 }
@@ -660,7 +660,7 @@ TEST(Conv, EmptyStringToDouble) {
 
   try {
     to<double>(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<float>(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<int>(42.1);
     LOG(ERROR) << "to<int> 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<char> returned "
                << static_cast<unsigned int>(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<A> returned "
                << static_cast<unsigned int>(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<int32_t>(x);
     LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
-    EXPECT_TRUE(false);
+    ADD_FAILURE();
   } catch (std::range_error&) {
   }
 }
@@ -915,7 +915,7 @@ void testConvError(
   std::string where = to<std::string>(__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());
index 828524974849ae7bdddb4e3cb28b0d311e414932..67ffe31a3c4054864a09cc46d54ceacfe0ae532e 100644 (file)
@@ -127,7 +127,7 @@ TEST(Enumerate, EmptyRange) {
   std::vector<std::string> v;
   for (auto it : folly::enumerate(v)) {
     (void)it; // Silence warnings.
-    EXPECT_TRUE(false);
+    ADD_FAILURE();
   }
 }
 
index 9d7db8655b31442bcbd728224f172fe1d4ca4b39..f6f00f38b84e9e320825a7a21ebee9b5f624dc64 100644 (file)
@@ -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);
 }
index d8291d0f39c2fefc52547ff8b37f311b623b8801..fe24055d29c73a2afa91248a5dcef5e01fe39ce1 100644 (file)
@@ -676,7 +676,7 @@ TEST(Expected, Then) {
     // Error case:
     Expected<int, E> ex = Expected<std::unique_ptr<int>, E>{
         unexpected, E::E1}.then([](std::unique_ptr<int> p) -> int {
-      EXPECT_TRUE(false);
+      ADD_FAILURE();
       return *p;
     });
     EXPECT_FALSE(bool(ex));
index 7aec67ba97e81b7c45505814b7a6c9496e5979be..24342671a3282346b9ef75e055415b5136166553 100644 (file)
@@ -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();
   }
 }
 
index 02c112b2a1f16cbed4e91da2e4ca872de7b1c335..c6c9b45f4cbe6e794f931ce8bf4a8682dbe5fded 100644 (file)
@@ -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());
index a3898856a99fc99fe332158022646dd85e1d4f7c..8070b80110b6eb192075d639527359e897c14780 100644 (file)
@@ -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
index 31c6e4ad91d8b7f6433c5424ff29bb25bcbb5303..9446a4aaeff70ce6b8cb158e9b4127ba97ffa5f4 100644 (file)
@@ -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);
index 2aeb205418cdc97ce9182baa2793355d77fc445f..fc9bdefb1a15ef2174f7d1a6ab736fa884846ceb 100644 (file)
@@ -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";
   }
 }