summaryrefslogtreecommitdiff
path: root/bs4/tests
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2015-06-28 15:39:36 -0400
committerLeonard Richardson <leonardr@segfault.org>2015-06-28 15:39:36 -0400
commit9428b9d6ed0d279a72414a986290821ca4f0caaf (patch)
treef792dd2d4e0b4c3913d4766a2717e57e7dd23b12 /bs4/tests
parent92ad5e0dee9503f507f6277b493dfa96010f3a44 (diff)
Changed the way soup objects work under copy.copy(). Copying a
NavigableString or a Tag will give you a new NavigableString that's equal to the old one but not connected to the parse tree. Patch by Martijn Peters. [bug=1307490]
Diffstat (limited to 'bs4/tests')
-rw-r--r--bs4/tests/test_tree.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py
index 22d4b4f..2371591 100644
--- a/bs4/tests/test_tree.py
+++ b/bs4/tests/test_tree.py
@@ -1330,6 +1330,32 @@ class TestPersistence(SoupTest):
self.assertEqual(s1, s2)
self.assertTrue(isinstance(s2, Comment))
+ def test_copy_entire_soup(self):
+ html = u"<div><b>Foo<a></a></b><b>Bar</b></div>end"
+ soup = self.soup(html)
+ soup_copy = copy.copy(soup)
+ self.assertEqual(soup, soup_copy)
+
+ def test_copy_tag_copies_contents(self):
+ html = u"<div><b>Foo<a></a></b><b>Bar</b></div>end"
+ soup = self.soup(html)
+ div = soup.div
+ div_copy = copy.copy(div)
+
+ # The two tags look the same, and evaluate to equal.
+ self.assertEqual(unicode(div), unicode(div_copy))
+ self.assertEqual(div, div_copy)
+
+ # But they're not the same object.
+ self.assertFalse(div is div_copy)
+
+ # And they don't have the same relation to the parse tree. The
+ # copy is not associated with a parse tree at all.
+ self.assertEqual(None, div_copy.parent)
+ self.assertEqual(None, div_copy.previous_element)
+ self.assertEqual(None, div_copy.find(string='Bar').next_element)
+ self.assertNotEqual(None, div.find(string='Bar').next_element)
+
class TestSubstitutions(SoupTest):
def test_default_formatter_is_minimal(self):