Commit b15db0d8 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

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
parent eb0730c9
...@@ -22,5 +22,5 @@ ...@@ -22,5 +22,5 @@
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
TEST(Basic, compiles) { TEST(Basic, compiles) {
EXPECT_TRUE(true); SUCCEED();
} }
...@@ -296,7 +296,7 @@ TEST_F(HHWheelTimerTest, Stress) { ...@@ -296,7 +296,7 @@ TEST_F(HHWheelTimerTest, Stress) {
timeout - 256); timeout - 256);
timeouts[i].fn = [&, i, timeout]() { timeouts[i].fn = [&, i, timeout]() {
LOG(INFO) << "FAIL:timer " << i << " still fired in " << timeout; LOG(INFO) << "FAIL:timer " << i << " still fired in " << timeout;
EXPECT_FALSE(true); ADD_FAILURE();
}; };
} else { } else {
t.scheduleTimeout(&timeouts[i], std::chrono::milliseconds(timeout)); t.scheduleTimeout(&timeouts[i], std::chrono::milliseconds(timeout));
......
...@@ -142,7 +142,7 @@ TEST(IOBuf, Cursor) { ...@@ -142,7 +142,7 @@ TEST(IOBuf, Cursor) {
c.write((uint8_t)40); // OK c.write((uint8_t)40); // OK
try { try {
c.write((uint8_t)10); // Bad write, checked should except. c.write((uint8_t)10); // Bad write, checked should except.
EXPECT_TRUE(false); ADD_FAILURE();
} catch (...) { } catch (...) {
} }
} }
......
...@@ -147,7 +147,7 @@ TEST(Conv, Floating2Floating) { ...@@ -147,7 +147,7 @@ TEST(Conv, Floating2Floating) {
EXPECT_TRUE(shouldWork == std::numeric_limits<float>::min() || EXPECT_TRUE(shouldWork == std::numeric_limits<float>::min() ||
shouldWork == 0.f); shouldWork == 0.f);
} catch (...) { } catch (...) {
EXPECT_TRUE(false); ADD_FAILURE();
} }
} }
...@@ -601,7 +601,7 @@ TEST(Conv, StringPieceToDouble) { ...@@ -601,7 +601,7 @@ TEST(Conv, StringPieceToDouble) {
// Test NaN conversion // Test NaN conversion
try { try {
to<double>("not a number"); to<double>("not a number");
EXPECT_TRUE(false); ADD_FAILURE();
} catch (const std::range_error &) { } catch (const std::range_error &) {
} }
...@@ -638,7 +638,7 @@ TEST(Conv, EmptyStringToInt) { ...@@ -638,7 +638,7 @@ TEST(Conv, EmptyStringToInt) {
try { try {
to<int>(pc); to<int>(pc);
EXPECT_TRUE(false); ADD_FAILURE();
} catch (const std::range_error &) { } catch (const std::range_error &) {
} }
} }
...@@ -649,7 +649,7 @@ TEST(Conv, CorruptedStringToInt) { ...@@ -649,7 +649,7 @@ TEST(Conv, CorruptedStringToInt) {
try { try {
to<int64_t>(&pc); to<int64_t>(&pc);
EXPECT_TRUE(false); ADD_FAILURE();
} catch (const std::range_error &) { } catch (const std::range_error &) {
} }
} }
...@@ -660,7 +660,7 @@ TEST(Conv, EmptyStringToDouble) { ...@@ -660,7 +660,7 @@ TEST(Conv, EmptyStringToDouble) {
try { try {
to<double>(pc); to<double>(pc);
EXPECT_TRUE(false); ADD_FAILURE();
} catch (const std::range_error &) { } catch (const std::range_error &) {
} }
} }
...@@ -671,7 +671,7 @@ TEST(Conv, IntToDouble) { ...@@ -671,7 +671,7 @@ TEST(Conv, IntToDouble) {
/* This seems not work in ubuntu11.10, gcc 4.6.1 /* This seems not work in ubuntu11.10, gcc 4.6.1
try { try {
auto f = to<float>(957837589847); auto f = to<float>(957837589847);
EXPECT_TRUE(false); ADD_FAILURE();
} catch (std::range_error& e) { } catch (std::range_error& e) {
//LOG(INFO) << e.what(); //LOG(INFO) << e.what();
} }
...@@ -684,7 +684,7 @@ TEST(Conv, DoubleToInt) { ...@@ -684,7 +684,7 @@ TEST(Conv, DoubleToInt) {
try { try {
auto i2 = to<int>(42.1); auto i2 = to<int>(42.1);
LOG(ERROR) << "to<int> returned " << i2 << " instead of throwing"; LOG(ERROR) << "to<int> returned " << i2 << " instead of throwing";
EXPECT_TRUE(false); ADD_FAILURE();
} catch (std::range_error&) { } catch (std::range_error&) {
//LOG(INFO) << e.what(); //LOG(INFO) << e.what();
} }
...@@ -701,7 +701,7 @@ TEST(Conv, EnumToInt) { ...@@ -701,7 +701,7 @@ TEST(Conv, EnumToInt) {
LOG(ERROR) << "to<char> returned " LOG(ERROR) << "to<char> returned "
<< static_cast<unsigned int>(i2) << static_cast<unsigned int>(i2)
<< " instead of throwing"; << " instead of throwing";
EXPECT_TRUE(false); ADD_FAILURE();
} catch (std::range_error&) { } catch (std::range_error&) {
//LOG(INFO) << e.what(); //LOG(INFO) << e.what();
} }
...@@ -726,7 +726,7 @@ TEST(Conv, IntToEnum) { ...@@ -726,7 +726,7 @@ TEST(Conv, IntToEnum) {
LOG(ERROR) << "to<A> returned " LOG(ERROR) << "to<A> returned "
<< static_cast<unsigned int>(i2) << static_cast<unsigned int>(i2)
<< " instead of throwing"; << " instead of throwing";
EXPECT_TRUE(false); ADD_FAILURE();
} catch (std::range_error&) { } catch (std::range_error&) {
//LOG(INFO) << e.what(); //LOG(INFO) << e.what();
} }
...@@ -743,7 +743,7 @@ TEST(Conv, UnsignedEnum) { ...@@ -743,7 +743,7 @@ TEST(Conv, UnsignedEnum) {
try { try {
auto i = to<int32_t>(x); auto i = to<int32_t>(x);
LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing"; LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
EXPECT_TRUE(false); ADD_FAILURE();
} catch (std::range_error&) { } catch (std::range_error&) {
} }
} }
...@@ -915,7 +915,7 @@ void testConvError( ...@@ -915,7 +915,7 @@ void testConvError(
std::string where = to<std::string>(__FILE__, "(", line, "): "); std::string where = to<std::string>(__FILE__, "(", line, "): ");
try { try {
auto res = expr(); auto res = expr();
EXPECT_TRUE(false) << where << exprStr << " -> " << res; ADD_FAILURE() << where << exprStr << " -> " << res;
} catch (const ConversionError& e) { } catch (const ConversionError& e) {
EXPECT_EQ(code, e.errorCode()) << where << exprStr; EXPECT_EQ(code, e.errorCode()) << where << exprStr;
std::string str(e.what()); std::string str(e.what());
......
...@@ -127,7 +127,7 @@ TEST(Enumerate, EmptyRange) { ...@@ -127,7 +127,7 @@ TEST(Enumerate, EmptyRange) {
std::vector<std::string> v; std::vector<std::string> v;
for (auto it : folly::enumerate(v)) { for (auto it : folly::enumerate(v)) {
(void)it; // Silence warnings. (void)it; // Silence warnings.
EXPECT_TRUE(false); ADD_FAILURE();
} }
} }
......
...@@ -508,10 +508,10 @@ TEST(ExceptionWrapper, handle_std_exception) { ...@@ -508,10 +508,10 @@ TEST(ExceptionWrapper, handle_std_exception) {
bool handled = false; bool handled = false;
auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) { auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::logic_error&) { EXPECT_TRUE(false); }, [](const std::logic_error&) { ADD_FAILURE(); },
[&](const std::runtime_error&) { handled = true; }, [&](const std::runtime_error&) { handled = true; },
[](const std::exception&) { EXPECT_TRUE(false); }, [](const std::exception&) { ADD_FAILURE(); },
[](...) { EXPECT_TRUE(false); }); [](...) { ADD_FAILURE(); });
}; };
expect_runtime_error_yes_catch_all(ew_eptr); expect_runtime_error_yes_catch_all(ew_eptr);
...@@ -526,9 +526,9 @@ TEST(ExceptionWrapper, handle_std_exception) { ...@@ -526,9 +526,9 @@ TEST(ExceptionWrapper, handle_std_exception) {
auto expect_runtime_error_no_catch_all = [&](const exception_wrapper& ew) { auto expect_runtime_error_no_catch_all = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::logic_error&) { EXPECT_TRUE(false); }, [](const std::logic_error&) { ADD_FAILURE(); },
[&](const std::runtime_error&) { handled = true; }, [&](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); expect_runtime_error_no_catch_all(ew_eptr);
...@@ -543,10 +543,10 @@ TEST(ExceptionWrapper, handle_std_exception) { ...@@ -543,10 +543,10 @@ TEST(ExceptionWrapper, handle_std_exception) {
auto expect_runtime_error_catch_non_std = [&](const exception_wrapper& ew) { auto expect_runtime_error_catch_non_std = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::logic_error&) { EXPECT_TRUE(false); }, [](const std::logic_error&) { ADD_FAILURE(); },
[&](const std::runtime_error&) { handled = true; }, [&](const std::runtime_error&) { handled = true; },
[](const std::exception&) { EXPECT_TRUE(false); }, [](const std::exception&) { ADD_FAILURE(); },
[](const int&) { EXPECT_TRUE(false); }); [](const int&) { ADD_FAILURE(); });
}; };
expect_runtime_error_catch_non_std(ew_eptr); expect_runtime_error_catch_non_std(ew_eptr);
...@@ -563,12 +563,12 @@ TEST(ExceptionWrapper, handle_std_exception) { ...@@ -563,12 +563,12 @@ TEST(ExceptionWrapper, handle_std_exception) {
// outer handler: // outer handler:
auto expect_runtime_error_rethrow = [&](const exception_wrapper& ew) { auto expect_runtime_error_rethrow = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::logic_error&) { EXPECT_TRUE(false); }, [](const std::logic_error&) { ADD_FAILURE(); },
[&](const std::runtime_error& e) { [&](const std::runtime_error& e) {
handled = true; handled = true;
throw e; throw e;
}, },
[](const std::exception&) { EXPECT_TRUE(false); }); [](const std::exception&) { ADD_FAILURE(); });
}; };
EXPECT_THROW(expect_runtime_error_rethrow(ew_eptr), std::runtime_error); EXPECT_THROW(expect_runtime_error_rethrow(ew_eptr), std::runtime_error);
...@@ -589,8 +589,8 @@ TEST(ExceptionWrapper, handle_std_exception_unhandled) { ...@@ -589,8 +589,8 @@ TEST(ExceptionWrapper, handle_std_exception_unhandled) {
bool handled = false; bool handled = false;
auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) { auto expect_runtime_error_yes_catch_all = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::logic_error&) { EXPECT_TRUE(false); }, [](const std::logic_error&) { ADD_FAILURE(); },
[](const std::runtime_error&) { EXPECT_TRUE(false); }, [](const std::runtime_error&) { ADD_FAILURE(); },
[&](...) { handled = true; }); [&](...) { handled = true; });
}; };
...@@ -610,7 +610,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) { ...@@ -610,7 +610,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) {
auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) { auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::exception&) { EXPECT_TRUE(false); }, [](const std::exception&) { ADD_FAILURE(); },
[&](...) { handled = true; }); [&](...) { handled = true; });
}; };
...@@ -626,7 +626,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) { ...@@ -626,7 +626,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) {
auto expect_int_no_catch_all = [&](const exception_wrapper& ew) { auto expect_int_no_catch_all = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::exception&) { EXPECT_TRUE(false); }, [](const std::exception&) { ADD_FAILURE(); },
[&](const int&) { handled = true; }); [&](const int&) { handled = true; });
}; };
...@@ -643,7 +643,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) { ...@@ -643,7 +643,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_small) {
auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) { auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[&](const int&) { handled = true; }, [&](const int&) { handled = true; },
[](const std::exception&) { EXPECT_TRUE(false); }); [](const std::exception&) { ADD_FAILURE(); });
}; };
expect_int_no_catch_all_2(ew_eptr1); expect_int_no_catch_all_2(ew_eptr1);
...@@ -665,7 +665,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) { ...@@ -665,7 +665,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) {
auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) { auto expect_int_yes_catch_all = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::exception&) { EXPECT_TRUE(false); }, [](const std::exception&) { ADD_FAILURE(); },
[&](...) { handled = true; }); [&](...) { handled = true; });
}; };
...@@ -681,7 +681,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) { ...@@ -681,7 +681,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) {
auto expect_int_no_catch_all = [&](const exception_wrapper& ew) { auto expect_int_no_catch_all = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[](const std::exception&) { EXPECT_TRUE(false); }, [](const std::exception&) { ADD_FAILURE(); },
[&](const BigNonStdError&) { handled = true; }); [&](const BigNonStdError&) { handled = true; });
}; };
...@@ -698,7 +698,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) { ...@@ -698,7 +698,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_big) {
auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) { auto expect_int_no_catch_all_2 = [&](const exception_wrapper& ew) {
ew.handle( ew.handle(
[&](const BigNonStdError&) { handled = true; }, [&](const BigNonStdError&) { handled = true; },
[](const std::exception&) { EXPECT_TRUE(false); }); [](const std::exception&) { ADD_FAILURE(); });
}; };
expect_int_no_catch_all_2(ew_eptr1); expect_int_no_catch_all_2(ew_eptr1);
...@@ -724,7 +724,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) { ...@@ -724,7 +724,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) {
handled = true; handled = true;
throw e; throw e;
}, },
[](const BaseException&) { EXPECT_TRUE(false); }), [](const BaseException&) { ADD_FAILURE(); }),
DerivedException); DerivedException);
EXPECT_TRUE(handled); EXPECT_TRUE(handled);
handled = false; handled = false;
...@@ -734,7 +734,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) { ...@@ -734,7 +734,7 @@ TEST(ExceptionWrapper, handle_non_std_exception_rethrow_base_derived) {
handled = true; handled = true;
throw e; throw e;
}, },
[](...) { EXPECT_TRUE(false); }), [](...) { ADD_FAILURE(); }),
DerivedException); DerivedException);
EXPECT_TRUE(handled); EXPECT_TRUE(handled);
} }
......
...@@ -676,7 +676,7 @@ TEST(Expected, Then) { ...@@ -676,7 +676,7 @@ TEST(Expected, Then) {
// Error case: // Error case:
Expected<int, E> ex = Expected<std::unique_ptr<int>, E>{ Expected<int, E> ex = Expected<std::unique_ptr<int>, E>{
unexpected, E::E1}.then([](std::unique_ptr<int> p) -> int { unexpected, E::E1}.then([](std::unique_ptr<int> p) -> int {
EXPECT_TRUE(false); ADD_FAILURE();
return *p; return *p;
}); });
EXPECT_FALSE(bool(ex)); EXPECT_FALSE(bool(ex));
......
...@@ -118,21 +118,21 @@ TEST(File, Truthy) { ...@@ -118,21 +118,21 @@ TEST(File, Truthy) {
if (temp) { if (temp) {
; ;
} else { } else {
EXPECT_FALSE(true); ADD_FAILURE();
} }
if (File file = File::temporary()) { if (File file = File::temporary()) {
; ;
} else { } else {
EXPECT_FALSE(true); ADD_FAILURE();
} }
EXPECT_FALSE(bool(File())); EXPECT_FALSE(bool(File()));
if (File()) { if (File()) {
EXPECT_TRUE(false); ADD_FAILURE();
} }
if (File notOpened = File()) { if (File notOpened = File()) {
EXPECT_TRUE(false); ADD_FAILURE();
} }
} }
......
...@@ -227,7 +227,7 @@ TEST(LifoSem, shutdown_multi) { ...@@ -227,7 +227,7 @@ TEST(LifoSem, shutdown_multi) {
threads.push_back(DSched::thread([&]{ threads.push_back(DSched::thread([&]{
try { try {
a.wait(); a.wait();
EXPECT_TRUE(false); ADD_FAILURE();
} catch (ShutdownSemError&) { } catch (ShutdownSemError&) {
// expected // expected
EXPECT_TRUE(a.isShutdown()); EXPECT_TRUE(a.isShutdown());
......
...@@ -1009,7 +1009,7 @@ void createProtectedBuf(StringPiece& contents, char** buf) { ...@@ -1009,7 +1009,7 @@ void createProtectedBuf(StringPiece& contents, char** buf) {
const size_t kSuccess = 0; const size_t kSuccess = 0;
char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize); char* pageAlignedBuf = (char*)aligned_malloc(2 * kPageSize, kPageSize);
if (pageAlignedBuf == nullptr) { if (pageAlignedBuf == nullptr) {
ASSERT_FALSE(true); FAIL();
} }
// Protect the page after the first full page-aligned region of the // Protect the page after the first full page-aligned region of the
// malloc'ed buffer // malloc'ed buffer
......
...@@ -420,7 +420,7 @@ TEST(PrettyToDouble, Basic) { ...@@ -420,7 +420,7 @@ TEST(PrettyToDouble, Basic) {
try{ try{
recoveredX = prettyToDouble(testString, formatType); recoveredX = prettyToDouble(testString, formatType);
} catch (const std::range_error& ex) { } 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) : double relativeError = fabs(x) < 1e-5 ? (x-recoveredX) :
(x - recoveredX) / x; (x - recoveredX) / x;
...@@ -438,7 +438,7 @@ TEST(PrettyToDouble, Basic) { ...@@ -438,7 +438,7 @@ TEST(PrettyToDouble, Basic) {
recoveredX = prettyToDouble(prettyPrint(x, formatType, addSpace), recoveredX = prettyToDouble(prettyPrint(x, formatType, addSpace),
formatType); formatType);
} catch (std::range_error&) { } catch (std::range_error&) {
EXPECT_TRUE(false); ADD_FAILURE();
} }
double relativeError = (x - recoveredX) / x; double relativeError = (x - recoveredX) / x;
EXPECT_NEAR(0, relativeError, 1e-3); EXPECT_NEAR(0, relativeError, 1e-3);
......
...@@ -530,7 +530,7 @@ TEST(ThreadLocal, Fork) { ...@@ -530,7 +530,7 @@ TEST(ThreadLocal, Fork) {
EXPECT_TRUE(WIFEXITED(status)); EXPECT_TRUE(WIFEXITED(status));
EXPECT_EQ(0, WEXITSTATUS(status)); EXPECT_EQ(0, WEXITSTATUS(status));
} else { } else {
EXPECT_TRUE(false) << "fork failed"; ADD_FAILURE() << "fork failed";
} }
EXPECT_EQ(2, totalValue()); EXPECT_EQ(2, totalValue());
...@@ -573,7 +573,7 @@ TEST(ThreadLocal, Fork2) { ...@@ -573,7 +573,7 @@ TEST(ThreadLocal, Fork2) {
EXPECT_TRUE(WIFEXITED(status)); EXPECT_TRUE(WIFEXITED(status));
EXPECT_EQ(0, WEXITSTATUS(status)); EXPECT_EQ(0, WEXITSTATUS(status));
} else { } else {
EXPECT_TRUE(false) << "fork failed"; ADD_FAILURE() << "fork failed";
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment