Exemplo n.º 1
0
    def testnotanumber(self):
        golden_data = (
            "\x5d\x00\x00\xc0\x7f"
            "\x61\x00\x00\x00\x00\x00\x00\xf8\x7f"
            "\xcd\x02\x00\x00\xc0\x7f"
            "\xd1\x02\x00\x00\x00\x00\x00\x00\xf8\x7f"
        )
        golden_message = unittest_pb2.testalltypes()
        golden_message.parsefromstring(golden_data)
        self.asserttrue(isnan(golden_message.optional_float))
        self.asserttrue(isnan(golden_message.optional_double))
        self.asserttrue(isnan(golden_message.repeated_float[0]))
        self.asserttrue(isnan(golden_message.repeated_double[0]))

        # the protocol buffer may serialize to any one of multiple different
        # representations of a nan.  rather than verify a specific representation,
        # verify the serialized string can be converted into a correctly
        # behaving protocol buffer.
        serialized = golden_message.serializetostring()
        message = unittest_pb2.testalltypes()
        message.parsefromstring(serialized)
        self.asserttrue(isnan(message.optional_float))
        self.asserttrue(isnan(message.optional_double))
        self.asserttrue(isnan(message.repeated_float[0]))
        self.asserttrue(isnan(message.repeated_double[0]))
  def testmergegolden(self):
    golden_text = '\n'.join(self.readgolden('text_format_unittest_data.txt'))
    parsed_message = unittest_pb2.testalltypes()
    text_format.merge(golden_text, parsed_message)

    message = unittest_pb2.testalltypes()
    test_util.setallfields(message)
    self.assertequals(message, parsed_message)
 def testprintrawutf8string(self):
   message = unittest_pb2.testalltypes()
   message.repeated_string.append(u'\u00fc\ua71f')
   text = text_format.messagetostring(message, as_utf8 = true)
   self.comparetogoldentext(text, 'repeated_string: "\303\274\352\234\237"\n')
   parsed_message = unittest_pb2.testalltypes()
   text_format.merge(text, parsed_message)
   self.assertequals(message, parsed_message)
  def testmergeallfields(self):
    message = unittest_pb2.testalltypes()
    test_util.setallfields(message)
    ascii_text = text_format.messagetostring(message)

    parsed_message = unittest_pb2.testalltypes()
    text_format.merge(ascii_text, parsed_message)
    self.assertequal(message, parsed_message)
    test_util.expectallfieldsset(self, message)
  def testmergeemptygroup(self):
    message = unittest_pb2.testalltypes()
    text = 'optionalgroup: {}'
    text_format.merge(text, message)
    self.asserttrue(message.hasfield('optionalgroup'))

    message.clear()

    message = unittest_pb2.testalltypes()
    text = 'optionalgroup: <>'
    text_format.merge(text, message)
    self.asserttrue(message.hasfield('optionalgroup'))
  def testmergeexotic(self):
    message = unittest_pb2.testalltypes()
    text = ('repeated_int64: -9223372036854775808\n'
            'repeated_uint64: 18446744073709551615\n'
            'repeated_double: 123.456\n'
            'repeated_double: 1.23e+22\n'
            'repeated_double: 1.23e-18\n'
            'repeated_string: \n'
            '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
            'repeated_string: "foo" \'corge\' "grault"\n'
            'repeated_string: "\\303\\274\\352\\234\\237"\n'
            'repeated_string: "\\xc3\\xbc"\n'
            'repeated_string: "\xc3\xbc"\n')
    text_format.merge(text, message)

    self.assertequal(-9223372036854775808, message.repeated_int64[0])
    self.assertequal(18446744073709551615, message.repeated_uint64[0])
    self.assertequal(123.456, message.repeated_double[0])
    self.assertequal(1.23e22, message.repeated_double[1])
    self.assertequal(1.23e-18, message.repeated_double[2])
    self.assertequal(
        '\000\001\a\b\f\n\r\t\v\\\'"', message.repeated_string[0])
    self.assertequal('foocorgegrault', message.repeated_string[1])
    self.assertequal(u'\u00fc\ua71f', message.repeated_string[2])
    self.assertequal(u'\u00fc', message.repeated_string[3])
 def testmergebadintvalue(self):
   message = unittest_pb2.testalltypes()
   text = 'optional_int32: bork'
   self.assertraiseswithmessage(
       text_format.parseerror,
       ('1:17 : couldn\'t parse integer: bork'),
       text_format.merge, text, message)
