]> git.scottworley.com Git - paperdoorknob/blame_incremental - glowfic_test.py
Always have Thread.__init__ fetch the HTML
[paperdoorknob] / glowfic_test.py
... / ...
CommitLineData
1# paperdoorknob: Print glowfic
2#
3# This program is free software: you can redistribute it and/or modify it
4# under the terms of the GNU General Public License as published by the
5# Free Software Foundation, version 3.
6
7
8from sys import stderr
9import unittest
10
11from fetch import FakeFetcher
12from images import FakeImageStore
13from glowfic import makeChunk, Thread
14from spec import Spec
15from texify import PandocTexifier
16
17
18def spec_for_testing(html: bytes) -> Spec:
19 return Spec('test',
20 FakeFetcher({'test?view=flat': html}),
21 FakeImageStore(),
22 lambda x: x,
23 lambda x: None,
24 PandocTexifier('pandoc'),
25 lambda x: x,
26 20,
27 b'',
28 None,
29 stderr.buffer,
30 lambda x: None)
31
32
33class TestSplit(unittest.TestCase):
34
35 def testSplit1(self) -> None:
36 t = Thread(spec_for_testing(b'''
37 <html><body><div class="post-container post-post">
38 The "post"
39 </div></body></html>'''))
40 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
41 [['The "post"']])
42
43 def testSplit2(self) -> None:
44 t = Thread(spec_for_testing(b'''
45 <html><body>
46 <div class="post-container post-post">The "post"</div>
47 <div class="flat-post-replies">
48 <div class="post-container post-reply">The "reply"</div>
49 </div>
50 </body></html>'''))
51 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
52 [['The "post"'], ['The "reply"']])
53
54 def testSplit3(self) -> None:
55 t = Thread(spec_for_testing(b'''
56 <html><body>
57 <div class="post-container post-post">The "post"</div>
58 <div class="flat-post-replies">
59 <div class="post-container post-reply">1st reply</div>
60 <div class="post-container post-reply">2nd reply</div>
61 </div>
62 </body></html>'''))
63 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
64 [['The "post"'], ['1st reply'], ['2nd reply']])
65
66
67class TestMakeChunk(unittest.TestCase):
68
69 def testEmptyContent(self) -> None:
70 with open('testdata/empty-content.html', 'rb') as f:
71 t = Thread(spec_for_testing(f.read((9999))))
72 c = makeChunk(next(iter(t.chunkDOMs())), FakeImageStore())
73 self.assertEqual(
74 c.icon,
75 'stored:https://d1anwqy6ci9o1i.cloudfront.net/' +
76 'users%2F366%2Ficons%2Fxqmypqvflgdy28aorw9ml_shock.png')
77 assert c.character
78 assert c.screen_name
79 assert c.author
80 self.assertEqual(list(c.character.stripped_strings), ['Keltham'])
81 self.assertEqual(
82 list(c.screen_name.stripped_strings), ['lawful chaotic'])
83 self.assertEqual(list(c.author.stripped_strings), ['Iarwain'])
84 self.assertEqual(str(c.content),
85 '<div class="post-content"><p></p></div>')
86
87 self.assertEqual(
88 PandocTexifier("pandoc").texify(c.character), b'{Keltham}\n')
89
90
91class TestThread(unittest.TestCase):
92
93 def testTitle(self) -> None:
94 t = Thread(spec_for_testing(b'''
95 <html><body>
96 <div class="content-header">
97 <span id="post-title">
98 <a href="/posts/1234">Teh Story!</a>
99 </span>
100 </div>
101 <div class="post-container post-post">The "post"</div>
102 </body></html>'''))
103 self.assertEqual(t.title(), 'Teh Story!')
104
105
106if __name__ == '__main__':
107 unittest.main()