• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #include "base/test/gmock_expected_support.h"
11 
12 #include <string>
13 
14 #include "base/types/expected.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 namespace base::test {
19 namespace {
20 
TEST(GmockExpectedSupportTest,AssertOkAndAssign)21 TEST(GmockExpectedSupportTest, AssertOkAndAssign) {
22   const expected<int, std::string> e_int = 1;
23   ASSERT_OK_AND_ASSIGN(int result, e_int);
24   EXPECT_EQ(1, result);
25 }
26 
TEST(GmockExpectedSupportTest,VoidOkEquals)27 TEST(GmockExpectedSupportTest, VoidOkEquals) {
28   EXPECT_EQ(ok(), ok());
29   EXPECT_NE(ok(), ok("test"));
30   const expected<void, std::string> is_ok = ok();
31   EXPECT_EQ(ok(), is_ok);
32   EXPECT_EQ(is_ok, ok());
33   const expected<void, std::string> not_ok = unexpected("test");
34   EXPECT_NE(ok(), not_ok);
35   EXPECT_NE(not_ok, ok());
36 }
37 
TEST(GmockExpectedSupportTest,PrintTest)38 TEST(GmockExpectedSupportTest, PrintTest) {
39   EXPECT_EQ(testing::PrintToString(ok()), "ok()");
40   EXPECT_EQ(testing::PrintToString(ok("test")), "ok(test)");
41 
42   EXPECT_EQ(testing::PrintToString(unexpected<std::string>("test")),
43             "Unexpected(test)");
44 
45   EXPECT_EQ(testing::PrintToString(expected<void, std::string>(ok())),
46             "Expected()");
47   EXPECT_EQ(
48       testing::PrintToString(expected<std::string, std::string>(ok("test"))),
49       "Expected(test)");
50   EXPECT_EQ(testing::PrintToString(
51                 expected<std::string, std::string>(unexpected("test"))),
52             "Unexpected(test)");
53 }
54 
TEST(GmockExpectedSupportTest,HasValue)55 TEST(GmockExpectedSupportTest, HasValue) {
56   const expected<void, std::string> e_void;
57   EXPECT_THAT(e_void, HasValue());
58 
59   const expected<int, std::string> e_int = 3;
60   EXPECT_THAT(e_int, HasValue());
61 
62   const expected<int, std::string> error = unexpected("Uh oh");
63   EXPECT_THAT(error, ::testing::Not(HasValue()));
64 }
65 
TEST(GmockExpectedSupportTest,ValueIs)66 TEST(GmockExpectedSupportTest, ValueIs) {
67   const expected<int, std::string> e_int = 3;
68   EXPECT_THAT(e_int, ValueIs(3));
69 
70   const expected<std::string, int> e_string = "OK";
71   EXPECT_THAT(e_string, ValueIs("OK"));
72   EXPECT_THAT(e_string, ::testing::Not(ValueIs("ERROR")));
73 
74   const expected<int, std::string> e_error = unexpected("ERROR");
75   EXPECT_THAT(e_error, ::testing::Not(ValueIs(3)));
76 }
77 
TEST(GmockExpectedSupportTest,ErrorIs)78 TEST(GmockExpectedSupportTest, ErrorIs) {
79   const expected<std::string, int> e_int = unexpected(3);
80   EXPECT_THAT(e_int, ErrorIs(3));
81 
82   const expected<int, std::string> e_string = unexpected("OK");
83   EXPECT_THAT(e_string, ErrorIs("OK"));
84   EXPECT_THAT(e_string, ::testing::Not(ErrorIs("ERROR")));
85 
86   const expected<std::string, int> e_value = "OK";
87   EXPECT_THAT(e_value, ::testing::Not(ErrorIs(3)));
88 }
89 
90 }  // namespace
91 }  // namespace base::test
92