Commit e37af8fa authored by Thomas Perret's avatar Thomas Perret

Add python2 support for tests

parent 303e4a85
......@@ -29,7 +29,10 @@ from . import builders
from . import util
from .builders import DigitBuilder # backward compatibility
from .error import TesseractError # backward compatibility
from .util import digits_only
from .util import (
digits_only,
to_unicode,
)
# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
TESSERACT_CMD = 'tesseract.exe' if os.name == 'nt' else 'tesseract'
......@@ -103,7 +106,7 @@ class CharBoxBuilder(builders.BaseBuilder):
The file_descriptor must support UTF-8 ! (see module 'codecs')
"""
for box in boxes:
file_descriptor.write(str(box) + " 0\n")
file_descriptor.write(to_unicode(str(box)) + " 0\n")
def __str__(self):
return "Character boxes"
......
import os
import unittest
from codecs import open
class BaseTest(unittest.TestCase):
tool = None
......
......@@ -3,7 +3,10 @@ import unittest
from io import StringIO
from itertools import product
from random import randint
from unittest.mock import patch
try:
from unittest.mock import patch
except ImportError:
from mock import patch
from pyocr import builders
......@@ -47,14 +50,14 @@ class TestTextBuilder(unittest.TestCase):
self.assertNotIn("--singlecolumn", builder.cuneiform_args)
def test_read_file(self):
txt = "first line\nsecond line\n0123456789\n🖨 "
txt = u"first line\nsecond line\n0123456789\n\U0001f5a8 "
input_fh = StringIO(txt)
output = self.builder.read_file(input_fh)
self.assertEqual(output, txt.strip())
def test_write_file(self):
output = StringIO()
txt = "first line\nsecond line\n0123456789\n🖨 "
txt = u"first line\nsecond line\n0123456789\n\U0001f5a8 "
self.builder.write_file(output, txt)
output.seek(0)
self.assertEqual(output.read(), txt)
......
import subprocess
from io import StringIO
from unittest.mock import patch, MagicMock
try:
from unittest.mock import patch, MagicMock
except ImportError:
from mock import patch, MagicMock
from PIL import Image
......
......@@ -3,7 +3,10 @@ import os
from ctypes import POINTER, cast, c_char_p, c_int
from random import randint
from unittest.mock import patch, call
try:
from unittest.mock import patch, call
except ImportError:
from mock import patch, call
from PIL import Image
......@@ -296,7 +299,7 @@ class TestLibTesseractRaw(BaseTest):
@patch("pyocr.libtesseract.tesseract_raw.g_libtesseract")
def test_set_debug_file(self, libtess):
for filename in ("file", b"file"):
for filename in (u"file", b"file"):
tesseract_raw.set_debug_file(self.handle, filename)
self.assertEqual(
libtess.TessBaseAPISetVariable.call_count,
......
import os
import subprocess
import tempfile
from io import StringIO
from unittest.mock import patch, MagicMock
try:
from unittest.mock import patch, MagicMock
except ImportError:
from mock import patch, MagicMock
try:
from tempfile import TemporaryDirectory
except ImportError:
from backports.tempfile import TemporaryDirectory
from PIL import Image
......@@ -283,7 +289,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -319,7 +325,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -355,7 +361,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -385,7 +391,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -421,7 +427,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -456,7 +462,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -491,7 +497,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -521,7 +527,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -557,7 +563,7 @@ class TestTesseract(BaseTest):
)
self.stdout.stdout.read.return_value = message.encode()
Popen.return_value = self.stdout
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -598,7 +604,7 @@ class TestTesseractTxt(BaseTest):
get_version.return_value = (4, 0, 0)
run_tesseract.return_value = (0, "")
copen.return_value = StringIO(self._get_file_content("text"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -619,7 +625,7 @@ class TestTesseractTxt(BaseTest):
def test_lang(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "")
copen.return_value = StringIO(self._get_file_content("text"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -641,7 +647,7 @@ class TestTesseractTxt(BaseTest):
def test_text(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "")
copen.return_value = StringIO(self._get_file_content("text"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -663,7 +669,7 @@ class TestTesseractTxt(BaseTest):
def test_text_error(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (1, "Error")
copen.return_value = StringIO(self._get_file_content("text"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -683,7 +689,7 @@ class TestTesseractTxt(BaseTest):
def test_text_cannot_open_file(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "")
copen.side_effect = OSError("Error opening file")
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -706,7 +712,7 @@ class TestTesseractTxt(BaseTest):
def test_text_no_output(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "No file output")
copen.return_value = StringIO(self._get_file_content("text"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -738,7 +744,7 @@ class TestTesseractCharBox(BaseTest):
def test_char(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "")
copen.return_value = StringIO(self._get_file_content("boxes"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -761,7 +767,7 @@ class TestTesseractCharBox(BaseTest):
def test_char_error(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (1, "Error")
copen.return_value = StringIO(self._get_file_content("boxes"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -781,7 +787,7 @@ class TestTesseractCharBox(BaseTest):
def test_char_no_output(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "No file output")
copen.return_value = StringIO(self._get_file_content("boxes"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -871,7 +877,7 @@ class TestTesseractDigits(BaseTest):
def test_digits(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "")
copen.return_value = StringIO(self._get_file_content("digits"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -903,7 +909,7 @@ class TestTesseractWordBox(BaseTest):
def test_word(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "")
copen.return_value = StringIO(self._get_file_content("words"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -926,7 +932,7 @@ class TestTesseractWordBox(BaseTest):
def test_word_error(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (1, "Error")
copen.return_value = StringIO(self._get_file_content("words"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -947,7 +953,7 @@ class TestTesseractWordBox(BaseTest):
def test_word_no_output(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "No file output")
copen.return_value = StringIO(self._get_file_content("words"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -978,7 +984,7 @@ class TestTesseractLineBox(BaseTest):
def test_line(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "")
copen.return_value = StringIO(self._get_file_content("tesseract.lines"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -1001,7 +1007,7 @@ class TestTesseractLineBox(BaseTest):
def test_line_error(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (1, "Error")
copen.return_value = StringIO(self._get_file_content("tesseract.lines"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -1022,7 +1028,7 @@ class TestTesseractLineBox(BaseTest):
def test_line_no_output(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "No file output")
copen.return_value = StringIO(self._get_file_content("tesseract.lines"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -1053,7 +1059,7 @@ class TestTesseractDigitsLineBox(BaseTest):
def test_line(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "")
copen.return_value = StringIO(self._get_file_content("digits.lines"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -1076,7 +1082,7 @@ class TestTesseractDigitsLineBox(BaseTest):
def test_line_error(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (1, "Error")
copen.return_value = StringIO(self._get_file_content("digits.lines"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......@@ -1097,7 +1103,7 @@ class TestTesseractDigitsLineBox(BaseTest):
def test_line_no_output(self, run_tesseract, copen, temp_dir):
run_tesseract.return_value = (0, "No file output")
copen.return_value = StringIO(self._get_file_content("digits.lines"))
with tempfile.TemporaryDirectory(prefix="tess_") as tmpdir:
with TemporaryDirectory(prefix="tess_") as tmpdir:
enter = MagicMock()
enter.__enter__.return_value = tmpdir
temp_dir.return_value = enter
......
import unittest
import sys
from unittest.mock import patch
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import pyocr
......@@ -76,6 +80,17 @@ class TestPyOCR(unittest.TestCase):
self.assertFalse(is_on_path("windows95"))
def test_to_unicode(self):
self.assertEqual(to_unicode("salut, ça va ?"), "salut, ça va ?")
self.assertEqual(to_unicode("salut, ça va ?".encode()),
"salut, ça va ?".encode())
self.assertEqual(to_unicode("salut, \u00e7a va ?"),
u"salut, \u00e7a va ?")
@unittest.skipUnless(sys.version_info >= (3, 0),
"Test for python3 to_unicode")
def test_to_unicode_python3(self):
self.assertEqual(to_unicode("salut, \u00e7a va ?".encode("utf-8")),
u"salut, \u00e7a va ?".encode("utf-8"))
@unittest.skipIf(sys.version_info >= (3, 0),
"Test for python2 unicode support")
def test_to_unicode_python2(self):
self.assertEqual(to_unicode("salut, \u00e7a va ?".encode("utf-8")),
u"salut, \u00e7a va ?")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment