| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
from __future__ import absolute_import |
| 21 |
|
| 22 |
import sys |
| 23 |
from uuid import uuid4 |
| 24 |
|
| 25 |
from proton import * |
| 26 |
|
| 27 |
from . import common |
| 28 |
|
| 29 |
|
| 30 |
class Test(common.Test): |
| 31 |
|
| 32 |
def setUp(self): |
| 33 |
self.data = Data() |
| 34 |
|
| 35 |
def tearDown(self): |
| 36 |
self.data = None |
| 37 |
|
| 38 |
|
| 39 |
class DataTest(Test): |
| 40 |
|
| 41 |
def testTopLevelNext(self): |
| 42 |
assert self.data.next() is None |
| 43 |
self.data.put_null() |
| 44 |
self.data.put_bool(False) |
| 45 |
self.data.put_int(0) |
| 46 |
assert self.data.next() is None |
| 47 |
self.data.rewind() |
| 48 |
assert self.data.next() == Data.NULL |
| 49 |
assert self.data.next() == Data.BOOL |
| 50 |
assert self.data.next() == Data.INT |
| 51 |
assert self.data.next() is None |
| 52 |
|
| 53 |
def testNestedNext(self): |
| 54 |
assert self.data.next() is None |
| 55 |
self.data.put_null() |
| 56 |
assert self.data.next() is None |
| 57 |
self.data.put_list() |
| 58 |
assert self.data.next() is None |
| 59 |
self.data.put_bool(False) |
| 60 |
assert self.data.next() is None |
| 61 |
self.data.rewind() |
| 62 |
assert self.data.next() is Data.NULL |
| 63 |
assert self.data.next() is Data.LIST |
| 64 |
self.data.enter() |
| 65 |
assert self.data.next() is None |
| 66 |
self.data.put_ubyte(0) |
| 67 |
assert self.data.next() is None |
| 68 |
self.data.put_uint(0) |
| 69 |
assert self.data.next() is None |
| 70 |
self.data.put_int(0) |
| 71 |
assert self.data.next() is None |
| 72 |
self.data.exit() |
| 73 |
assert self.data.next() is Data.BOOL |
| 74 |
assert self.data.next() is None |
| 75 |
|
| 76 |
self.data.rewind() |
| 77 |
assert self.data.next() is Data.NULL |
| 78 |
assert self.data.next() is Data.LIST |
| 79 |
assert self.data.enter() |
| 80 |
assert self.data.next() is Data.UBYTE |
| 81 |
assert self.data.next() is Data.UINT |
| 82 |
assert self.data.next() is Data.INT |
| 83 |
assert self.data.next() is None |
| 84 |
assert self.data.exit() |
| 85 |
assert self.data.next() is Data.BOOL |
| 86 |
assert self.data.next() is None |
| 87 |
|
| 88 |
def testEnterExit(self): |
| 89 |
assert self.data.next() is None |
| 90 |
assert not self.data.enter() |
| 91 |
self.data.put_list() |
| 92 |
assert self.data.enter() |
| 93 |
assert self.data.next() is None |
| 94 |
self.data.put_list() |
| 95 |
assert self.data.enter() |
| 96 |
self.data.put_list() |
| 97 |
assert self.data.enter() |
| 98 |
assert self.data.exit() |
| 99 |
assert self.data.get_list() == 0 |
| 100 |
assert self.data.exit() |
| 101 |
assert self.data.get_list() == 1 |
| 102 |
assert self.data.exit() |
| 103 |
assert self.data.get_list() == 1 |
| 104 |
assert not self.data.exit() |
| 105 |
assert self.data.get_list() == 1 |
| 106 |
assert self.data.next() is None |
| 107 |
|
| 108 |
self.data.rewind() |
| 109 |
assert self.data.next() is Data.LIST |
| 110 |
assert self.data.get_list() == 1 |
| 111 |
assert self.data.enter() |
| 112 |
assert self.data.next() is Data.LIST |
| 113 |
assert self.data.get_list() == 1 |
| 114 |
assert self.data.enter() |
| 115 |
assert self.data.next() is Data.LIST |
| 116 |
assert self.data.get_list() == 0 |
| 117 |
assert self.data.enter() |
| 118 |
assert self.data.next() is None |
| 119 |
assert self.data.exit() |
| 120 |
assert self.data.get_list() == 0 |
| 121 |
assert self.data.exit() |
| 122 |
assert self.data.get_list() == 1 |
| 123 |
assert self.data.exit() |
| 124 |
assert self.data.get_list() == 1 |
| 125 |
assert not self.data.exit() |
| 126 |
|
| 127 |
def put(self, putter, v): |
| 128 |
"""More informative exception from putters, include bad value""" |
| 129 |
try: |
| 130 |
putter(v) |
| 131 |
except Exception: |
| 132 |
etype, value, trace = sys.exc_info() |
| 133 |
v = etype("%s(%r): %s" % (putter.__name__, v, value)) |
| 134 |
if trace is None: |
| 135 |
raise v |
| 136 |
else: |
| 137 |
raise v.with_traceback(trace) |
| 138 |
|
| 139 |
return putter |
| 140 |
|
| 141 |
# (bits, signed) for each integer type |
| 142 |
INT_TYPES = { |
| 143 |
"byte": (8, True), |
| 144 |
"ubyte": (8, False), |
| 145 |
"short": (16, True), |
| 146 |
"ushort": (16, False), |
| 147 |
"int": (32, True), |
| 148 |
"uint": (32, False), |
| 149 |
"long": (64, True), |
| 150 |
"ulong": (64, False) |
| 151 |
} |
| 152 |
|
| 153 |
def int_values(self, dtype): |
| 154 |
"""Set of test values for integer type dtype, include extreme and medial values""" |
| 155 |
bits, signed = self.INT_TYPES[dtype] |
| 156 |
values = [0, 1, 2, 5, 42] |
| 157 |
if signed: |
| 158 |
min, max = -2**(bits - 1), 2**(bits - 1) - 1 |
| 159 |
values.append(max // 2) |
| 160 |
values += [-i for i in values if i] |
| 161 |
values += [min, max] |
| 162 |
else: |
| 163 |
max = 2**(bits) - 1 |
| 164 |
values += [max // 2, max] |
| 165 |
return sorted(values) |
| 166 |
|
| 167 |
def _testArray(self, dtype, descriptor, atype, *values): |
| 168 |
if dtype: |
| 169 |
dTYPE = getattr(self.data, dtype.upper()) |
| 170 |
aTYPE = getattr(self.data, atype.upper()) |
| 171 |
self.data.put_array(dtype is not None, aTYPE) |
| 172 |
self.data.enter() |
| 173 |
if dtype is not None: |
| 174 |
putter = getattr(self.data, "put_%s" % dtype) |
| 175 |
self.put(putter, descriptor) |
| 176 |
putter = getattr(self.data, "put_%s" % atype) |
| 177 |
for v in values: |
| 178 |
self.put(putter, v) |
| 179 |
self.data.exit() |
| 180 |
self.data.rewind() |
| 181 |
assert self.data.next() == Data.ARRAY |
| 182 |
count, described, type = self.data.get_array() |
| 183 |
assert count == len(values), count |
| 184 |
if dtype is None: |
| 185 |
assert described == False |
| 186 |
else: |
| 187 |
assert described |
| 188 |
assert type == aTYPE, type |
| 189 |
assert self.data.enter() |
| 190 |
if described: |
| 191 |
assert self.data.next() == dTYPE |
| 192 |
getter = getattr(self.data, "get_%s" % dtype) |
| 193 |
gotten = getter() |
| 194 |
assert gotten == descriptor, gotten |
| 195 |
if values: |
| 196 |
getter = getattr(self.data, "get_%s" % atype) |
| 197 |
for v in values: |
| 198 |
assert self.data.next() == aTYPE |
| 199 |
gotten = getter() |
| 200 |
assert gotten == v, gotten |
| 201 |
assert self.data.next() is None |
| 202 |
assert self.data.exit() |
| 203 |
|
| 204 |
def testStringArray(self): |
| 205 |
self._testArray(None, None, "string", "one", "two", "three") |
| 206 |
|
| 207 |
def testDescribedStringArray(self): |
| 208 |
self._testArray("symbol", "url", "string", "one", "two", "three") |
| 209 |
|
| 210 |
def _test_int_array(self, atype): |
| 211 |
self._testArray(None, None, atype, *self.int_values(atype)) |
| 212 |
|
| 213 |
def testByteArray(self): self._test_int_array("byte") |
| 214 |
def testUbyteArray(self): self._test_int_array("ubyte") |
| 215 |
def testShortArray(self): self._test_int_array("short") |
| 216 |
def testUshortArray(self): self._test_int_array("ushort") |
| 217 |
def testIntArray(self): self._test_int_array("int") |
| 218 |
def testUintArray(self): self._test_int_array("uint") |
| 219 |
def testLongArray(self): self._test_int_array("long") |
| 220 |
def testUlongArray(self): self._test_int_array("ulong") |
| 221 |
|
| 222 |
def testUUIDArray(self): |
| 223 |
self._testArray(None, None, "uuid", uuid4(), uuid4(), uuid4()) |
| 224 |
|
| 225 |
def testEmptyArray(self): |
| 226 |
self._testArray(None, None, "null") |
| 227 |
|
| 228 |
def testDescribedEmptyArray(self): |
| 229 |
self._testArray("long", 0, "null") |
| 230 |
|
| 231 |
def testPropertyDict(self): |
| 232 |
a = PropertyDict(one=1, two=2, three=3) |
| 233 |
b = PropertyDict({'one': 1, 'two': 2, 'three': 3}) |
| 234 |
c = PropertyDict(zip(['one', 'two', 'three'], [1, 2, 3])) |
| 235 |
d = PropertyDict([('two', 2), ('one', 1), ('three', 3)]) |
| 236 |
e = PropertyDict({symbol('three'): 3, symbol('one'): 1, symbol('two'): 2}) |
| 237 |
f = PropertyDict(a) |
| 238 |
g = PropertyDict() |
| 239 |
g['one'] = 1 |
| 240 |
g[symbol('two')] = 2 |
| 241 |
g['three'] = 3 |
| 242 |
assert a == b == c == d == e == f == g |
| 243 |
for k in a.keys(): |
| 244 |
assert isinstance(k, symbol) |
| 245 |
self.assertRaises(KeyError, AnnotationDict, {'one': 1, None: 'none'}) |
| 246 |
self.assertRaises(KeyError, AnnotationDict, {'one': 1, 1.23: 4}) |
| 247 |
|
| 248 |
def testPropertyDictNoRaiseError(self): |
| 249 |
a = PropertyDict(one=1, two=2, three=3, raise_on_error=False) |
| 250 |
a[4] = 'four' |
| 251 |
b = PropertyDict({'one': 1, 'two': 2, 'three': 3, 4: 'four'}, raise_on_error=False) |
| 252 |
c = PropertyDict(zip(['one', 'two', 'three', 4], [1, 2, 3, 'four']), raise_on_error=False) |
| 253 |
d = PropertyDict([('two', 2), ('one', 1), ('three', 3), (4, 'four')], raise_on_error=False) |
| 254 |
e = PropertyDict({4: 'four', symbol('three'): 3, symbol('one'): 1, symbol('two'): 2}, raise_on_error=False) |
| 255 |
f = PropertyDict(a, raise_on_error=False) |
| 256 |
g = PropertyDict(raise_on_error=False) |
| 257 |
g['one'] = 1 |
| 258 |
g[4] = 'four' |
| 259 |
g[symbol('two')] = 2 |
| 260 |
g['three'] = 3 |
| 261 |
assert a == b == c == d == e == f == g |
| 262 |
|
| 263 |
def testAnnotationDict(self): |
| 264 |
# AnnotationMap c'tor calls update(), so this method is also covered |
| 265 |
a = AnnotationDict(one=1, two=2, three=3) |
| 266 |
a[ulong(4)] = 'four' |
| 267 |
b = AnnotationDict({'one': 1, 'two': 2, 'three': 3, ulong(4): 'four'}) |
| 268 |
c = AnnotationDict(zip(['one', 'two', 'three', ulong(4)], [1, 2, 3, 'four'])) |
| 269 |
d = AnnotationDict([('two', 2), ('one', 1), ('three', 3), (ulong(4), 'four')]) |
| 270 |
e = AnnotationDict({symbol('three'): 3, ulong(4): 'four', symbol('one'): 1, symbol('two'): 2}) |
| 271 |
f = AnnotationDict(a) |
| 272 |
g = AnnotationDict() |
| 273 |
g[ulong(4)] = 'four' |
| 274 |
g['one'] = 1 |
| 275 |
g[symbol('two')] = 2 |
| 276 |
g['three'] = 3 |
| 277 |
assert a == b == c == d == e == f == g |
| 278 |
for k in a.keys(): |
| 279 |
assert isinstance(k, (symbol, ulong)) |
| 280 |
self.assertRaises(KeyError, AnnotationDict, {'one': 1, None: 'none'}) |
| 281 |
self.assertRaises(KeyError, AnnotationDict, {'one': 1, 1.23: 4}) |
| 282 |
|
| 283 |
def testAnnotationDictNoRaiseError(self): |
| 284 |
a = AnnotationDict(one=1, two=2, three=3, raise_on_error=False) |
| 285 |
a[ulong(4)] = 'four' |
| 286 |
a[5] = 'five' |
| 287 |
b = AnnotationDict({'one': 1, 'two': 2, 'three': 3, ulong(4): 'four', 5: 'five'}, raise_on_error=False) |
| 288 |
c = AnnotationDict(zip(['one', 'two', 'three', ulong(4), 5], [1, 2, 3, 'four', 'five']), raise_on_error=False) |
| 289 |
d = AnnotationDict([('two', 2), ('one', 1), ('three', 3), |
| 290 |
(ulong(4), 'four'), (5, 'five')], raise_on_error=False) |
| 291 |
e = AnnotationDict({5: 'five', symbol('three'): 3, ulong(4): 'four', |
| 292 |
symbol('one'): 1, symbol('two'): 2}, raise_on_error=False) |
| 293 |
f = AnnotationDict(a, raise_on_error=False) |
| 294 |
g = AnnotationDict(raise_on_error=False) |
| 295 |
g[ulong(4)] = 'four' |
| 296 |
g['one'] = 1 |
| 297 |
g[symbol('two')] = 2 |
| 298 |
g[5] = 'five' |
| 299 |
g['three'] = 3 |
| 300 |
assert a == b == c == d == e == f == g |
| 301 |
|
| 302 |
def testSymbolList(self): |
| 303 |
a = SymbolList(['one', 'two', 'three']) |
| 304 |
b = SymbolList([symbol('one'), symbol('two'), symbol('three')]) |
| 305 |
c = SymbolList() |
| 306 |
c.append('one') |
| 307 |
c.extend([symbol('two'), 'three']) |
| 308 |
d1 = SymbolList(['one']) |
| 309 |
d2 = SymbolList(['two', symbol('three')]) |
| 310 |
d = d1 + d2 |
| 311 |
e = SymbolList(['one']) |
| 312 |
e += SymbolList(['two', symbol('three')]) |
| 313 |
f = SymbolList(['one', 'hello', 'goodbye']) |
| 314 |
f[1] = symbol('two') |
| 315 |
f[2] = 'three' |
| 316 |
g = SymbolList(a) |
| 317 |
assert a == b == c == d == e == f == g |
| 318 |
for v in a: |
| 319 |
assert isinstance(v, symbol) |
| 320 |
self.assertRaises(TypeError, SymbolList, ['one', None]) |
| 321 |
self.assertRaises(TypeError, SymbolList, ['one', 2]) |
| 322 |
self.assertRaises(TypeError, SymbolList, ['one', ['two']]) |
| 323 |
self.assertRaises(TypeError, SymbolList, ['one', {'two': 3}]) |
| 324 |
|
| 325 |
def testSymbolListNoRaiseError(self): |
| 326 |
a = SymbolList(['one', 'two', 'three', 4], raise_on_error=False) |
| 327 |
b = SymbolList([symbol('one'), symbol('two'), symbol('three'), 4], raise_on_error=False) |
| 328 |
c = SymbolList(raise_on_error=False) |
| 329 |
c.append('one') |
| 330 |
c.extend([symbol('two'), 'three', 4]) |
| 331 |
d1 = SymbolList(['one'], raise_on_error=False) |
| 332 |
d2 = SymbolList(['two', symbol('three'), 4], raise_on_error=False) |
| 333 |
d = d1 + d2 |
| 334 |
e = SymbolList(['one'], raise_on_error=False) |
| 335 |
e += SymbolList(['two', symbol('three'), 4], raise_on_error=False) |
| 336 |
f = SymbolList(['one', 'hello', 'goodbye', 'what?'], raise_on_error=False) |
| 337 |
f[1] = symbol('two') |
| 338 |
f[2] = 'three' |
| 339 |
f[3] = 4 |
| 340 |
g = SymbolList(a, raise_on_error=False) |
| 341 |
assert a == b == c == d == e == f == g |
| 342 |
|
| 343 |
def _test(self, dtype, *values, **kwargs): |
| 344 |
eq = kwargs.get("eq", lambda x, y: x == y) |
| 345 |
ntype = getattr(Data, dtype.upper()) |
| 346 |
putter = getattr(self.data, "put_%s" % dtype) |
| 347 |
getter = getattr(self.data, "get_%s" % dtype) |
| 348 |
|
| 349 |
for v in values: |
| 350 |
self.put(putter, v) |
| 351 |
gotten = getter() |
| 352 |
assert eq(gotten, v), (gotten, v) |
| 353 |
|
| 354 |
self.data.rewind() |
| 355 |
|
| 356 |
for v in values: |
| 357 |
vtype = self.data.next() |
| 358 |
assert vtype == ntype, vtype |
| 359 |
gotten = getter() |
| 360 |
assert eq(gotten, v), (gotten, v) |
| 361 |
|
| 362 |
encoded = self.data.encode() |
| 363 |
copy = Data(0) |
| 364 |
while encoded: |
| 365 |
n = copy.decode(encoded) |
| 366 |
encoded = encoded[n:] |
| 367 |
copy.rewind() |
| 368 |
|
| 369 |
cgetter = getattr(copy, "get_%s" % dtype) |
| 370 |
|
| 371 |
for v in values: |
| 372 |
vtype = copy.next() |
| 373 |
assert vtype == ntype, vtype |
| 374 |
gotten = cgetter() |
| 375 |
assert eq(gotten, v), (gotten, v) |
| 376 |
|
| 377 |
def _test_int(self, itype): |
| 378 |
self._test(itype, *self.int_values(itype)) |
| 379 |
|
| 380 |
def testByte(self): self._test_int("byte") |
| 381 |
|
| 382 |
def testUbyte(self): |
| 383 |
self._test_int("ubyte") |
| 384 |
self.assertRaises(AssertionError, ubyte, -1) |
| 385 |
|
| 386 |
def testShort(self): self._test_int("short") |
| 387 |
|
| 388 |
def testUshort(self): |
| 389 |
self._test("ushort") |
| 390 |
self.assertRaises(AssertionError, ushort, -1) |
| 391 |
|
| 392 |
def testInt(self): self._test_int("int") |
| 393 |
|
| 394 |
def testUint(self): |
| 395 |
self._test_int("uint") |
| 396 |
self.assertRaises(AssertionError, uint, -1) |
| 397 |
|
| 398 |
def testLong(self): self._test_int("long") |
| 399 |
|
| 400 |
def testUlong(self): |
| 401 |
self._test_int("ulong") |
| 402 |
self.assertRaises(AssertionError, ulong, -1) |
| 403 |
|
| 404 |
def testString(self): |
| 405 |
self._test("string", "one", "two", "three", "this is a test", "") |
| 406 |
|
| 407 |
def testFloat(self): |
| 408 |
# we have to use a special comparison here because python |
| 409 |
# internally only uses doubles and converting between floats and |
| 410 |
# doubles is imprecise |
| 411 |
self._test("float", 0, 1, 2, 3, 0.1, 0.2, 0.3, -1, -2, -3, -0.1, -0.2, -0.3, |
| 412 |
eq=lambda x, y: x - y < 0.000001) |
| 413 |
|
| 414 |
def testDouble(self): |
| 415 |
self._test("double", 0, 1, 2, 3, 0.1, 0.2, 0.3, -1, -2, -3, -0.1, -0.2, -0.3) |
| 416 |
|
| 417 |
def testBinary(self): |
| 418 |
self._test("binary", b"this", b"is", b"a", b"test", b"of" b"b\x00inary") |
| 419 |
|
| 420 |
def testSymbol(self): |
| 421 |
self._test("symbol", symbol("this is a symbol test"), symbol("bleh"), symbol("blah")) |
| 422 |
|
| 423 |
def testTimestamp(self): |
| 424 |
self._test("timestamp", timestamp(0), timestamp(12345), timestamp(1000000)) |
| 425 |
|
| 426 |
def testChar(self): |
| 427 |
self._test("char", char('a'), char('b'), char('c'), char(u'\u20AC')) |
| 428 |
|
| 429 |
def testUUID(self): |
| 430 |
self._test("uuid", uuid4(), uuid4(), uuid4()) |
| 431 |
|
| 432 |
def testDecimal32(self): |
| 433 |
self._test("decimal32", decimal32(0), decimal32(1), decimal32(2), decimal32(3), decimal32(4), decimal32(2**30)) |
| 434 |
|
| 435 |
def testDecimal64(self): |
| 436 |
self._test("decimal64", decimal64(0), decimal64(1), decimal64(2), decimal64(3), decimal64(4), decimal64(2**60)) |
| 437 |
|
| 438 |
def testDecimal128(self): |
| 439 |
self._test("decimal128", decimal128(b"fdsaasdf;lkjjkl;"), decimal128(b"x" * 16)) |
| 440 |
|
| 441 |
def testCopy(self): |
| 442 |
self.data.put_described() |
| 443 |
self.data.enter() |
| 444 |
self.data.put_ulong(123) |
| 445 |
self.data.put_map() |
| 446 |
self.data.enter() |
| 447 |
self.data.put_string("pi") |
| 448 |
self.data.put_double(3.14159265359) |
| 449 |
|
| 450 |
dst = Data() |
| 451 |
dst.copy(self.data) |
| 452 |
|
| 453 |
copy = dst.format() |
| 454 |
orig = self.data.format() |
| 455 |
assert copy == orig, (copy, orig) |
| 456 |
|
| 457 |
def testCopyNested(self): |
| 458 |
nested = [1, 2, 3, [4, 5, 6], 7, 8, 9] |
| 459 |
self.data.put_object(nested) |
| 460 |
dst = Data() |
| 461 |
dst.copy(self.data) |
| 462 |
assert dst.format() == self.data.format() |
| 463 |
|
| 464 |
def testCopyNestedArray(self): |
| 465 |
nested = [Array(UNDESCRIBED, Data.LIST, |
| 466 |
["first", [Array(UNDESCRIBED, Data.INT, 1, 2, 3)]], |
| 467 |
["second", [Array(UNDESCRIBED, Data.INT, 1, 2, 3)]], |
| 468 |
["third", [Array(UNDESCRIBED, Data.INT, 1, 2, 3)]], |
| 469 |
), |
| 470 |
"end"] |
| 471 |
self.data.put_object(nested) |
| 472 |
dst = Data() |
| 473 |
dst.copy(self.data) |
| 474 |
assert dst.format() == self.data.format() |
| 475 |
|
| 476 |
def testRoundTrip(self): |
| 477 |
obj = {symbol("key"): timestamp(1234), |
| 478 |
ulong(123): "blah", |
| 479 |
char("c"): "bleh", |
| 480 |
u"desc": Described(symbol("url"), u"http://example.org"), |
| 481 |
u"array": Array(UNDESCRIBED, Data.INT, 1, 2, 3), |
| 482 |
u"list": [1, 2, 3, None, 4], |
| 483 |
u"boolean": True} |
| 484 |
self.data.put_object(obj) |
| 485 |
enc = self.data.encode() |
| 486 |
data = Data() |
| 487 |
data.decode(enc) |
| 488 |
data.rewind() |
| 489 |
assert data.next() |
| 490 |
copy = data.get_object() |
| 491 |
assert copy == obj, (copy, obj) |
| 492 |
|
| 493 |
def testBuffer(self): |
| 494 |
try: |
| 495 |
self.data.put_object(buffer(b"foo")) |
| 496 |
except NameError: |
| 497 |
# python >= 3.0 does not have `buffer` |
| 498 |
return |
| 499 |
data = Data() |
| 500 |
data.decode(self.data.encode()) |
| 501 |
data.rewind() |
| 502 |
assert data.next() |
| 503 |
assert data.type() == Data.BINARY |
| 504 |
assert data.get_object() == b"foo" |
| 505 |
|
| 506 |
def testMemoryView(self): |
| 507 |
self.data.put_object(memoryview(b"foo")) |
| 508 |
data = Data() |
| 509 |
data.decode(self.data.encode()) |
| 510 |
data.rewind() |
| 511 |
assert data.next() |
| 512 |
assert data.type() == Data.BINARY |
| 513 |
assert data.get_object() == b"foo" |
| 514 |
|
| 515 |
def testLookup(self): |
| 516 |
obj = {symbol("key"): u"value", |
| 517 |
symbol("pi"): 3.14159, |
| 518 |
symbol("list"): [1, 2, 3, 4]} |
| 519 |
self.data.put_object(obj) |
| 520 |
self.data.rewind() |
| 521 |
self.data.next() |
| 522 |
self.data.enter() |
| 523 |
self.data.narrow() |
| 524 |
assert self.data.lookup("pi") |
| 525 |
assert self.data.get_object() == 3.14159 |
| 526 |
self.data.rewind() |
| 527 |
assert self.data.lookup("key") |
| 528 |
assert self.data.get_object() == u"value" |
| 529 |
self.data.rewind() |
| 530 |
assert self.data.lookup("list") |
| 531 |
assert self.data.get_object() == [1, 2, 3, 4] |
| 532 |
self.data.widen() |
| 533 |
self.data.rewind() |
| 534 |
assert not self.data.lookup("pi") |