From 3bbb7de34487e43bb1373b44f45b32cd792cf914 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Thu, 16 Aug 2012 13:28:14 -0400 Subject: As per PEP-8, allow searching by CSS class using the 'class_' keyword argument. [bug=1037624] --- bs4/tests/test_tree.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'bs4/tests/test_tree.py') diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index cc573ed..9397f24 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -228,18 +228,24 @@ class TestFindAllByAttribute(TreeTest): self.assertSelects(tree.find_all(attrs={'name' : 'name1'}), ["Name match."]) - # Passing class='class2' would cause a syntax error. self.assertSelects(tree.find_all(attrs={'class' : 'class2'}), ["Class match."]) def test_find_all_by_class(self): - # Passing in a string to 'attrs' will search the CSS class. tree = self.soup(""" Class 1. Class 2. Class 1. Class 3 and 4. """) + + # Passing in the class_ keyword argument will search against + # the 'class' attribute. + self.assertSelects(tree.find_all('a', class_='1'), ['Class 1.']) + self.assertSelects(tree.find_all('c', class_='3'), ['Class 3 and 4.']) + self.assertSelects(tree.find_all('c', class_='4'), ['Class 3 and 4.']) + + # Passing in a string to 'attrs' will also search the CSS class. self.assertSelects(tree.find_all('a', '1'), ['Class 1.']) self.assertSelects(tree.find_all(attrs='1'), ['Class 1.', 'Class 1.']) self.assertSelects(tree.find_all('c', '3'), ['Class 3 and 4.']) @@ -248,17 +254,15 @@ class TestFindAllByAttribute(TreeTest): def test_find_by_class_when_multiple_classes_present(self): tree = self.soup("Found it") - attrs = { 'class' : re.compile("o") } - f = tree.find_all("gar", attrs=attrs) + f = tree.find_all("gar", class_=re.compile("o")) self.assertSelects(f, ["Found it"]) - f = tree.find_all("gar", re.compile("a")) + f = tree.find_all("gar", class_=re.compile("a")) self.assertSelects(f, ["Found it"]) # Since the class is not the string "foo bar", but the two # strings "foo" and "bar", this will not find anything. - attrs = { 'class' : re.compile("o b") } - f = tree.find_all("gar", attrs=attrs) + f = tree.find_all("gar", class_=re.compile("o b")) self.assertSelects(f, []) def test_find_all_with_non_dictionary_for_attrs_finds_by_class(self): @@ -283,8 +287,9 @@ class TestFindAllByAttribute(TreeTest): self.assertEqual([a, a2], soup.find_all("a", "foo")) self.assertEqual([a], soup.find_all("a", "bar")) - # If you specify the attribute as a string that contains a + # If you specify the class as a string that contains a # space, only that specific value will be found. + self.assertEqual([a], soup.find_all("a", class_="foo bar")) self.assertEqual([a], soup.find_all("a", "foo bar")) self.assertEqual([], soup.find_all("a", "bar foo")) -- cgit v1.2.3