1
3
# Protocol Buffers - Google's data interchange format
2
# Copyright 2008 Google Inc.
4
# Copyright 2008 Google Inc. All rights reserved.
3
5
# http://code.google.com/p/protobuf/
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
7
# Redistribution and use in source and binary forms, with or without
8
# modification, are permitted provided that the following conditions are
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
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.
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.
17
33
"""Test for google.protobuf.internal.encoder."""
43
59
def AppendScalarTestHelper(self, test_name, encoder_method,
44
60
expected_stream_method_name,
45
wire_type, field_value, expected_value=None):
61
wire_type, field_value,
62
expected_value=None, expected_length=None):
46
63
"""Helper for testAppendScalars.
48
65
Calls one of the Encoder methods, and ensures that the Encoder
63
80
expected_value: The value we expect Encoder to pass into
64
81
the OutputStream method. If None, we expect field_value
65
82
to pass through unmodified.
83
expected_length: The length we expect Encoder to pass to the
84
AppendVarUInt32 method. If None we expect the length of the
67
87
if expected_value is None:
68
88
expected_value = field_value
78
98
self.mock_stream.AppendVarUInt32(self.PackTag(field_number, wire_type))
79
99
# If we're length-delimited, we should then append the length.
80
100
if wire_type == wire_format.WIRETYPE_LENGTH_DELIMITED:
81
self.mock_stream.AppendVarUInt32(len(field_value))
101
if expected_length is None:
102
expected_length = len(field_value)
103
self.mock_stream.AppendVarUInt32(expected_length)
82
104
# Should then append the value itself.
83
105
# We have to use names instead of methods to work around some
84
106
# mox weirdness. (ResetAll() is overzealous).
92
114
self.mox.ResetAll()
94
116
def testAppendScalars(self):
117
utf8_bytes = '\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82'
118
utf8_string = unicode(utf8_bytes, 'utf-8')
96
120
['int32', self.encoder.AppendInt32, 'AppendVarint32',
97
121
wire_format.WIRETYPE_VARINT, 0],
120
144
['string', self.encoder.AppendString, 'AppendRawBytes',
121
145
wire_format.WIRETYPE_LENGTH_DELIMITED,
122
146
"You're in a maze of twisty little passages, all alike."],
147
['utf8-string', self.encoder.AppendString, 'AppendRawBytes',
148
wire_format.WIRETYPE_LENGTH_DELIMITED, utf8_string,
149
utf8_bytes, len(utf8_bytes)],
123
150
# We test zigzag encoding routines more extensively below.
124
151
['sint32', self.encoder.AppendSInt32, 'AppendVarUInt32',
125
152
wire_format.WIRETYPE_VARINT, -1, 1],
129
156
# Ensure that we're testing different Encoder methods and using
130
157
# different test names in all test cases above.
131
158
self.assertEqual(len(scalar_tests), len(set(t[0] for t in scalar_tests)))
132
self.assertEqual(len(scalar_tests), len(set(t[1] for t in scalar_tests)))
159
self.assert_(len(scalar_tests) >= len(set(t[1] for t in scalar_tests)))
133
160
for args in scalar_tests:
134
161
self.AppendScalarTestHelper(*args)