def test_repr_dict(self): d = Dictionary({ '/Boolean': True, '/Integer': 42, '/Real': Decimal('42.42'), '/String': String('hi'), '/Array': Array([1, 2, 3.14]), '/Operator': Operator('q'), '/Dictionary': Dictionary({'/Color': 'Red'}), }) expected = """\ pikepdf.Dictionary({ "/Array": [ 1, 2, Decimal('3.140000') ], "/Boolean": True, "/Dictionary": { "/Color": "Red" }, "/Integer": 42, "/Operator": pikepdf.Operator("q"), "/Real": Decimal('42.42'), "/String": "hi" }) """ def strip_all_whitespace(s): return ''.join(s.split()) assert strip_all_whitespace(repr(d)) == strip_all_whitespace(expected) assert eval(repr(d)) == d
def test_repr_scalar(self): scalars = [ False, 666, Decimal('3.14'), String('scalar'), Name('/Bob'), Operator('Q') ] for s in scalars: assert eval(repr(s)) == s
def test_copy(): d = Dictionary({ '/Boolean': True, '/Integer': 42, '/Real': Decimal('42.42'), '/String': String('hi'), '/Array': Array([1, 2, 3.14]), '/Dictionary': Dictionary({'/Color': 'Red'}), }) d2 = copy(d) assert d2 == d assert d2 is not d assert d2['/Dictionary'] == d['/Dictionary']
def test_json(): d = Dictionary({ '/Boolean': True, '/Integer': 42, '/Real': Decimal('42.42'), '/String': String('hi'), '/Array': Array([1, 2, 3.14]), '/Dictionary': Dictionary({'/Color': 'Red'}), }) json_bytes = d.to_json(False) as_dict = json.loads(json_bytes) assert as_dict == { "/Array": [1, 2, 3.14], "/Boolean": True, "/Dictionary": { "/Color": "Red" }, "/Integer": 42, "/Real": 42.42, "/String": "hi", }
def test_repr_dict(self): d = Dictionary( { '/Boolean': True, '/Integer': 42, '/Real': Decimal('42.42'), '/String': String('hi'), '/Array': Array([1, 2, 3.14]), '/Operator': Operator('q'), '/Dictionary': Dictionary({'/Color': 'Red'}), '/None': None, } ) if LooseVersion(pikepdf.__libqpdf_version__) >= LooseVersion('10.2.0'): short_pi = '3.14' else: short_pi = '3.140000' expected = ( """\ pikepdf.Dictionary({ "/Array": [ 1, 2, Decimal('%s') ], "/Boolean": True, "/Dictionary": { "/Color": "Red" }, "/Integer": 42, "/None": None, "/Operator": pikepdf.Operator("q"), "/Real": Decimal('42.42'), "/String": "hi" }) """ % short_pi ) def strip_all_whitespace(s): return ''.join(s.split()) assert strip_all_whitespace(repr(d)) == strip_all_whitespace(expected) assert eval(repr(d)) == d
def test_json(): d = Dictionary({ '/Boolean': True, '/Integer': 42, '/Real': Decimal('42.42'), '/String': String('hi'), '/Array': Array([1, 2, 3.14]), '/Dictionary': Dictionary({'/Color': 'Red'}), }) json_bytes = d.to_json(False) try: as_dict = json.loads(json_bytes) except TypeError: as_dict = json.loads(json_bytes.decode('utf-8')) # Py3.5 shim assert as_dict == { "/Array": [1, 2, 3.140000], "/Boolean": True, "/Dictionary": { "/Color": "Red" }, "/Integer": 42, "/Real": 42.42, "/String": "hi", }
def test_string(self): utf16 = b'\xfe\xff' + 'hello'.encode('utf-16be') self.check(String(utf16), String('hello'))
def test_unequal_but_similar(self): assert Name('/Foo') != String('/Foo')
def test_no_len(): with pytest.raises(TypeError): len(Name.Foo) len(String('abc'))
def test_object_iteration(sandwich): expected = len(sandwich.objects) loops = 0 for obj in sandwich.objects: loops += 1 if isinstance(obj, Dictionary): assert len(obj.keys()) >= 1 assert expected == loops @pytest.mark.parametrize( 'obj', [Array([1]), Dictionary({'/A': 'b'}), Operator('q'), String('s')]) def test_object_isinstance(obj): assert isinstance(obj, (Array, Dictionary, Operator, String, Stream)) assert isinstance(obj, type(obj)) assert isinstance(obj, Object) def test_stream_isinstance(): pdf = pikepdf.new() stream = Stream(pdf, b'xyz') assert isinstance(stream, Stream) assert isinstance(stream, Object) def test_object_classes(): classes = [Array, Dictionary, Operator, String, Stream]
assert d2 is not d assert d2['/Dictionary'] == d['/Dictionary'] def test_object_iteration(sandwich): expected = len(sandwich.objects) loops = 0 for obj in sandwich.objects: loops += 1 if isinstance(obj, Dictionary): assert len(obj.keys()) >= 1 assert expected == loops @pytest.mark.parametrize( 'obj', [Array([1]), Dictionary({'/A': 'b'}), Operator('q'), String('s')] ) def test_object_isinstance(obj): assert isinstance(obj, (Array, Dictionary, Operator, String, Stream)) assert isinstance(obj, type(obj)) assert isinstance(obj, Object) def test_stream_isinstance(): pdf = pikepdf.new() stream = Stream(pdf, b'xyz') assert isinstance(stream, Stream) assert isinstance(stream, Object) def test_object_classes():