]> git.scottworley.com Git - paperdoorknob/blob - glowfic_test.py
898aafba43c2c25a2b7e7f28c966d7e1ae5e6a0f
[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 def testMinimal(self) -> None:
92 t = Thread(spec_for_testing(b'''
93 <html><body>
94 <div class="post-container post-post">
95 <div class="post-content">Just content</div>
96 </div>
97 </body></html>'''))
98 c = makeChunk(next(iter(t.chunkDOMs())), FakeImageStore())
99 self.assertIsNone(c.icon)
100 self.assertIsNone(c.character)
101 self.assertIsNone(c.screen_name)
102 self.assertIsNone(c.author)
103 self.assertEqual(str(c.content),
104 '<div class="post-content">Just content</div>')
105
106
107 class TestThread(unittest.TestCase):
108
109 def testTitle(self) -> None:
110 t = Thread(spec_for_testing(b'''
111 <html><body>
112 <div class="content-header">
113 <span id="post-title">
114 <a href="/posts/1234">Teh Story!</a>
115 </span>
116 </div>
117 <div class="post-container post-post">The "post"</div>
118 </body></html>'''))
119 self.assertEqual(t.title(), 'Teh Story!')
120
121 def testNextThreadRelative(self) -> None:
122 t = Thread(spec_for_testing(b'''
123 <html><body>
124 <div class="post-navheader">
125 <a class="view-button-link" href="/posts/4567"><div class="view-button">Next Post &raquo;</div>
126 </a><a class="view-button-link" href="/posts/4321"><div class="view-button float-none">&laquo; Previous Post</div>
127 </a>
128 </div>
129 <div class="post-container post-post">The "post"</div>
130 </body></html>'''))
131 self.assertEqual(t.next_thread(), 'https://fake/posts/4567')
132
133 def testNextThreadAbsolute(self) -> None:
134 t = Thread(spec_for_testing(b'''
135 <html><body>
136 <div class="post-navheader">
137 <a class="view-button-link" href="https://elsewhere/posts/4567"><div class="view-button">Next Post &raquo;</div>
138 </a><a class="view-button-link" href="https://elsewhere/posts/4321"><div class="view-button float-none">&laquo; Previous Post</div>
139 </a>
140 </div>
141 <div class="post-container post-post">The "post"</div>
142 </body></html>'''))
143 self.assertEqual(t.next_thread(), 'https://elsewhere/posts/4567')
144
145
146 if __name__ == '__main__':
147 unittest.main()