PageRenderTime 78ms CodeModel.GetById 40ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/python/google/protobuf/internal/text_format_test.py

http://github.com/tomahawk-player/tomahawk
Python | 571 lines | 530 code | 9 blank | 32 comment | 3 complexity | 166cac120fffa6225b078b1c6436b1f2 MD5 | raw file
  1#! /usr/bin/python
  2#
  3# Protocol Buffers - Google's data interchange format
  4# Copyright 2008 Google Inc.  All rights reserved.
  5# http://code.google.com/p/protobuf/
  6#
  7# Redistribution and use in source and binary forms, with or without
  8# modification, are permitted provided that the following conditions are
  9# met:
 10#
 11#     * 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.
 20#
 21# 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.
 32
 33"""Test for google.protobuf.text_format."""
 34
 35__author__ = 'kenton@google.com (Kenton Varda)'
 36
 37import difflib
 38import re
 39
 40import unittest
 41from google.protobuf import text_format
 42from google.protobuf.internal import test_util
 43from google.protobuf import unittest_pb2
 44from google.protobuf import unittest_mset_pb2
 45
 46
 47class TextFormatTest(unittest.TestCase):
 48  def ReadGolden(self, golden_filename):
 49    f = test_util.GoldenFile(golden_filename)
 50    golden_lines = f.readlines()
 51    f.close()
 52    return golden_lines
 53
 54  def CompareToGoldenFile(self, text, golden_filename):
 55    golden_lines = self.ReadGolden(golden_filename)
 56    self.CompareToGoldenLines(text, golden_lines)
 57
 58  def CompareToGoldenText(self, text, golden_text):
 59    self.CompareToGoldenLines(text, golden_text.splitlines(1))
 60
 61  def CompareToGoldenLines(self, text, golden_lines):
 62    actual_lines = text.splitlines(1)
 63    self.assertEqual(golden_lines, actual_lines,
 64      "Text doesn't match golden.  Diff:\n" +
 65      ''.join(difflib.ndiff(golden_lines, actual_lines)))
 66
 67  def testPrintAllFields(self):
 68    message = unittest_pb2.TestAllTypes()
 69    test_util.SetAllFields(message)
 70    self.CompareToGoldenFile(
 71      self.RemoveRedundantZeros(text_format.MessageToString(message)),
 72      'text_format_unittest_data.txt')
 73
 74  def testPrintAllExtensions(self):
 75    message = unittest_pb2.TestAllExtensions()
 76    test_util.SetAllExtensions(message)
 77    self.CompareToGoldenFile(
 78      self.RemoveRedundantZeros(text_format.MessageToString(message)),
 79      'text_format_unittest_extensions_data.txt')
 80
 81  def testPrintMessageSet(self):
 82    message = unittest_mset_pb2.TestMessageSetContainer()
 83    ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension
 84    ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension
 85    message.message_set.Extensions[ext1].i = 23
 86    message.message_set.Extensions[ext2].str = 'foo'
 87    self.CompareToGoldenText(text_format.MessageToString(message),
 88      'message_set {\n'
 89      '  [protobuf_unittest.TestMessageSetExtension1] {\n'
 90      '    i: 23\n'
 91      '  }\n'
 92      '  [protobuf_unittest.TestMessageSetExtension2] {\n'
 93      '    str: \"foo\"\n'
 94      '  }\n'
 95      '}\n')
 96
 97  def testPrintExotic(self):
 98    message = unittest_pb2.TestAllTypes()
 99    message.repeated_int64.append(-9223372036854775808)
