]> git.scottworley.com Git - paperdoorknob/blame - glowfic_test.py
next_thread should be an absolute URL
[paperdoorknob] / glowfic_test.py
CommitLineData
e6adf6ce
SW
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
19ed28f3 8from sys import stderr
e6adf6ce
SW
9import unittest
10
19ed28f3 11from fetch import FakeFetcher
aa060d9b 12from images import FakeImageStore
94027099 13from glowfic import makeChunk, Thread
19ed28f3 14from spec import Spec
62043b2b 15from texify import PandocTexifier
e6adf6ce
SW
16
17
19ed28f3 18def spec_for_testing(html: bytes) -> Spec:
075f2f08
SW
19 return Spec('https://fake/test',
20 FakeFetcher({'https://fake/test': html,
21 'https://fake/test?view=flat': html}),
19ed28f3
SW
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
e6adf6ce
SW
34class TestSplit(unittest.TestCase):
35
36 def testSplit1(self) -> None:
19ed28f3 37 t = Thread(spec_for_testing(b'''
e6adf6ce
SW
38 <html><body><div class="post-container post-post">
39 The "post"
19ed28f3 40 </div></body></html>'''))
94027099 41 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
e6adf6ce
SW
42 [['The "post"']])
43
44 def testSplit2(self) -> None:
19ed28f3 45 t = Thread(spec_for_testing(b'''
e6adf6ce
SW
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>
19ed28f3 51 </body></html>'''))
94027099 52 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
e6adf6ce
SW
53 [['The "post"'], ['The "reply"']])
54
55 def testSplit3(self) -> None:
19ed28f3 56 t = Thread(spec_for_testing(b'''
e6adf6ce
SW
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>
19ed28f3 63 </body></html>'''))
94027099 64 self.assertEqual([list(t.stripped_strings) for t in t.chunkDOMs()],
e6adf6ce
SW
65 [['The "post"'], ['1st reply'], ['2nd reply']])
66
67
aa060d9b
SW
68class TestMakeChunk(unittest.TestCase):
69
70 def testEmptyContent(self) -> None:
71 with open('testdata/empty-content.html', 'rb') as f:
19ed28f3 72 t = Thread(spec_for_testing(f.read((9999))))
94027099 73 c = makeChunk(next(iter(t.chunkDOMs())), FakeImageStore())
aa060d9b
SW
74 self.assertEqual(
75 c.icon,
76 'stored:https://d1anwqy6ci9o1i.cloudfront.net/' +
77 'users%2F366%2Ficons%2Fxqmypqvflgdy28aorw9ml_shock.png')
37c47bc2
SW
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'])
aa060d9b
SW
85 self.assertEqual(str(c.content),
86 '<div class="post-content"><p></p></div>')
87
62043b2b
SW
88 self.assertEqual(
89 PandocTexifier("pandoc").texify(c.character), b'{Keltham}\n')
90
aa060d9b 91
21e82200
SW
92class TestThread(unittest.TestCase):
93
94 def testTitle(self) -> None:
19ed28f3 95 t = Thread(spec_for_testing(b'''
21e82200
SW
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>
19ed28f3 103 </body></html>'''))
21e82200
SW
104 self.assertEqual(t.title(), 'Teh Story!')
105
fc2aaa75 106 def testNextThreadRelative(self) -> None:
53f396b4
SW
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>'''))
fc2aaa75
SW
116 self.assertEqual(t.next_thread(), 'https://fake/posts/4567')
117
118 def testNextThreadAbsolute(self) -> None:
119 t = Thread(spec_for_testing(b'''
120 <html><body>
121 <div class="post-navheader">
122 <a class="view-button-link" href="https://elsewhere/posts/4567"><div class="view-button">Next Post &raquo;</div>
123 </a><a class="view-button-link" href="https://elsewhere/posts/4321"><div class="view-button float-none">&laquo; Previous Post</div>
124 </a>
125 </div>
126 <div class="post-container post-post">The "post"</div>
127 </body></html>'''))
128 self.assertEqual(t.next_thread(), 'https://elsewhere/posts/4567')
53f396b4 129
21e82200 130
e6adf6ce
SW
131if __name__ == '__main__':
132 unittest.main()