1 | #region Copyright notice and license
|
---|
2 | // Protocol Buffers - Google's data interchange format
|
---|
3 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
4 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
5 | // Original C++/Java/Python code:
|
---|
6 | // http://code.google.com/p/protobuf/
|
---|
7 | //
|
---|
8 | // Redistribution and use in source and binary forms, with or without
|
---|
9 | // modification, are permitted provided that the following conditions are
|
---|
10 | // met:
|
---|
11 | //
|
---|
12 | // * Redistributions of source code must retain the above copyright
|
---|
13 | // notice, this list of conditions and the following disclaimer.
|
---|
14 | // * Redistributions in binary form must reproduce the above
|
---|
15 | // copyright notice, this list of conditions and the following disclaimer
|
---|
16 | // in the documentation and/or other materials provided with the
|
---|
17 | // distribution.
|
---|
18 | // * Neither the name of Google Inc. nor the names of its
|
---|
19 | // contributors may be used to endorse or promote products derived from
|
---|
20 | // this software without specific prior written permission.
|
---|
21 | //
|
---|
22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
26 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
33 | #endregion
|
---|
34 |
|
---|
35 | using System;
|
---|
36 | using System.IO;
|
---|
37 | using System.Text;
|
---|
38 | using Google.ProtocolBuffers.TestProtos;
|
---|
39 | using NUnit.Framework;
|
---|
40 |
|
---|
41 | namespace Google.ProtocolBuffers {
|
---|
42 | [TestFixture]
|
---|
43 | public class TextFormatTest {
|
---|
44 |
|
---|
45 | private static readonly string AllFieldsSetText = TestUtil.ReadTextFromFile("text_format_unittest_data.txt");
|
---|
46 | private static readonly string AllExtensionsSetText = TestUtil.ReadTextFromFile("text_format_unittest_extensions_data.txt");
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | /// Note that this is slightly different to the Java - 123.0 becomes 123, and 1.23E17 becomes 1.23E+17.
|
---|
50 | /// Both of these differences can be parsed by the Java and the C++, and we can parse their output too.
|
---|
51 | /// </summary>
|
---|
52 | private const string ExoticText =
|
---|
53 | "repeated_int32: -1\n" +
|
---|
54 | "repeated_int32: -2147483648\n" +
|
---|
55 | "repeated_int64: -1\n" +
|
---|
56 | "repeated_int64: -9223372036854775808\n" +
|
---|
57 | "repeated_uint32: 4294967295\n" +
|
---|
58 | "repeated_uint32: 2147483648\n" +
|
---|
59 | "repeated_uint64: 18446744073709551615\n" +
|
---|
60 | "repeated_uint64: 9223372036854775808\n" +
|
---|
61 | "repeated_double: 123\n" +
|
---|
62 | "repeated_double: 123.5\n" +
|
---|
63 | "repeated_double: 0.125\n" +
|
---|
64 | "repeated_double: 1.23E+17\n" +
|
---|
65 | "repeated_double: 1.235E+22\n" +
|
---|
66 | "repeated_double: 1.235E-18\n" +
|
---|
67 | "repeated_double: 123.456789\n" +
|
---|
68 | "repeated_double: Infinity\n" +
|
---|
69 | "repeated_double: -Infinity\n" +
|
---|
70 | "repeated_double: NaN\n" +
|
---|
71 | "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"" +
|
---|
72 | "\\341\\210\\264\"\n" +
|
---|
73 | "repeated_bytes: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\376\"\n";
|
---|
74 |
|
---|
75 | private const string MessageSetText =
|
---|
76 | "[protobuf_unittest.TestMessageSetExtension1] {\n" +
|
---|
77 | " i: 123\n" +
|
---|
78 | "}\n" +
|
---|
79 | "[protobuf_unittest.TestMessageSetExtension2] {\n" +
|
---|
80 | " str: \"foo\"\n" +
|
---|
81 | "}\n";
|
---|
82 |
|
---|
83 | /// <summary>
|
---|
84 | /// Print TestAllTypes and compare with golden file.
|
---|
85 | /// </summary>
|
---|
86 | [Test]
|
---|
87 | public void PrintMessage() {
|
---|
88 | TestUtil.TestInMultipleCultures(() => {
|
---|
89 | string text = TextFormat.PrintToString(TestUtil.GetAllSet());
|
---|
90 | Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
|
---|
91 | });
|
---|
92 | }
|
---|
93 |
|
---|
94 | /// <summary>
|
---|
95 | /// Print TestAllExtensions and compare with golden file.
|
---|
96 | /// </summary>
|
---|
97 | [Test]
|
---|
98 | public void PrintExtensions() {
|
---|
99 | string text = TextFormat.PrintToString(TestUtil.GetAllExtensionsSet());
|
---|
100 |
|
---|
101 | Assert.AreEqual(AllExtensionsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
|
---|
102 | }
|
---|
103 |
|
---|
104 | /// <summary>
|
---|
105 | /// Test printing of unknown fields in a message.
|
---|
106 | /// </summary>
|
---|
107 | [Test]
|
---|
108 | public void PrintUnknownFields() {
|
---|
109 | TestEmptyMessage message =
|
---|
110 | TestEmptyMessage.CreateBuilder()
|
---|
111 | .SetUnknownFields(
|
---|
112 | UnknownFieldSet.CreateBuilder()
|
---|
113 | .AddField(5,
|
---|
114 | UnknownField.CreateBuilder()
|
---|
115 | .AddVarint(1)
|
---|
116 | .AddFixed32(2)
|
---|
117 | .AddFixed64(3)
|
---|
118 | .AddLengthDelimited(ByteString.CopyFromUtf8("4"))
|
---|
119 | .AddGroup(
|
---|
120 | UnknownFieldSet.CreateBuilder()
|
---|
121 | .AddField(10,
|
---|
122 | UnknownField.CreateBuilder()
|
---|
123 | .AddVarint(5)
|
---|
124 | .Build())
|
---|
125 | .Build())
|
---|
126 | .Build())
|
---|
127 | .AddField(8,
|
---|
128 | UnknownField.CreateBuilder()
|
---|
129 | .AddVarint(1)
|
---|
130 | .AddVarint(2)
|
---|
131 | .AddVarint(3)
|
---|
132 | .Build())
|
---|
133 | .AddField(15,
|
---|
134 | UnknownField.CreateBuilder()
|
---|
135 | .AddVarint(0xABCDEF1234567890L)
|
---|
136 | .AddFixed32(0xABCD1234)
|
---|
137 | .AddFixed64(0xABCDEF1234567890L)
|
---|
138 | .Build())
|
---|
139 | .Build())
|
---|
140 | .Build();
|
---|
141 |
|
---|
142 | Assert.AreEqual(
|
---|
143 | "5: 1\n" +
|
---|
144 | "5: 0x00000002\n" +
|
---|
145 | "5: 0x0000000000000003\n" +
|
---|
146 | "5: \"4\"\n" +
|
---|
147 | "5 {\n" +
|
---|
148 | " 10: 5\n" +
|
---|
149 | "}\n" +
|
---|
150 | "8: 1\n" +
|
---|
151 | "8: 2\n" +
|
---|
152 | "8: 3\n" +
|
---|
153 | "15: 12379813812177893520\n" +
|
---|
154 | "15: 0xabcd1234\n" +
|
---|
155 | "15: 0xabcdef1234567890\n",
|
---|
156 | TextFormat.PrintToString(message));
|
---|
157 | }
|
---|
158 |
|
---|
159 | /// <summary>
|
---|
160 | /// Helper to construct a ByteString from a string containing only 8-bit
|
---|
161 | /// characters. The characters are converted directly to bytes, *not*
|
---|
162 | /// encoded using UTF-8.
|
---|
163 | /// </summary>
|
---|
164 | private static ByteString Bytes(string str) {
|
---|
165 | return ByteString.CopyFrom(Encoding.GetEncoding(28591).GetBytes(str));
|
---|
166 | }
|
---|
167 |
|
---|
168 | [Test]
|
---|
169 | public void PrintExotic() {
|
---|
170 | IMessage message = TestAllTypes.CreateBuilder()
|
---|
171 | // Signed vs. unsigned numbers.
|
---|
172 | .AddRepeatedInt32(-1)
|
---|
173 | .AddRepeatedUint32(uint.MaxValue)
|
---|
174 | .AddRepeatedInt64(-1)
|
---|
175 | .AddRepeatedUint64(ulong.MaxValue)
|
---|
176 |
|
---|
177 | .AddRepeatedInt32(1 << 31)
|
---|
178 | .AddRepeatedUint32(1U << 31)
|
---|
179 | .AddRepeatedInt64(1L << 63)
|
---|
180 | .AddRepeatedUint64(1UL << 63)
|
---|
181 |
|
---|
182 | // Floats of various precisions and exponents.
|
---|
183 | .AddRepeatedDouble(123)
|
---|
184 | .AddRepeatedDouble(123.5)
|
---|
185 | .AddRepeatedDouble(0.125)
|
---|
186 | .AddRepeatedDouble(123e15)
|
---|
187 | .AddRepeatedDouble(123.5e20)
|
---|
188 | .AddRepeatedDouble(123.5e-20)
|
---|
189 | .AddRepeatedDouble(123.456789)
|
---|
190 | .AddRepeatedDouble(Double.PositiveInfinity)
|
---|
191 | .AddRepeatedDouble(Double.NegativeInfinity)
|
---|
192 | .AddRepeatedDouble(Double.NaN)
|
---|
193 |
|
---|
194 | // Strings and bytes that needing escaping.
|
---|
195 | .AddRepeatedString("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u1234")
|
---|
196 | .AddRepeatedBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u00fe"))
|
---|
197 | .Build();
|
---|
198 |
|
---|
199 | Assert.AreEqual(ExoticText, message.ToString());
|
---|
200 | }
|
---|
201 |
|
---|
202 | [Test]
|
---|
203 | public void PrintMessageSet() {
|
---|
204 | TestMessageSet messageSet =
|
---|
205 | TestMessageSet.CreateBuilder()
|
---|
206 | .SetExtension(
|
---|
207 | TestMessageSetExtension1.MessageSetExtension,
|
---|
208 | TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
|
---|
209 | .SetExtension(
|
---|
210 | TestMessageSetExtension2.MessageSetExtension,
|
---|
211 | TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
|
---|
212 | .Build();
|
---|
213 |
|
---|
214 | Assert.AreEqual(MessageSetText, messageSet.ToString());
|
---|
215 | }
|
---|
216 |
|
---|
217 | // =================================================================
|
---|
218 |
|
---|
219 | [Test]
|
---|
220 | public void Parse() {
|
---|
221 | TestUtil.TestInMultipleCultures(() => {
|
---|
222 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
223 | TextFormat.Merge(AllFieldsSetText, builder);
|
---|
224 | TestUtil.AssertAllFieldsSet(builder.Build());
|
---|
225 | });
|
---|
226 | }
|
---|
227 |
|
---|
228 | [Test]
|
---|
229 | public void ParseReader() {
|
---|
230 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
231 | TextFormat.Merge(new StringReader(AllFieldsSetText), builder);
|
---|
232 | TestUtil.AssertAllFieldsSet(builder.Build());
|
---|
233 | }
|
---|
234 |
|
---|
235 | [Test]
|
---|
236 | public void ParseExtensions() {
|
---|
237 | TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
|
---|
238 | TextFormat.Merge(AllExtensionsSetText,
|
---|
239 | TestUtil.CreateExtensionRegistry(),
|
---|
240 | builder);
|
---|
241 | TestUtil.AssertAllExtensionsSet(builder.Build());
|
---|
242 | }
|
---|
243 |
|
---|
244 | [Test]
|
---|
245 | public void ParseCompatibility() {
|
---|
246 | string original = "repeated_float: inf\n" +
|
---|
247 | "repeated_float: -inf\n" +
|
---|
248 | "repeated_float: nan\n" +
|
---|
249 | "repeated_float: inff\n" +
|
---|
250 | "repeated_float: -inff\n" +
|
---|
251 | "repeated_float: nanf\n" +
|
---|
252 | "repeated_float: 1.0f\n" +
|
---|
253 | "repeated_float: infinityf\n" +
|
---|
254 | "repeated_float: -Infinityf\n" +
|
---|
255 | "repeated_double: infinity\n" +
|
---|
256 | "repeated_double: -infinity\n" +
|
---|
257 | "repeated_double: nan\n";
|
---|
258 | string canonical = "repeated_float: Infinity\n" +
|
---|
259 | "repeated_float: -Infinity\n" +
|
---|
260 | "repeated_float: NaN\n" +
|
---|
261 | "repeated_float: Infinity\n" +
|
---|
262 | "repeated_float: -Infinity\n" +
|
---|
263 | "repeated_float: NaN\n" +
|
---|
264 | "repeated_float: 1\n" + // Java has 1.0; this is fine
|
---|
265 | "repeated_float: Infinity\n" +
|
---|
266 | "repeated_float: -Infinity\n" +
|
---|
267 | "repeated_double: Infinity\n" +
|
---|
268 | "repeated_double: -Infinity\n" +
|
---|
269 | "repeated_double: NaN\n";
|
---|
270 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
271 | TextFormat.Merge(original, builder);
|
---|
272 | Assert.AreEqual(canonical, builder.Build().ToString());
|
---|
273 | }
|
---|
274 |
|
---|
275 | [Test]
|
---|
276 | public void ParseExotic() {
|
---|
277 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
278 | TextFormat.Merge(ExoticText, builder);
|
---|
279 |
|
---|
280 | // Too lazy to check things individually. Don't try to debug this
|
---|
281 | // if testPrintExotic() is Assert.Failing.
|
---|
282 | Assert.AreEqual(ExoticText, builder.Build().ToString());
|
---|
283 | }
|
---|
284 |
|
---|
285 | [Test]
|
---|
286 | public void ParseMessageSet() {
|
---|
287 | ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
|
---|
288 | extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension);
|
---|
289 | extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension);
|
---|
290 |
|
---|
291 | TestMessageSet.Builder builder = TestMessageSet.CreateBuilder();
|
---|
292 | TextFormat.Merge(MessageSetText, extensionRegistry, builder);
|
---|
293 | TestMessageSet messageSet = builder.Build();
|
---|
294 |
|
---|
295 | Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension1.MessageSetExtension));
|
---|
296 | Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
|
---|
297 | Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension2.MessageSetExtension));
|
---|
298 | Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);
|
---|
299 | }
|
---|
300 |
|
---|
301 | [Test]
|
---|
302 | public void ParseNumericEnum() {
|
---|
303 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
304 | TextFormat.Merge("optional_nested_enum: 2", builder);
|
---|
305 | Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, builder.OptionalNestedEnum);
|
---|
306 | }
|
---|
307 |
|
---|
308 | [Test]
|
---|
309 | public void ParseAngleBrackets() {
|
---|
310 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
311 | TextFormat.Merge("OptionalGroup: < a: 1 >", builder);
|
---|
312 | Assert.IsTrue(builder.HasOptionalGroup);
|
---|
313 | Assert.AreEqual(1, builder.OptionalGroup.A);
|
---|
314 | }
|
---|
315 |
|
---|
316 | [Test]
|
---|
317 | public void ParseComment() {
|
---|
318 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
319 | TextFormat.Merge(
|
---|
320 | "# this is a comment\n" +
|
---|
321 | "optional_int32: 1 # another comment\n" +
|
---|
322 | "optional_int64: 2\n" +
|
---|
323 | "# EOF comment", builder);
|
---|
324 | Assert.AreEqual(1, builder.OptionalInt32);
|
---|
325 | Assert.AreEqual(2, builder.OptionalInt64);
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | private static void AssertParseError(string error, string text) {
|
---|
330 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
331 | try {
|
---|
332 | TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder);
|
---|
333 | Assert.Fail("Expected parse exception.");
|
---|
334 | }
|
---|
335 | catch (FormatException e) {
|
---|
336 | Assert.AreEqual(error, e.Message);
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | [Test]
|
---|
341 | public void ParseErrors() {
|
---|
342 | AssertParseError(
|
---|
343 | "1:16: Expected \":\".",
|
---|
344 | "optional_int32 123");
|
---|
345 | AssertParseError(
|
---|
346 | "1:23: Expected identifier.",
|
---|
347 | "optional_nested_enum: ?");
|
---|
348 | AssertParseError(
|
---|
349 | "1:18: Couldn't parse integer: Number must be positive: -1",
|
---|
350 | "optional_uint32: -1");
|
---|
351 | AssertParseError(
|
---|
352 | "1:17: Couldn't parse integer: Number out of range for 32-bit signed " +
|
---|
353 | "integer: 82301481290849012385230157",
|
---|
354 | "optional_int32: 82301481290849012385230157");
|
---|
355 | AssertParseError(
|
---|
356 | "1:16: Expected \"true\" or \"false\".",
|
---|
357 | "optional_bool: maybe");
|
---|
358 | AssertParseError(
|
---|
359 | "1:18: Expected string.",
|
---|
360 | "optional_string: 123");
|
---|
361 | AssertParseError(
|
---|
362 | "1:18: String missing ending quote.",
|
---|
363 | "optional_string: \"ueoauaoe");
|
---|
364 | AssertParseError(
|
---|
365 | "1:18: String missing ending quote.",
|
---|
366 | "optional_string: \"ueoauaoe\n" +
|
---|
367 | "optional_int32: 123");
|
---|
368 | AssertParseError(
|
---|
369 | "1:18: Invalid escape sequence: '\\z'",
|
---|
370 | "optional_string: \"\\z\"");
|
---|
371 | AssertParseError(
|
---|
372 | "1:18: String missing ending quote.",
|
---|
373 | "optional_string: \"ueoauaoe\n" +
|
---|
374 | "optional_int32: 123");
|
---|
375 | AssertParseError(
|
---|
376 | "1:2: Extension \"nosuchext\" not found in the ExtensionRegistry.",
|
---|
377 | "[nosuchext]: 123");
|
---|
378 | AssertParseError(
|
---|
379 | "1:20: Extension \"protobuf_unittest.optional_int32_extension\" does " +
|
---|
380 | "not extend message type \"protobuf_unittest.TestAllTypes\".",
|
---|
381 | "[protobuf_unittest.optional_int32_extension]: 123");
|
---|
382 | AssertParseError(
|
---|
383 | "1:1: Message type \"protobuf_unittest.TestAllTypes\" has no field " +
|
---|
384 | "named \"nosuchfield\".",
|
---|
385 | "nosuchfield: 123");
|
---|
386 | AssertParseError(
|
---|
387 | "1:21: Expected \">\".",
|
---|
388 | "OptionalGroup < a: 1");
|
---|
389 | AssertParseError(
|
---|
390 | "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " +
|
---|
391 | "value named \"NO_SUCH_VALUE\".",
|
---|
392 | "optional_nested_enum: NO_SUCH_VALUE");
|
---|
393 | AssertParseError(
|
---|
394 | "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " +
|
---|
395 | "value with number 123.",
|
---|
396 | "optional_nested_enum: 123");
|
---|
397 |
|
---|
398 | // Delimiters must match.
|
---|
399 | AssertParseError(
|
---|
400 | "1:22: Expected identifier.",
|
---|
401 | "OptionalGroup < a: 1 }");
|
---|
402 | AssertParseError(
|
---|
403 | "1:22: Expected identifier.",
|
---|
404 | "OptionalGroup { a: 1 >");
|
---|
405 | }
|
---|
406 |
|
---|
407 | // =================================================================
|
---|
408 |
|
---|
409 | private static ByteString Bytes(params byte[] bytes) {
|
---|
410 | return ByteString.CopyFrom(bytes);
|
---|
411 | }
|
---|
412 |
|
---|
413 | private delegate void FormattingAction();
|
---|
414 |
|
---|
415 | private static void AssertFormatException(FormattingAction action) {
|
---|
416 | try {
|
---|
417 | action();
|
---|
418 | Assert.Fail("Should have thrown an exception.");
|
---|
419 | }
|
---|
420 | catch (FormatException) {
|
---|
421 | // success
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | [Test]
|
---|
426 | public void Escape() {
|
---|
427 | // Escape sequences.
|
---|
428 | Assert.AreEqual("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
|
---|
429 | TextFormat.EscapeBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"")));
|
---|
430 | Assert.AreEqual("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
|
---|
431 | TextFormat.EscapeText("\0\u0001\u0007\b\f\n\r\t\v\\\'\""));
|
---|
432 | Assert.AreEqual(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\""),
|
---|
433 | TextFormat.UnescapeBytes("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
|
---|
434 | Assert.AreEqual("\0\u0001\u0007\b\f\n\r\t\v\\\'\"",
|
---|
435 | TextFormat.UnescapeText("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
|
---|
436 |
|
---|
437 | // Unicode handling.
|
---|
438 | Assert.AreEqual("\\341\\210\\264", TextFormat.EscapeText("\u1234"));
|
---|
439 | Assert.AreEqual("\\341\\210\\264", TextFormat.EscapeBytes(Bytes(0xe1, 0x88, 0xb4)));
|
---|
440 | Assert.AreEqual("\u1234", TextFormat.UnescapeText("\\341\\210\\264"));
|
---|
441 | Assert.AreEqual(Bytes(0xe1, 0x88, 0xb4), TextFormat.UnescapeBytes("\\341\\210\\264"));
|
---|
442 | Assert.AreEqual("\u1234", TextFormat.UnescapeText("\\xe1\\x88\\xb4"));
|
---|
443 | Assert.AreEqual(Bytes(0xe1, 0x88, 0xb4), TextFormat.UnescapeBytes("\\xe1\\x88\\xb4"));
|
---|
444 |
|
---|
445 | // Errors.
|
---|
446 | AssertFormatException(() => TextFormat.UnescapeText("\\x"));
|
---|
447 | AssertFormatException(() => TextFormat.UnescapeText("\\z"));
|
---|
448 | AssertFormatException(() => TextFormat.UnescapeText("\\"));
|
---|
449 | }
|
---|
450 |
|
---|
451 | [Test]
|
---|
452 | public void ParseInteger() {
|
---|
453 | Assert.AreEqual(0, TextFormat.ParseInt32("0"));
|
---|
454 | Assert.AreEqual(1, TextFormat.ParseInt32("1"));
|
---|
455 | Assert.AreEqual(-1, TextFormat.ParseInt32("-1"));
|
---|
456 | Assert.AreEqual(12345, TextFormat.ParseInt32("12345"));
|
---|
457 | Assert.AreEqual(-12345, TextFormat.ParseInt32("-12345"));
|
---|
458 | Assert.AreEqual(2147483647, TextFormat.ParseInt32("2147483647"));
|
---|
459 | Assert.AreEqual(-2147483648, TextFormat.ParseInt32("-2147483648"));
|
---|
460 |
|
---|
461 | Assert.AreEqual(0, TextFormat.ParseUInt32("0"));
|
---|
462 | Assert.AreEqual(1, TextFormat.ParseUInt32("1"));
|
---|
463 | Assert.AreEqual(12345, TextFormat.ParseUInt32("12345"));
|
---|
464 | Assert.AreEqual(2147483647, TextFormat.ParseUInt32("2147483647"));
|
---|
465 | Assert.AreEqual(2147483648U, TextFormat.ParseUInt32("2147483648"));
|
---|
466 | Assert.AreEqual(4294967295U, TextFormat.ParseUInt32("4294967295"));
|
---|
467 |
|
---|
468 | Assert.AreEqual(0L, TextFormat.ParseInt64("0"));
|
---|
469 | Assert.AreEqual(1L, TextFormat.ParseInt64("1"));
|
---|
470 | Assert.AreEqual(-1L, TextFormat.ParseInt64("-1"));
|
---|
471 | Assert.AreEqual(12345L, TextFormat.ParseInt64("12345"));
|
---|
472 | Assert.AreEqual(-12345L, TextFormat.ParseInt64("-12345"));
|
---|
473 | Assert.AreEqual(2147483647L, TextFormat.ParseInt64("2147483647"));
|
---|
474 | Assert.AreEqual(-2147483648L, TextFormat.ParseInt64("-2147483648"));
|
---|
475 | Assert.AreEqual(4294967295L, TextFormat.ParseInt64("4294967295"));
|
---|
476 | Assert.AreEqual(4294967296L, TextFormat.ParseInt64("4294967296"));
|
---|
477 | Assert.AreEqual(9223372036854775807L, TextFormat.ParseInt64("9223372036854775807"));
|
---|
478 | Assert.AreEqual(-9223372036854775808L, TextFormat.ParseInt64("-9223372036854775808"));
|
---|
479 |
|
---|
480 | Assert.AreEqual(0L, TextFormat.ParseUInt64("0"));
|
---|
481 | Assert.AreEqual(1L, TextFormat.ParseUInt64("1"));
|
---|
482 | Assert.AreEqual(12345L, TextFormat.ParseUInt64("12345"));
|
---|
483 | Assert.AreEqual(2147483647L, TextFormat.ParseUInt64("2147483647"));
|
---|
484 | Assert.AreEqual(4294967295L, TextFormat.ParseUInt64("4294967295"));
|
---|
485 | Assert.AreEqual(4294967296L, TextFormat.ParseUInt64("4294967296"));
|
---|
486 | Assert.AreEqual(9223372036854775807UL, TextFormat.ParseUInt64("9223372036854775807"));
|
---|
487 | Assert.AreEqual(9223372036854775808UL, TextFormat.ParseUInt64("9223372036854775808"));
|
---|
488 | Assert.AreEqual(18446744073709551615UL, TextFormat.ParseUInt64("18446744073709551615"));
|
---|
489 |
|
---|
490 | // Hex
|
---|
491 | Assert.AreEqual(0x1234abcd, TextFormat.ParseInt32("0x1234abcd"));
|
---|
492 | Assert.AreEqual(-0x1234abcd, TextFormat.ParseInt32("-0x1234abcd"));
|
---|
493 | Assert.AreEqual(0xffffffffffffffffUL, TextFormat.ParseUInt64("0xffffffffffffffff"));
|
---|
494 | Assert.AreEqual(0x7fffffffffffffffL,
|
---|
495 | TextFormat.ParseInt64("0x7fffffffffffffff"));
|
---|
496 |
|
---|
497 | // Octal
|
---|
498 | Assert.AreEqual(342391, TextFormat.ParseInt32("01234567"));
|
---|
499 |
|
---|
500 | // Out-of-range
|
---|
501 | AssertFormatException(() => TextFormat.ParseInt32("2147483648"));
|
---|
502 | AssertFormatException(() => TextFormat.ParseInt32("-2147483649"));
|
---|
503 | AssertFormatException(() => TextFormat.ParseUInt32("4294967296"));
|
---|
504 | AssertFormatException(() => TextFormat.ParseUInt32("-1"));
|
---|
505 | AssertFormatException(() => TextFormat.ParseInt64("9223372036854775808"));
|
---|
506 | AssertFormatException(() => TextFormat.ParseInt64("-9223372036854775809"));
|
---|
507 | AssertFormatException(() => TextFormat.ParseUInt64("18446744073709551616"));
|
---|
508 | AssertFormatException(() => TextFormat.ParseUInt64("-1"));
|
---|
509 | AssertFormatException(() => TextFormat.ParseInt32("abcd"));
|
---|
510 | }
|
---|
511 |
|
---|
512 | [Test]
|
---|
513 | public void ParseLongString() {
|
---|
514 | string longText =
|
---|
515 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
516 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
517 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
518 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
519 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
520 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
521 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
522 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
523 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
524 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
525 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
526 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
527 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
528 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
529 | "123456789012345678901234567890123456789012345678901234567890" +
|
---|
530 | "123456789012345678901234567890123456789012345678901234567890";
|
---|
531 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
---|
532 | TextFormat.Merge("optional_string: \"" + longText + "\"", builder);
|
---|
533 | Assert.AreEqual(longText, builder.OptionalString);
|
---|
534 | }
|
---|
535 | }
|
---|
536 | }
|
---|