fix proxy change detection
[straw.git] / test / TestParser.py
blob413f320f501f2b9e86fd7a76a555375550893a7d
1 import unittest
2 import sys
3 import locale
4 sys.path.insert(0, "../src/lib")
5 import SummaryParser
7 try:
8 import pmock
9 except ImportError:
10 sys.exit("You need python-mock 0.3 to run this test. http://pmock.sf.net")
13 class TitleImgParserTest(unittest.TestCase):
14 def setUp(self):
15 self._parser = SummaryParser.TitleImgParser()
17 def tearDown(self):
18 self._parser.close()
20 def testHTMLinTitle(self):
21 fragment = "<p><a href=http://www.nongnu.org/straw>Straw</a>: <b>GNOME</b> <i>Desktop</i> Aggregator"
22 self._parser.feed(fragment)
23 self.assertEqual("Straw: GNOME Desktop Aggregator", self._parser.get_text())
24 self._parser.close()
25 fragment = "<span class=\"courtcase\">Lawrence</span>"
26 self._parser.feed(fragment)
27 self.assertEqual("Lawrence", self._parser.get_text())
29 def testImgInHTML(self):
30 fragment = "<p><img src='foo.jpg'/></p>"
31 mock = pmock.Mock()
32 mock.expects(pmock.at_least_once()).get_location().will(pmock.return_value('http://foo.com'))
33 self._parser.set_feed(mock)
34 self._parser.feed(fragment)
35 mock.verify()
36 self.assertEqual(["http://foo.com/foo.jpg"], self._parser.get_image_urls())
37 fragment = "<a href='http://foo.com'><img src='/~jan/bar/foo/straw.png'></a>"
38 self._parser.close()
39 self._parser.feed(fragment)
40 mock.verify()
41 self.assertEqual(["http://foo.com/~jan/bar/foo/straw.png"], self._parser.get_image_urls())
43 def testPlainTextInTitle(self):
44 # Make sure plain text works too
45 fragment = "Straw Rocks!"
46 self._parser.feed(fragment)
47 self.assertEqual("Straw Rocks!", self._parser.get_text())
49 def testEntityInTitle(self):
50 # http://bugzilla.gnome.org/show_bug.cgi?id=149924
51 tdata =[("Joe &amp; Bob. One &lt; Two but &gt; 0","Joe & Bob. One < Two but > 0"),
52 ("<b>Is<b> 2 &lt; <i>1<i>?\"foo\"","Is 2 < 1?\"foo\""),
53 ("&lt;h2&gt;","<h2>")]
55 for fragment,expected in tdata:
56 self._parser.feed(fragment)
57 self.assertEqual(expected, self._parser.get_text())
58 self._parser.close()
60 def testTitleLength(self):
61 CHARS = 60
62 # Excerpt from one of Jarno Virtannen's blog entries
63 fragment = """
64 I am not going to write much here for few weeks, because I
65 <span class="bar">am taking</span> regular visits to a
66 <a href="test.com">dentist</a>. I am not sure about the correct English term for my previous
67 procedure, but I think it is called root canal treatment. The
68 operation itself wasn't as painful as I was expecting (though not too
69 pleasurable either) but I have to take antibiotics, which mess up my
70 stomach and therefore I am not too energetic.
71 """
72 self._parser.feed(fragment)
73 self.assertEqual(CHARS, len(self._parser.get_text(CHARS)))
75 def testBrAndFontTagsInTitle(self):
76 # http://bugzilla.gnome.org/show_bug.cgi?id=148105
77 fragment="""
78 <br><br><br><br><font color=444444 size=1>Shadow</font><br><br>
79 """
80 self._parser.feed(fragment)
81 self.assertEqual("Shadow", self._parser.get_text())
84 def suite():
85 return unittest.makeSuite(TitleParserTest, "test")
87 if __name__ == "__main__":
88 unittest.main()