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 17__author__ = 'jdtang@google.com (Jonathan Tang)' 18 19import unittest 20 21import soup_adapter 22 23 24class SoupAdapterTest(unittest.TestCase): 25 26 def testSimpleParse(self): 27 soup = soup_adapter.parse( 28 """ 29 <ul> 30 <li class=odd><a href="one.html">One</a> 31 <li class="even"><a href="two.html">Two</a> 32 <li class='odd'><a href="three.html">Three</a> 33 <li class="even"><a href="four.html">Four</a> 34 </ul> 35 """) 36 37 head = soup.head 38 self.assertEquals(soup, head.parent.parent) 39 self.assertEquals(u'head', head.name) 40 self.assertEquals(0, len(head)) 41 42 body = soup.body 43 self.assertEquals(head, body.previousSibling) 44 self.assertEquals(2, len(body)) # <ul> + trailing whitespace 45 self.assertEquals(u'ul', body.contents[0].name) 46 self.assertEquals(body, head.next) 47 self.assertEquals(head, body.previous) 48 49 list_items = body.findAll('li') 50 self.assertEquals(4, len(list_items)) 51 52 evens = body('li', 'even') 53 self.assertEquals(2, len(evens)) 54 55 a2 = body.find('a', href='two.html') 56 self.assertEquals(u'a', a2.name) 57 self.assertEquals(u'Two', a2.contents[0]) 58 self.assertEquals(a2, evens[0].next) 59 self.assertEquals(evens[0], a2.previous) 60 61 li2 = a2.parent 62 self.assertEquals(u'li', li2.name) 63 self.assertEquals(u'even', li2['class']) 64 self.assertEquals(list_items[1], li2) 65 self.assertEquals(evens[0], li2) 66 67if __name__ == '__main__': 68 unittest.main() 69