|
7 | 7 |
|
8 | 8 | class TestVisitor(unittest.TestCase): |
9 | 9 | def setUp(self): |
10 | | - self.example = jast.parse( |
11 | | - """ |
12 | | - public class Example { |
13 | | - |
14 | | - public int add(int a, int b) { |
15 | | - return a + b; |
16 | | - } |
17 | | - |
18 | | - public static void main(String[] args) { |
19 | | - System.out.println(add(27, 55)); |
20 | | - } |
21 | | - } |
22 | | - """ |
23 | | - ) |
| 10 | + self.source = """public class Example { |
| 11 | + |
| 12 | + public int add(int a, int b) { |
| 13 | + return a + b; |
| 14 | + } |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + System.out.println(add(27, 55)); |
| 18 | + } |
| 19 | +}""" |
| 20 | + self.example = jast.parse(self.source) |
24 | 21 |
|
25 | 22 | def test_IdentifierVisitor(self): |
26 | 23 | class IdentifierVisitor(jast.JNodeVisitor): |
@@ -116,16 +113,7 @@ def visit_BinOp(self, node): |
116 | 113 | ) |
117 | 114 | self.assertIsNot(self.example, new_tree) |
118 | 115 | self.assertEqual( |
119 | | - """public class Example { |
120 | | - |
121 | | - public int add(int a, int b) { |
122 | | - return a + b; |
123 | | - } |
124 | | - |
125 | | - public static void main(String[] args) { |
126 | | - System.out.println(add(27, 55)); |
127 | | - } |
128 | | -}""", |
| 116 | + self.source, |
129 | 117 | jast.unparse(self.example), |
130 | 118 | ) |
131 | 119 |
|
@@ -177,3 +165,115 @@ def test_copy_TextBlock(self): |
177 | 165 | self.assertIsNot(text_block, new_text_block) |
178 | 166 | self.assertEqual(text_block, new_text_block) |
179 | 167 | self.assertEqual(text_block.value, new_text_block.value) |
| 168 | + |
| 169 | + def test_DeleteReturn(self): |
| 170 | + class DeleteReturn(jast.JNodeTransformer): |
| 171 | + def visit_Return(self, node): |
| 172 | + return None |
| 173 | + |
| 174 | + delete_return = DeleteReturn() |
| 175 | + new_tree = delete_return.visit(self.example) |
| 176 | + code = jast.unparse(new_tree) |
| 177 | + self.assertEqual( |
| 178 | + """public class Example { |
| 179 | + |
| 180 | + public int add(int a, int b) { |
| 181 | + } |
| 182 | + |
| 183 | + public static void main(String[] args) { |
| 184 | + System.out.println(add(27, 55)); |
| 185 | + } |
| 186 | +}""", |
| 187 | + code, |
| 188 | + ) |
| 189 | + |
| 190 | + def test_DeleteReturn_keep(self): |
| 191 | + class DeleteReturn(jast.JNodeKeepTransformer): |
| 192 | + def visit_Return(self, node): |
| 193 | + return None |
| 194 | + |
| 195 | + delete_return = DeleteReturn() |
| 196 | + new_tree = delete_return.visit(self.example) |
| 197 | + code = jast.unparse(new_tree) |
| 198 | + self.assertEqual( |
| 199 | + """public class Example { |
| 200 | + |
| 201 | + public int add(int a, int b) { |
| 202 | + } |
| 203 | + |
| 204 | + public static void main(String[] args) { |
| 205 | + System.out.println(add(27, 55)); |
| 206 | + } |
| 207 | +}""", |
| 208 | + code, |
| 209 | + ) |
| 210 | + self.assertIsNot(self.example, new_tree) |
| 211 | + self.assertEqual( |
| 212 | + self.source, |
| 213 | + jast.unparse(self.example), |
| 214 | + ) |
| 215 | + |
| 216 | + def test_DeleteAndAdd(self): |
| 217 | + class DeleteAndAdd(jast.JNodeTransformer): |
| 218 | + def __init__(self): |
| 219 | + self.to_add = None |
| 220 | + |
| 221 | + def visit_Method(self, node): |
| 222 | + if node.id == "add": |
| 223 | + self.to_add = node |
| 224 | + return None |
| 225 | + elif node.id == "main": |
| 226 | + return [node, self.to_add] |
| 227 | + return node |
| 228 | + |
| 229 | + delete_and_add = DeleteAndAdd() |
| 230 | + new_tree = delete_and_add.visit(self.example) |
| 231 | + code = jast.unparse(new_tree) |
| 232 | + self.assertEqual( |
| 233 | + """public class Example { |
| 234 | + |
| 235 | + public static void main(String[] args) { |
| 236 | + System.out.println(add(27, 55)); |
| 237 | + } |
| 238 | + |
| 239 | + public int add(int a, int b) { |
| 240 | + return a + b; |
| 241 | + } |
| 242 | +}""", |
| 243 | + code, |
| 244 | + ) |
| 245 | + |
| 246 | + def test_DeleteAndAdd_keep(self): |
| 247 | + class DeleteAndAdd(jast.JNodeKeepTransformer): |
| 248 | + def __init__(self): |
| 249 | + self.to_add = None |
| 250 | + |
| 251 | + def visit_Method(self, node): |
| 252 | + if node.id == "add": |
| 253 | + self.to_add = node |
| 254 | + return None |
| 255 | + elif node.id == "main": |
| 256 | + return [node, self.to_add] |
| 257 | + return node |
| 258 | + |
| 259 | + delete_and_add = DeleteAndAdd() |
| 260 | + new_tree = delete_and_add.visit(self.example) |
| 261 | + code = jast.unparse(new_tree) |
| 262 | + self.assertEqual( |
| 263 | + """public class Example { |
| 264 | + |
| 265 | + public static void main(String[] args) { |
| 266 | + System.out.println(add(27, 55)); |
| 267 | + } |
| 268 | + |
| 269 | + public int add(int a, int b) { |
| 270 | + return a + b; |
| 271 | + } |
| 272 | +}""", |
| 273 | + code, |
| 274 | + ) |
| 275 | + self.assertIsNot(self.example, new_tree) |
| 276 | + self.assertEqual( |
| 277 | + self.source, |
| 278 | + jast.unparse(self.example), |
| 279 | + ) |
0 commit comments