100    message.repeated_uint64.append(18446744073709551615)
101    message.repeated_double.append(123.456)
102    message.repeated_double.append(1.23e22)
103    message.repeated_double.append(1.23e-18)
104    message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
105    message.repeated_string.append(u'\u00fc\ua71f')
106    self.CompareToGoldenText(
107      self.RemoveRedundantZeros(text_format.MessageToString(message)),
108      'repeated_int64: -9223372036854775808\n'
109      'repeated_uint64: 18446744073709551615\n'
110      'repeated_double: 123.456\n'
111      'repeated_double: 1.23e+22\n'
112      'repeated_double: 1.23e-18\n'
113      'repeated_string: '
114        '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
115      'repeated_string: "\\303\\274\\352\\234\\237"\n')
116
117  def testPrintNestedMessageAsOneLine(self):
118    message = unittest_pb2.TestAllTypes()
119    msg = message.repeated_nested_message.add()
120    msg.bb = 42;
121    self.CompareToGoldenText(
122        text_format.MessageToString(message, as_one_line=True),
123        'repeated_nested_message { bb: 42 }')
124
125  def testPrintRepeatedFieldsAsOneLine(self):
126    message = unittest_pb2.TestAllTypes()
127    message.repeated_int32.append(1)
128    message.repeated_int32.append(1)
129    message.repeated_int32.append(3)
130    message.repeated_string.append("Google")
131    message.repeated_string.append("Zurich")
132    self.CompareToGoldenText(
133        text_format.MessageToString(message, as_one_line=True),
134        'repeated_int32: 1 repeated_int32: 1 repeated_int32: 3 '
135        'repeated_string: "Google" repeated_string: "Zurich"')
136
137  def testPrintNestedNewLineInStringAsOneLine(self):
138    message = unittest_pb2.TestAllTypes()
139    message.optional_string = "a\nnew\nline"
140    self.CompareToGoldenText(
141        text_format.MessageToString(message, as_one_line=True),
142        'optional_string: "a\\nnew\\nline"')
143
144  def testPrintMessageSetAsOneLine(self):
145    message = unittest_mset_pb2.TestMessageSetContainer()
146    ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension
147    ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension
148    message.message_set.Extensions[ext1].i = 23
149    message.message_set.Extensions[ext2].str = 'foo'
150    self.CompareToGoldenText(
151        text_format.MessageToString(message, as_one_line=True),
152        'message_set {'
153        ' [protobuf_unittest.TestMessageSetExtension1] {'
154        ' i: 23'
155        ' }'
156        ' [protobuf_unittest.TestMessageSetExtension2] {'
157        ' str: \"foo\"'
158        ' }'
159        ' }')
160
161  def testPrintExoticAsOneLine(self):
162    message = unittest_pb2.TestAllTypes()
163    message.repeated_int64.append(-9223372036854775808)
164    message.repeated_uint64.append(18446744073709551615)
165    message.repeated_double.append(123.456)
166    message.repeated_double.append(1.23e22)
167    message.repeated_double.append(1.23e-18)
168    message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
169    message.repeated_string.append(u'\u00fc\ua71f')
170    self.CompareToGoldenText(
171      self.RemoveRedundantZeros(
172          text_format.MessageToString(message, as_one_line=True)),
173      'repeated_int64: -9223372036854775808'
174      ' repeated_uint64: 18446744073709551615'
175      ' repeated_double: 123.456'
176      ' repeated_double: 1.23e+22'
177      ' repeated_double: 1.23e-18'
178      ' repeated_string: '
179      '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""'
180      ' repeated_string: "\\303\\274\\352\\234\\237"')
181
182  def testRoundTripExoticAsOneLine(self):
183    message = unittest_pb2.TestAllTypes()
184    message.repeated_int64.append(-9223372036854775808)
185    message.repeated_uint64.append(18446744073709551615)
186    message.repeated_double.append(123.456)
187    message.repeated_double.append(1.23e22)
188    message.repeated_double.append(1.23e-18)
189    message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
190    message.repeated_string.append(u'\u00fc\ua71f')
191
192    # Test as_utf8 = False.
193    wire_text = text_format.MessageToString(
194        message, as_one_line=True, as_utf8=False)
195    parsed_message = unittest_pb2.TestAllTypes()
196    text_format.Merge(wire_text, parsed_message)
197    self.assertEquals(message, parsed_message)
198
199    # Test as_utf8 = True.
200    wire_text = text_format.MessageToString(
201        message, as_one_line=True, as_utf8=True)
202    parsed_message = unittest_pb2.TestAllTypes()
203    text_format.Merge(wire_text, parsed_message)
204    self.assertEquals(message, parsed_message)
205
206  def testPrintRawUtf8String(self):
207    message = unittest_pb2.TestAllTypes()
208    message.repeated_string.append(u'\u00fc\ua71f')
209    text = text_format.MessageToString(message, as_utf8 = True)
210    self.CompareToGoldenText(text, 'repeated_string: "\303\274\352\234\237"\n')
211    parsed_message = unittest_pb2.TestAllTypes()
212    text_format.Merge(text, parsed_message)
213    self.assertEquals(message, parsed_message)
214
215  def testMessageToString(self):
216    message = unittest_pb2.ForeignMessage()
217    message.c = 123
218    self.assertEqual('c: 123\n', str(message))
219
220  def RemoveRedundantZeros(self, text):
221    # Some platforms print 1e+5 as 1e+005.  This is fine, but we need to remove
222    # these zeros in order to match the golden file.
223    text = text.replace('e+0','e+').replace('e+0','e+') \
224               .replace('e-0','e-').replace('e-0','e-')
225    # Floating point fields are printed with .0 suffix even if they are
226    # actualy integer numbers.
227    text = re.compile('\.0$', re.MULTILINE).sub('', text)
228    return text
229
230  def testMergeGolden(self):
231    golden_text = '\n'.join(self.ReadGolden('text_format_unittest_data.txt'))
232    parsed_message = unittest_pb2.TestAllTypes()
233    text_format.Merge(golden_text, parsed_message)
234
235    message = unittest_pb2.TestAllTypes()
236    test_util.SetAllFields(message)
237    self.assertEquals(message, parsed_message)
238
239  def testMergeGoldenExtensions(self):
240    golden_text = '\n'.join(self.ReadGolden(
241        'text_format_unittest_extensions_data.txt'))
242    parsed_message = unittest_pb2.TestAllExtensions()
243    text_format.Merge(golden_text, parsed_message)
244
245    message = unittest_pb2.TestAllExtensions()
246    test_util.SetAllExtensions(message)
247    self.assertEquals(message, parsed_message)
248
249  def testMergeAllFields(self):
250    message = unittest_pb2.TestAllTypes()
251    test_util.SetAllFields(message)
252    ascii_text = text_format.MessageToString(message)
253
254    parsed_message = unittest_pb2.TestAllTypes()
255    text_format.Merge(ascii_text, parsed_message)
256    self.assertEqual(message, parsed_message)
257    test_util.ExpectAllFieldsSet(self, message)
258
259  def testMergeAllExtensions(self):
260    message = unittest_pb2.TestAllExtensions()
261    test_util.SetAllExtensions(message)
262    ascii_text = text_format.MessageToString(message)
263
264    parsed_message = unittest_pb2.TestAllExtensions()
265    text_format.Merge(ascii_text, parsed_message)
266    self.assertEqual(message, parsed_message)
267
268  def testMergeMessageSet(self):
269    message = unittest_pb2.TestAllTypes()
270    text = ('repeated_uint64: 1\n'
271            'repeated_uint64: 2\n')
272    text_format.Merge(text, message)
273    self.assertEqual(1, message.repeated_uint64[0])
274    self.assertEqual(2, message.repeated_uint64[1])
275
276    message = unittest_mset_pb2.TestMessageSetContainer()
277    text = ('message_set {\n'
278            '  [protobuf_unittest.TestMessageSetExtension1] {\n'
279            '    i: 23\n'
280            '  }\n'
281            '  [protobuf_unittest.TestMessageSetExtension2] {\n'
282            '    str: \"foo\"\n'
283            '  }\n'
284            '}\n')
285    text_format.Merge(text, message)
286    ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension
287    ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension
288    self.assertEquals(23, message.message_set.Extensions[ext1].i)
289    self.assertEquals('foo', message.message_set.Extensions[ext2].str)
290
291  def testMergeExotic(self):
292    message = unittest_pb2.TestAllTypes()
293    text = ('repeated_int64: -9223372036854775808\n'
294            'repeated_uint64: 18446744073709551615\n'
295            'repeated_double: 123.456\n'
296            'repeated_double: 1.23e+22\n'
297            'repeated_double: 1.23e-18\n'
298            'repeated_string: \n'
299            '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
300            'repeated_string: "foo" \'corge\' "grault"\n'
301            'repeated_string: "\\303\\274\\352\\234\\237"\n'
302            'repeated_string: "\\xc3\\xbc"\n'
303            'repeated_string: "\xc3\xbc"\n')
304    text_format.Merge(text, message)
305
306    self.assertEqual(-9223372036854775808, message.repeated_int64[0])
307    self.assertEqual(18446744073709551615, message.repeated_uint64[0])
308    self.assertEqual(123.456, message.repeated_double[0])
309    self.assertEqual(1.23e22, message.repeated_double[1])
310    self.assertEqual(1.23e-18, message.repeated_double[2])
311    self.assertEqual(
312        '\000\001\a\b\f\n\r\t\v\\\'"', message.repeated_string[0])
313    self.assertEqual('foocorgegrault', message.repeated_string[1])
314    self.assertEqual(u'\u00fc\ua71f', message.repeated_string[2])
315    self.assertEqual(u'\u00fc', message.repeated_string[3])
316
317  def testMergeEmptyText(self):
318    message = unittest_pb2.TestAllTypes()
319    text = ''
320    text_format.Merge(text, message)
321    self.assertEquals(unittest_pb2.TestAllTypes(), message)
322
323  def testMergeInvalidUtf8(self):
324    message = unittest_pb2.TestAllTypes()
325    text = 'repeated_string: "\\xc3\\xc3"'
326    self.assertRaises(text_format.ParseError, text_format.Merge, text, message)
327
328  def testMergeSingleWord(self):
329    message = unittest_pb2.TestAllTypes()
330    text = 'foo'
331    self.assertRaisesWithMessage(
332        text_format.ParseError,
333        ('1:1 : Message type "protobuf_unittest.TestAllTypes" has no field named '
334         '"foo".'),
335        text_format.Merge, text, message)
336
337  def testMergeUnknownField(self):
338    message = unittest_pb2.TestAllTypes()
339    text = 'unknown_field: 8\n'
340    self.assertRaisesWithMessage(
341        text_format.ParseError,
342        ('1:1 : Message type "protobuf_unittest.TestAllTypes" has no field named '
343         '"unknown_field".'),
344        text_format.Merge, text, message)
345
346  def testMergeBadExtension(self):
347    message = unittest_pb2.TestAllExtensions()
348    text = '[unknown_extension]: 8\n'
349    self.assertRaisesWithMessage(
350        text_format.ParseError,
351        '1:2 : Extension "unknown_extension" not registered.',
352        text_format.Merge, text, message)
353    message = unittest_pb2.TestAllTypes()
354    self.assertRaisesWithMessage(
355        text_format.ParseError,
356        ('1:2 : Message type "protobuf_unittest.TestAllTypes" does not have '
357         'extensions.'),
358        text_format.Merge, text, message)
359
360  def testMergeGroupNotClosed(self):
361    message = unittest_pb2.TestAllTypes()
362    text = 'RepeatedGroup: <'
363    self.assertRaisesWithMessage(
364        text_format.ParseError, '1:16 : Expected ">".',
365        text_format.Merge, text, message)
366
367    text = 'RepeatedGroup: {'
368    self.assertRaisesWithMessage(
369        text_format.ParseError, '1:16 : Expected "}".',
370        text_format.Merge, text, message)
371
372  def testMergeEmptyGroup(self):
373    message = unittest_pb2.TestAllTypes()
374    text = 'OptionalGroup: {}'
375    text_format.Merge(text, message)
376    self.assertTrue(message.HasField('optionalgroup'))
377
378    message.Clear()
379
380    message = unittest_pb2.TestAllTypes()
381    text = 'OptionalGroup: <>'
382    text_format.Merge(text, message)
383    self.assertTrue(message.HasField('optionalgroup'))
384
385  def testMergeBadEnumValue(self):
386    message = unittest_pb2.TestAllTypes()
387    text = 'optional_nested_enum: BARR'
388    self.assertRaisesWithMessage(
389        text_format.ParseError,
390        ('1:23 : Enum type "protobuf_unittest.TestAllTypes.NestedEnum" '
391         'has no value named BARR.'),
392        text_format.Merge, text, message)
393
394    message = unittest_pb2.TestAllTypes()
395    text = 'optional_nested_enum: 100'
396    self.assertRaisesWithMessage(
397        text_format.ParseError,
398        ('1:23 : Enum type "protobuf_unittest.TestAllTypes.NestedEnum" '
399         'has no value with number 100.'),
400        text_format.Merge, text, message)
401
402  def assertRaisesWithMessage(self, e_class, e, func, *args, **kwargs):
403    """Same as assertRaises, but also compares the exception message."""
404    if hasattr(e_class, '__name__'):
405      exc_name = e_class.__name__
406    else:
407      exc_name = str(e_class)
408
409    try:
410      func(*args, **kwargs)
411    except e_class, expr:
412      if str(expr) != e:
413        msg = '%s raised, but with wrong message: "%s" instead of "%s"'
414        raise self.failureException(msg % (exc_name,
415                                           str(expr).encode('string_escape'),
416                                           e.encode('string_escape')))
417      return
418    else:
419      raise self.failureException('%s not raised' % exc_name)
420
421
422class TokenizerTest(unittest.TestCase):
423
424  def testSimpleTokenCases(self):
425    text = ('identifier1:"string1"\n     \n\n'
426            'identifier2 : \n \n123  \n  identifier3 :\'string\'\n'
427            'identifiER_4 : 1.1e+2 ID5:-0.23 ID6:\'aaaa\\\'bbbb\'\n'
428            'ID7 : "aa\\"bb"\n\n\n\n ID8: {A:inf B:-inf C:true D:false}\n'
429            'ID9: 22 ID10: -111111111111111111 ID11: -22\n'
430            'ID12: 2222222222222222222 '
431            'false_bool:  0 true_BOOL:t \n true_bool1:  1 false_BOOL1:f ' )
432    tokenizer = text_format._Tokenizer(text)
433    methods = [(tokenizer.ConsumeIdentifier, 'identifier1'),
434               ':',
435               (tokenizer.ConsumeString, 'string1'),
436               (tokenizer.ConsumeIdentifier, 'identifier2'),
437               ':',
438               (tokenizer.ConsumeInt32, 123),
439               (tokenizer.ConsumeIdentifier, 'identifier3'),
440               ':',
441               (tokenizer.ConsumeString, 'string'),
442               (tokenizer.ConsumeIdentifier, 'identifiER_4'),
443               ':',
444               (tokenizer.ConsumeFloat, 1.1e+2),
445               (tokenizer.ConsumeIdentifier, 'ID5'),
446               ':',
447               (tokenizer.ConsumeFloat, -0.23),
448               (tokenizer.ConsumeIdentifier, 'ID6'),
449               ':',
450               (tokenizer.ConsumeString, 'aaaa\'bbbb'),
451               (tokenizer.ConsumeIdentifier, 'ID7'),
452               ':',
453               (tokenizer.ConsumeString, 'aa\"bb'),
454               (tokenizer.ConsumeIdentifier, 'ID8'),
455               ':',
456               '{',
457               (tokenizer.ConsumeIdentifier, 'A'),
458               ':',
459               (tokenizer.ConsumeFloat, text_format._INFINITY),
460               (tokenizer.ConsumeIdentifier, 'B'),
461               ':',
462               (tokenizer.ConsumeFloat, -text_format._INFINITY),
463               (tokenizer.ConsumeIdentifier, 'C'),
464               ':',
465               (tokenizer.ConsumeBool, True),
466               (tokenizer.ConsumeIdentifier, 'D'),
467               ':',
468               (tokenizer.ConsumeBool, False),
469               '}',
470               (tokenizer.ConsumeIdentifier, 'ID9'),
471               ':',
472               (tokenizer.ConsumeUint32, 22),
473               (tokenizer.ConsumeIdentifier, 'ID10'),
474               ':',
475               (tokenizer.ConsumeInt64, -111111111111111111),
476               (tokenizer.ConsumeIdentifier, 'ID11'),
477               ':',
478               (tokenizer.ConsumeInt32, -22),
479               (tokenizer.ConsumeIdentifier, 'ID12'),
480               ':',
481               (tokenizer.ConsumeUint64, 2222222222222222222),
482               (tokenizer.ConsumeIdentifier, 'false_bool'),
483               ':',
484               (tokenizer.ConsumeBool, False),
485               (tokenizer.ConsumeIdentifier, 'true_BOOL'),
486               ':',
487               (tokenizer.ConsumeBool, True),
488               (tokenizer.ConsumeIdentifier, 'true_bool1'),
489               ':',
490               (tokenizer.ConsumeBool, True),
491               (tokenizer.ConsumeIdentifier, 'false_BOOL1'),
492               ':',
493               (tokenizer.ConsumeBool, False)]
494
495    i = 0
496    while not tokenizer.AtEnd():
497      m = methods[i]
498      if type(m) == str:
499        token = tokenizer.token
500        self.assertEqual(token, m)
501        tokenizer.NextToken()
502      else:
503        self.assertEqual(m[1], m[0]())
504      i += 1
505
506  def testConsumeIntegers(self):
507    # This test only tests the failures in the integer parsing methods as well
508    # as the '0' special cases.
509    int64_max = (1 << 63) - 1
510    uint32_max = (1 << 32) - 1
511    text = '-1 %d %d' % (uint32_max + 1, int64_max + 1)
512    tokenizer = text_format._Tokenizer(text)
513    self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint32)
514    self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint64)
515    self.assertEqual(-1, tokenizer.ConsumeInt32())
516
517    self.assertRaises(text_format.ParseError, tokenizer.ConsumeUint32)
518    self.assertRaises(text_format.ParseError, tokenizer.ConsumeInt32)
519    self.assertEqual(uint32_max + 1, tokenizer.ConsumeInt64())
520
521    self.assertRaises(text_format.ParseError, tokenizer.ConsumeInt64)
522    self.assertEqual(int64_max + 1, tokenizer.ConsumeUint64())
523    self.assertTrue(tokenizer.AtEnd())
524
525    text = '-0 -0 0 0'
526    tokenizer = text_format._Tokenizer(text)
527    self.assertEqual(0, tokenizer.ConsumeUint32())
528    self.assertEqual(0, tokenizer.ConsumeUint64())
529    self.assertEqual(0, tokenizer.ConsumeUint32())
530    self.assertEqual(0, tokenizer.ConsumeUint64())
531    self.assertTrue(tokenizer.AtEnd())
532
533  def testConsumeByteString(self):
534    text = '"string1\''
535    tokenizer = text_format._Tokenizer(text)
536    self.assertRaises(text_format.ParseError, tokenizer.ConsumeByteString)
537
538    text = 'string1"'
539    tokenizer = text_format._Tokenizer(text)
540    self.assertRaises(text_format.ParseError, tokenizer.ConsumeByteString)
541
542    text = '\n"\\xt"'
543    tokenizer = text_format._Tokenizer(text)
544    self.assertRaises(text_format.ParseError, tokenizer.ConsumeByteString)
545
546    text = '\n"\\"'
547    tokenizer = text_format._Tokenizer(text)
548    self.assertRaises(text_format.ParseError, tokenizer.ConsumeByteString)
549
550    text = '\n"\\x"'
551    tokenizer = text_format._Tokenizer(text)
552    self.assertRaises(text_format.ParseError, tokenizer.ConsumeByteString)
553
554  def testConsumeBool(self):
555    text = 'not-a-bool'
556    tokenizer = text_format._Tokenizer(text)
557    self.assertRaises(text_format.ParseError, tokenizer.ConsumeBool)
558
559  def testInfNan(self):
560    # Make sure our infinity and NaN definitions are sound.
561    self.assertEquals(float, type(text_format._INFINITY))
562    self.assertEquals(float, type(text_format._NAN))
563    self.assertTrue(text_format._NAN != text_format._NAN)
564
565    inf_times_zero = text_format._INFINITY * 0
566    self.assertTrue(inf_times_zero != inf_times_zero)
567    self.assertTrue(text_format._INFINITY > 0)
568
569
570if __name__ == '__main__':
571  unittest.main()