Skip to content

Commit

Permalink
Handle raw_bits==None correctly in repr()
Browse files Browse the repository at this point in the history
This should fix #36.

Also adds tests to verify that `BarCode.parse()` results in a working
`__repr__`, whether or not the "Raw bits:" section appears in the ZXing CLR
output.

However, I can't actually reproduce this condition using the "normal" entry
point of `BarCodeReader.decode()` → `BarCode.parse()` on the latest version.
  • Loading branch information
dlenski committed Mar 25, 2024
1 parent ea517d0 commit 88ca242
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from nose2.tools.decorators import with_setup
from nose2.tools.such import helper
from nose2.tools import params
import unittest

import zxing
Expand Down Expand Up @@ -98,30 +99,34 @@ def test_decoding_multiple():
'{}: Expected {!r} but got {!r}'.format(filename, expected_format, dec.format))


def test_parsing():
dec = zxing.BarCode.parse("""
@params(False, True)
def test_parsing(with_raw_bits):
stdout = ("""
file:///tmp/default%20file.png (format: FAKE_DATA, type: TEXT):
Raw result:
Élan|\tthe barcode is taking off
Parsed result:
Élan
\tthe barcode is taking off
\tthe barcode is taking off""") + ("""
Raw bits:
f00f00cafe
f00f00cafe""" if with_raw_bits else "") + ("""
Found 4 result points:
Point 0: (24.0,18.0)
Point 1: (21.0,196.0)
Point 2: (201.0,198.0)
Point 3: (205.23952,21.0)
""".encode())
""")
dec = zxing.BarCode.parse(stdout.encode())
assert dec.uri == 'file:///tmp/default%20file.png'
assert dec.path == '/tmp/default file.png'
assert dec.format == 'FAKE_DATA'
assert dec.type == 'TEXT'
assert dec.raw == 'Élan|\tthe barcode is taking off'
assert dec.raw_bits == bytes.fromhex('f00f00cafe')
assert dec.raw_bits == (bytes.fromhex('f00f00cafe') if with_raw_bits else b'')
assert dec.parsed == 'Élan\n\tthe barcode is taking off'
assert dec.points == [(24.0, 18.0), (21.0, 196.0), (201.0, 198.0), (205.23952, 21.0)]
r = repr(dec)
assert r.startswith('BarCode(') and r.endswith(')')


def test_wrong_formats():
Expand Down
2 changes: 1 addition & 1 deletion zxing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,6 @@ def path(self):

def __repr__(self):
return '{}(raw={!r}, parsed={!r}, raw_bits={!r}, {}={!r}, format={!r}, type={!r}, points={!r})'.format(
self.__class__.__name__, self.raw, self.parsed, self.raw_bits.hex(),
self.__class__.__name__, self.raw, self.parsed, self.raw_bits.hex() if self.raw_bits else None,
'path' if self.path else 'uri', self.path or self.uri,
self.format, self.type, self.points)

0 comments on commit 88ca242

Please sign in to comment.