From 7f7153c164447c1da96f32f39eaaf5900b8dc743 Mon Sep 17 00:00:00 2001 From: Andreas Pehrson Date: Mon, 6 May 2024 19:54:00 +0000 Subject: [PATCH] Bug 1404972 - To Result add operator==. r=glandium This is needed for using Result with gtest matchers. Differential Revision: https://phabricator.services.mozilla.com/D205958 --- mfbt/Result.h | 9 ++++++++ mfbt/tests/TestResult.cpp | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/mfbt/Result.h b/mfbt/Result.h index 052920fdbf17..fba5db25b958 100644 --- a/mfbt/Result.h +++ b/mfbt/Result.h @@ -834,6 +834,15 @@ class [[nodiscard]] Result final { constexpr auto andThen(F f) -> std::invoke_result_t { return MOZ_LIKELY(isOk()) ? f(unwrap()) : propagateErr(); } + + bool operator==(const Result& aOther) const { + return (isOk() && aOther.isOk() && inspect() == aOther.inspect()) || + (isErr() && aOther.isErr() && inspectErr() == aOther.inspectErr()); + } + + bool operator!=(const Result& aOther) const { + return !(*this == aOther); + } }; /** diff --git a/mfbt/tests/TestResult.cpp b/mfbt/tests/TestResult.cpp index a2e10640c5d9..f03f06b03cd6 100644 --- a/mfbt/tests/TestResult.cpp +++ b/mfbt/tests/TestResult.cpp @@ -852,6 +852,50 @@ void UpcastTest() { } } +void EqualityTest() { + { + Result result(1); + Result other(1); + + MOZ_RELEASE_ASSERT(result == other); + } + + { + Result result(true); + Result other(true); + + MOZ_RELEASE_ASSERT(result == other); + } + + { + Result result(1); + Result other(2); + + MOZ_RELEASE_ASSERT(result != other); + } + + { + Result result(true); + Result other(false); + + MOZ_RELEASE_ASSERT(result != other); + } + + { + Result result(0); + Result other(false); + + MOZ_RELEASE_ASSERT(result != other); + } + + { + Result result(1); + Result other(1u); + + MOZ_RELEASE_ASSERT(result != other); + } +} + /* * */ int main() { @@ -866,5 +910,6 @@ int main() { UniquePtrTest(); ZeroIsEmptyErrorTest(); UpcastTest(); + EqualityTest(); return 0; }