Exemplo n.º 8
0
    def testrepeatedscalarfieldsortarguments(self):
        """check sorting a scalar field using list.sort() arguments."""
        message = unittest_pb2.testalltypes()

        abs_cmp = lambda a, b: cmp(abs(a), abs(b))
        message.repeated_int32.append(-3)
        message.repeated_int32.append(-2)
        message.repeated_int32.append(-1)
        message.repeated_int32.sort(key=abs)
        self.assertequal(list(message.repeated_int32), [-1, -2, -3])
        message.repeated_int32.sort(key=abs, reverse=true)
        self.assertequal(list(message.repeated_int32), [-3, -2, -1])
        message.repeated_int32.sort(sort_function=abs_cmp)
        self.assertequal(list(message.repeated_int32), [-1, -2, -3])
        message.repeated_int32.sort(cmp=abs_cmp, reverse=true)
        self.assertequal(list(message.repeated_int32), [-3, -2, -1])

        len_cmp = lambda a, b: cmp(len(a), len(b))
        message.repeated_string.append("aaa")
        message.repeated_string.append("bb")
        message.repeated_string.append("c")
        message.repeated_string.sort(key=len)
        self.assertequal(list(message.repeated_string), ["c", "bb", "aaa"])
        message.repeated_string.sort(key=len, reverse=true)
        self.assertequal(list(message.repeated_string), ["aaa", "bb", "c"])
        message.repeated_string.sort(sort_function=len_cmp)
        self.assertequal(list(message.repeated_string), ["c", "bb", "aaa"])
        message.repeated_string.sort(cmp=len_cmp, reverse=true)
        self.assertequal(list(message.repeated_string), ["aaa", "bb", "c"])
  def testmergebadenumvalue(self):
    message = unittest_pb2.testalltypes()
    text = 'optional_nested_enum: barr'
    self.assertraiseswithmessage(
        text_format.parseerror,
        ('1:23 : enum type "protobuf_unittest.testalltypes.nestedenum" '
         'has no value named barr.'),
        text_format.merge, text, message)

    message = unittest_pb2.testalltypes()
    text = 'optional_nested_enum: 100'
    self.assertraiseswithmessage(
        text_format.parseerror,
        ('1:23 : enum type "protobuf_unittest.testalltypes.nestedenum" '
         'has no value with number 100.'),
        text_format.merge, text, message)
Exemplo n.º 10
0
 def testprintnestedmessageasoneline(self):
   message = unittest_pb2.testalltypes()
   msg = message.repeated_nested_message.add()
   msg.bb = 42;
   self.comparetogoldentext(
       text_format.messagetostring(message, as_one_line=true),
       'repeated_nested_message { bb: 42 }')
Exemplo n.º 11
0
 def testgoldenmessage(self):
     golden_data = test_util.goldenfile("golden_message").read()
     golden_message = unittest_pb2.testalltypes()
     golden_message.parsefromstring(golden_data)
     test_util.expectallfieldsset(self, golden_message)
     self.assertequal(golden_data, golden_message.serializetostring())
     golden_copy = copy.deepcopy(golden_message)
     self.assertequal(golden_data, golden_copy.serializetostring())
Exemplo n.º 12
0
 def testmergesingleword(self):
   message = unittest_pb2.testalltypes()
   text = 'foo'
   self.assertraiseswithmessage(
       text_format.parseerror,
       ('1:1 : message type "protobuf_unittest.testalltypes" has no field named '
        '"foo".'),
       text_format.merge, text, message)
Exemplo n.º 13
0
    def testpicklesupport(self):
        golden_data = test_util.goldenfile("golden_message").read()
        golden_message = unittest_pb2.testalltypes()
        golden_message.parsefromstring(golden_data)
        pickled_message = pickle.dumps(golden_message)

        unpickled_message = pickle.loads(pickled_message)
        self.assertequals(unpickled_message, golden_message)
 def setup(self):
   self.descriptor = unittest_pb2.testalltypes.descriptor
   self.all_fields = unittest_pb2.testalltypes()
   test_util.setallfields(self.all_fields)
   self.all_fields_data = self.all_fields.serializetostring()
   self.empty_message = unittest_pb2.testemptymessage()
   self.empty_message.parsefromstring(self.all_fields_data)
   self.unknown_fields = self.empty_message._unknown_fields
Exemplo n.º 15
0
 def testmergeunknownfield(self):
   message = unittest_pb2.testalltypes()
   text = 'unknown_field: 8\n'
   self.assertraiseswithmessage(
       text_format.parseerror,
       ('1:1 : message type "protobuf_unittest.testalltypes" has no field named '
        '"unknown_field".'),
       text_format.merge, text, message)
