def t(self, name, sname): if sname is None: sname = name if py3k: [uname, bname] = [sname, sname.encode('UTF-8')] else: [uname, bname] = [sname.decode('UTF-8'), sname] sym = Symbol(name) x = Expression(sym) assert_is(x, Expression(x)) # __repr__(x) assert_repr(x, 'Expression({sym!r})'.format(sym=sym)) # value: v = x.value assert_equal(type(v), Symbol) assert_equal(v, sym) # lvalue: v = x.lvalue assert_equal(type(v), Symbol) assert_equal(v, sym) # __str__(): assert_equal(str(x), sname) assert_repr(x, repr(Expression.from_string(sname))) # __unicode__(): assert_equal(unicode(x), uname) assert_repr(x, repr(Expression.from_string(uname))) # __eq__(), __ne__(): assert_equal(x, Expression(sym)) assert_not_equal(x, Expression(name)) assert_not_equal(x, sym) # __hash__(): assert_equal(hash(x), hash(bname.strip(b('|')))) # pickle: assert_pickle_equal(x) return x
def test_unpickle(self): # pickle as generated by python-djvulibre 0.3.3: p = b( "cdjvu.sexpr\n_expression_from_string\np0\n(S'42'\np1\ntp2\nRp3\n." ) x = pickle.loads(p) self.t(42, x)
def test_file_io_binary(self): with tempfile.TemporaryFile(mode='w+b') as fp: fp.write(b(self.expr)) fp.flush() fp.seek(0) self.t(fp)
def test_bytesio(self): fp = io.BytesIO(b(self.expr)) self.t(fp)
def test_nonascii(self): x = self.t(b('ветчина'), '|ветчина|') y = self.t(u('ветчина'), '|ветчина|') assert_equal(x, y) assert_equal(hash(x), hash(y))
def test_nonascii(self): x = self.t(b('ветчина'), 'ветчина') y = self.t(u('ветчина'), 'ветчина') assert x is y
def test_file_io_binary_8(self): with tempfile.TemporaryFile(mode='w+b') as fp: self.expr.print_into(fp, escape_unicode=False) fp.seek(0) assert_equal(fp.read(), b(self.urepr))
def test_file_io_binary_7(self): with tempfile.TemporaryFile(mode='w+b') as fp: self.expr.print_into(fp) fp.seek(0) assert_equal(fp.read(), b(self.repr))
def test_bytesio_7(self): fp = io.BytesIO() self.expr.print_into(fp) assert_equal(fp.getvalue(), b(self.repr))
def test_save(self): skip_unless_command_exists('djvudump') context = Context() original_filename = images + 'test0.djvu' document = context.new_document(FileUri(original_filename)) message = document.get_message() assert_equal(type(message), DocInfoMessage) assert_true(document.decoding_done) assert_false(document.decoding_error) assert_equal(document.decoding_status, JobOK) assert_equal(document.type, DOCUMENT_TYPE_BUNDLED) assert_equal(len(document.pages), 2) assert_equal(len(document.files), 3) (stdout0, stderr0) = run('djvudump', original_filename, LC_ALL='C') assert_equal(stderr0, b'') stdout0 = stdout0.replace(b'\r\n', b'\n') tmpdir = tempfile.mkdtemp() try: tmp = open(os.path.join(tmpdir, 'tmp.djvu'), 'wb') job = document.save(tmp) assert_equal(type(job), SaveJob) assert_true(job.is_done) assert_false(job.is_error) tmp.close() (stdout, stderr) = run('djvudump', tmp.name, LC_ALL='C') assert_equal(stderr, b'') stdout = stdout.replace(b'\r\n', b'\n') assert_equal(stdout, stdout0) finally: shutil.rmtree(tmpdir) tmp = None tmpdir = tempfile.mkdtemp() try: tmp = open(os.path.join(tmpdir, 'tmp.djvu'), 'wb') job = document.save(tmp, pages=(0, )) assert_equal(type(job), SaveJob) assert_true(job.is_done) assert_false(job.is_error) tmp.close() stdout, stderr = run('djvudump', tmp.name, LC_ALL='C') assert_equal(stderr, b'') stdout = stdout.replace(b'\r\n', b'\n') stdout0 = stdout0.split(b'\n') stdout = stdout.split(b'\n') stdout[4] = stdout[4].replace(b' (1)', b'') assert_equal(len(stdout), 10) assert_equal(stdout[3:-1], stdout0[4:10]) assert_equal(stdout[-1], b'') finally: shutil.rmtree(tmpdir) tmp = None tmpdir = tempfile.mkdtemp() try: tmpfname = os.path.join(tmpdir, 'index.djvu') job = document.save(indirect=tmpfname) assert_equal(type(job), SaveJob) assert_true(job.is_done) assert_false(job.is_error) (stdout, stderr) = run('djvudump', tmpfname, LC_ALL='C') assert_equal(stderr, b'') stdout = stdout.replace(b'\r\n', b'\n') stdout = stdout.split(b'\n') stdout0 = ([b' shared_anno.iff -> shared_anno.iff'] + [ b(' p{n:04}.djvu -> p{n:04}.djvu'.format(n=n)) for n in range(1, 3) ]) assert_equal(len(stdout), 7) assert_equal(stdout[2:-2], stdout0) assert_equal(stdout[-1], b'') finally: shutil.rmtree(tmpdir) tmpdir = tempfile.mkdtemp() try: tmpfname = os.path.join(tmpdir, 'index.djvu') job = document.save(indirect=tmpfname, pages=(0, )) assert_equal(type(job), SaveJob) assert_true(job.is_done) assert_false(job.is_error) (stdout, stderr) = run('djvudump', tmpfname, LC_ALL='C') stdout = stdout.replace(b'\r\n', b'\n') assert_equal(stderr, b'') stdout = stdout.split(b'\n') assert_equal(len(stdout), 5) assert_equal(stdout[2], b' shared_anno.iff -> shared_anno.iff') assert_equal(stdout[3], b' p0001.djvu -> p0001.djvu') assert_equal(stdout[-1], b'') finally: shutil.rmtree(tmpdir)
def test_bytesio_8(self): fp = io.BytesIO() self.expr.print_into(fp, escape_unicode=False) assert_equal(fp.getvalue(), b(self.urepr))
def test_save(self): skip_unless_command_exists('djvudump') context = Context() original_filename = images + 'test0.djvu' document = context.new_document(FileUri(original_filename)) message = document.get_message() assert_equal(type(message), DocInfoMessage) assert_true(document.decoding_done) assert_false(document.decoding_error) assert_equal(document.decoding_status, JobOK) assert_equal(document.type, DOCUMENT_TYPE_BUNDLED) assert_equal(len(document.pages), 2) assert_equal(len(document.files), 3) (stdout0, stderr0) = run('djvudump', original_filename, LC_ALL='C') assert_equal(stderr0, b'') stdout0 = stdout0.replace(b'\r\n', b'\n') tmpdir = tempfile.mkdtemp() try: tmp = open(os.path.join(tmpdir, 'tmp.djvu'), 'wb') job = document.save(tmp) assert_equal(type(job), SaveJob) assert_true(job.is_done) assert_false(job.is_error) tmp.close() (stdout, stderr) = run('djvudump', tmp.name, LC_ALL='C') assert_equal(stderr, b'') stdout = stdout.replace(b'\r\n', b'\n') assert_equal(stdout, stdout0) finally: shutil.rmtree(tmpdir) tmp = None tmpdir = tempfile.mkdtemp() try: tmp = open(os.path.join(tmpdir, 'tmp.djvu'), 'wb') job = document.save(tmp, pages=(0,)) assert_equal(type(job), SaveJob) assert_true(job.is_done) assert_false(job.is_error) tmp.close() stdout, stderr = run('djvudump', tmp.name, LC_ALL='C') assert_equal(stderr, b'') stdout = stdout.replace(b'\r\n', b'\n') stdout0 = stdout0.split(b'\n') stdout = stdout.split(b'\n') stdout[4] = stdout[4].replace(b' (1)', b'') assert_equal(len(stdout), 10) assert_equal(stdout[3:-1], stdout0[4:10]) assert_equal(stdout[-1], b'') finally: shutil.rmtree(tmpdir) tmp = None tmpdir = tempfile.mkdtemp() try: tmpfname = os.path.join(tmpdir, 'index.djvu') job = document.save(indirect=tmpfname) assert_equal(type(job), SaveJob) assert_true(job.is_done) assert_false(job.is_error) (stdout, stderr) = run('djvudump', tmpfname, LC_ALL='C') assert_equal(stderr, b'') stdout = stdout.replace(b'\r\n', b'\n') stdout = stdout.split(b'\n') stdout0 = ( [b' shared_anno.iff -> shared_anno.iff'] + [b(' p{n:04}.djvu -> p{n:04}.djvu'.format(n=n)) for n in range(1, 3)] ) assert_equal(len(stdout), 7) assert_equal(stdout[2:-2], stdout0) assert_equal(stdout[-1], b'') finally: shutil.rmtree(tmpdir) tmpdir = tempfile.mkdtemp() try: tmpfname = os.path.join(tmpdir, 'index.djvu') job = document.save(indirect=tmpfname, pages=(0,)) assert_equal(type(job), SaveJob) assert_true(job.is_done) assert_false(job.is_error) (stdout, stderr) = run('djvudump', tmpfname, LC_ALL='C') stdout = stdout.replace(b'\r\n', b'\n') assert_equal(stderr, b'') stdout = stdout.split(b'\n') assert_equal(len(stdout), 5) assert_equal(stdout[2], b' shared_anno.iff -> shared_anno.iff') assert_equal(stdout[3], b' p0001.djvu -> p0001.djvu') assert_equal(stdout[-1], b'') finally: shutil.rmtree(tmpdir)