OLD | NEW |
| (Empty) |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // http://code.google.com/p/protobuf/ | |
4 // | |
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. | |
30 | |
31 package com.google.protobuf.test; | |
32 import com.google.protobuf.*; | |
33 | |
34 import com.google.protobuf.Descriptors.FieldDescriptor; | |
35 import protobuf_unittest.UnittestMset.TestMessageSet; | |
36 import protobuf_unittest.UnittestMset.TestMessageSetExtension1; | |
37 import protobuf_unittest.UnittestMset.TestMessageSetExtension2; | |
38 import protobuf_unittest.UnittestProto.OneString; | |
39 import protobuf_unittest.UnittestProto.TestAllExtensions; | |
40 import protobuf_unittest.UnittestProto.TestAllTypes; | |
41 import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage; | |
42 import protobuf_unittest.UnittestProto.TestEmptyMessage; | |
43 | |
44 import junit.framework.TestCase; | |
45 | |
46 import java.io.StringReader; | |
47 | |
48 /** | |
49 * Test case for {@link TextFormat}. | |
50 * | |
51 * TODO(wenboz): ExtensionTest and rest of text_format_unittest.cc. | |
52 * | |
53 * @author wenboz@google.com (Wenbo Zhu) | |
54 */ | |
55 public class TextFormatTest extends TestCase { | |
56 | |
57 // A basic string with different escapable characters for testing. | |
58 private final static String kEscapeTestString = | |
59 "\"A string with ' characters \n and \r newlines and \t tabs and \001 " | |
60 + "slashes \\"; | |
61 | |
62 // A representation of the above string with all the characters escaped. | |
63 private final static String kEscapeTestStringEscaped = | |
64 "\\\"A string with \\' characters \\n and \\r newlines " | |
65 + "and \\t tabs and \\001 slashes \\\\"; | |
66 | |
67 private static String allFieldsSetText = TestUtil.readTextFromFile( | |
68 "text_format_unittest_data.txt"); | |
69 private static String allExtensionsSetText = TestUtil.readTextFromFile( | |
70 "text_format_unittest_extensions_data.txt"); | |
71 | |
72 private static String exoticText = | |
73 "repeated_int32: -1\n" + | |
74 "repeated_int32: -2147483648\n" + | |
75 "repeated_int64: -1\n" + | |
76 "repeated_int64: -9223372036854775808\n" + | |
77 "repeated_uint32: 4294967295\n" + | |
78 "repeated_uint32: 2147483648\n" + | |
79 "repeated_uint64: 18446744073709551615\n" + | |
80 "repeated_uint64: 9223372036854775808\n" + | |
81 "repeated_double: 123.0\n" + | |
82 "repeated_double: 123.5\n" + | |
83 "repeated_double: 0.125\n" + | |
84 "repeated_double: .125\n" + | |
85 "repeated_double: -.125\n" + | |
86 "repeated_double: 1.23E17\n" + | |
87 "repeated_double: 1.23E+17\n" + | |
88 "repeated_double: -1.23e-17\n" + | |
89 "repeated_double: .23e+17\n" + | |
90 "repeated_double: -.23E17\n" + | |
91 "repeated_double: 1.235E22\n" + | |
92 "repeated_double: 1.235E-18\n" + | |
93 "repeated_double: 123.456789\n" + | |
94 "repeated_double: Infinity\n" + | |
95 "repeated_double: -Infinity\n" + | |
96 "repeated_double: NaN\n" + | |
97 "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"" + | |
98 "\\341\\210\\264\"\n" + | |
99 "repeated_bytes: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\376\"\n"; | |
100 | |
101 private static String canonicalExoticText = | |
102 exoticText.replace(": .", ": 0.").replace(": -.", ": -0.") // short-form
double | |
103 .replace("23e", "23E").replace("E+", "E").replace("0.23E17", "2.3E16"); | |
104 | |
105 private String messageSetText = | |
106 "[protobuf_unittest.TestMessageSetExtension1] {\n" + | |
107 " i: 123\n" + | |
108 "}\n" + | |
109 "[protobuf_unittest.TestMessageSetExtension2] {\n" + | |
110 " str: \"foo\"\n" + | |
111 "}\n"; | |
112 | |
113 /** Print TestAllTypes and compare with golden file. */ | |
114 public void testPrintMessage() throws Exception { | |
115 String javaText = TextFormat.printToString(TestUtil.getAllSet()); | |
116 | |
117 // Java likes to add a trailing ".0" to floats and doubles. C printf | |
118 // (with %g format) does not. Our golden files are used for both | |
119 // C++ and Java TextFormat classes, so we need to conform. | |
120 javaText = javaText.replace(".0\n", "\n"); | |
121 | |
122 assertEquals(allFieldsSetText, javaText); | |
123 } | |
124 | |
125 /** Print TestAllTypes as Builder and compare with golden file. */ | |
126 public void testPrintMessageBuilder() throws Exception { | |
127 String javaText = TextFormat.printToString(TestUtil.getAllSetBuilder()); | |
128 | |
129 // Java likes to add a trailing ".0" to floats and doubles. C printf | |
130 // (with %g format) does not. Our golden files are used for both | |
131 // C++ and Java TextFormat classes, so we need to conform. | |
132 javaText = javaText.replace(".0\n", "\n"); | |
133 | |
134 assertEquals(allFieldsSetText, javaText); | |
135 } | |
136 | |
137 /** Print TestAllExtensions and compare with golden file. */ | |
138 public void testPrintExtensions() throws Exception { | |
139 String javaText = TextFormat.printToString(TestUtil.getAllExtensionsSet()); | |
140 | |
141 // Java likes to add a trailing ".0" to floats and doubles. C printf | |
142 // (with %g format) does not. Our golden files are used for both | |
143 // C++ and Java TextFormat classes, so we need to conform. | |
144 javaText = javaText.replace(".0\n", "\n"); | |
145 | |
146 assertEquals(allExtensionsSetText, javaText); | |
147 } | |
148 | |
149 // Creates an example unknown field set. | |
150 private UnknownFieldSet makeUnknownFieldSet() { | |
151 return UnknownFieldSet.newBuilder() | |
152 .addField(5, | |
153 UnknownFieldSet.Field.newBuilder() | |
154 .addVarint(1) | |
155 .addFixed32(2) | |
156 .addFixed64(3) | |
157 .addLengthDelimited(ByteString.copyFromUtf8("4")) | |
158 .addGroup( | |
159 UnknownFieldSet.newBuilder() | |
160 .addField(10, | |
161 UnknownFieldSet.Field.newBuilder() | |
162 .addVarint(5) | |
163 .build()) | |
164 .build()) | |
165 .build()) | |
166 .addField(8, | |
167 UnknownFieldSet.Field.newBuilder() | |
168 .addVarint(1) | |
169 .addVarint(2) | |
170 .addVarint(3) | |
171 .build()) | |
172 .addField(15, | |
173 UnknownFieldSet.Field.newBuilder() | |
174 .addVarint(0xABCDEF1234567890L) | |
175 .addFixed32(0xABCD1234) | |
176 .addFixed64(0xABCDEF1234567890L) | |
177 .build()) | |
178 .build(); | |
179 } | |
180 | |
181 public void testPrintUnknownFields() throws Exception { | |
182 // Test printing of unknown fields in a message. | |
183 | |
184 TestEmptyMessage message = | |
185 TestEmptyMessage.newBuilder() | |
186 .setUnknownFields(makeUnknownFieldSet()) | |
187 .build(); | |
188 | |
189 assertEquals( | |
190 "5: 1\n" + | |
191 "5: 0x00000002\n" + | |
192 "5: 0x0000000000000003\n" + | |
193 "5: \"4\"\n" + | |
194 "5 {\n" + | |
195 " 10: 5\n" + | |
196 "}\n" + | |
197 "8: 1\n" + | |
198 "8: 2\n" + | |
199 "8: 3\n" + | |
200 "15: 12379813812177893520\n" + | |
201 "15: 0xabcd1234\n" + | |
202 "15: 0xabcdef1234567890\n", | |
203 TextFormat.printToString(message)); | |
204 } | |
205 | |
206 public void testPrintField() throws Exception { | |
207 final FieldDescriptor dataField = | |
208 OneString.getDescriptor().findFieldByName("data"); | |
209 assertEquals( | |
210 "data: \"test data\"\n", | |
211 TextFormat.printFieldToString(dataField, "test data")); | |
212 | |
213 final FieldDescriptor optionalField = | |
214 TestAllTypes.getDescriptor().findFieldByName("optional_nested_message"); | |
215 final Object value = NestedMessage.newBuilder().setBb(42).build(); | |
216 | |
217 assertEquals( | |
218 "optional_nested_message {\n bb: 42\n}\n", | |
219 TextFormat.printFieldToString(optionalField, value)); | |
220 } | |
221 | |
222 /** | |
223 * Helper to construct a ByteString from a String containing only 8-bit | |
224 * characters. The characters are converted directly to bytes, *not* | |
225 * encoded using UTF-8. | |
226 */ | |
227 private ByteString bytes(String str) throws Exception { | |
228 return ByteString.copyFrom(str.getBytes("ISO-8859-1")); | |
229 } | |
230 | |
231 /** | |
232 * Helper to construct a ByteString from a bunch of bytes. The inputs are | |
233 * actually ints so that I can use hex notation and not get stupid errors | |
234 * about precision. | |
235 */ | |
236 private ByteString bytes(int... bytesAsInts) { | |
237 byte[] bytes = new byte[bytesAsInts.length]; | |
238 for (int i = 0; i < bytesAsInts.length; i++) { | |
239 bytes[i] = (byte) bytesAsInts[i]; | |
240 } | |
241 return ByteString.copyFrom(bytes); | |
242 } | |
243 | |
244 public void testPrintExotic() throws Exception { | |
245 Message message = TestAllTypes.newBuilder() | |
246 // Signed vs. unsigned numbers. | |
247 .addRepeatedInt32 (-1) | |
248 .addRepeatedUint32(-1) | |
249 .addRepeatedInt64 (-1) | |
250 .addRepeatedUint64(-1) | |
251 | |
252 .addRepeatedInt32 (1 << 31) | |
253 .addRepeatedUint32(1 << 31) | |
254 .addRepeatedInt64 (1l << 63) | |
255 .addRepeatedUint64(1l << 63) | |
256 | |
257 // Floats of various precisions and exponents. | |
258 .addRepeatedDouble(123) | |
259 .addRepeatedDouble(123.5) | |
260 .addRepeatedDouble(0.125) | |
261 .addRepeatedDouble(.125) | |
262 .addRepeatedDouble(-.125) | |
263 .addRepeatedDouble(123e15) | |
264 .addRepeatedDouble(123e15) | |
265 .addRepeatedDouble(-1.23e-17) | |
266 .addRepeatedDouble(.23e17) | |
267 .addRepeatedDouble(-23e15) | |
268 .addRepeatedDouble(123.5e20) | |
269 .addRepeatedDouble(123.5e-20) | |
270 .addRepeatedDouble(123.456789) | |
271 .addRepeatedDouble(Double.POSITIVE_INFINITY) | |
272 .addRepeatedDouble(Double.NEGATIVE_INFINITY) | |
273 .addRepeatedDouble(Double.NaN) | |
274 | |
275 // Strings and bytes that needing escaping. | |
276 .addRepeatedString("\0\001\007\b\f\n\r\t\013\\\'\"\u1234") | |
277 .addRepeatedBytes(bytes("\0\001\007\b\f\n\r\t\013\\\'\"\u00fe")) | |
278 .build(); | |
279 | |
280 assertEquals(canonicalExoticText, message.toString()); | |
281 } | |
282 | |
283 public void testPrintMessageSet() throws Exception { | |
284 TestMessageSet messageSet = | |
285 TestMessageSet.newBuilder() | |
286 .setExtension( | |
287 TestMessageSetExtension1.messageSetExtension, | |
288 TestMessageSetExtension1.newBuilder().setI(123).build()) | |
289 .setExtension( | |
290 TestMessageSetExtension2.messageSetExtension, | |
291 TestMessageSetExtension2.newBuilder().setStr("foo").build()) | |
292 .build(); | |
293 | |
294 assertEquals(messageSetText, messageSet.toString()); | |
295 } | |
296 | |
297 // ================================================================= | |
298 | |
299 public void testParse() throws Exception { | |
300 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
301 TextFormat.merge(allFieldsSetText, builder); | |
302 TestUtil.assertAllFieldsSet(builder.build()); | |
303 } | |
304 | |
305 public void testParseReader() throws Exception { | |
306 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
307 TextFormat.merge(new StringReader(allFieldsSetText), builder); | |
308 TestUtil.assertAllFieldsSet(builder.build()); | |
309 } | |
310 | |
311 public void testParseExtensions() throws Exception { | |
312 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); | |
313 TextFormat.merge(allExtensionsSetText, | |
314 TestUtil.getExtensionRegistry(), | |
315 builder); | |
316 TestUtil.assertAllExtensionsSet(builder.build()); | |
317 } | |
318 | |
319 public void testParseCompatibility() throws Exception { | |
320 String original = "repeated_float: inf\n" + | |
321 "repeated_float: -inf\n" + | |
322 "repeated_float: nan\n" + | |
323 "repeated_float: inff\n" + | |
324 "repeated_float: -inff\n" + | |
325 "repeated_float: nanf\n" + | |
326 "repeated_float: 1.0f\n" + | |
327 "repeated_float: infinityf\n" + | |
328 "repeated_float: -Infinityf\n" + | |
329 "repeated_double: infinity\n" + | |
330 "repeated_double: -infinity\n" + | |
331 "repeated_double: nan\n"; | |
332 String canonical = "repeated_float: Infinity\n" + | |
333 "repeated_float: -Infinity\n" + | |
334 "repeated_float: NaN\n" + | |
335 "repeated_float: Infinity\n" + | |
336 "repeated_float: -Infinity\n" + | |
337 "repeated_float: NaN\n" + | |
338 "repeated_float: 1.0\n" + | |
339 "repeated_float: Infinity\n" + | |
340 "repeated_float: -Infinity\n" + | |
341 "repeated_double: Infinity\n" + | |
342 "repeated_double: -Infinity\n" + | |
343 "repeated_double: NaN\n"; | |
344 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
345 TextFormat.merge(original, builder); | |
346 assertEquals(canonical, builder.build().toString()); | |
347 } | |
348 | |
349 public void testParseExotic() throws Exception { | |
350 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
351 TextFormat.merge(exoticText, builder); | |
352 | |
353 // Too lazy to check things individually. Don't try to debug this | |
354 // if testPrintExotic() is failing. | |
355 assertEquals(canonicalExoticText, builder.build().toString()); | |
356 } | |
357 | |
358 public void testParseMessageSet() throws Exception { | |
359 ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); | |
360 extensionRegistry.add(TestMessageSetExtension1.messageSetExtension); | |
361 extensionRegistry.add(TestMessageSetExtension2.messageSetExtension); | |
362 | |
363 TestMessageSet.Builder builder = TestMessageSet.newBuilder(); | |
364 TextFormat.merge(messageSetText, extensionRegistry, builder); | |
365 TestMessageSet messageSet = builder.build(); | |
366 | |
367 assertTrue(messageSet.hasExtension( | |
368 TestMessageSetExtension1.messageSetExtension)); | |
369 assertEquals(123, messageSet.getExtension( | |
370 TestMessageSetExtension1.messageSetExtension).getI()); | |
371 assertTrue(messageSet.hasExtension( | |
372 TestMessageSetExtension2.messageSetExtension)); | |
373 assertEquals("foo", messageSet.getExtension( | |
374 TestMessageSetExtension2.messageSetExtension).getStr()); | |
375 } | |
376 | |
377 public void testParseNumericEnum() throws Exception { | |
378 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
379 TextFormat.merge("optional_nested_enum: 2", builder); | |
380 assertEquals(TestAllTypes.NestedEnum.BAR, builder.getOptionalNestedEnum()); | |
381 } | |
382 | |
383 public void testParseAngleBrackets() throws Exception { | |
384 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
385 TextFormat.merge("OptionalGroup: < a: 1 >", builder); | |
386 assertTrue(builder.hasOptionalGroup()); | |
387 assertEquals(1, builder.getOptionalGroup().getA()); | |
388 } | |
389 | |
390 public void testParseComment() throws Exception { | |
391 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
392 TextFormat.merge( | |
393 "# this is a comment\n" + | |
394 "optional_int32: 1 # another comment\n" + | |
395 "optional_int64: 2\n" + | |
396 "# EOF comment", builder); | |
397 assertEquals(1, builder.getOptionalInt32()); | |
398 assertEquals(2, builder.getOptionalInt64()); | |
399 } | |
400 | |
401 // ================================================================= | |
402 | |
403 public void testParseString() throws Exception { | |
404 final String zh = "\u9999\u6e2f\u4e0a\u6d77\ud84f\udf80\u8c50\u9280\u884c"; | |
405 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
406 TextFormat.merge("optional_string: \"" + zh + "\"", builder); | |
407 assertEquals(zh, builder.getOptionalString()); | |
408 } | |
409 | |
410 public void testParseLongString() throws Exception { | |
411 String longText = | |
412 "123456789012345678901234567890123456789012345678901234567890" + | |
413 "123456789012345678901234567890123456789012345678901234567890" + | |
414 "123456789012345678901234567890123456789012345678901234567890" + | |
415 "123456789012345678901234567890123456789012345678901234567890" + | |
416 "123456789012345678901234567890123456789012345678901234567890" + | |
417 "123456789012345678901234567890123456789012345678901234567890" + | |
418 "123456789012345678901234567890123456789012345678901234567890" + | |
419 "123456789012345678901234567890123456789012345678901234567890" + | |
420 "123456789012345678901234567890123456789012345678901234567890" + | |
421 "123456789012345678901234567890123456789012345678901234567890" + | |
422 "123456789012345678901234567890123456789012345678901234567890" + | |
423 "123456789012345678901234567890123456789012345678901234567890" + | |
424 "123456789012345678901234567890123456789012345678901234567890" + | |
425 "123456789012345678901234567890123456789012345678901234567890" + | |
426 "123456789012345678901234567890123456789012345678901234567890" + | |
427 "123456789012345678901234567890123456789012345678901234567890" + | |
428 "123456789012345678901234567890123456789012345678901234567890" + | |
429 "123456789012345678901234567890123456789012345678901234567890" + | |
430 "123456789012345678901234567890123456789012345678901234567890" + | |
431 "123456789012345678901234567890123456789012345678901234567890"; | |
432 | |
433 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
434 TextFormat.merge("optional_string: \"" + longText + "\"", builder); | |
435 assertEquals(longText, builder.getOptionalString()); | |
436 } | |
437 | |
438 public void testParseBoolean() throws Exception { | |
439 String goodText = | |
440 "repeated_bool: t repeated_bool : 0\n" + | |
441 "repeated_bool :f repeated_bool:1"; | |
442 String goodTextCanonical = | |
443 "repeated_bool: true\n" + | |
444 "repeated_bool: false\n" + | |
445 "repeated_bool: false\n" + | |
446 "repeated_bool: true\n"; | |
447 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
448 TextFormat.merge(goodText, builder); | |
449 assertEquals(goodTextCanonical, builder.build().toString()); | |
450 | |
451 try { | |
452 TestAllTypes.Builder badBuilder = TestAllTypes.newBuilder(); | |
453 TextFormat.merge("optional_bool:2", badBuilder); | |
454 fail("Should have thrown an exception."); | |
455 } catch (TextFormat.ParseException e) { | |
456 // success | |
457 } | |
458 try { | |
459 TestAllTypes.Builder badBuilder = TestAllTypes.newBuilder(); | |
460 TextFormat.merge("optional_bool: foo", badBuilder); | |
461 fail("Should have thrown an exception."); | |
462 } catch (TextFormat.ParseException e) { | |
463 // success | |
464 } | |
465 } | |
466 | |
467 public void testParseAdjacentStringLiterals() throws Exception { | |
468 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
469 TextFormat.merge("optional_string: \"foo\" 'corge' \"grault\"", builder); | |
470 assertEquals("foocorgegrault", builder.getOptionalString()); | |
471 } | |
472 | |
473 public void testPrintFieldValue() throws Exception { | |
474 assertPrintFieldValue("\"Hello\"", "Hello", "repeated_string"); | |
475 assertPrintFieldValue("123.0", 123f, "repeated_float"); | |
476 assertPrintFieldValue("123.0", 123d, "repeated_double"); | |
477 assertPrintFieldValue("123", 123, "repeated_int32"); | |
478 assertPrintFieldValue("123", 123L, "repeated_int64"); | |
479 assertPrintFieldValue("true", true, "repeated_bool"); | |
480 assertPrintFieldValue("4294967295", 0xFFFFFFFF, "repeated_uint32"); | |
481 assertPrintFieldValue("18446744073709551615", 0xFFFFFFFFFFFFFFFFL, | |
482 "repeated_uint64"); | |
483 assertPrintFieldValue("\"\\001\\002\\003\"", | |
484 ByteString.copyFrom(new byte[] {1, 2, 3}), "repeated_bytes"); | |
485 } | |
486 | |
487 private void assertPrintFieldValue(String expect, Object value, | |
488 String fieldName) throws Exception { | |
489 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | |
490 StringBuilder sb = new StringBuilder(); | |
491 TextFormat.printFieldValue( | |
492 TestAllTypes.getDescriptor().findFieldByName(fieldName), | |
493 value, sb); | |
494 assertEquals(expect, sb.toString()); | |
495 } | |
496 | |
497 public void testShortDebugString() { | |
498 assertEquals("optional_nested_message { bb: 42 } repeated_int32: 1" | |
499 + " repeated_uint32: 2", | |
500 TextFormat.shortDebugString(TestAllTypes.newBuilder() | |
501 .addRepeatedInt32(1) | |
502 .addRepeatedUint32(2) | |
503 .setOptionalNestedMessage( | |
504 NestedMessage.newBuilder().setBb(42).build()) | |
505 .build())); | |
506 } | |
507 | |
508 public void testShortDebugString_unknown() { | |
509 assertEquals("5: 1 5: 0x00000002 5: 0x0000000000000003 5: \"4\" 5 { 10: 5 }" | |
510 + " 8: 1 8: 2 8: 3 15: 12379813812177893520 15: 0xabcd1234 15:" | |
511 + " 0xabcdef1234567890", | |
512 TextFormat.shortDebugString(makeUnknownFieldSet())); | |
513 } | |
514 | |
515 public void testPrintToUnicodeString() { | |
516 assertEquals( | |
517 "optional_string: \"abc\u3042efg\"\n" + | |
518 "optional_bytes: \"\\343\\201\\202\"\n" + | |
519 "repeated_string: \"\u3093XYZ\"\n", | |
520 TextFormat.printToUnicodeString(TestAllTypes.newBuilder() | |
521 .setOptionalString("abc\u3042efg") | |
522 .setOptionalBytes(bytes(0xe3, 0x81, 0x82)) | |
523 .addRepeatedString("\u3093XYZ") | |
524 .build())); | |
525 } | |
526 | |
527 public void testPrintToUnicodeString_unknown() { | |
528 assertEquals( | |
529 "1: \"\\343\\201\\202\"\n", | |
530 TextFormat.printToUnicodeString(UnknownFieldSet.newBuilder() | |
531 .addField(1, | |
532 UnknownFieldSet.Field.newBuilder() | |
533 .addLengthDelimited(bytes(0xe3, 0x81, 0x82)).build()) | |
534 .build())); | |
535 } | |
536 } | |
OLD | NEW |