| 1 |
// |
| 2 |
// Copyright 2022 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include "src/core/lib/gprpp/validation_errors.h" |
| 18 |
|
| 19 |
#include "gtest/gtest.h" |
| 20 |
|
| 21 |
#include "test/core/util/test_config.h" |
| 22 |
|
| 23 |
namespace grpc_core { |
| 24 |
namespace testing { |
| 25 |
namespace { |
| 26 |
|
| 27 |
TEST(ValidationErrors, NoErrors) { |
| 28 |
ValidationErrors errors; |
| 29 |
EXPECT_TRUE(errors.ok()); |
| 30 |
EXPECT_EQ(errors.size(), 0); |
| 31 |
{ |
| 32 |
ValidationErrors::ScopedField field(&errors, "foo"); |
| 33 |
{ ValidationErrors::ScopedField field(&errors, ".bar"); } |
| 34 |
} |
| 35 |
EXPECT_TRUE(errors.ok()); |
| 36 |
EXPECT_EQ(errors.size(), 0); |
| 37 |
absl::Status status = errors.status("errors validating config"); |
| 38 |
EXPECT_TRUE(status.ok()) << status; |
| 39 |
} |
| 40 |
|
| 41 |
TEST(ValidationErrors, OneError) { |
| 42 |
ValidationErrors errors; |
| 43 |
{ |
| 44 |
ValidationErrors::ScopedField field(&errors, "foo"); |
| 45 |
{ |
| 46 |
ValidationErrors::ScopedField field(&errors, ".bar"); |
| 47 |
errors.AddError("value smells funny"); |
| 48 |
} |
| 49 |
} |
| 50 |
EXPECT_FALSE(errors.ok()); |
| 51 |
EXPECT_EQ(errors.size(), 1); |
| 52 |
absl::Status status = errors.status("errors validating config"); |
| 53 |
EXPECT_EQ(status.code(), absl::StatusCode::kInvalidArgument); |
| 54 |
EXPECT_EQ( |
| 55 |
status.message(), |
| 56 |
"errors validating config: [field:foo.bar error:value smells funny]") |
| 57 |
<< status; |
| 58 |
} |
| 59 |
|
| 60 |
TEST(ValidationErrors, MultipleErrorsForSameField) { |
| 61 |
ValidationErrors errors; |
| 62 |
{ |
| 63 |
ValidationErrors::ScopedField field(&errors, "foo"); |
| 64 |
{ |
| 65 |
ValidationErrors::ScopedField field(&errors, ".bar"); |
| 66 |
errors.AddError("value smells funny"); |
| 67 |
errors.AddError("value is ugly"); |
| 68 |
} |
| 69 |
} |
| 70 |
EXPECT_FALSE(errors.ok()); |
| 71 |
EXPECT_EQ(errors.size(), 1); |
| 72 |
absl::Status status = errors.status("errors validating config"); |
| 73 |
EXPECT_EQ(status.code(), absl::StatusCode::kInvalidArgument); |
| 74 |
EXPECT_EQ(status.message(), |
| 75 |
"errors validating config: [field:foo.bar errors:[" |
| 76 |
"value smells funny; value is ugly]]") |
| 77 |
<< status; |
| 78 |
} |
| 79 |
|
| 80 |
TEST(ValidationErrors, ErrorsForMultipleFields) { |
| 81 |
ValidationErrors errors; |
| 82 |
{ |
| 83 |
ValidationErrors::ScopedField field(&errors, "foo"); |
| 84 |
{ |
| 85 |
ValidationErrors::ScopedField field(&errors, ".bar"); |
| 86 |
errors.AddError("value smells funny"); |
| 87 |
} |
| 88 |
errors.AddError("too hot"); |
| 89 |
} |
| 90 |
EXPECT_FALSE(errors.ok()); |
| 91 |
EXPECT_EQ(errors.size(), 2); |
| 92 |
absl::Status status = errors.status("errors validating config"); |
| 93 |
EXPECT_EQ(status.code(), absl::StatusCode::kInvalidArgument); |
| 94 |
EXPECT_EQ(status.message(), |
| 95 |
"errors validating config: [" |
| 96 |
"field:foo error:too hot; field:foo.bar error:value smells funny]") |
| 97 |
<< status; |
| 98 |
} |
| 99 |
|
| 100 |
} // namespace |
| 101 |
} // namespace testing |
| 102 |
} // namespace grpc_core |
| 103 |
|
| 104 |
int main(int argc, char** argv) { |
| 105 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 106 |
::testing::InitGoogleTest(&argc, argv); |
| 107 |
return RUN_ALL_TESTS(); |
| 108 |
} |