|
| 1 | +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. |
| 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 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "tensorflow/core/util/proto/proto_utils.h" |
| 17 | + |
| 18 | +#include <gmock/gmock.h> |
| 19 | +#include "tensorflow/core/lib/core/status_test_util.h" |
| 20 | +#include "tensorflow/core/platform/protobuf.h" |
| 21 | +#include "tensorflow/core/platform/test.h" |
| 22 | + |
| 23 | +namespace tensorflow { |
| 24 | + |
| 25 | +using proto_utils::ParseTextFormatFromString; |
| 26 | +using proto_utils::StringErrorCollector; |
| 27 | +using ::testing::ContainsRegex; |
| 28 | + |
| 29 | +TEST(ParseTextFormatFromStringTest, Success) { |
| 30 | + protobuf::DescriptorProto output; |
| 31 | + TF_ASSERT_OK(ParseTextFormatFromString("name: \"foo\"", &output)); |
| 32 | + EXPECT_EQ(output.name(), "foo"); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(ParseTextFormatFromStringTest, ErrorOnInvalidSyntax) { |
| 36 | + protobuf::DescriptorProto output; |
| 37 | + Status status = ParseTextFormatFromString("name: foo", &output); |
| 38 | + EXPECT_EQ(status.code(), error::INVALID_ARGUMENT); |
| 39 | + EXPECT_THAT(status.error_message(), ContainsRegex("foo")); |
| 40 | + EXPECT_FALSE(output.has_name()); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(ParseTextFormatFromStringTest, ErrorOnUnknownFieldName) { |
| 44 | + protobuf::DescriptorProto output; |
| 45 | + Status status = ParseTextFormatFromString("badname: \"foo\"", &output); |
| 46 | + EXPECT_EQ(status.code(), error::INVALID_ARGUMENT); |
| 47 | + EXPECT_THAT(status.error_message(), ContainsRegex("badname")); |
| 48 | + EXPECT_FALSE(output.has_name()); |
| 49 | +} |
| 50 | + |
| 51 | +TEST(ParseTextFormatFromStringTest, DiesOnNullOutputPointer) { |
| 52 | +#ifndef NDEBUG |
| 53 | + ASSERT_DEATH(ParseTextFormatFromString("foo", nullptr).IgnoreError(), |
| 54 | + "output.*non NULL"); |
| 55 | +#else |
| 56 | + // Under NDEBUG we don't die but should still return an error status. |
| 57 | + Status status = ParseTextFormatFromString("foo", nullptr); |
| 58 | + EXPECT_EQ(status.code(), error::INVALID_ARGUMENT); |
| 59 | + EXPECT_THAT(status.error_message(), ContainsRegex("output.*non NULL")); |
| 60 | +#endif |
| 61 | +} |
| 62 | + |
| 63 | +TEST(StringErrorCollectorTest, AppendsError) { |
| 64 | + string err; |
| 65 | + StringErrorCollector collector(&err); |
| 66 | + collector.AddError(1, 2, "foo"); |
| 67 | + EXPECT_EQ("1(2): foo\n", err); |
| 68 | +} |
| 69 | + |
| 70 | +TEST(StringErrorCollectorTest, AppendsWarning) { |
| 71 | + string err; |
| 72 | + StringErrorCollector collector(&err); |
| 73 | + collector.AddWarning(1, 2, "foo"); |
| 74 | + EXPECT_EQ("1(2): foo\n", err); |
| 75 | +} |
| 76 | + |
| 77 | +TEST(StringErrorCollectorTest, AppendsMultipleError) { |
| 78 | + string err; |
| 79 | + StringErrorCollector collector(&err); |
| 80 | + collector.AddError(1, 2, "foo"); |
| 81 | + collector.AddError(3, 4, "bar"); |
| 82 | + EXPECT_EQ("1(2): foo\n3(4): bar\n", err); |
| 83 | +} |
| 84 | + |
| 85 | +TEST(StringErrorCollectorTest, AppendsMultipleWarning) { |
| 86 | + string err; |
| 87 | + StringErrorCollector collector(&err); |
| 88 | + collector.AddWarning(1, 2, "foo"); |
| 89 | + collector.AddWarning(3, 4, "bar"); |
| 90 | + EXPECT_EQ("1(2): foo\n3(4): bar\n", err); |
| 91 | +} |
| 92 | + |
| 93 | +TEST(StringErrorCollectorTest, OffsetWorks) { |
| 94 | + string err; |
| 95 | + StringErrorCollector collector(&err, true); |
| 96 | + collector.AddError(1, 2, "foo"); |
| 97 | + collector.AddWarning(3, 4, "bar"); |
| 98 | + EXPECT_EQ("2(3): foo\n4(5): bar\n", err); |
| 99 | +} |
| 100 | + |
| 101 | +TEST(StringErrorCollectorTest, DiesOnNullErrorText) { |
| 102 | +#ifndef NDEBUG |
| 103 | + ASSERT_DEATH(StringErrorCollector(nullptr), "error_text.*non NULL"); |
| 104 | +#else |
| 105 | + // Under NDEBUG we don't die and instead AddError/AddWarning just do nothing. |
| 106 | + StringErrorCollector collector(nullptr); |
| 107 | + collector.AddError(1, 2, "foo"); |
| 108 | + collector.AddWarning(3, 4, "bar"); |
| 109 | +#endif |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace tensorflow |
0 commit comments