2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "absl/memory/memory.h"
|
2020-04-28 21:12:31 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-03 22:40:09 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
|
|
|
#include "sandboxed_api/util/status.h"
|
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(ReturnIfError, ReturnsOnErrorStatus) {
|
2020-02-28 01:23:44 +08:00
|
|
|
auto func = []() -> absl::Status {
|
|
|
|
SAPI_RETURN_IF_ERROR(absl::OkStatus());
|
|
|
|
SAPI_RETURN_IF_ERROR(absl::OkStatus());
|
2020-04-28 21:12:31 +08:00
|
|
|
SAPI_RETURN_IF_ERROR(absl::UnknownError("EXPECTED"));
|
|
|
|
return absl::UnknownError("ERROR");
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(func(), StatusIs(absl::StatusCode::kUnknown, "EXPECTED"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ReturnIfError, ReturnsOnErrorFromLambda) {
|
2020-02-28 01:23:44 +08:00
|
|
|
auto func = []() -> absl::Status {
|
|
|
|
SAPI_RETURN_IF_ERROR([] { return absl::OkStatus(); }());
|
2020-04-28 21:12:31 +08:00
|
|
|
SAPI_RETURN_IF_ERROR([] { return absl::UnknownError("EXPECTED"); }());
|
|
|
|
return absl::UnknownError("ERROR");
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(func(), StatusIs(absl::StatusCode::kUnknown, "EXPECTED"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssignOrReturn, AssignsMultipleVariablesInSequence) {
|
2020-02-28 01:23:44 +08:00
|
|
|
auto func = []() -> absl::Status {
|
2019-03-19 00:21:48 +08:00
|
|
|
int value1;
|
2020-09-03 22:40:09 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(value1, absl::StatusOr<int>(1));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(1, value1);
|
|
|
|
int value2;
|
2020-09-03 22:40:09 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(value2, absl::StatusOr<int>(2));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(2, value2);
|
|
|
|
int value3;
|
2020-09-03 22:40:09 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(value3, absl::StatusOr<int>(3));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(3, value3);
|
|
|
|
int value4;
|
2020-04-28 21:12:31 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(value4,
|
2020-09-03 22:40:09 +08:00
|
|
|
absl::StatusOr<int>(absl::UnknownError("EXPECTED")));
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::UnknownError(absl::StrCat("ERROR: assigned value ", value4));
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(func(), StatusIs(absl::StatusCode::kUnknown, "EXPECTED"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssignOrReturn, AssignsRepeatedlyToSingleVariable) {
|
2020-02-28 01:23:44 +08:00
|
|
|
auto func = []() -> absl::Status {
|
2019-03-19 00:21:48 +08:00
|
|
|
int value = 1;
|
2020-09-03 22:40:09 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(value, absl::StatusOr<int>(2));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(2, value);
|
2020-09-03 22:40:09 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(value, absl::StatusOr<int>(3));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(3, value);
|
2020-09-03 22:40:09 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(value,
|
|
|
|
absl::StatusOr<int>(absl::UnknownError("EXPECTED")));
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::UnknownError("ERROR");
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(func(), StatusIs(absl::StatusCode::kUnknown, "EXPECTED"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssignOrReturn, MovesUniquePtr) {
|
2020-02-28 01:23:44 +08:00
|
|
|
auto func = []() -> absl::Status {
|
2019-03-19 00:21:48 +08:00
|
|
|
std::unique_ptr<int> ptr;
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
2020-09-03 22:40:09 +08:00
|
|
|
ptr, absl::StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(1)));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(*ptr, 1);
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::UnknownError("EXPECTED");
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(func(), StatusIs(absl::StatusCode::kUnknown, "EXPECTED"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssignOrReturn, DoesNotAssignUniquePtrOnErrorStatus) {
|
2020-02-28 01:23:44 +08:00
|
|
|
auto func = []() -> absl::Status {
|
2019-03-19 00:21:48 +08:00
|
|
|
std::unique_ptr<int> ptr;
|
2020-09-03 22:40:09 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(ptr, absl::StatusOr<std::unique_ptr<int>>(
|
|
|
|
absl::UnknownError("EXPECTED")));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(ptr, nullptr);
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(func(), StatusIs(absl::StatusCode::kUnknown, "EXPECTED"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssignOrReturn, MovesUniquePtrRepeatedlyToSingleVariable) {
|
2020-02-28 01:23:44 +08:00
|
|
|
auto func = []() -> absl::Status {
|
2019-03-19 00:21:48 +08:00
|
|
|
std::unique_ptr<int> ptr;
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
2020-09-03 22:40:09 +08:00
|
|
|
ptr, absl::StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(1)));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(*ptr, 1);
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
2020-09-03 22:40:09 +08:00
|
|
|
ptr, absl::StatusOr<std::unique_ptr<int>>(absl::make_unique<int>(2)));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_EQ(*ptr, 2);
|
2020-04-28 21:12:31 +08:00
|
|
|
return absl::UnknownError("EXPECTED");
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(func(), StatusIs(absl::StatusCode::kUnknown, "EXPECTED"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace sapi
|