chromium/third_party/abseil-cpp/absl/status/statusor_test.cc

// Copyright 2020 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "absl/status/statusor.h"

#include <array>
#include <cstddef>
#include <initializer_list>
#include <map>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/casts.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/any.h"
#include "absl/types/variant.h"
#include "absl/utility/utility.h"

namespace {

IsOk;
IsOkAndHolds;
AllOf;
AnyOf;
AnyWith;
ElementsAre;
EndsWith;
Field;
HasSubstr;
Ne;
Not;
Pointee;
StartsWith;
VariantWith;

struct CopyDetector {};

testing::Matcher<const CopyDetector&> CopyDetectorHas(int a, bool b, bool c) {}

class Base1 {};

class Base2 {};

class Derived : public Base1, public Base2 {};

class CopyNoAssign {};

absl::StatusOr<std::unique_ptr<int>> ReturnUniquePtr() {}

TEST(StatusOr, ElementType) {}

TEST(StatusOr, TestMoveOnlyInitialization) {}

TEST(StatusOr, TestMoveOnlyValueExtraction) {}

TEST(StatusOr, TestMoveOnlyInitializationFromTemporaryByValueOrDie) {}

TEST(StatusOr, TestValueOrDieOverloadForConstTemporary) {}

TEST(StatusOr, TestMoveOnlyConversion) {}

TEST(StatusOr, TestMoveOnlyVector) {}

TEST(StatusOr, TestDefaultCtor) {}

TEST(StatusOr, StatusCtorForwards) {}

TEST(BadStatusOrAccessTest, CopyConstructionWhatOk) {}

TEST(BadStatusOrAccessTest, CopyAssignmentWhatOk) {}

TEST(BadStatusOrAccessTest, MoveConstructionWhatOk) {}

TEST(BadStatusOrAccessTest, MoveAssignmentWhatOk) {}

// Define `EXPECT_DEATH_OR_THROW` to test the behavior of `StatusOr::value`,
// which either throws `BadStatusOrAccess` or `LOG(FATAL)` based on whether
// exceptions are enabled.
#ifdef ABSL_HAVE_EXCEPTIONS
#define EXPECT_DEATH_OR_THROW
#else  // ABSL_HAVE_EXCEPTIONS
#define EXPECT_DEATH_OR_THROW(statement, status)
#endif  // ABSL_HAVE_EXCEPTIONS

TEST(StatusOrDeathTest, TestDefaultCtorValue) {}

TEST(StatusOrDeathTest, TestValueNotOk) {}

TEST(StatusOrDeathTest, TestValueNotOkConst) {}

TEST(StatusOrDeathTest, TestPointerDefaultCtorValue) {}

TEST(StatusOrDeathTest, TestPointerValueNotOk) {}

TEST(StatusOrDeathTest, TestPointerValueNotOkConst) {}

#if GTEST_HAS_DEATH_TEST
TEST(StatusOrDeathTest, TestStatusCtorStatusOk) {}

TEST(StatusOrDeathTest, TestPointerStatusCtorStatusOk) {}
#endif

TEST(StatusOr, ValueAccessor) {}

TEST(StatusOr, BadValueAccess) {}

TEST(StatusOr, TestStatusCtor) {}

TEST(StatusOr, TestValueCtor) {}

struct Foo {};

TEST(StatusOr, InPlaceConstruction) {}

struct InPlaceHelper {};

TEST(StatusOr, InPlaceInitListConstruction) {}

TEST(StatusOr, Emplace) {}

TEST(StatusOr, EmplaceInitializerList) {}

TEST(StatusOr, TestCopyCtorStatusOk) {}

TEST(StatusOr, TestCopyCtorStatusNotOk) {}

TEST(StatusOr, TestCopyCtorNonAssignable) {}

TEST(StatusOr, TestCopyCtorStatusOKConverting) {}

TEST(StatusOr, TestCopyCtorStatusNotOkConverting) {}

TEST(StatusOr, TestAssignmentStatusOk) {}

TEST(StatusOr, TestAssignmentStatusNotOk) {}

TEST(StatusOr, TestAssignmentStatusOKConverting) {}

struct A {};

struct ImplicitConstructibleFromA {};

TEST(StatusOr, ImplicitConvertingConstructor) {}

struct ExplicitConstructibleFromA {};

TEST(StatusOr, ExplicitConvertingConstructor) {}

struct ImplicitConstructibleFromBool {};

struct ConvertibleToBool {};

TEST(StatusOr, ImplicitBooleanConstructionWithImplicitCasts) {}

TEST(StatusOr, BooleanConstructionWithImplicitCasts) {}

TEST(StatusOr, ConstImplicitCast) {}

TEST(StatusOr, ConstExplicitConstruction) {}

struct ExplicitConstructibleFromInt {};

TEST(StatusOr, ExplicitConstruction) {}

TEST(StatusOr, ImplicitConstruction) {}

TEST(StatusOr, ImplicitConstructionFromInitliazerList) {}

TEST(StatusOr, UniquePtrImplicitConstruction) {}

TEST(StatusOr, NestedStatusOrCopyAndMoveConstructorTests) {}

TEST(StatusOr, NestedStatusOrCopyAndMoveAssignment) {}

struct Copyable {};

struct MoveOnly {};

struct NonMovable {};

TEST(StatusOr, CopyAndMoveAbility) {}

TEST(StatusOr, StatusOrAnyCopyAndMoveConstructorTests) {}

TEST(StatusOr, StatusOrAnyCopyAndMoveAssignment) {}

TEST(StatusOr, StatusOrCopyAndMoveTestsConstructor) {}

TEST(StatusOr, StatusOrCopyAndMoveTestsAssignment) {}

TEST(StatusOr, AbslAnyAssignment) {}

TEST(StatusOr, ImplicitAssignment) {}

TEST(StatusOr, SelfDirectInitAssignment) {}

TEST(StatusOr, ImplicitCastFromInitializerList) {}

TEST(StatusOr, UniquePtrImplicitAssignment) {}

TEST(StatusOr, Pointer) {}

TEST(StatusOr, TestAssignmentStatusNotOkConverting) {}

TEST(StatusOr, SelfAssignment) {}

// These types form the overload sets of the constructors and the assignment
// operators of `MockValue`. They distinguish construction from assignment,
// lvalue from rvalue.
struct FromConstructibleAssignableLvalue {};
struct FromConstructibleAssignableRvalue {};
struct FromImplicitConstructibleOnly {};
struct FromAssignableOnly {};

// This class is for testing the forwarding value assignments of `StatusOr`.
// `from_rvalue` indicates whether the constructor or the assignment taking
// rvalue reference is called. `from_assignment` indicates whether any
// assignment is called.
struct MockValue {};

// operator=(U&&)
TEST(StatusOr, PerfectForwardingAssignment) {}

TEST(StatusOr, TestStatus) {}

TEST(StatusOr, OperatorStarRefQualifiers) {}

TEST(StatusOr, OperatorStar) {}

TEST(StatusOr, OperatorArrowQualifiers) {}

TEST(StatusOr, OperatorArrow) {}

TEST(StatusOr, RValueStatus) {}

TEST(StatusOr, TestValue) {}

TEST(StatusOr, TestValueConst) {}

TEST(StatusOr, TestPointerDefaultCtor) {}

TEST(StatusOr, TestPointerStatusCtor) {}

TEST(StatusOr, TestPointerValueCtor) {}

TEST(StatusOr, TestPointerCopyCtorStatusOk) {}

TEST(StatusOr, TestPointerCopyCtorStatusNotOk) {}

TEST(StatusOr, TestPointerCopyCtorStatusOKConverting) {}

TEST(StatusOr, TestPointerCopyCtorStatusNotOkConverting) {}

TEST(StatusOr, TestPointerAssignmentStatusOk) {}

TEST(StatusOr, TestPointerAssignmentStatusNotOk) {}

TEST(StatusOr, TestPointerAssignmentStatusOKConverting) {}

TEST(StatusOr, TestPointerAssignmentStatusNotOkConverting) {}

TEST(StatusOr, TestPointerStatus) {}

TEST(StatusOr, TestPointerValue) {}

TEST(StatusOr, TestPointerValueConst) {}

TEST(StatusOr, StatusOrVectorOfUniquePointerCanReserveAndResize) {}

TEST(StatusOr, ConstPayload) {}

TEST(StatusOr, MapToStatusOrUniquePtr) {}

TEST(StatusOr, ValueOrOk) {}

TEST(StatusOr, ValueOrDefault) {}

TEST(StatusOr, MoveOnlyValueOrOk) {}

TEST(StatusOr, MoveOnlyValueOrDefault) {}

static absl::StatusOr<int> MakeStatus() {}

TEST(StatusOr, TestIgnoreError) {}

TEST(StatusOr, EqualityOperator) {}

struct MyType {};

enum class ConvTraits {};

// This class has conversion operator to `StatusOr<T>` based on value of
// `conv_traits`.
template <typename T, ConvTraits conv_traits = ConvTraits::kNone>
struct StatusOrConversionBase {};

StatusOrConversionBase<T, ConvTraits::kImplicit>;

StatusOrConversionBase<T, ConvTraits::kExplicit>;

// This class has conversion operator to `T` based on the value of
// `conv_traits`.
template <typename T, ConvTraits conv_traits = ConvTraits::kNone>
struct ConversionBase {};

ConversionBase<T, ConvTraits::kImplicit>;

ConversionBase<T, ConvTraits::kExplicit>;

// This class has conversion operator to `absl::Status` based on the value of
// `conv_traits`.
template <ConvTraits conv_traits = ConvTraits::kNone>
struct StatusConversionBase {};

template <>
struct StatusConversionBase<ConvTraits::kImplicit> {};

template <>
struct StatusConversionBase<ConvTraits::kExplicit> {};

static constexpr int kConvToStatus =;
static constexpr int kConvToStatusOr =;
static constexpr int kConvToT =;
static constexpr int kConvExplicit =;

constexpr ConvTraits GetConvTraits(int bit, int config) {}

// This class conditionally has conversion operator to `absl::Status`, `T`,
// `StatusOr<T>`, based on values of the template parameters.
template <typename T, int config>
struct CustomType
    : StatusOrConversionBase<T, GetConvTraits(kConvToStatusOr, config)>,
      ConversionBase<T, GetConvTraits(kConvToT, config)>,
      StatusConversionBase<GetConvTraits(kConvToStatus, config)> {};

struct ConvertibleToAnyStatusOr {};

// Test the rank of overload resolution for `StatusOr<T>` constructor and
// assignment, from highest to lowest:
// 1. T/Status
// 2. U that has conversion operator to absl::StatusOr<T>
// 3. U that is convertible to Status
// 4. U that is convertible to T
TEST(StatusOr, ConstructionFromT) {}

// Construct absl::StatusOr<T> from U when U is explicitly convertible to T
TEST(StatusOr, ConstructionFromTypeConvertibleToT) {}

// Construct absl::StatusOr<T> from U when U has explicit conversion operator to
// absl::StatusOr<T>
TEST(StatusOr, ConstructionFromTypeWithConversionOperatorToStatusOrT) {}

TEST(StatusOr, ConstructionFromTypeConvertibleToStatus) {}

TEST(StatusOr, AssignmentFromT) {}

TEST(StatusOr, AssignmentFromTypeConvertibleToT) {}

TEST(StatusOr, AssignmentFromTypeWithConversionOperatortoStatusOrT) {}

TEST(StatusOr, AssignmentFromTypeConvertibleToStatus) {}

TEST(StatusOr, StatusAssignmentFromStatusError) {}

#if GTEST_HAS_DEATH_TEST
TEST(StatusOr, StatusAssignmentFromStatusOk) {}
#endif

TEST(StatusOr, StatusAssignmentFromTypeConvertibleToStatus) {}

struct PrintTestStruct {};

TEST(StatusOr, OkPrinting) {}

TEST(StatusOr, ErrorPrinting) {}

}  // namespace