from six import PY2
from six import text_type
from six import unichr as six_unichr
+from six.moves import xrange as six_xrange
from six.moves.cPickle import dumps as pickle_dumps
from six.moves.cPickle import HIGHEST_PROTOCOL as pickle_proto
from six.moves.cPickle import loads as pickle_loads
list(obj.pps())
-class TestUTF8String(StringMixin, CommonMixin, TestCase):
- base_klass = UTF8String
-
-
cyrillic_letters = text(
alphabet="".join(six_unichr(i) for i in list(range(0x0410, 0x044f + 1))),
min_size=1,
)
+class TestUTF8String(StringMixin, CommonMixin, TestCase):
+ base_klass = UTF8String
+
+ @given(cyrillic_letters)
+ def test_byte_per_primitive(self, chars):
+ char = chars[0]
+ char_raw = char.encode("utf-8")
+ encoded = b"".join((
+ self.base_klass().tag_constructed,
+ LENINDEF,
+ OctetString(char_raw[:1]).encode(),
+ OctetString(char_raw[1:2]).encode(),
+ EOC,
+ ))
+ self.assertEqual(
+ self.base_klass().decod(encoded, ctx={"bered": True}),
+ char,
+ )
+
+
class UnicodeDecodeErrorMixin(object):
@given(cyrillic_letters)
def test_unicode_decode_error(self, cyrillic_text):
self.assertEqual(err.exception.offset, offset)
self.assertEqual(err.exception.decode_path, decode_path)
+ def test_byte_per_primitive(self):
+ encoded = b"".join((
+ self.base_klass().tag_constructed,
+ LENINDEF,
+ OctetString(b"1").encode(),
+ OctetString(b"2").encode(),
+ EOC,
+ ))
+ self.assertEqual(
+ self.base_klass().decod(encoded, ctx={"bered": True}),
+ "12",
+ )
+
class TestPrintableString(
UnicodeDecodeErrorMixin,
self.assertEqual(obj1, obj2)
self.assertSequenceEqual(list(obj1), list(obj2))
+ def test_iterator_pickling(self):
+ class SeqOf(SequenceOf):
+ schema = Integer()
+ register_class(SeqOf)
+ seqof = SeqOf()
+ pickle_dumps(seqof)
+ seqof = seqof(iter(six_xrange(10)))
+ with assertRaisesRegex(self, ValueError, "iterator"):
+ pickle_dumps(seqof)
+
+ def test_iterator_bounds(self):
+ class SeqOf(SequenceOf):
+ schema = Integer()
+ bounds = (10, 20)
+ seqof = None
+ def gen(n):
+ for i in six_xrange(n):
+ yield Integer(i)
+ for n in (9, 21):
+ seqof = SeqOf(gen(n))
+ self.assertTrue(seqof.ready)
+ with self.assertRaises(BoundsError):
+ seqof.encode()
+ self.assertFalse(seqof.ready)
+ seqof = seqof(gen(n))
+ self.assertTrue(seqof.ready)
+ with self.assertRaises(BoundsError):
+ encode_cer(seqof)
+ self.assertFalse(seqof.ready)
+
+ def test_iterator_twice(self):
+ class SeqOf(SequenceOf):
+ schema = Integer()
+ bounds = (1, float("+inf"))
+ def gen():
+ for i in six_xrange(10):
+ yield Integer(i)
+ seqof = SeqOf(gen())
+ self.assertTrue(seqof.ready)
+ seqof.encode()
+ self.assertFalse(seqof.ready)
+ register_class(SeqOf)
+ pickle_dumps(seqof)
+
+ def test_non_ready_bound_min(self):
+ class SeqOf(SequenceOf):
+ schema = Integer()
+ bounds = (1, float("+inf"))
+ seqof = SeqOf()
+ self.assertFalse(seqof.ready)
+
class TestSetOf(SeqOfMixing, CommonMixin, TestCase):
class SeqOf(SetOf):
class TestX690PrefixedType(TestCase):
- def runTest(self):
+ def test_1(self):
self.assertSequenceEqual(
VisibleString("Jones").encode(),
hexdec("1A054A6F6E6573"),
)
+
+ def test_2(self):
self.assertSequenceEqual(
VisibleString(
"Jones",
).encode(),
hexdec("43054A6F6E6573"),
)
+
+ def test_3(self):
self.assertSequenceEqual(
Any(
VisibleString(
).encode(),
hexdec("A20743054A6F6E6573"),
)
+
+ def test_4(self):
self.assertSequenceEqual(
OctetString(
VisibleString(
).encode(),
hexdec("670743054A6F6E6573"),
)
+
+ def test_5(self):
self.assertSequenceEqual(
VisibleString("Jones", impl=tag_ctxp(2)).encode(),
hexdec("82054A6F6E6573"),