soup_adapter_test.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright 2012 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. """Tests for the Gumbo's BeautifulSoup Python adapter."""
  16. __author__ = 'jdtang@google.com (Jonathan Tang)'
  17. import unittest
  18. import soup_adapter
  19. class SoupAdapterTest(unittest.TestCase):
  20. def testSimpleParse(self):
  21. soup = soup_adapter.parse(
  22. """
  23. <ul>
  24. <li class=odd><a href="one.html">One</a>
  25. <li class="even"><a href="two.html">Two</a>
  26. <li class='odd'><a href="three.html">Three</a>
  27. <li class="even"><a href="four.html">Four</a>
  28. </ul>
  29. """)
  30. head = soup.head
  31. self.assertEquals(soup, head.parent.parent)
  32. self.assertEquals(u'head', head.name)
  33. self.assertEquals(0, len(head))
  34. body = soup.body
  35. self.assertEquals(head, body.previousSibling)
  36. self.assertEquals(2, len(body)) # <ul> + trailing whitespace
  37. self.assertEquals(u'ul', body.contents[0].name)
  38. self.assertEquals(body, head.next)
  39. self.assertEquals(head, body.previous)
  40. list_items = body.findAll('li')
  41. self.assertEquals(4, len(list_items))
  42. evens = body('li', 'even')
  43. self.assertEquals(2, len(evens))
  44. a2 = body.find('a', href='two.html')
  45. self.assertEquals(u'a', a2.name)
  46. self.assertEquals(u'Two', a2.contents[0])
  47. self.assertEquals(a2, evens[0].next)
  48. self.assertEquals(evens[0], a2.previous)
  49. li2 = a2.parent
  50. self.assertEquals(u'li', li2.name)
  51. self.assertEquals(u'even', li2['class'])
  52. self.assertEquals(list_items[1], li2)
  53. self.assertEquals(evens[0], li2)
  54. if __name__ == '__main__':
  55. unittest.main()