diff options
author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2013-01-21 17:33:56 +0000 |
---|---|---|
committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2013-01-21 17:33:56 +0000 |
commit | d1fc1cefa9918397d780f8a2afe8b28b691a83ed (patch) | |
tree | 7fb55d671e7cbd0bfd6a66127b3ddf82abb3021a /test | |
parent | 4b9fe2e89c8b1e121b8f260beafde95a73077f16 (diff) | |
download | docutils-d1fc1cefa9918397d780f8a2afe8b28b691a83ed.tar.gz |
Apply [ 2714873 ] Fix for the overwritting of document attributes.
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7595 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'test')
-rwxr-xr-x | test/test_nodes.py | 111 | ||||
-rwxr-xr-x | test/test_transforms/test_doctitle.py | 36 |
2 files changed, 146 insertions, 1 deletions
diff --git a/test/test_nodes.py b/test/test_nodes.py index 6a785b516..ecfa98108 100755 --- a/test/test_nodes.py +++ b/test/test_nodes.py @@ -166,6 +166,117 @@ class ElementTests(unittest.TestCase): # 'test' is not overwritten because it is not a basic attribute. self.assertEqual(element1['test'], ['test1']) + def test_update_all_atts(self): + # Note: Also tests is_not_list_attribute and is_not_known_attribute + # and various helpers + ## Test for full attribute replacement + element1 = nodes.Element(ids=['foo', 'bar'], parent_only='parent', + all_nodes='mom') + element2 = nodes.Element(ids=['baz', 'qux'], child_only='child', + all_nodes='dad', source='source') + + # Test for when same fields are replaced as well as source... + element1.update_all_atts_consistantly(element2, True, True) + # 'ids' are appended because 'ids' is a basic attribute. + self.assertEquals(element1['ids'], ['foo', 'bar', 'baz', 'qux']) + # 'parent_only' should remain unaffected. + self.assertEquals(element1['parent_only'], 'parent') + # 'all_nodes' is overwritten due to the second parameter == True. + self.assertEquals(element1['all_nodes'], 'dad') + # 'child_only' should have been added. + self.assertEquals(element1['child_only'], 'child') + # 'source' is also overwritten due to the third parameter == True. + self.assertEquals(element1['source'], 'source') + + # Test for when same fields are replaced but not source... + element1 = nodes.Element(ids=['foo', 'bar'], parent_only='parent', + all_nodes='mom') + element1.update_all_atts_consistantly(element2) + # 'ids' are appended because 'ids' is a basic attribute. + self.assertEquals(element1['ids'], ['foo', 'bar', 'baz', 'qux']) + # 'parent_only' should remain unaffected. + self.assertEquals(element1['parent_only'], 'parent') + # 'all_nodes' is overwritten due to the second parameter default of True. + self.assertEquals(element1['all_nodes'], 'dad') + # 'child_only' should have been added. + self.assertEquals(element1['child_only'], 'child') + # 'source' remains unset due to the third parameter default of False. + self.assertEquals(element1.get('source'), None) + + # Test for when fields are NOT replaced but source is... + element1 = nodes.Element(ids=['foo', 'bar'], parent_only='parent', + all_nodes='mom') + element1.update_all_atts_consistantly(element2, False, True) + # 'ids' are appended because 'ids' is a basic attribute. + self.assertEquals(element1['ids'], ['foo', 'bar', 'baz', 'qux']) + # 'parent_only' should remain unaffected. + self.assertEquals(element1['parent_only'], 'parent') + # 'all_nodes' is preserved due to the second parameter == False. + self.assertEquals(element1['all_nodes'], 'mom') + # 'child_only' should have been added. + self.assertEquals(element1['child_only'], 'child') + # 'source' is added due to the third parameter == True. + self.assertEquals(element1['source'], 'source') + element1 = nodes.Element(source='destination') + element1.update_all_atts_consistantly(element2, False, True) + # 'source' remains unchanged due to the second parameter == False. + self.assertEquals(element1['source'], 'destination') + + # Test for when same fields are replaced but not source... + element1 = nodes.Element(ids=['foo', 'bar'], parent_only='parent', + all_nodes='mom') + element1.update_all_atts_consistantly(element2, False) + # 'ids' are appended because 'ids' is a basic attribute. + self.assertEquals(element1['ids'], ['foo', 'bar', 'baz', 'qux']) + # 'parent_only' should remain unaffected. + self.assertEquals(element1['parent_only'], 'parent') + # 'all_nodes' is preserved due to the second parameter == False. + self.assertEquals(element1['all_nodes'], 'mom') + # 'child_only' should have been added. + self.assertEquals(element1['child_only'], 'child') + # 'source' remains unset due to the third parameter default of False. + self.assertEquals(element1.get('source'), None) + + ## Test for List attribute merging + # Attribute Concatination + element1 = nodes.Element(ss='a', sl='1', ls=['I'], ll=['A']) + element2 = nodes.Element(ss='b', sl=['2'], ls='II', ll=['B']) + element1.update_all_atts_concatenating(element2) + # 'ss' is replaced because non-list + self.assertEquals(element1['ss'], 'b') + # 'sl' is replaced because they are both not lists + self.assertEquals(element1['sl'], ['2']) + # 'ls' is replaced because they are both not lists + self.assertEquals(element1['ls'], 'II') + # 'll' is extended because they are both lists + self.assertEquals(element1['ll'], ['A', 'B']) + + # Attribute Coercion + element1 = nodes.Element(ss='a', sl='1', ls=['I'], ll=['A']) + element2 = nodes.Element(ss='b', sl=['2'], ls='II', ll=['B']) + element1.update_all_atts_coercion(element2) + # 'ss' is replaced because non-list + self.assertEquals(element1['ss'], 'b') + # 'sl' is converted to a list and appended because element2 has a list + self.assertEquals(element1['sl'], ['1', '2']) + # 'ls' has element2's value appended to the list + self.assertEquals(element1['ls'], ['I', 'II']) + # 'll' is extended because they are both lists + self.assertEquals(element1['ll'], ['A', 'B']) + + # Attribute Conversion + element1 = nodes.Element(ss='a', sl='1', ls=['I'], ll=['A']) + element2 = nodes.Element(ss='b', sl=['2'], ls='II', ll=['B']) + element1.update_all_atts_convert(element2) + # 'ss' is converted to a list with the values from each element + self.assertEquals(element1['ss'], ['a', 'b']) + # 'sl' is converted to a list and appended + self.assertEquals(element1['sl'], ['1', '2']) + # 'ls' has element2's value appended to the list + self.assertEquals(element1['ls'], ['I', 'II']) + # 'll' is extended + self.assertEquals(element1['ll'], ['A', 'B']) + def test_replace_self(self): parent = nodes.Element(ids=['parent']) child1 = nodes.Element(ids=['child1']) diff --git a/test/test_transforms/test_doctitle.py b/test/test_transforms/test_doctitle.py index 0807bce70..c00113e63 100755 --- a/test/test_transforms/test_doctitle.py +++ b/test/test_transforms/test_doctitle.py @@ -10,8 +10,23 @@ Tests for docutils.transforms.frontmatter.DocTitle. from __init__ import DocutilsTestSupport from docutils.transforms.frontmatter import DocTitle, SectionSubTitle -from docutils.parsers.rst import Parser +from docutils.parsers.rst import Parser, Directive +from docutils.parsers.rst.directives import register_directive +# dummy directive to test attribute merging: +class AddNameToDocumentTitle(Directive): + required_arguments = 0 + optional_arguments = 0 + final_argument_whitespace = True + option_spec = { } + has_content = False + + def run(self): + document = self.state_machine.document + document['names'].append('Name') + return [] + +register_directive('add-name-to-title', AddNameToDocumentTitle) def suite(): parser = Parser() @@ -221,6 +236,25 @@ Another Subtitle <subtitle ids="another-subtitle" names="another\ subtitle"> Another Subtitle """], +["""\ +----- +Title +----- + +This is a document, it flows nicely, so the attributes of it are at the +bottom. + +.. add-name-to-title:: + +""", +"""\ +<document ids="title" names="Name title" source="test data" title="Title"> + <title> + Title + <paragraph> + This is a document, it flows nicely, so the attributes of it are at the + bottom. +"""] ]) |