зеркало из https://github.com/mozilla/gecko-dev.git
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
This commit is contained in:
Родитель
08cbb7600c
Коммит
5977b6fdeb
|
@ -1022,6 +1022,34 @@ auto ErrToDefaultOkOrErr(nsresult aValue) -> Result<V, nsresult> {
|
|||
return Err(aValue);
|
||||
}
|
||||
|
||||
// Helper template function so that QM_TRY predicates checking for a specific
|
||||
// error can be concisely written as IsSpecificError<NS_SOME_ERROR> instead of
|
||||
// as a more verbose lambda.
|
||||
template <nsresult ErrorValue>
|
||||
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<SuccessValueToReturn> (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 SuccessValue, typename V = decltype(SuccessValue)>
|
||||
auto ErrToOk(const nsresult aValue) -> Result<V, nsresult> {
|
||||
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 <typename V = mozilla::Ok>
|
||||
auto ErrToDefaultOk(const nsresult aValue) -> Result<V, nsresult> {
|
||||
return V{};
|
||||
}
|
||||
|
||||
// TODO: Maybe move this to mfbt/ResultExtensions.h
|
||||
template <typename R, typename Func, typename... Args>
|
||||
Result<R, nsresult> ToResultGet(const Func& aFunc, Args&&... aArgs) {
|
||||
|
|
|
@ -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>(NS_ERROR_FAILURE)); }
|
||||
|
||||
TEST(QuotaCommon_IsSpecificError, Mismatch)
|
||||
{ EXPECT_FALSE(IsSpecificError<NS_ERROR_FAILURE>(NS_ERROR_UNEXPECTED)); }
|
||||
|
||||
TEST(QuotaCommon_ErrToOk, Bool_True)
|
||||
{
|
||||
auto res = ErrToOk<true>(NS_ERROR_FAILURE);
|
||||
EXPECT_TRUE(res.isOk());
|
||||
EXPECT_EQ(res.unwrap(), true);
|
||||
}
|
||||
|
||||
TEST(QuotaCommon_ErrToOk, Bool_False)
|
||||
{
|
||||
auto res = ErrToOk<false>(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<nullptr, nsCOMPtr<nsISupports>>(NS_ERROR_FAILURE);
|
||||
EXPECT_TRUE(res.isOk());
|
||||
EXPECT_EQ(res.unwrap(), nullptr);
|
||||
}
|
||||
|
||||
TEST(QuotaCommon_ErrToDefaultOk, Ok)
|
||||
{
|
||||
auto res = ErrToDefaultOk<Ok>(NS_ERROR_FAILURE);
|
||||
EXPECT_TRUE(res.isOk());
|
||||
}
|
||||
|
||||
TEST(QuotaCommon_ErrToDefaultOk, NsCOMPtr)
|
||||
{
|
||||
auto res = ErrToDefaultOk<nsCOMPtr<nsISupports>>(NS_ERROR_FAILURE);
|
||||
EXPECT_TRUE(res.isOk());
|
||||
EXPECT_EQ(res.unwrap(), nullptr);
|
||||
}
|
||||
|
||||
class StringPairParameterized
|
||||
: public ::testing::TestWithParam<std::pair<const char*, const char*>> {};
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче