(t *testing.T)
| 271 | } |
| 272 | |
| 273 | func TestRemoveFromTree(t *testing.T) { |
| 274 | xmlStr := `<?procinst?> |
| 275 | <!--comment--> |
| 276 | <aaa><bbb/> |
| 277 | <ddd><eee><fff/></eee></ddd> |
| 278 | <ggg/></aaa>` |
| 279 | parseXML := func() *Node { |
| 280 | doc, err := Parse(strings.NewReader(xmlStr)) |
| 281 | testTrue(t, err == nil) |
| 282 | return doc |
| 283 | } |
| 284 | |
| 285 | t.Run("remove an elem node that is the only child of its parent", func(t *testing.T) { |
| 286 | doc := parseXML() |
| 287 | n := FindOne(doc, "//aaa/ddd/eee") |
| 288 | testTrue(t, n != nil) |
| 289 | RemoveFromTree(n) |
| 290 | verifyNodePointers(t, doc) |
| 291 | testValue(t, doc.OutputXMLWithOptions(WithoutPreserveSpace()), |
| 292 | `<?procinst?><!--comment--><aaa><bbb></bbb><ddd></ddd><ggg></ggg></aaa>`) |
| 293 | }) |
| 294 | |
| 295 | t.Run("remove an elem node that is the first but not the last child of its parent", func(t *testing.T) { |
| 296 | doc := parseXML() |
| 297 | n := FindOne(doc, "//aaa/bbb") |
| 298 | testTrue(t, n != nil) |
| 299 | RemoveFromTree(n) |
| 300 | verifyNodePointers(t, doc) |
| 301 | testValue(t, doc.OutputXMLWithOptions(WithoutPreserveSpace()), |
| 302 | `<?procinst?><!--comment--><aaa><ddd><eee><fff></fff></eee></ddd><ggg></ggg></aaa>`) |
| 303 | }) |
| 304 | |
| 305 | t.Run("remove an elem node that is neither the first nor the last child of its parent", func(t *testing.T) { |
| 306 | doc := parseXML() |
| 307 | n := FindOne(doc, "//aaa/ddd") |
| 308 | testTrue(t, n != nil) |
| 309 | RemoveFromTree(n) |
| 310 | verifyNodePointers(t, doc) |
| 311 | testValue(t, doc.OutputXMLWithOptions(WithoutPreserveSpace()), |
| 312 | `<?procinst?><!--comment--><aaa><bbb></bbb><ggg></ggg></aaa>`) |
| 313 | }) |
| 314 | |
| 315 | t.Run("remove an elem node that is the last but not the first child of its parent", func(t *testing.T) { |
| 316 | doc := parseXML() |
| 317 | n := FindOne(doc, "//aaa/ggg") |
| 318 | testTrue(t, n != nil) |
| 319 | RemoveFromTree(n) |
| 320 | verifyNodePointers(t, doc) |
| 321 | testValue(t, doc.OutputXMLWithOptions(WithoutPreserveSpace()), |
| 322 | `<?procinst?><!--comment--><aaa><bbb></bbb><ddd><eee><fff></fff></eee></ddd></aaa>`) |
| 323 | }) |
| 324 | |
| 325 | t.Run("remove decl node works", func(t *testing.T) { |
| 326 | doc := parseXML() |
| 327 | procInst := doc.FirstChild |
| 328 | testValue(t, procInst.Type, ProcessingInstruction) |
| 329 | RemoveFromTree(procInst) |
| 330 | verifyNodePointers(t, doc) |
nothing calls this directly
no test coverage detected
searching dependent graphs…