Exemplo n.º 16
0
 def testprintbadenumvalue(self):
   message = unittest_pb2.testalltypes()
   message.optional_nested_enum = 100
   message.optional_foreign_enum = 101
   message.optional_import_enum = 102
   self.comparetogoldentext(
       text_format.messagetostring(message),
       'optional_nested_enum: 100\n'
       'optional_foreign_enum: 101\n'
       'optional_import_enum: 102\n')
Exemplo n.º 17
0
 def testprintrepeatedfieldsasoneline(self):
   message = unittest_pb2.testalltypes()
   message.repeated_int32.append(1)
   message.repeated_int32.append(1)
   message.repeated_int32.append(3)
   message.repeated_string.append("google")
   message.repeated_string.append("zurich")
   self.comparetogoldentext(
       text_format.messagetostring(message, as_one_line=true),
       'repeated_int32: 1 repeated_int32: 1 repeated_int32: 3 '
       'repeated_string: "google" repeated_string: "zurich"')
Exemplo n.º 18
0
  def testpublicimports(self):
    # test public imports as embedded message.
    all_type_proto = unittest_pb2.testalltypes()
    self.assertequal(0, all_type_proto.optional_public_import_message.e)

    # publicimportmessage is actually defined in unittest_import_public_pb2
    # module, and is public imported by unittest_import_pb2 module.
    public_import_proto = unittest_import_pb2.publicimportmessage()
    self.assertequal(0, public_import_proto.e)
    self.asserttrue(unittest_import_public_pb2.publicimportmessage is
                    unittest_import_pb2.publicimportmessage)
Exemplo n.º 19
0
  def testmergegroupnotclosed(self):
    message = unittest_pb2.testalltypes()
    text = 'repeatedgroup: <'
    self.assertraiseswithmessage(
        text_format.parseerror, '1:16 : expected ">".',
        text_format.merge, text, message)

    text = 'repeatedgroup: {'
    self.assertraiseswithmessage(
        text_format.parseerror, '1:16 : expected "}".',
        text_format.merge, text, message)
Exemplo n.º 20
0
    def testparsingmerge(self):
        """check the merge behavior when a required or optional field appears
    multiple times in the input."""
        messages = [unittest_pb2.testalltypes(), unittest_pb2.testalltypes(), unittest_pb2.testalltypes()]
        messages[0].optional_int32 = 1
        messages[1].optional_int64 = 2
        messages[2].optional_int32 = 3
        messages[2].optional_string = "hello"

        merged_message = unittest_pb2.testalltypes()
        merged_message.optional_int32 = 3
        merged_message.optional_int64 = 2
        merged_message.optional_string = "hello"

        generator = unittest_pb2.testparsingmerge.repeatedfieldsgenerator()
        generator.field1.extend(messages)
        generator.field2.extend(messages)
        generator.field3.extend(messages)
        generator.ext1.extend(messages)
        generator.ext2.extend(messages)
        generator.group1.add().field1.mergefrom(messages[0])
        generator.group1.add().field1.mergefrom(messages[1])
        generator.group1.add().field1.mergefrom(messages[2])
        generator.group2.add().field1.mergefrom(messages[0])
        generator.group2.add().field1.mergefrom(messages[1])
        generator.group2.add().field1.mergefrom(messages[2])

        data = generator.serializetostring()
        parsing_merge = unittest_pb2.testparsingmerge()
        parsing_merge.parsefromstring(data)

        # required and optional fields should be merged.
        self.assertequal(parsing_merge.required_all_types, merged_message)
        self.assertequal(parsing_merge.optional_all_types, merged_message)
        self.assertequal(parsing_merge.optionalgroup.optional_group_all_types, merged_message)
        self.assertequal(parsing_merge.extensions[unittest_pb2.testparsingmerge.optional_ext], merged_message)

        # repeated fields should not be merged.
        self.assertequal(len(parsing_merge.repeated_all_types), 3)
        self.assertequal(len(parsing_merge.repeatedgroup), 3)
        self.assertequal(len(parsing_merge.extensions[unittest_pb2.testparsingmerge.repeated_ext]), 3)
Exemplo n.º 21
0
 def testmergebadextension(self):
   message = unittest_pb2.testallextensions()
   text = '[unknown_extension]: 8\n'
   self.assertraiseswithmessage(
       text_format.parseerror,
       '1:2 : extension "unknown_extension" not registered.',
       text_format.merge, text, message)
   message = unittest_pb2.testalltypes()
   self.assertraiseswithmessage(
       text_format.parseerror,
       ('1:2 : message type "protobuf_unittest.testalltypes" does not have '
        'extensions.'),
       text_format.merge, text, message)
