]>
git.scottworley.com Git - paperdoorknob/blob - glowfic_test.py
1 # paperdoorknob: Print glowfic
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.
11 from typing
import Optional
13 from fetch
import FakeFetcher
14 from images
import FakeImageStore
15 from glowfic
import makeChunk
, Thread
17 from texify
import PandocTexifier
20 def spec_for_testing(html
: bytes, outbuf
: Optional
[BytesIO
] = None) -> Spec
:
21 return Spec('https://fake/test',
22 FakeFetcher({'https://fake/test': html
,
23 'https://fake/test?view=flat': html
}),
27 PandocTexifier('pandoc'),
32 stderr
.buffer if outbuf
is None else outbuf
,
36 class TestSplit(unittest
.TestCase
):
38 def testSplit1(self
) -> None:
39 t
= Thread(spec_for_testing(b
'''
40 <html><body><div class="post-container post-post">
42 </div></body></html>'''))
43 self
.assertEqual([list(t
.stripped_strings
) for t
in t
.chunkDOMs()],
46 def testSplit2(self
) -> None:
47 t
= Thread(spec_for_testing(b
'''
49 <div class="post-container post-post">The "post"</div>
50 <div class="flat-post-replies">
51 <div class="post-container post-reply">The "reply"</div>
54 self
.assertEqual([list(t
.stripped_strings
) for t
in t
.chunkDOMs()],
55 [['The "post"'], ['The "reply"']])
57 def testSplit3(self
) -> None:
58 t
= Thread(spec_for_testing(b
'''
60 <div class="post-container post-post">The "post"</div>
61 <div class="flat-post-replies">
62 <div class="post-container post-reply">1st reply</div>
63 <div class="post-container post-reply">2nd reply</div>
66 self
.assertEqual([list(t
.stripped_strings
) for t
in t
.chunkDOMs()],
67 [['The "post"'], ['1st reply'], ['2nd reply']])
70 class TestMakeChunk(unittest
.TestCase
):
72 def testEmptyContent(self
) -> None:
73 with open('testdata/empty-content.html', 'rb') as f
:
74 t
= Thread(spec_for_testing(f
.read((9999))))
75 c
= makeChunk(next(iter(t
.chunkDOMs())), FakeImageStore())
78 'stored:https://d1anwqy6ci9o1i.cloudfront.net/' +
79 'users%2F366%2Ficons%2Fxqmypqvflgdy28aorw9ml_shock.png')
83 self
.assertEqual(list(c
.character
.stripped_strings
), ['Keltham'])
85 list(c
.screen_name
.stripped_strings
), ['lawful chaotic'])
86 self
.assertEqual(list(c
.author
.stripped_strings
), ['Iarwain'])
87 self
.assertEqual(str(c
.content
),
88 '<div class="post-content"><p></p></div>')
91 PandocTexifier("pandoc").texify(c
.character
), b
'{Keltham}\n')
93 def testMinimal(self
) -> None:
94 t
= Thread(spec_for_testing(b
'''
96 <div class="post-container post-post">
97 <div class="post-content">Just content</div>
100 c
= makeChunk(next(iter(t
.chunkDOMs())), FakeImageStore())
101 self
.assertIsNone(c
.icon
)
102 self
.assertIsNone(c
.character
)
103 self
.assertIsNone(c
.screen_name
)
104 self
.assertIsNone(c
.author
)
105 self
.assertEqual(str(c
.content
),
106 '<div class="post-content">Just content</div>')
109 class TestThread(unittest
.TestCase
):
111 def testTitle(self
) -> None:
112 t
= Thread(spec_for_testing(b
'''
114 <div class="content-header">
115 <span id="post-title">
116 <a href="/posts/1234">Teh Story!</a>
119 <div class="post-container post-post">The "post"</div>
121 self
.assertEqual(t
.title(), 'Teh Story!')
123 def testNextThreadRelative(self
) -> None:
124 t
= Thread(spec_for_testing(b
'''
126 <div class="post-navheader">
127 <a class="view-button-link" href="/posts/4567"><div class="view-button">Next Post »</div>
128 </a><a class="view-button-link" href="/posts/4321"><div class="view-button float-none">« Previous Post</div>
131 <div class="post-container post-post">The "post"</div>
133 self
.assertEqual(t
.next_thread(), 'https://fake/posts/4567')
135 def testNextThreadAbsolute(self
) -> None:
136 t
= Thread(spec_for_testing(b
'''
138 <div class="post-navheader">
139 <a class="view-button-link" href="https://elsewhere/posts/4567"><div class="view-button">Next Post »</div>
140 </a><a class="view-button-link" href="https://elsewhere/posts/4321"><div class="view-button float-none">« Previous Post</div>
143 <div class="post-container post-post">The "post"</div>
145 self
.assertEqual(t
.next_thread(), 'https://elsewhere/posts/4567')
148 class TestEmit(unittest
.TestCase
):
150 def testEmit(self
) -> None:
152 Thread(spec_for_testing(b
'''
154 <div class="post-container post-post">
155 <div class="post-content">A</div>
157 <div class="flat-post-replies">
158 <div class="post-container post-reply">
159 <div class="post-content">B</div>
162 </body></html>''', buf
)).emit()
163 self
.assertEqual(buf
.getvalue(), rb
'''\glowhead{}{}{}{}A
168 if __name__
== '__main__':