1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <google/protobuf/stubs/status.h>
31
32 #include <stdio.h>
33
34 #include <google/protobuf/testing/googletest.h>
35 #include <gtest/gtest.h>
36
37 namespace google {
38 namespace protobuf {
39 namespace {
TEST(Status,Empty)40 TEST(Status, Empty) {
41 util::Status status;
42 EXPECT_EQ(util::error::OK, util::Status::OK.error_code());
43 EXPECT_EQ(util::error::OK, util::Status::OK.code());
44 EXPECT_EQ("OK", util::Status::OK.ToString());
45 }
46
TEST(Status,GenericCodes)47 TEST(Status, GenericCodes) {
48 EXPECT_EQ(util::error::OK, util::Status::OK.error_code());
49 EXPECT_EQ(util::error::OK, util::Status::OK.code());
50 EXPECT_EQ(util::error::CANCELLED, util::Status::CANCELLED.error_code());
51 EXPECT_EQ(util::error::CANCELLED, util::Status::CANCELLED.code());
52 EXPECT_EQ(util::error::UNKNOWN, util::Status::UNKNOWN.error_code());
53 EXPECT_EQ(util::error::UNKNOWN, util::Status::UNKNOWN.code());
54 }
55
TEST(Status,ConstructorZero)56 TEST(Status, ConstructorZero) {
57 util::Status status(util::error::OK, "msg");
58 EXPECT_TRUE(status.ok());
59 EXPECT_EQ("OK", status.ToString());
60 }
61
TEST(Status,CheckOK)62 TEST(Status, CheckOK) {
63 util::Status status;
64 GOOGLE_CHECK_OK(status);
65 GOOGLE_CHECK_OK(status) << "Failed";
66 GOOGLE_DCHECK_OK(status) << "Failed";
67 }
68
TEST(Status,ErrorMessage)69 TEST(Status, ErrorMessage) {
70 util::Status status(util::error::INVALID_ARGUMENT, "");
71 EXPECT_FALSE(status.ok());
72 EXPECT_EQ("", status.error_message().ToString());
73 EXPECT_EQ("", status.message().ToString());
74 EXPECT_EQ("INVALID_ARGUMENT", status.ToString());
75 status = util::Status(util::error::INVALID_ARGUMENT, "msg");
76 EXPECT_FALSE(status.ok());
77 EXPECT_EQ("msg", status.error_message().ToString());
78 EXPECT_EQ("msg", status.message().ToString());
79 EXPECT_EQ("INVALID_ARGUMENT:msg", status.ToString());
80 status = util::Status(util::error::OK, "msg");
81 EXPECT_TRUE(status.ok());
82 EXPECT_EQ("", status.error_message().ToString());
83 EXPECT_EQ("", status.message().ToString());
84 EXPECT_EQ("OK", status.ToString());
85 }
86
TEST(Status,Copy)87 TEST(Status, Copy) {
88 util::Status a(util::error::UNKNOWN, "message");
89 util::Status b(a);
90 ASSERT_EQ(a.ToString(), b.ToString());
91 }
92
TEST(Status,Assign)93 TEST(Status, Assign) {
94 util::Status a(util::error::UNKNOWN, "message");
95 util::Status b;
96 b = a;
97 ASSERT_EQ(a.ToString(), b.ToString());
98 }
99
TEST(Status,AssignEmpty)100 TEST(Status, AssignEmpty) {
101 util::Status a(util::error::UNKNOWN, "message");
102 util::Status b;
103 a = b;
104 ASSERT_EQ(string("OK"), a.ToString());
105 ASSERT_TRUE(b.ok());
106 ASSERT_TRUE(a.ok());
107 }
108
TEST(Status,EqualsOK)109 TEST(Status, EqualsOK) {
110 ASSERT_EQ(util::Status::OK, util::Status());
111 }
112
TEST(Status,EqualsSame)113 TEST(Status, EqualsSame) {
114 const util::Status a = util::Status(util::error::CANCELLED, "message");
115 const util::Status b = util::Status(util::error::CANCELLED, "message");
116 ASSERT_EQ(a, b);
117 }
118
TEST(Status,EqualsCopy)119 TEST(Status, EqualsCopy) {
120 const util::Status a = util::Status(util::error::CANCELLED, "message");
121 const util::Status b = a;
122 ASSERT_EQ(a, b);
123 }
124
TEST(Status,EqualsDifferentCode)125 TEST(Status, EqualsDifferentCode) {
126 const util::Status a = util::Status(util::error::CANCELLED, "message");
127 const util::Status b = util::Status(util::error::UNKNOWN, "message");
128 ASSERT_NE(a, b);
129 }
130
TEST(Status,EqualsDifferentMessage)131 TEST(Status, EqualsDifferentMessage) {
132 const util::Status a = util::Status(util::error::CANCELLED, "message");
133 const util::Status b = util::Status(util::error::CANCELLED, "another");
134 ASSERT_NE(a, b);
135 }
136 } // namespace
137 } // namespace protobuf
138 } // namespace google
139