|
8 | 8 |
|
9 | 9 | from codemodder.codemods.xml_transformer import ( |
10 | 10 | ElementAttributeXMLTransformer, |
| 11 | + NewElement, |
| 12 | + NewElementXMLTransformer, |
11 | 13 | XMLTransformer, |
12 | 14 | ) |
13 | 15 |
|
@@ -55,7 +57,6 @@ class TestElementAttributeXMLTransformer: |
55 | 57 |
|
56 | 58 | def run_and_assert(self, name_attr_map, input_code, expected_output): |
57 | 59 | with StringIO() as result, StringIO(dedent(input_code)) as input_stream: |
58 | | - result = StringIO() |
59 | 60 | transformer = ElementAttributeXMLTransformer( |
60 | 61 | result, name_attributes_map=name_attr_map |
61 | 62 | ) |
@@ -94,3 +95,91 @@ def test_change_multiple_attr_and_preserve_existing(self): |
94 | 95 | <element first="one" second="two" three="three"></element>""" |
95 | 96 | name_attr_map = {"element": {"first": "one", "second": "two"}} |
96 | 97 | self.run_and_assert(name_attr_map, input_code, expected_output) |
| 98 | + |
| 99 | + |
| 100 | +class TestNewElementXMLTransformer: |
| 101 | + |
| 102 | + def run_and_assert(self, new_elements, input_code, expected_output): |
| 103 | + with StringIO() as result, StringIO(dedent(input_code)) as input_stream: |
| 104 | + transformer = NewElementXMLTransformer(result, new_elements=new_elements) |
| 105 | + parser = make_parser() |
| 106 | + parser.setContentHandler(transformer) |
| 107 | + parser.setProperty(handler.property_lexical_handler, transformer) |
| 108 | + parser.parse(input_stream) |
| 109 | + assert result.getvalue() == dedent(expected_output) |
| 110 | + |
| 111 | + def test_add_new_element(self): |
| 112 | + input_code = """\ |
| 113 | + <root></root> |
| 114 | + """ |
| 115 | + expected_output = """\ |
| 116 | + <?xml version="1.0" encoding="utf-8"?> |
| 117 | + <root><child1></child1><child2 one="1">2</child2></root>""" |
| 118 | + new_elements = [ |
| 119 | + NewElement(name="child1", parent_name="root"), |
| 120 | + NewElement( |
| 121 | + name="child2", parent_name="root", content="2", attributes={"one": "1"} |
| 122 | + ), |
| 123 | + ] |
| 124 | + self.run_and_assert(new_elements, input_code, expected_output) |
| 125 | + |
| 126 | + def test_add_new_sibling_same_name(self): |
| 127 | + input_code = """\ |
| 128 | + <root> |
| 129 | + <child>child 1</child> |
| 130 | + </root> |
| 131 | + """ |
| 132 | + expected_output = """\ |
| 133 | + <?xml version="1.0" encoding="utf-8"?> |
| 134 | + <root> |
| 135 | + <child>child 1</child> |
| 136 | + <child>child 2</child></root>""" |
| 137 | + new_elements = [ |
| 138 | + NewElement(name="child", parent_name="root", content="child 2"), |
| 139 | + ] |
| 140 | + self.run_and_assert(new_elements, input_code, expected_output) |
| 141 | + |
| 142 | + def test_add_nested_elementsl(self): |
| 143 | + input_code = """\ |
| 144 | + <?xml version="1.0" encoding="utf-8" ?> |
| 145 | + <configuration> |
| 146 | + <system.web> |
| 147 | + </system.web> |
| 148 | + <system.webServer> |
| 149 | + <validation validateIntegratedModeConfiguration="false" /> |
| 150 | + <modules> |
| 151 | + <remove name="ScriptModule" /> |
| 152 | + <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> |
| 153 | + </modules> |
| 154 | + </system.webServer> |
| 155 | + </configuration> |
| 156 | + """ |
| 157 | + expected_output = """\ |
| 158 | + <?xml version="1.0" encoding="utf-8"?> |
| 159 | + <configuration> |
| 160 | + <system.web> |
| 161 | + </system.web> |
| 162 | + <system.webServer> |
| 163 | + <validation validateIntegratedModeConfiguration="false"></validation> |
| 164 | + <modules> |
| 165 | + <remove name="ScriptModule"></remove> |
| 166 | + <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"></add> |
| 167 | + </modules> |
| 168 | + <httpProtocol><customHeaders><add name="X-Frame-Options" value="DENY"></add></customHeaders></httpProtocol></system.webServer> |
| 169 | + </configuration>""" |
| 170 | + new_elements = [ |
| 171 | + NewElement( |
| 172 | + name="httpProtocol", |
| 173 | + parent_name="system.webServer", |
| 174 | + content=NewElement( |
| 175 | + name="customHeaders", |
| 176 | + parent_name="httpProtocol", |
| 177 | + content=NewElement( |
| 178 | + name="add", |
| 179 | + parent_name="customHeaders", |
| 180 | + attributes={"name": "X-Frame-Options", "value": "DENY"}, |
| 181 | + ), |
| 182 | + ), |
| 183 | + ), |
| 184 | + ] |
| 185 | + self.run_and_assert(new_elements, input_code, expected_output) |
0 commit comments