// Copyright 2019 Google LLC // // 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 "runtime/cpp/emboss_maybe.h" #include "gmock/gmock.h" #include "gtest/gtest.h" namespace emboss { namespace support { namespace test { enum class Foo : ::std::int64_t { BAR = 1, BAZ = 2, }; TEST(Maybe, Known) { EXPECT_TRUE(Maybe(10).Known()); EXPECT_EQ(10, Maybe(10).ValueOr(3)); EXPECT_EQ(10, Maybe(10).ValueOrDefault()); EXPECT_EQ(10, Maybe(10).Value()); EXPECT_TRUE(Maybe(true).Value()); EXPECT_EQ(Foo::BAZ, Maybe(Foo::BAZ).ValueOrDefault()); Maybe x = Maybe(1000); Maybe y = Maybe(); y = x; EXPECT_TRUE(y.Known()); EXPECT_EQ(1000, y.Value()); } TEST(Maybe, Unknown) { EXPECT_FALSE(Maybe().Known()); EXPECT_EQ(3, Maybe().ValueOr(3)); EXPECT_EQ(0, Maybe().ValueOrDefault()); EXPECT_FALSE(Maybe().ValueOrDefault()); #if EMBOSS_CHECK_ABORTS EXPECT_DEATH(Maybe().Value(), "Known()"); #endif // EMBOSS_CHECK_ABORTS EXPECT_FALSE(Maybe().ValueOrDefault()); EXPECT_EQ(static_cast(0), Maybe().ValueOrDefault()); Maybe x = Maybe(); Maybe y = Maybe(1000); y = x; EXPECT_FALSE(y.Known()); } } // namespace test } // namespace support } // namespace emboss