1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "runtime/cpp/emboss_maybe.h"
16
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19
20 namespace emboss {
21 namespace support {
22 namespace test {
23
24 enum class Foo : ::std::int64_t {
25 BAR = 1,
26 BAZ = 2,
27 };
28
TEST(Maybe,Known)29 TEST(Maybe, Known) {
30 EXPECT_TRUE(Maybe<int>(10).Known());
31 EXPECT_EQ(10, Maybe<int>(10).ValueOr(3));
32 EXPECT_EQ(10, Maybe<int>(10).ValueOrDefault());
33 EXPECT_EQ(10, Maybe<int>(10).Value());
34 EXPECT_TRUE(Maybe<bool>(true).Value());
35 EXPECT_EQ(Foo::BAZ, Maybe<Foo>(Foo::BAZ).ValueOrDefault());
36
37 Maybe<int> x = Maybe<int>(1000);
38 Maybe<int> y = Maybe<int>();
39 y = x;
40 EXPECT_TRUE(y.Known());
41 EXPECT_EQ(1000, y.Value());
42 }
43
TEST(Maybe,Unknown)44 TEST(Maybe, Unknown) {
45 EXPECT_FALSE(Maybe<int>().Known());
46 EXPECT_EQ(3, Maybe<int>().ValueOr(3));
47 EXPECT_EQ(0, Maybe<int>().ValueOrDefault());
48 EXPECT_FALSE(Maybe<bool>().ValueOrDefault());
49 #if EMBOSS_CHECK_ABORTS
50 EXPECT_DEATH(Maybe<int>().Value(), "Known()");
51 #endif // EMBOSS_CHECK_ABORTS
52 EXPECT_FALSE(Maybe<bool>().ValueOrDefault());
53 EXPECT_EQ(static_cast<Foo>(0), Maybe<Foo>().ValueOrDefault());
54
55 Maybe<int> x = Maybe<int>();
56 Maybe<int> y = Maybe<int>(1000);
57 y = x;
58 EXPECT_FALSE(y.Known());
59 }
60
61 } // namespace test
62 } // namespace support
63 } // namespace emboss
64