]> git.scottworley.com Git - paperdoorknob/blob - glowfic_test.py
22eea257c21d4fe4d9b7776fdd159b579a8016f8
[paperdoorknob] / glowfic_test.py
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
8 from sys import stderr
9 import unittest
10
11 from fetch import FakeFetcher
12 from images import FakeImageStore
13 from glowfic import makeChunk, Thread
14 from spec import Spec
15 from texify import PandocTexifier
16
17
18 def spec_for_testing(html: bytes) -> Spec:
19 return Spec('https://fake/test',
20 FakeFetcher({'https://fake/test': html,
21 'https://fake/test?view=flat': html}),
22 FakeImageStore(),
23 lambda x: x,
24 lambda x: None,
25 PandocTexifier('pandoc'),
26 lambda x: x,
27 20,
28 b'',
29 None,
30 stderr.buffer,
31 lambda x: None)
32
33
34 class TestSplit(unittest.TestCase):
35
36 def testSplit1(self) -> None:
37 t = Thread(spec_for_testing(b'''
38 <html><body><div class="post-container post-post">
39 The "post"
40 </div></body></html>'''))
41 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
42 [['The "post"']])
43
44 def testSplit2(self) -> None:
45 t = Thread(spec_for_testing(b'''
46 <html><body>
47 <div class="post-container post-post">The "post"</div>
48 <div class="flat-post-replies">
49 <div class="post-container post-reply">The "reply"</div>
50 </div>
51 </body></html>'''))
52 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
53 [['The "post"'], ['The "reply"']])
54
55 def testSplit3(self) -> None:
56 t = Thread(spec_for_testing(b'''
57 <html><body>
58 <div class="post-container post-post">The "post"</div>
59 <div class="flat-post-replies">
60 <div class="post-container post-reply">1st reply</div>
61 <div class="post-container post-reply">2nd reply</div>
62 </div>
63 </body></html>'''))
64 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
65 [['The "post"'], ['1st reply'], ['2nd reply']])
66
67
68 class TestMakeChunk(unittest.TestCase):
69
70 def testEmptyContent(self) -> None:
71 with open('testdata/empty-content.html', 'rb') as f:
72 t = Thread(spec_for_testing(f.read((9999))))
73 c = makeChunk(next(iter(t.chunkDOMs())), FakeImageStore())
74 self.assertEqual(
75 c.icon,
76 'stored:https://d1anwqy6ci9o1i.cloudfront.net/' +
77 'users%2F366%2Ficons%2Fxqmypqvflgdy28aorw9ml_shock.png')
78 assert c.character
79 assert c.screen_name
80 assert c.author
81 self.assertEqual(list(c.character.stripped_strings), ['Keltham'])
82 self.assertEqual(
83 list(c.screen_name.stripped_strings), ['lawful chaotic'])
84 self.assertEqual(list(c.author.stripped_strings), ['Iarwain'])
85 self.assertEqual(str(c.content),
86 '<div class="post-content"><p></p></div>')
87
88 self.assertEqual(
89 PandocTexifier("pandoc").texify(c.character), b'{Keltham}\n')
90
91
92 class TestThread(unittest.TestCase):
93
94 def testTitle(self) -> None:
95 t = Thread(spec_for_testing(b'''
96 <html><body>
97 <div class="content-header">
98 <span id="post-title">
99 <a href="/posts/1234">Teh Story!</a>
100 </span>
101 </div>
102 <div class="post-container post-post">The "post"</div>
103 </body></html>'''))
104 self.assertEqual(t.title(), 'Teh Story!')
105
106 def testNextThread(self) -> None:
107 t = Thread(spec_for_testing(b'''
108 <html><body>
109 <div class="post-navheader">
110 <a class="view-button-link" href="/posts/4567"><div class="view-button">Next Post &raquo;</div>
111 </a><a class="view-button-link" href="/posts/4321"><div class="view-button float-none">&laquo; Previous Post</div>
112 </a>
113 </div>
114 <div class="post-container post-post">The "post"</div>
115 </body></html>'''))
116 self.assertEqual(t.next_thread(), '/posts/4567')
117
118
119 if __name__ == '__main__':
120 unittest.main()