1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
using System;
using System.IO;
using System.Text;
using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;
namespace Google.ProtocolBuffers {
[TestFixture]
public class TextFormatTest {
/// <summary>
/// A basic string with different escapable characters for testing.
/// </summary>
private const string EscapeTestString = "\"A string with ' characters \n and \r newlines and \t tabs and \001 "
+ "slashes \\";
/// <summary>
/// A representation of the above string with all the characters escaped.
/// </summary>
private const string EscapeTestStringEscaped = "\"\\\"A string with \\' characters \\n and \\r newlines "
+ "and \\t tabs and \\001 slashes \\\\\"";
private static readonly string AllFieldsSetText = TestUtil.ReadTextFromFile("text_format_unittest_data.txt");
private static readonly string AllExtensionsSetText = TestUtil.ReadTextFromFile("text_format_unittest_extensions_data.txt");
/// <summary>
/// Note that this is slightly different to the Java - 123.0 becomes 123, and 1.23E17 becomes 1.23E+17.
/// Both of these differences can be parsed by the Java and the C++, and we can parse their output too.
/// </summary>
private const string ExoticText =
"repeated_int32: -1\n" +
"repeated_int32: -2147483648\n" +
"repeated_int64: -1\n" +
"repeated_int64: -9223372036854775808\n" +
"repeated_uint32: 4294967295\n" +
"repeated_uint32: 2147483648\n" +
"repeated_uint64: 18446744073709551615\n" +
"repeated_uint64: 9223372036854775808\n" +
"repeated_double: 123\n" +
"repeated_double: 123.5\n" +
"repeated_double: 0.125\n" +
"repeated_double: 1.23E+17\n" +
"repeated_double: 1.235E+22\n" +
"repeated_double: 1.235E-18\n" +
"repeated_double: 123.456789\n" +
"repeated_double: Infinity\n" +
"repeated_double: -Infinity\n" +
"repeated_double: NaN\n" +
"repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"" +
"\\341\\210\\264\"\n" +
"repeated_bytes: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\376\"\n";
private const string MessageSetText =
"[protobuf_unittest.TestMessageSetExtension1] {\n" +
" i: 123\n" +
"}\n" +
"[protobuf_unittest.TestMessageSetExtension2] {\n" +
" str: \"foo\"\n" +
"}\n";
/// <summary>
/// Print TestAllTypes and compare with golden file.
/// </summary>
[Test]
public void PrintMessage() {
string text = TextFormat.PrintToString(TestUtil.GetAllSet());
Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
}
/// <summary>
/// Print TestAllExtensions and compare with golden file.
/// </summary>
[Test]
public void PrintExtensions() {
string text = TextFormat.PrintToString(TestUtil.GetAllExtensionsSet());
Assert.AreEqual(AllExtensionsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
}
/// <summary>
/// Test printing of unknown fields in a message.
/// </summary>
[Test]
public void PrintUnknownFields() {
TestEmptyMessage message =
TestEmptyMessage.CreateBuilder()
.SetUnknownFields(
UnknownFieldSet.CreateBuilder()
.AddField(5,
UnknownField.CreateBuilder()
.AddVarint(1)
.AddFixed32(2)
.AddFixed64(3)
.AddLengthDelimited(ByteString.CopyFromUtf8("4"))
.AddGroup(
UnknownFieldSet.CreateBuilder()
.AddField(10,
UnknownField.CreateBuilder()
.AddVarint(5)
.Build())
.Build())
.Build())
.AddField(8,
UnknownField.CreateBuilder()
.AddVarint(1)
.AddVarint(2)
.AddVarint(3)
.Build())
.AddField(15,
UnknownField.CreateBuilder()
.AddVarint(0xABCDEF1234567890L)
.AddFixed32(0xABCD1234)
.AddFixed64(0xABCDEF1234567890L)
.Build())
.Build())
.Build();
Assert.AreEqual(
"5: 1\n" +
"5: 0x00000002\n" +
"5: 0x0000000000000003\n" +
"5: \"4\"\n" +
"5 {\n" +
" 10: 5\n" +
"}\n" +
"8: 1\n" +
"8: 2\n" +
"8: 3\n" +
"15: 12379813812177893520\n" +
"15: 0xabcd1234\n" +
"15: 0xabcdef1234567890\n",
TextFormat.PrintToString(message));
}
/// <summary>
/// Helper to construct a ByteString from a string containing only 8-bit
/// characters. The characters are converted directly to bytes, *not*
/// encoded using UTF-8.
/// </summary>
private static ByteString Bytes(string str) {
return ByteString.CopyFrom(Encoding.GetEncoding(28591).GetBytes(str));
}
[Test]
public void PrintExotic() {
IMessage message = TestAllTypes.CreateBuilder()
// Signed vs. unsigned numbers.
.AddRepeatedInt32 (-1)
.AddRepeatedUint32(uint.MaxValue)
.AddRepeatedInt64 (-1)
.AddRepeatedUint64(ulong.MaxValue)
.AddRepeatedInt32 (1 << 31)
.AddRepeatedUint32(1U << 31)
.AddRepeatedInt64 (1L << 63)
.AddRepeatedUint64(1UL << 63)
// Floats of various precisions and exponents.
.AddRepeatedDouble(123)
.AddRepeatedDouble(123.5)
.AddRepeatedDouble(0.125)
.AddRepeatedDouble(123e15)
.AddRepeatedDouble(123.5e20)
.AddRepeatedDouble(123.5e-20)
.AddRepeatedDouble(123.456789)
.AddRepeatedDouble(Double.PositiveInfinity)
.AddRepeatedDouble(Double.NegativeInfinity)
.AddRepeatedDouble(Double.NaN)
// Strings and bytes that needing escaping.
.AddRepeatedString("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u1234")
.AddRepeatedBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u00fe"))
.Build();
Assert.AreEqual(ExoticText, message.ToString());
}
[Test]
public void PrintMessageSet() {
TestMessageSet messageSet =
TestMessageSet.CreateBuilder()
.SetExtension(
TestMessageSetExtension1.MessageSetExtension,
TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
.SetExtension(
TestMessageSetExtension2.MessageSetExtension,
TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
.Build();
Assert.AreEqual(MessageSetText, messageSet.ToString());
}
// =================================================================
[Test]
[Ignore("Parsing not implemented")]
public void Parse() {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
TextFormat.Merge(AllFieldsSetText, builder);
TestUtil.AssertAllFieldsSet(builder.Build());
}
[Test]
[Ignore("Parsing not implemented")]
public void ParseReader() {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
TextFormat.Merge(new StringReader(AllFieldsSetText), builder);
TestUtil.AssertAllFieldsSet(builder.Build());
}
[Test]
[Ignore("Parsing not implemented")]
public void ParseExtensions() {
TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
TextFormat.Merge(AllExtensionsSetText,
TestUtil.CreateExtensionRegistry(),
builder);
TestUtil.AssertAllExtensionsSet(builder.Build());
}
[Test]
[Ignore("Parsing not implemented")]
public void ParseExotic() {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
TextFormat.Merge(ExoticText, builder);
// Too lazy to check things individually. Don't try to debug this
// if testPrintExotic() is Assert.Failing.
Assert.AreEqual(ExoticText, builder.Build().ToString());
}
[Test]
[Ignore("Parsing not implemented")]
public void ParseMessageSet() {
ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension);
extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension);
TestMessageSet.Builder builder = TestMessageSet.CreateBuilder();
TextFormat.Merge(MessageSetText, extensionRegistry, builder);
TestMessageSet messageSet = builder.Build();
Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension1.MessageSetExtension));
Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension2.MessageSetExtension));
Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);
}
[Test]
[Ignore("Parsing not implemented")]
public void ParseNumericEnum() {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
TextFormat.Merge("optional_nested_enum: 2", builder);
Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, builder.OptionalNestedEnum);
}
[Test]
[Ignore("Parsing not implemented")]
public void ParseAngleBrackets() {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
TextFormat.Merge("OptionalGroup: < a: 1 >", builder);
Assert.IsTrue(builder.HasOptionalGroup);
Assert.AreEqual(1, builder.OptionalGroup.A);
}
private static void AssertParseError(string error, string text) {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
try {
TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder);
Assert.Fail("Expected parse exception.");
} catch (FormatException e) {
Assert.AreEqual(error, e.Message);
}
}
[Test]
[Ignore("Parsing not implemented")]
public void ParseErrors() {
AssertParseError(
"1:16: Expected \":\".",
"optional_int32 123");
AssertParseError(
"1:23: Expected identifier.",
"optional_nested_enum: ?");
AssertParseError(
"1:18: Couldn't parse integer: Number must be positive: -1",
"optional_uint32: -1");
AssertParseError(
"1:17: Couldn't parse integer: Number out of range for 32-bit signed " +
"integer: 82301481290849012385230157",
"optional_int32: 82301481290849012385230157");
AssertParseError(
"1:16: Expected \"true\" or \"false\".",
"optional_bool: maybe");
AssertParseError(
"1:18: Expected string.",
"optional_string: 123");
AssertParseError(
"1:18: string missing ending quote.",
"optional_string: \"ueoauaoe");
AssertParseError(
"1:18: string missing ending quote.",
"optional_string: \"ueoauaoe\n" +
"optional_int32: 123");
AssertParseError(
"1:18: Invalid escape sequence: '\\z'",
"optional_string: \"\\z\"");
AssertParseError(
"1:18: string missing ending quote.",
"optional_string: \"ueoauaoe\n" +
"optional_int32: 123");
AssertParseError(
"1:2: Extension \"nosuchext\" not found in the ExtensionRegistry.",
"[nosuchext]: 123");
AssertParseError(
"1:20: Extension \"protobuf_unittest.optional_int32_extension\" does " +
"not extend message type \"protobuf_unittest.TestAllTypes\".",
"[protobuf_unittest.optional_int32_extension]: 123");
AssertParseError(
"1:1: Message type \"protobuf_unittest.TestAllTypes\" has no field " +
"named \"nosuchfield\".",
"nosuchfield: 123");
AssertParseError(
"1:21: Expected \">\".",
"OptionalGroup < a: 1");
AssertParseError(
"1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " +
"value named \"NO_SUCH_VALUE\".",
"optional_nested_enum: NO_SUCH_VALUE");
AssertParseError(
"1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " +
"value with number 123.",
"optional_nested_enum: 123");
// Delimiters must match.
AssertParseError(
"1:22: Expected identifier.",
"OptionalGroup < a: 1 }");
AssertParseError(
"1:22: Expected identifier.",
"OptionalGroup { a: 1 >");
}
// =================================================================
private static ByteString Bytes(params byte[] bytes) {
return ByteString.CopyFrom(bytes);
}
private delegate void FormattingAction();
private static void AssertFormatException(FormattingAction action) {
try {
action();
Assert.Fail("Should have thrown an exception.");
} catch (FormatException) {
// success
}
}
[Test]
public void Escape() {
// Escape sequences.
Assert.AreEqual("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
TextFormat.EscapeBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"")));
Assert.AreEqual("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
TextFormat.EscapeText("\0\u0001\u0007\b\f\n\r\t\v\\\'\""));
Assert.AreEqual(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\""),
TextFormat.UnescapeBytes("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
Assert.AreEqual("\0\u0001\u0007\b\f\n\r\t\v\\\'\"",
TextFormat.UnescapeText("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
// Unicode handling.
Assert.AreEqual("\\341\\210\\264", TextFormat.EscapeText("\u1234"));
Assert.AreEqual("\\341\\210\\264", TextFormat.EscapeBytes(Bytes(0xe1, 0x88, 0xb4)));
Assert.AreEqual("\u1234", TextFormat.UnescapeText("\\341\\210\\264"));
Assert.AreEqual(Bytes(0xe1, 0x88, 0xb4), TextFormat.UnescapeBytes("\\341\\210\\264"));
Assert.AreEqual("\u1234", TextFormat.UnescapeText("\\xe1\\x88\\xb4"));
Assert.AreEqual(Bytes(0xe1, 0x88, 0xb4), TextFormat.UnescapeBytes("\\xe1\\x88\\xb4"));
// Errors.
AssertFormatException(() => TextFormat.UnescapeText("\\x"));
AssertFormatException(() => TextFormat.UnescapeText("\\z"));
AssertFormatException(() => TextFormat.UnescapeText("\\"));
}
[Test]
public void ParseInteger() {
Assert.AreEqual( 0, TextFormat.ParseInt32( "0"));
Assert.AreEqual( 1, TextFormat.ParseInt32( "1"));
Assert.AreEqual( -1, TextFormat.ParseInt32( "-1"));
Assert.AreEqual( 12345, TextFormat.ParseInt32( "12345"));
Assert.AreEqual( -12345, TextFormat.ParseInt32( "-12345"));
Assert.AreEqual( 2147483647, TextFormat.ParseInt32( "2147483647"));
Assert.AreEqual(-2147483648, TextFormat.ParseInt32("-2147483648"));
Assert.AreEqual( 0, TextFormat.ParseUInt32( "0"));
Assert.AreEqual( 1, TextFormat.ParseUInt32( "1"));
Assert.AreEqual( 12345, TextFormat.ParseUInt32( "12345"));
Assert.AreEqual( 2147483647, TextFormat.ParseUInt32("2147483647"));
Assert.AreEqual(2147483648U, TextFormat.ParseUInt32("2147483648"));
Assert.AreEqual(4294967295U, TextFormat.ParseUInt32("4294967295"));
Assert.AreEqual( 0L, TextFormat.ParseInt64( "0"));
Assert.AreEqual( 1L, TextFormat.ParseInt64( "1"));
Assert.AreEqual( -1L, TextFormat.ParseInt64( "-1"));
Assert.AreEqual( 12345L, TextFormat.ParseInt64( "12345"));
Assert.AreEqual( -12345L, TextFormat.ParseInt64( "-12345"));
Assert.AreEqual( 2147483647L, TextFormat.ParseInt64( "2147483647"));
Assert.AreEqual(-2147483648L, TextFormat.ParseInt64("-2147483648"));
Assert.AreEqual( 4294967295L, TextFormat.ParseInt64( "4294967295"));
Assert.AreEqual( 4294967296L, TextFormat.ParseInt64( "4294967296"));
Assert.AreEqual(9223372036854775807L, TextFormat.ParseInt64("9223372036854775807"));
Assert.AreEqual(-9223372036854775808L, TextFormat.ParseInt64("-9223372036854775808"));
Assert.AreEqual( 0L, TextFormat.ParseUInt64( "0"));
Assert.AreEqual( 1L, TextFormat.ParseUInt64( "1"));
Assert.AreEqual( 12345L, TextFormat.ParseUInt64( "12345"));
Assert.AreEqual( 2147483647L, TextFormat.ParseUInt64( "2147483647"));
Assert.AreEqual( 4294967295L, TextFormat.ParseUInt64( "4294967295"));
Assert.AreEqual( 4294967296L, TextFormat.ParseUInt64( "4294967296"));
Assert.AreEqual(9223372036854775807UL, TextFormat.ParseUInt64("9223372036854775807"));
Assert.AreEqual(9223372036854775808UL, TextFormat.ParseUInt64("9223372036854775808"));
Assert.AreEqual(18446744073709551615UL, TextFormat.ParseUInt64("18446744073709551615"));
// Hex
Assert.AreEqual(0x1234abcd, TextFormat.ParseInt32("0x1234abcd"));
Assert.AreEqual(-0x1234abcd, TextFormat.ParseInt32("-0x1234abcd"));
Assert.AreEqual(0xffffffffffffffffUL, TextFormat.ParseUInt64("0xffffffffffffffff"));
Assert.AreEqual(0x7fffffffffffffffL,
TextFormat.ParseInt64("0x7fffffffffffffff"));
// Octal
Assert.AreEqual(342391, TextFormat.ParseInt32("01234567"));
// Out-of-range
AssertFormatException(() => TextFormat.ParseInt32("2147483648"));
AssertFormatException(() => TextFormat.ParseInt32("-2147483649"));
AssertFormatException(() => TextFormat.ParseUInt32("4294967296"));
AssertFormatException(() => TextFormat.ParseUInt32("-1"));
AssertFormatException(() => TextFormat.ParseInt64("9223372036854775808"));
AssertFormatException(() => TextFormat.ParseInt64("-9223372036854775809"));
AssertFormatException(() => TextFormat.ParseUInt64("18446744073709551616"));
AssertFormatException(() => TextFormat.ParseUInt64("-1"));
AssertFormatException(() => TextFormat.ParseInt32("abcd"));
}
}
}
|