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