~ubuntu-branches/debian/squeeze/protobuf/squeeze

« back to all changes in this revision

Viewing changes to src/google/protobuf/text_format_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Julien Cristau
  • Date: 2009-06-02 16:19:00 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090602161900-vm176i3ryt35yk91
Tags: 2.0.3-2.2
* Non-maintainer upload.
* Fix FTBFS from -2.1: don't fail when we can't clean up the java build,
  such as when openjdk isn't installed.
* Disable parallel builds, because libtool is made of fail (if binary-arch
  and build-indep run concurrently, we relink a library while it's being
  used; that doesn't work so well).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// Protocol Buffers - Google's data interchange format
2
 
// Copyright 2008 Google Inc.
 
2
// Copyright 2008 Google Inc.  All rights reserved.
3
3
// http://code.google.com/p/protobuf/
4
4
//
5
 
// Licensed under the Apache License, Version 2.0 (the "License");
6
 
// you may not use this file except in compliance with the License.
7
 
// You may obtain a copy of the License at
8
 
//
9
 
//      http://www.apache.org/licenses/LICENSE-2.0
10
 
//
11
 
// Unless required by applicable law or agreed to in writing, software
12
 
// distributed under the License is distributed on an "AS IS" BASIS,
13
 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
// See the License for the specific language governing permissions and
15
 
// limitations under the License.
 
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.
16
30
 
17
31
// Author: jschorr@google.com (Joseph Schorr)
18
32
//  Based on original Protocol Buffers design by
38
52
 
39
53
namespace google {
40
54
namespace protobuf {
41
 
namespace {
 
55
 
 
56
// Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
 
57
namespace text_format_unittest {
42
58
 
43
59
inline bool IsNaN(double value) {
44
60
  // NaN is never equal to anything, even itself.
48
64
// A basic string with different escapable characters for testing.
49
65
const string kEscapeTestString =
50
66
  "\"A string with ' characters \n and \r newlines and \t tabs and \001 "
51
 
  "slashes \\";
 
67
  "slashes \\ and  multiple   spaces";
52
68
 
53
69
// A representation of the above string with all the characters escaped.
54
70
const string kEscapeTestStringEscaped =
55
71
  "\"\\\"A string with \\' characters \\n and \\r newlines "
56
 
  "and \\t tabs and \\001 slashes \\\\\"";
 
72
  "and \\t tabs and \\001 slashes \\\\ and  multiple   spaces\"";
57
73
 
58
74
class TextFormatTest : public testing::Test {
59
75
 public:
110
126
  EXPECT_EQ(proto_debug_string_, proto_.DebugString());
111
127
}
112
128
 
 
129
TEST_F(TextFormatTest, ShortDebugString) {
 
130
  proto_.set_optional_int32(1);
 
131
  proto_.set_optional_string("hello");
 
132
  proto_.mutable_optional_nested_message()->set_bb(2);
 
133
  proto_.mutable_optional_foreign_message();
 
134
 
 
135
  EXPECT_EQ("optional_int32: 1 optional_string: \"hello\" "
 
136
            "optional_nested_message { bb: 2 } "
 
137
            "optional_foreign_message { }",
 
138
            proto_.ShortDebugString());
 
139
}
 
140
 
113
141
TEST_F(TextFormatTest, StringEscape) {
114
142
  // Set the string value to test.
115
143
  proto_.set_optional_string(kEscapeTestString);
124
152
 
125
153
  // Compare.
126
154
  EXPECT_EQ(correct_string, debug_string);
 
155
 
 
156
  string expected_short_debug_string = "optional_string: "
 
157
      + kEscapeTestStringEscaped;
 
158
  EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString());
127
159
}
128
160
 
129
161
TEST_F(TextFormatTest, PrintUnknownFields) {
158
190
    message.DebugString());
159
191
}
160
192
 
 
193
TEST_F(TextFormatTest, PrintUnknownMessage) {
 
194
  // Test heuristic printing of messages in an UnknownFieldSet.
 
195
 
 
196
  protobuf_unittest::TestAllTypes message;
 
197
 
 
198
  // Cases which should not be interpreted as sub-messages.
 
199
 
 
200
  // 'a' is a valid FIXED64 tag, so for the string to be parseable as a message
 
201
  // it should be followed by 8 bytes.  Since this string only has two
 
202
  // subsequent bytes, it should be treated as a string.
 
203
  message.add_repeated_string("abc");
 
204
 
 
205
  // 'd' happens to be a valid ENDGROUP tag.  So,
 
206
  // UnknownFieldSet::MergeFromCodedStream() will successfully parse "def", but
 
207
  // the ConsumedEntireMessage() check should fail.
 
208
  message.add_repeated_string("def");
 
209
 
 
210
  // A zero-length string should never be interpreted as a message even though
 
211
  // it is technically valid as one.
 
212
  message.add_repeated_string("");
 
213
 
 
214
  // Case which should be interpreted as a sub-message.
 
215
 
 
216
  // An actual nested message with content should always be interpreted as a
 
217
  // nested message.
 
218
  message.add_repeated_nested_message()->set_bb(123);
 
219
 
 
220
  string data;
 
221
  message.SerializeToString(&data);
 
222
 
 
223
  string text;
 
224
  UnknownFieldSet unknown_fields;
 
225
  EXPECT_TRUE(unknown_fields.ParseFromString(data));
 
226
  EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text));
 
227
  EXPECT_EQ(
 
228
    "44: \"abc\"\n"
 
229
    "44: \"def\"\n"
 
230
    "44: \"\"\n"
 
231
    "48 {\n"
 
232
    "  1: 123\n"
 
233
    "}\n",
 
234
    text);
 
235
}
 
236
 
161
237
TEST_F(TextFormatTest, ParseBasic) {
162
238
  io::ArrayInputStream input_stream(proto_debug_string_.data(),
163
239
                                    proto_debug_string_.size());
631
707
                0, 1, &message);
632
708
}
633
709
 
 
710
TEST_F(TextFormatParserTest, ParseDuplicateRequired) {
 
711
  unittest::TestRequired message;
 
712
  ExpectFailure("a: 1 b: 2 c: 3 a: 1",
 
713
                "Non-repeated field \"a\" is specified multiple times.",
 
714
                1, 17, &message);
 
715
}
 
716
 
 
717
TEST_F(TextFormatParserTest, ParseDuplicateOptional) {
 
718
  unittest::ForeignMessage message;
 
719
  ExpectFailure("c: 1 c: 2",
 
720
                "Non-repeated field \"c\" is specified multiple times.",
 
721
                1, 7, &message);
 
722
}
 
723
 
 
724
TEST_F(TextFormatParserTest, MergeDuplicateRequired) {
 
725
  unittest::TestRequired message;
 
726
  TextFormat::Parser parser;
 
727
  EXPECT_TRUE(parser.MergeFromString("a: 1 b: 2 c: 3 a: 4", &message));
 
728
  EXPECT_EQ(4, message.a());
 
729
}
 
730
 
 
731
TEST_F(TextFormatParserTest, MergeDuplicateOptional) {
 
732
  unittest::ForeignMessage message;
 
733
  TextFormat::Parser parser;
 
734
  EXPECT_TRUE(parser.MergeFromString("c: 1 c: 2", &message));
 
735
  EXPECT_EQ(2, message.c());
 
736
}
 
737
 
634
738
TEST_F(TextFormatParserTest, PrintErrorsToStderr) {
635
739
  vector<string> errors;
636
740
 
648
752
            errors[0]);
649
753
}
650
754
 
 
755
TEST_F(TextFormatParserTest, FailsOnTokenizationError) {
 
756
  vector<string> errors;
 
757
 
 
758
  {
 
759
    ScopedMemoryLog log;
 
760
    unittest::TestAllTypes proto;
 
761
    EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto));
 
762
    errors = log.GetMessages(ERROR);
 
763
  }
 
764
 
 
765
  ASSERT_EQ(1, errors.size());
 
766
  EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: "
 
767
            "1:1: Invalid control characters encountered in text.",
 
768
            errors[0]);
 
769
}
 
770
 
651
771
 
652
772
class TextFormatMessageSetTest : public testing::Test {
653
773
 protected:
687
807
 
688
808
  // Ensure that these are the only entries present.
689
809
  vector<const FieldDescriptor*> descriptors;
690
 
  proto.message_set().GetReflection()->ListFields(&descriptors);
 
810
  proto.message_set().GetReflection()->ListFields(
 
811
    proto.message_set(), &descriptors);
691
812
  EXPECT_EQ(2, descriptors.size());
692
813
}
693
814
 
694
 
} // namespace
 
815
}  // namespace text_format_unittest
695
816
}  // namespace protobuf
696
817
 
697
818
}  // namespace google