Skip to content

Commit b2218c9

Browse files
author
Nicklas Tegner
authored
Merge pull request #238 from jayvdb/raises-regex
tests.py: Add exception msg check
2 parents b52a847 + 3504f7c commit b2218c9

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
@@ -114,6 +114,10 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
114114

115115
class TestPypandoc(unittest.TestCase):
116116

117+
# Python 2 compatibility
118+
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
119+
assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
120+
117121
def setUp(self):
118122
if 'HOME' not in os.environ:
119123
# if this is used with older versions of pandoc-citeproc
@@ -148,13 +152,15 @@ def test_does_not_convert_to_invalid_format(self):
148152
def f():
149153
pypandoc.convert_text("ok", format='md', to='invalid')
150154

151-
self.assertRaises(RuntimeError, f)
155+
with self.assertRaisesRegex(RuntimeError, "Invalid output format! Got invalid but "):
156+
f()
152157

153158
def test_does_not_convert_from_invalid_format(self):
154159
def f():
155160
pypandoc.convert_text("ok", format='invalid', to='rest')
156161

157-
self.assertRaises(RuntimeError, f)
162+
with self.assertRaisesRegex(RuntimeError, 'Invalid input format! Got "invalid" but '):
163+
f()
158164

159165
def test_basic_conversion_from_file(self):
160166
with closed_tempfile('.md', text='# some title\n') as file_name:
@@ -240,7 +246,8 @@ def f():
240246
pypandoc.convert_text('# some title\n', to='odf', format='md',
241247
outputfile=None)
242248

243-
self.assertRaises(RuntimeError, f)
249+
with self.assertRaisesRegex(RuntimeError, "Invalid output format! Got odf but "):
250+
f()
244251

245252
def test_conversion_with_empty_filter(self):
246253
# we just want to get a temp file name, where we can write to
@@ -260,7 +267,8 @@ def test_conversion_error(self):
260267
def f():
261268
pypandoc.convert_text('<h1>Primary Heading</h1>', 'md', format='html', extra_args=["--blah"])
262269

263-
self.assertRaises(RuntimeError, f)
270+
with self.assertRaisesRegex(RuntimeError, 'Pandoc died with exitcode "6" during conversion: Unknown option --blah'):
271+
f()
264272

265273
def test_unicode_input(self):
266274
# make sure that pandoc always returns unicode and does not mishandle it
@@ -317,21 +325,24 @@ def f():
317325
# needs an outputfile
318326
pypandoc.convert_text('# some title\n', to='pdf', format='md')
319327

320-
self.assertRaises(RuntimeError, f)
328+
with self.assertRaisesRegex(RuntimeError, "Output to pdf only works by using a outputfile"):
329+
f()
321330

322331
# outputfile needs to end in pdf
323332
with closed_tempfile('.WRONG') as file_name:
324333
def f():
325334
pypandoc.convert_text('# some title\n', to='pdf', format='md', outputfile=file_name)
326335

327-
self.assertRaises(RuntimeError, f)
336+
with self.assertRaisesRegex(RuntimeError, 'PDF output needs an outputfile with ".pdf" as a fileending'):
337+
f()
328338

329339
# no extensions allowed
330340
with closed_tempfile('.pdf') as file_name:
331341
def f():
332342
pypandoc.convert_text('# some title\n', to='pdf+somethign', format='md', outputfile=file_name)
333343

334-
self.assertRaises(RuntimeError, f)
344+
with self.assertRaisesRegex(RuntimeError, r"PDF output can't contain any extensions: pdf\+somethign"):
345+
f()
335346

336347
def test_get_pandoc_path(self):
337348
result = pypandoc.get_pandoc_path()
@@ -349,7 +360,8 @@ def f(filepath):
349360
pypandoc.convert_file(filepath, 'rst')
350361

351362
for filepath in files:
352-
self.assertRaises(RuntimeError, f, filepath)
363+
with self.assertRaisesRegex(RuntimeError, "source_file is not a valid path"):
364+
f(filepath)
353365

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

0 commit comments

Comments
 (0)