Exemplo n.º 22
0
 def testnegativeinfinity(self):
     golden_data = (
         "\x5d\x00\x00\x80\xff"
         "\x61\x00\x00\x00\x00\x00\x00\xf0\xff"
         "\xcd\x02\x00\x00\x80\xff"
         "\xd1\x02\x00\x00\x00\x00\x00\x00\xf0\xff"
     )
     golden_message = unittest_pb2.testalltypes()
     golden_message.parsefromstring(golden_data)
     self.asserttrue(isneginf(golden_message.optional_float))
     self.asserttrue(isneginf(golden_message.optional_double))
     self.asserttrue(isneginf(golden_message.repeated_float[0]))
     self.asserttrue(isneginf(golden_message.repeated_double[0]))
     self.assertequal(golden_data, golden_message.serializetostring())
Exemplo n.º 23
0
  def testroundtripexoticasoneline(self):
    message = unittest_pb2.testalltypes()
    message.repeated_int64.append(-9223372036854775808)
    message.repeated_uint64.append(18446744073709551615)
    message.repeated_double.append(123.456)
    message.repeated_double.append(1.23e22)
    message.repeated_double.append(1.23e-18)
    message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
    message.repeated_string.append(u'\u00fc\ua71f')

    # test as_utf8 = false.
    wire_text = text_format.messagetostring(
        message, as_one_line=true, as_utf8=false)
    parsed_message = unittest_pb2.testalltypes()
    text_format.merge(wire_text, parsed_message)
    self.assertequals(message, parsed_message)

    # test as_utf8 = true.
    wire_text = text_format.messagetostring(
        message, as_one_line=true, as_utf8=true)
    parsed_message = unittest_pb2.testalltypes()
    text_format.merge(wire_text, parsed_message)
    self.assertequals(message, parsed_message)
Exemplo n.º 24
0
  def testenums(self):
    # we test only module-level enums here.
    # todo(robinson): examine descriptors directly to check
    # enum descriptor output.
    self.assertequal(4, unittest_pb2.foreign_foo)
    self.assertequal(5, unittest_pb2.foreign_bar)
    self.assertequal(6, unittest_pb2.foreign_baz)

    proto = unittest_pb2.testalltypes()
    self.assertequal(1, proto.foo)
    self.assertequal(1, unittest_pb2.testalltypes.foo)
    self.assertequal(2, proto.bar)
    self.assertequal(2, unittest_pb2.testalltypes.bar)
    self.assertequal(3, proto.baz)
    self.assertequal(3, unittest_pb2.testalltypes.baz)
  def testmergefrom(self):
    message = unittest_pb2.testalltypes()
    message.optional_int32 = 1
    message.optional_uint32 = 2
    source = unittest_pb2.testemptymessage()
    source.parsefromstring(message.serializetostring())

    message.clearfield('optional_int32')
    message.optional_int64 = 3
    message.optional_uint32 = 4
    destination = unittest_pb2.testemptymessage()
    destination.parsefromstring(message.serializetostring())
    unknown_fields = destination._unknown_fields[:]

    destination.mergefrom(source)
    self.assertequal(unknown_fields + source._unknown_fields,
                     destination._unknown_fields)
Exemplo n.º 26
0
    def testsortingrepeatedcompositefieldscustomcomparator(self):
        """check passing a custom comparator to sort a repeated composite field."""
        message = unittest_pb2.testalltypes()

        message.repeated_nested_message.add().bb = 1
        message.repeated_nested_message.add().bb = 3
        message.repeated_nested_message.add().bb = 2
        message.repeated_nested_message.add().bb = 6
        message.repeated_nested_message.add().bb = 5
        message.repeated_nested_message.add().bb = 4
        message.repeated_nested_message.sort(lambda x, y: cmp(x.bb, y.bb))
        self.assertequal(message.repeated_nested_message[0].bb, 1)
        self.assertequal(message.repeated_nested_message[1].bb, 2)
        self.assertequal(message.repeated_nested_message[2].bb, 3)
        self.assertequal(message.repeated_nested_message[3].bb, 4)
        self.assertequal(message.repeated_nested_message[4].bb, 5)
        self.assertequal(message.repeated_nested_message[5].bb, 6)
