Skip to content

Commit

Permalink
Improve error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
alugowski committed Aug 23, 2023
1 parent 4f6ce60 commit 0b250b4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion matrepr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get(self, **kwargs):
ret.float_formatter = lambda f: format(f, fmt_str)

# validate
self._assert_one_of("cell_align", ['center', 'left', 'right'])
ret._assert_one_of("cell_align", ['center', 'left', 'right'])

# Apply some default rules
if ret.title_latex is None:
Expand Down
61 changes: 57 additions & 4 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,76 @@

import unittest

import matrepr

class ImportTests(unittest.TestCase):
def test_import_not_in_jupyter(self):

class JupyterImportTests(unittest.TestCase):
def test_import_jupyter(self):
# noinspection PyUnresolvedReferences
import matrepr.jupyter
self.assertEqual(True, True) # did not raise

def test_jupyter_html(self):
def test_import_jupyter_html(self):
# noinspection PyUnresolvedReferences
import matrepr.jupyter_html
self.assertEqual(True, True) # did not raise

def test_jupyter_latex(self):
def test_import_jupyter_latex(self):
# noinspection PyUnresolvedReferences
import matrepr.jupyter_latex
self.assertEqual(True, True) # did not raise


class BasicTests(unittest.TestCase):
def test_mdisplay(self):
try:
import IPython
except ImportError:
self.skipTest("no Jupyter")
return

mat = [[1, 2], [1003, 1004]]
matrepr.mdisplay(mat, "html")
matrepr.mdisplay(mat, "latex")

with self.assertRaises(ValueError):
matrepr.mdisplay(mat, "foobar")

def test_adaptation_errors(self):
with self.assertRaises(AttributeError):
matrepr._get_adapter(set())

def test_arguments(self):
mat = [[1, 2], [1003, 1004]]
with self.assertRaises(ValueError):
matrepr.to_html(mat, cell_align="foobar")

res = matrepr.to_latex(mat, title="TITLE", title_latex=None)
self.assertIn("TITLE", res)

res = matrepr.to_latex(mat, title=None, title_latex="TITLE")
self.assertIn("TITLE", res)

def test_driver_registration_notify(self):
from unittest.mock import MagicMock

callback = MagicMock()

matrepr._driver_registration_notify.append(callback)
self.assertFalse(callback.called)

class MockDriver(matrepr.Driver):
@staticmethod
def get_supported_types():
return []

@staticmethod
def adapt(_):
return None

matrepr.register_driver(MockDriver)
self.assertTrue(callback.called)


if __name__ == '__main__':
unittest.main()

0 comments on commit 0b250b4

Please sign in to comment.