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
This commit is contained in:
@@ -834,6 +834,15 @@ class [[nodiscard]] Result final {
|
|||||||
constexpr auto andThen(F f) -> std::invoke_result_t<F, V&&> {
|
constexpr auto andThen(F f) -> std::invoke_result_t<F, V&&> {
|
||||||
return MOZ_LIKELY(isOk()) ? f(unwrap()) : propagateErr();
|
return MOZ_LIKELY(isOk()) ? f(unwrap()) : propagateErr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(const Result<V, E>& aOther) const {
|
||||||
|
return (isOk() && aOther.isOk() && inspect() == aOther.inspect()) ||
|
||||||
|
(isErr() && aOther.isErr() && inspectErr() == aOther.inspectErr());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const Result<V, E>& aOther) const {
|
||||||
|
return !(*this == aOther);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -852,6 +852,50 @@ void UpcastTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EqualityTest() {
|
||||||
|
{
|
||||||
|
Result<int, bool> result(1);
|
||||||
|
Result<int, bool> other(1);
|
||||||
|
|
||||||
|
MOZ_RELEASE_ASSERT(result == other);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Result<int, bool> result(true);
|
||||||
|
Result<int, bool> other(true);
|
||||||
|
|
||||||
|
MOZ_RELEASE_ASSERT(result == other);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Result<int, bool> result(1);
|
||||||
|
Result<int, bool> other(2);
|
||||||
|
|
||||||
|
MOZ_RELEASE_ASSERT(result != other);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Result<int, bool> result(true);
|
||||||
|
Result<int, bool> other(false);
|
||||||
|
|
||||||
|
MOZ_RELEASE_ASSERT(result != other);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Result<int, bool> result(0);
|
||||||
|
Result<int, bool> other(false);
|
||||||
|
|
||||||
|
MOZ_RELEASE_ASSERT(result != other);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Result<int, unsigned int> result(1);
|
||||||
|
Result<int, unsigned int> other(1u);
|
||||||
|
|
||||||
|
MOZ_RELEASE_ASSERT(result != other);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* * */
|
/* * */
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@@ -866,5 +910,6 @@ int main() {
|
|||||||
UniquePtrTest();
|
UniquePtrTest();
|
||||||
ZeroIsEmptyErrorTest();
|
ZeroIsEmptyErrorTest();
|
||||||
UpcastTest();
|
UpcastTest();
|
||||||
|
EqualityTest();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user