Skip to content

Commit 3504f7c

Browse files
committed
tests.py: Add exception msg check
1 parent 1a6629c commit 3504f7c

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

tests.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
118118

119119
class TestPypandoc(unittest.TestCase):
120120

121+
# Python 2 compatibility
122+
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
123+
assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
124+
121125
def setUp(self):
122126
if 'HOME' not in os.environ:
123127
# if this is used with older versions of pandoc-citeproc
@@ -152,13 +156,15 @@ def test_does_not_convert_to_invalid_format(self):
152156
def f():
153157
pypandoc.convert_text("ok", format='md', to='invalid')
154158

155-
self.assertRaises(RuntimeError, f)
159+
with self.assertRaisesRegex(RuntimeError, "Invalid output format! Got invalid but "):
160+
f()
156161

157162
def test_does_not_convert_from_invalid_format(self):
158163
def f():
159164
pypandoc.convert_text("ok", format='invalid', to='rest')
160165

161-
self.assertRaises(RuntimeError, f)
166+
with self.assertRaisesRegex(RuntimeError, 'Invalid input format! Got "invalid" but '):
167+
f()
162168

163169
def test_basic_conversion_from_file(self):
164170
with closed_tempfile('.md', text='# some title\n') as file_name:
@@ -245,7 +251,8 @@ def f():
245251
pypandoc.convert_text('# some title\n', to='odf', format='md',
246252
outputfile=None)
247253

248-
self.assertRaises(RuntimeError, f)
254+
with self.assertRaisesRegex(RuntimeError, "Invalid output format! Got odf but "):
255+
f()
249256

250257
def test_conversion_with_empty_filter(self):
251258
# we just want to get a temp file name, where we can write to
@@ -265,7 +272,8 @@ def test_conversion_error(self):
265272
def f():
266273
pypandoc.convert_text('<h1>Primary Heading</h1>', 'md', format='html', extra_args=["--blah"])
267274

268-
self.assertRaises(RuntimeError, f)
275+
with self.assertRaisesRegex(RuntimeError, 'Pandoc died with exitcode "6" during conversion: Unknown option --blah'):
276+
f()
269277

270278
def test_unicode_input(self):
271279
# make sure that pandoc always returns unicode and does not mishandle it
@@ -322,21 +330,24 @@ def f():
322330
# needs an outputfile
323331
pypandoc.convert_text('# some title\n', to='pdf', format='md')
324332

325-
self.assertRaises(RuntimeError, f)
333+
with self.assertRaisesRegex(RuntimeError, "Output to pdf only works by using a outputfile"):
334+
f()
326335

327336
# outputfile needs to end in pdf
328337
with closed_tempfile('.WRONG') as file_name:
329338
def f():
330339
pypandoc.convert_text('# some title\n', to='pdf', format='md', outputfile=file_name)
331340

332-
self.assertRaises(RuntimeError, f)
341+
with self.assertRaisesRegex(RuntimeError, 'PDF output needs an outputfile with ".pdf" as a fileending'):
342+
f()
333343

334344
# no extensions allowed
335345
with closed_tempfile('.pdf') as file_name:
336346
def f():
337347
pypandoc.convert_text('# some title\n', to='pdf+somethign', format='md', outputfile=file_name)
338348

339-
self.assertRaises(RuntimeError, f)
349+
with self.assertRaisesRegex(RuntimeError, r"PDF output can't contain any extensions: pdf\+somethign"):
350+
f()
340351

341352
def test_get_pandoc_path(self):
342353
result = pypandoc.get_pandoc_path()
@@ -354,7 +365,8 @@ def f(filepath):
354365
pypandoc.convert_file(filepath, 'rst')
355366

356367
for filepath in files:
357-
self.assertRaises(RuntimeError, f, filepath)
368+
with self.assertRaisesRegex(RuntimeError, "source_file is not a valid path"):
369+
f(filepath)
358370

359371
def test_convert_text_with_existing_file(self):
360372
with closed_tempfile('.md', text='# some title\n') as file_name:

0 commit comments

Comments
 (0)