status_macros_test.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2020 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. // http://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. #include "status_macros.h"
  15. #include <sstream>
  16. #include <gmock/gmock.h>
  17. #include <gtest/gtest.h>
  18. #include "absl/status/status.h"
  19. #include "statusor.h"
  20. namespace rlwe {
  21. namespace {
  22. TEST(StatusMacrosTest, TestAssignOrReturn) {
  23. StatusOr<StatusOr<int>> a(StatusOr<int>(2));
  24. auto f = [&]() -> absl::Status {
  25. RLWE_ASSIGN_OR_RETURN(StatusOr<int> status_or_a, a.value());
  26. EXPECT_EQ(2, status_or_a.value());
  27. return absl::OkStatus();
  28. };
  29. auto status = f();
  30. EXPECT_TRUE(status.ok()) << status;
  31. }
  32. TEST(StatusMacrosTest, TestAssignOrReturnFails) {
  33. auto a = []() -> StatusOr<int> { return absl::InternalError("error"); };
  34. auto f = [&]() -> absl::Status {
  35. RLWE_ASSIGN_OR_RETURN(auto result, a());
  36. result++;
  37. return absl::OkStatus();
  38. };
  39. auto status = f();
  40. EXPECT_EQ(absl::StatusCode::kInternal, status.code());
  41. EXPECT_EQ("error", status.message());
  42. }
  43. } // namespace
  44. } // namespace rlwe