From 5977b6fdebe32451ada35fa2cbd7c0752cfea982 Mon Sep 17 00:00:00 2001 From: Jan Varga Date: Sun, 30 May 2021 06:37:28 +0000 Subject: [PATCH] Bug 1708643 - Add generic predicate and fallback functions for QM_OR_ELSE_(WARN|NOTE|LOG)_IF; r=dom-storage-reviewers,asuth Differential Revision: https://phabricator.services.mozilla.com/D114935 --- dom/quota/QuotaCommon.h | 28 ++++++++++++++ dom/quota/test/gtest/TestQuotaCommon.cpp | 47 ++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/dom/quota/QuotaCommon.h b/dom/quota/QuotaCommon.h index f03b942a9ecf8..c6f186b0e6aa7 100644 --- a/dom/quota/QuotaCommon.h +++ b/dom/quota/QuotaCommon.h @@ -1022,6 +1022,34 @@ auto ErrToDefaultOkOrErr(nsresult aValue) -> Result { return Err(aValue); } +// Helper template function so that QM_TRY predicates checking for a specific +// error can be concisely written as IsSpecificError instead of +// as a more verbose lambda. +template +bool IsSpecificError(const nsresult aValue) { + return aValue == ErrorValue; +} + +// Helper template function so that QM_TRY fallback functions that are +// converting errors into specific in-band success values can be concisely +// written as ErrToOk (with the return type inferred). +// For example, many file-related APIs that access information about a file may +// return an nsresult error code if the file does not exist. From an +// application perspective, the file not existing is not actually exceptional +// and can instead be handled by the success case. +template +auto ErrToOk(const nsresult aValue) -> Result { + return V{SuccessValue}; +} + +// Helper template function so that QM_TRY fallback functions that are +// suppressing errors by converting them into (generic) success can be +// concisely written as ErrToDefaultOk<>. +template +auto ErrToDefaultOk(const nsresult aValue) -> Result { + return V{}; +} + // TODO: Maybe move this to mfbt/ResultExtensions.h template Result ToResultGet(const Func& aFunc, Args&&... aArgs) { diff --git a/dom/quota/test/gtest/TestQuotaCommon.cpp b/dom/quota/test/gtest/TestQuotaCommon.cpp index f196ede1b3a16..a1ea5fde2cd47 100644 --- a/dom/quota/test/gtest/TestQuotaCommon.cpp +++ b/dom/quota/test/gtest/TestQuotaCommon.cpp @@ -1478,6 +1478,53 @@ TEST(QuotaCommon_ErrToDefaultOkOrErr, NsCOMPtr_Err) EXPECT_EQ(res.unwrapErr(), NS_ERROR_UNEXPECTED); } +TEST(QuotaCommon_IsSpecificError, Match) +{ EXPECT_TRUE(IsSpecificError(NS_ERROR_FAILURE)); } + +TEST(QuotaCommon_IsSpecificError, Mismatch) +{ EXPECT_FALSE(IsSpecificError(NS_ERROR_UNEXPECTED)); } + +TEST(QuotaCommon_ErrToOk, Bool_True) +{ + auto res = ErrToOk(NS_ERROR_FAILURE); + EXPECT_TRUE(res.isOk()); + EXPECT_EQ(res.unwrap(), true); +} + +TEST(QuotaCommon_ErrToOk, Bool_False) +{ + auto res = ErrToOk(NS_ERROR_FAILURE); + EXPECT_TRUE(res.isOk()); + EXPECT_EQ(res.unwrap(), false); +} + +TEST(QuotaCommon_ErrToOk, Int_42) +{ + auto res = ErrToOk<42>(NS_ERROR_FAILURE); + EXPECT_TRUE(res.isOk()); + EXPECT_EQ(res.unwrap(), 42); +} + +TEST(QuotaCommon_ErrToOk, NsCOMPtr_nullptr) +{ + auto res = ErrToOk>(NS_ERROR_FAILURE); + EXPECT_TRUE(res.isOk()); + EXPECT_EQ(res.unwrap(), nullptr); +} + +TEST(QuotaCommon_ErrToDefaultOk, Ok) +{ + auto res = ErrToDefaultOk(NS_ERROR_FAILURE); + EXPECT_TRUE(res.isOk()); +} + +TEST(QuotaCommon_ErrToDefaultOk, NsCOMPtr) +{ + auto res = ErrToDefaultOk>(NS_ERROR_FAILURE); + EXPECT_TRUE(res.isOk()); + EXPECT_EQ(res.unwrap(), nullptr); +} + class StringPairParameterized : public ::testing::TestWithParam> {};