Exemplo n.º 27
0
    def testextremedoublevalues(self):
        message = unittest_pb2.testalltypes()

        # most positive exponent, no significand bits set.
        kmostposexponentnosigbits = math.pow(2, 1023)
        message.optional_double = kmostposexponentnosigbits
        message.parsefromstring(message.serializetostring())
        self.asserttrue(message.optional_double == kmostposexponentnosigbits)

        # most positive exponent, one significand bit set.
        kmostposexponentonesigbit = 1.5 * math.pow(2, 1023)
        message.optional_double = kmostposexponentonesigbit
        message.parsefromstring(message.serializetostring())
        self.asserttrue(message.optional_double == kmostposexponentonesigbit)

        # repeat last two cases with values of same magnitude, but negative.
        message.optional_double = -kmostposexponentnosigbits
        message.parsefromstring(message.serializetostring())
        self.asserttrue(message.optional_double == -kmostposexponentnosigbits)

        message.optional_double = -kmostposexponentonesigbit
        message.parsefromstring(message.serializetostring())
        self.asserttrue(message.optional_double == -kmostposexponentonesigbit)

        # most negative exponent, no significand bits set.
        kmostnegexponentnosigbits = math.pow(2, -1023)
        message.optional_double = kmostnegexponentnosigbits
        message.parsefromstring(message.serializetostring())
        self.asserttrue(message.optional_double == kmostnegexponentnosigbits)

        # most negative exponent, one significand bit set.
        kmostnegexponentonesigbit = 1.5 * math.pow(2, -1023)
        message.optional_double = kmostnegexponentonesigbit
        message.parsefromstring(message.serializetostring())
        self.asserttrue(message.optional_double == kmostnegexponentonesigbit)

        # repeat last two cases with values of the same magnitude, but negative.
        message.optional_double = -kmostnegexponentnosigbits
        message.parsefromstring(message.serializetostring())
        self.asserttrue(message.optional_double == -kmostnegexponentnosigbits)

        message.optional_double = -kmostnegexponentonesigbit
        message.parsefromstring(message.serializetostring())
        self.asserttrue(message.optional_double == -kmostnegexponentonesigbit)
Exemplo n.º 28
0
    def testsortingrepeatedscalarfieldscustomcomparator(self):
        """check some different types with custom comparator."""
        message = unittest_pb2.testalltypes()

        message.repeated_int32.append(-3)
        message.repeated_int32.append(-2)
        message.repeated_int32.append(-1)
        message.repeated_int32.sort(lambda x, y: cmp(abs(x), abs(y)))
        self.assertequal(message.repeated_int32[0], -1)
        self.assertequal(message.repeated_int32[1], -2)
        self.assertequal(message.repeated_int32[2], -3)

        message.repeated_string.append("aaa")
        message.repeated_string.append("bb")
        message.repeated_string.append("c")
        message.repeated_string.sort(lambda x, y: cmp(len(x), len(y)))
        self.assertequal(message.repeated_string[0], "c")
        self.assertequal(message.repeated_string[1], "bb")
        self.assertequal(message.repeated_string[2], "aaa")
Exemplo n.º 29
0
  def testmergestringfieldunescape(self):
    message = unittest_pb2.testalltypes()
    text = r'''repeated_string: "\xf\x62"
               repeated_string: "\\xf\\x62"
               repeated_string: "\\\xf\\\x62"
               repeated_string: "\\\\xf\\\\x62"
               repeated_string: "\\\\\xf\\\\\x62"
               repeated_string: "\x5cx20"'''
    text_format.merge(text, message)

    slash = '\\'
    self.assertequal('\x0fb', message.repeated_string[0])
    self.assertequal(slash + 'xf' + slash + 'x62', message.repeated_string[1])
    self.assertequal(slash + '\x0f' + slash + 'b', message.repeated_string[2])
    self.assertequal(slash + slash + 'xf' + slash + slash + 'x62',
                     message.repeated_string[3])
    self.assertequal(slash + slash + '\x0f' + slash + slash + 'b',
                     message.repeated_string[4])
    self.assertequal(slash + 'x20', message.repeated_string[5])
Exemplo n.º 30
0
 def testprintexotic(self):
   message = unittest_pb2.testalltypes()
   message.repeated_int64.append(-9223372036854775808)
   message.repeated_uint64.append(18446744073709551615)
   message.repeated_double.append(123.456)
   message.repeated_double.append(1.23e22)
   message.repeated_double.append(1.23e-18)
   message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
   message.repeated_string.append(u'\u00fc\ua71f')
   self.comparetogoldentext(
     self.removeredundantzeros(text_format.messagetostring(message)),
     'repeated_int64: -9223372036854775808\n'
     'repeated_uint64: 18446744073709551615\n'
     'repeated_double: 123.456\n'
     'repeated_double: 1.23e+22\n'
     'repeated_double: 1.23e-18\n'
     'repeated_string: '
       '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
     'repeated_string: "\\303\\274\\352\\234\\237"\n')