From 6ee07d2735d86d7a3d0b31c3409d42d31997a96c Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Fri, 27 Jul 2018 10:23:55 -0400 Subject: Replace homegrown OrderedDict with purpose-built Registry. (#688) All processors and patterns now get "registered" to a Registry. Each item is given a name (string) and a priority. The name is for later reference and the priority can be either an integer or float and is used to sort. Priority is sorted from highest to lowest. A Registry instance is a list-like iterable with the items auto-sorted by priority. If two items have the same priority, then they are listed in the order there were "registered". Registering a new item with the same name as an already registered item replaces the old item with the new item (however, the new item is sorted by its newly assigned priority). To remove an item, "deregister" it by name or index. A backwards compatible shim is included so that existing simple extensions should continue to work. DeprecationWarnings will be raised for any code which calls the old API. Fixes #418. --- tests/test_apis.py | 359 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 212 insertions(+), 147 deletions(-) (limited to 'tests') diff --git a/tests/test_apis.py b/tests/test_apis.py index 2875c85..3941bd4 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -202,152 +202,217 @@ class TestHtmlStash(unittest.TestCase): self.assertEqual(self.stash.rawHtmlBlocks, []) -class TestOrderedDict(unittest.TestCase): - """ Test OrderedDict storage class. """ - - def setUp(self): - self.odict = markdown.odict.OrderedDict() - self.odict['first'] = 'This' - self.odict['third'] = 'a' - self.odict['fourth'] = 'self' - self.odict['fifth'] = 'test' - - def testValues(self): - """ Test output of OrderedDict.values(). """ - self.assertEqual(list(self.odict.values()), ['This', 'a', 'self', 'test']) - - def testKeys(self): - """ Test output of OrderedDict.keys(). """ - self.assertEqual( - list(self.odict.keys()), - ['first', 'third', 'fourth', 'fifth'] - ) - - def testItems(self): - """ Test output of OrderedDict.items(). """ - self.assertEqual( - list(self.odict.items()), [ - ('first', 'This'), - ('third', 'a'), - ('fourth', 'self'), - ('fifth', 'test') - ] - ) - - def testAddBefore(self): - """ Test adding an OrderedDict item before a given key. """ - self.odict.add('second', 'is', 'first') - self.assertEqual( - list(self.odict.items()), [ - ('first', 'This'), - ('second', 'is'), - ('third', 'a'), - ('fourth', 'self'), - ('fifth', 'test') - ] - ) - - def testAddAfterEnd(self): - """ Test adding an OrderedDict item after the last key. """ - self.odict.add('sixth', '.', '>fifth') - self.assertEqual( - list(self.odict.items()), [ - ('first', 'This'), - ('third', 'a'), - ('fourth', 'self'), - ('fifth', 'test'), - ('sixth', '.') - ] - ) - - def testAdd_begin(self): - """ Test adding an OrderedDict item using "_begin". """ - self.odict.add('zero', 'CRAZY', '_begin') - self.assertEqual( - list(self.odict.items()), [ - ('zero', 'CRAZY'), - ('first', 'This'), - ('third', 'a'), - ('fourth', 'self'), - ('fifth', 'test') - ] - ) - - def testAdd_end(self): - """ Test adding an OrderedDict item using "_end". """ - self.odict.add('sixth', '.', '_end') - self.assertEqual( - list(self.odict.items()), [ - ('first', 'This'), - ('third', 'a'), - ('fourth', 'self'), - ('fifth', 'test'), - ('sixth', '.') - ] - ) - - def testAddBadLocation(self): - """ Test Error on bad location in OrderedDict.add(). """ - self.assertRaises(ValueError, self.odict.add, 'sixth', '.', 'b') + self.assertEqual(list(r), ['a', 'a1', 'b', 'b1', 'c']) + # Add after last item + r.add('d', Item('d'), '>c') + self.assertEqual(list(r), ['a', 'a1', 'b', 'b1', 'c', 'd']) + # Add to end + r.add('e', Item('e'), '_end') + self.assertEqual(list(r), ['a', 'a1', 'b', 'b1', 'c', 'd', 'e']) + with self.assertRaises(ValueError): + r.add('f', Item('f'), 'badlocation') + + # Check the warnings + self.assertEqual(len(w), 7) + self.assertTrue(all(issubclass(x.category, DeprecationWarning) for x in w)) class TestErrors(unittest.TestCase): @@ -862,7 +927,7 @@ class TestAncestorExclusion(unittest.TestCase): """Modify inline patterns.""" pattern = r'(\+)([^\+]+)\1' - md.inlinePatterns["ancestor-test"] = TestAncestorExclusion.AncestorExample(pattern, 'strong') + md.inlinePatterns.register(TestAncestorExclusion.AncestorExample(pattern, 'strong'), 'ancestor-test', 0) def setUp(self): """Setup markdown object.""" -- cgit v1.2.3