status_test.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <stdio.h>
  32. #include <google/protobuf/testing/googletest.h>
  33. #include <gtest/gtest.h>
  34. namespace google {
  35. namespace protobuf {
  36. namespace {
  37. TEST(Status, Empty) {
  38. util::Status status;
  39. EXPECT_EQ(util::error::OK, util::Status::OK.error_code());
  40. EXPECT_EQ("OK", util::Status::OK.ToString());
  41. }
  42. TEST(Status, GenericCodes) {
  43. EXPECT_EQ(util::error::OK, util::Status::OK.error_code());
  44. EXPECT_EQ(util::error::CANCELLED, util::Status::CANCELLED.error_code());
  45. EXPECT_EQ(util::error::UNKNOWN, util::Status::UNKNOWN.error_code());
  46. }
  47. TEST(Status, ConstructorZero) {
  48. util::Status status(util::error::OK, "msg");
  49. EXPECT_TRUE(status.ok());
  50. EXPECT_EQ("OK", status.ToString());
  51. }
  52. TEST(Status, CheckOK) {
  53. util::Status status;
  54. GOOGLE_CHECK_OK(status);
  55. GOOGLE_CHECK_OK(status) << "Failed";
  56. GOOGLE_DCHECK_OK(status) << "Failed";
  57. }
  58. TEST(Status, ErrorMessage) {
  59. util::Status status(util::error::INVALID_ARGUMENT, "");
  60. EXPECT_FALSE(status.ok());
  61. EXPECT_EQ("", status.error_message().ToString());
  62. EXPECT_EQ("INVALID_ARGUMENT", status.ToString());
  63. status = util::Status(util::error::INVALID_ARGUMENT, "msg");
  64. EXPECT_FALSE(status.ok());
  65. EXPECT_EQ("msg", status.error_message().ToString());
  66. EXPECT_EQ("INVALID_ARGUMENT:msg", status.ToString());
  67. status = util::Status(util::error::OK, "msg");
  68. EXPECT_TRUE(status.ok());
  69. EXPECT_EQ("", status.error_message().ToString());
  70. EXPECT_EQ("OK", status.ToString());
  71. }
  72. TEST(Status, Copy) {
  73. util::Status a(util::error::UNKNOWN, "message");
  74. util::Status b(a);
  75. ASSERT_EQ(a.ToString(), b.ToString());
  76. }
  77. TEST(Status, Assign) {
  78. util::Status a(util::error::UNKNOWN, "message");
  79. util::Status b;
  80. b = a;
  81. ASSERT_EQ(a.ToString(), b.ToString());
  82. }
  83. TEST(Status, AssignEmpty) {
  84. util::Status a(util::error::UNKNOWN, "message");
  85. util::Status b;
  86. a = b;
  87. ASSERT_EQ(string("OK"), a.ToString());
  88. ASSERT_TRUE(b.ok());
  89. ASSERT_TRUE(a.ok());
  90. }
  91. TEST(Status, EqualsOK) {
  92. ASSERT_EQ(util::Status::OK, util::Status());
  93. }
  94. TEST(Status, EqualsSame) {
  95. const util::Status a = util::Status(util::error::CANCELLED, "message");
  96. const util::Status b = util::Status(util::error::CANCELLED, "message");
  97. ASSERT_EQ(a, b);
  98. }
  99. TEST(Status, EqualsCopy) {
  100. const util::Status a = util::Status(util::error::CANCELLED, "message");
  101. const util::Status b = a;
  102. ASSERT_EQ(a, b);
  103. }
  104. TEST(Status, EqualsDifferentCode) {
  105. const util::Status a = util::Status(util::error::CANCELLED, "message");
  106. const util::Status b = util::Status(util::error::UNKNOWN, "message");
  107. ASSERT_NE(a, b);
  108. }
  109. TEST(Status, EqualsDifferentMessage) {
  110. const util::Status a = util::Status(util::error::CANCELLED, "message");
  111. const util::Status b = util::Status(util::error::CANCELLED, "another");
  112. ASSERT_NE(a, b);
  113. }
  114. } // namespace
  115. } // namespace protobuf
  116. } // namespace google