pdfminer.six/tests/test_utils.py

111 lines
4.1 KiB
Python
Raw Normal View History

from nose.tools import assert_equal, assert_raises
import pathlib
from helpers import absolute_sample_path
from pdfminer.layout import LTComponent
from pdfminer.utils import (format_int_alpha, format_int_roman, open_filename,
Plane, shorten_str)
class TestOpenFilename:
def test_string_input(self):
filename = absolute_sample_path("simple1.pdf")
opened = open_filename(filename)
assert_equal(opened.closing, True)
def test_pathlib_input(self):
filename = pathlib.Path(absolute_sample_path("simple1.pdf"))
opened = open_filename(filename)
assert_equal(opened.closing, True)
def test_file_input(self):
filename = absolute_sample_path("simple1.pdf")
with open(filename, "rb") as in_file:
opened = open_filename(in_file)
assert_equal(opened.file_handler, in_file)
def test_unsupported_input(self):
assert_raises(TypeError, open_filename, 0)
class TestPlane:
def test_find_nothing_in_empty_bbox(self):
plane, _ = self.given_plane_with_one_object()
result = list(plane.find((50, 50, 100, 100)))
assert_equal(result, [])
def test_find_nothing_after_removing(self):
plane, obj = self.given_plane_with_one_object()
plane.remove(obj)
result = list(plane.find((0, 0, 100, 100)))
assert_equal(result, [])
def test_find_object_in_whole_plane(self):
plane, obj = self.given_plane_with_one_object()
result = list(plane.find((0, 0, 100, 100)))
assert_equal(result, [obj])
def test_find_if_object_is_smaller_than_gridsize(self):
Enforce pep8 coding-style (#345) * Code Refractor: Use code-style enforcement #312 * Add flake8 to travis-ci * Remove python 2 3 comment on six library. 891 errors > 870 errors. * Remove class and functions comments that consist of just the name. 870 errors > 855 errors. * Fix flake8 errors in pdftypes.py. 855 errors > 833 errors. * Moving flake8 testing from .travis.yml to tox.ini to ensure local testing before commiting * Cleanup pdfinterp.py and add documentation from PDF Reference * Cleanup pdfpage.py * Cleanup pdffont.py * Clean psparser.py * Cleanup high_level.py * Cleanup layout.py * Cleanup pdfparser.py * Cleanup pdfcolor.py * Cleanup rijndael.py * Cleanup converter.py * Rename klass to cls if it is the class variable, to be more consistent with standard practice * Cleanup cmap.py * Cleanup pdfdevice.py * flake8 ignore fontmetrics.py * Cleanup test_pdfminer_psparser.py * Fix flake8 in pdfdocument.py; 339 errors to go * Fix flake8 utils.py; 326 errors togo * pep8 correction for few files in /tools/ 328 > 160 to go (#342) * pep8 correction for few files in /tools/ 328 > 160 to go * pep8 correction: 160 > 5 to go * Fix ascii85.py errors * Fix error in getting index from target that does not exists * Remove commented print lines * Fix flake8 error in pdfinterp.py * Fix python2 specific error by removing argument from print statement * Ignore invalid python2 syntax * Update contributing.md * Added changelog * Remove unused import Co-authored-by: Fakabbir Amin <f4amin@gmail.com>
2019-12-29 20:20:20 +00:00
plane, obj = self.given_plane_with_one_object(object_size=1,
gridsize=100)
result = list(plane.find((0, 0, 100, 100)))
assert_equal(result, [obj])
def test_find_object_if_much_larger_than_gridsize(self):
Enforce pep8 coding-style (#345) * Code Refractor: Use code-style enforcement #312 * Add flake8 to travis-ci * Remove python 2 3 comment on six library. 891 errors > 870 errors. * Remove class and functions comments that consist of just the name. 870 errors > 855 errors. * Fix flake8 errors in pdftypes.py. 855 errors > 833 errors. * Moving flake8 testing from .travis.yml to tox.ini to ensure local testing before commiting * Cleanup pdfinterp.py and add documentation from PDF Reference * Cleanup pdfpage.py * Cleanup pdffont.py * Clean psparser.py * Cleanup high_level.py * Cleanup layout.py * Cleanup pdfparser.py * Cleanup pdfcolor.py * Cleanup rijndael.py * Cleanup converter.py * Rename klass to cls if it is the class variable, to be more consistent with standard practice * Cleanup cmap.py * Cleanup pdfdevice.py * flake8 ignore fontmetrics.py * Cleanup test_pdfminer_psparser.py * Fix flake8 in pdfdocument.py; 339 errors to go * Fix flake8 utils.py; 326 errors togo * pep8 correction for few files in /tools/ 328 > 160 to go (#342) * pep8 correction for few files in /tools/ 328 > 160 to go * pep8 correction: 160 > 5 to go * Fix ascii85.py errors * Fix error in getting index from target that does not exists * Remove commented print lines * Fix flake8 error in pdfinterp.py * Fix python2 specific error by removing argument from print statement * Ignore invalid python2 syntax * Update contributing.md * Added changelog * Remove unused import Co-authored-by: Fakabbir Amin <f4amin@gmail.com>
2019-12-29 20:20:20 +00:00
plane, obj = self.given_plane_with_one_object(object_size=100,
gridsize=10)
result = list(plane.find((0, 0, 100, 100)))
assert_equal(result, [obj])
@staticmethod
def given_plane_with_one_object(object_size=50, gridsize=50):
bounding_box = (0, 0, 100, 100)
plane = Plane(bounding_box, gridsize)
obj = LTComponent((0, 0, object_size, object_size))
plane.add(obj)
return plane, obj
class TestFunctions(object):
def test_shorten_str(self):
s = shorten_str('Hello there World', 15)
assert_equal(s, 'Hello ... World')
def test_shorten_short_str_is_same(self):
s = 'Hello World'
assert_equal(s, shorten_str(s, 50))
def test_shorten_to_really_short(self):
Enforce pep8 coding-style (#345) * Code Refractor: Use code-style enforcement #312 * Add flake8 to travis-ci * Remove python 2 3 comment on six library. 891 errors > 870 errors. * Remove class and functions comments that consist of just the name. 870 errors > 855 errors. * Fix flake8 errors in pdftypes.py. 855 errors > 833 errors. * Moving flake8 testing from .travis.yml to tox.ini to ensure local testing before commiting * Cleanup pdfinterp.py and add documentation from PDF Reference * Cleanup pdfpage.py * Cleanup pdffont.py * Clean psparser.py * Cleanup high_level.py * Cleanup layout.py * Cleanup pdfparser.py * Cleanup pdfcolor.py * Cleanup rijndael.py * Cleanup converter.py * Rename klass to cls if it is the class variable, to be more consistent with standard practice * Cleanup cmap.py * Cleanup pdfdevice.py * flake8 ignore fontmetrics.py * Cleanup test_pdfminer_psparser.py * Fix flake8 in pdfdocument.py; 339 errors to go * Fix flake8 utils.py; 326 errors togo * pep8 correction for few files in /tools/ 328 > 160 to go (#342) * pep8 correction for few files in /tools/ 328 > 160 to go * pep8 correction: 160 > 5 to go * Fix ascii85.py errors * Fix error in getting index from target that does not exists * Remove commented print lines * Fix flake8 error in pdfinterp.py * Fix python2 specific error by removing argument from print statement * Ignore invalid python2 syntax * Update contributing.md * Added changelog * Remove unused import Co-authored-by: Fakabbir Amin <f4amin@gmail.com>
2019-12-29 20:20:20 +00:00
assert_equal('Hello', shorten_str('Hello World', 5))
def test_format_int_alpha(self):
assert_equal('a', format_int_alpha(1))
assert_equal('b', format_int_alpha(2))
assert_equal('z', format_int_alpha(26))
assert_equal('aa', format_int_alpha(27))
assert_equal('ab', format_int_alpha(28))
assert_equal('az', format_int_alpha(26*2))
assert_equal('ba', format_int_alpha(26*2 + 1))
assert_equal('zz', format_int_alpha(26*27))
assert_equal('aaa', format_int_alpha(26*27 + 1))
def test_format_int_roman(self):
assert_equal('i', format_int_roman(1))
assert_equal('ii', format_int_roman(2))
assert_equal('iii', format_int_roman(3))
assert_equal('iv', format_int_roman(4))
assert_equal('v', format_int_roman(5))
assert_equal('vi', format_int_roman(6))
assert_equal('vii', format_int_roman(7))
assert_equal('viii', format_int_roman(8))
assert_equal('ix', format_int_roman(9))
assert_equal('x', format_int_roman(10))
assert_equal('xi', format_int_roman(11))
assert_equal('xx', format_int_roman(20))
assert_equal('xl', format_int_roman(40))
assert_equal('xlv', format_int_roman(45))
assert_equal('l', format_int_roman(50))
assert_equal('xc', format_int_roman(90))
assert_equal('xci', format_int_roman(91))
assert_equal('c', format_int_roman(100))