]>
git.scottworley.com Git - paperdoorknob/blob - glowfic_test.py
898aafba43c2c25a2b7e7f28c966d7e1ae5e6a0f
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 fetch
import FakeFetcher
12 from images
import FakeImageStore
13 from glowfic
import makeChunk
, Thread
15 from texify
import PandocTexifier
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
}),
25 PandocTexifier('pandoc'),
34 class TestSplit(unittest
.TestCase
):
36 def testSplit1(self
) -> None:
37 t
= Thread(spec_for_testing(b
'''
38 <html><body><div class="post-container post-post">
40 </div></body></html>'''))
41 self
.assertEqual([list(t
.stripped_strings
) for t
in t
.chunkDOMs()],
44 def testSplit2(self
) -> None:
45 t
= Thread(spec_for_testing(b
'''
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>
52 self
.assertEqual([list(t
.stripped_strings
) for t
in t
.chunkDOMs()],
53 [['The "post"'], ['The "reply"']])
55 def testSplit3(self
) -> None:
56 t
= Thread(spec_for_testing(b
'''
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>
64 self
.assertEqual([list(t
.stripped_strings
) for t
in t
.chunkDOMs()],
65 [['The "post"'], ['1st reply'], ['2nd reply']])
68 class TestMakeChunk(unittest
.TestCase
):
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())
76 'stored:https://d1anwqy6ci9o1i.cloudfront.net/' +
77 'users%2F366%2Ficons%2Fxqmypqvflgdy28aorw9ml_shock.png')
81 self
.assertEqual(list(c
.character
.stripped_strings
), ['Keltham'])
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>')
89 PandocTexifier("pandoc").texify(c
.character
), b
'{Keltham}\n')
91 def testMinimal(self
) -> None:
92 t
= Thread(spec_for_testing(b
'''
94 <div class="post-container post-post">
95 <div class="post-content">Just content</div>
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>')
107 class TestThread(unittest
.TestCase
):
109 def testTitle(self
) -> None:
110 t
= Thread(spec_for_testing(b
'''
112 <div class="content-header">
113 <span id="post-title">
114 <a href="/posts/1234">Teh Story!</a>
117 <div class="post-container post-post">The "post"</div>
119 self
.assertEqual(t
.title(), 'Teh Story!')
121 def testNextThreadRelative(self
) -> None:
122 t
= Thread(spec_for_testing(b
'''
124 <div class="post-navheader">
125 <a class="view-button-link" href="/posts/4567"><div class="view-button">Next Post »</div>
126 </a><a class="view-button-link" href="/posts/4321"><div class="view-button float-none">« Previous Post</div>
129 <div class="post-container post-post">The "post"</div>
131 self
.assertEqual(t
.next_thread(), 'https://fake/posts/4567')
133 def testNextThreadAbsolute(self
) -> None:
134 t
= Thread(spec_for_testing(b
'''
136 <div class="post-navheader">
137 <a class="view-button-link" href="https://elsewhere/posts/4567"><div class="view-button">Next Post »</div>
138 </a><a class="view-button-link" href="https://elsewhere/posts/4321"><div class="view-button float-none">« Previous Post</div>
141 <div class="post-container post-post">The "post"</div>
143 self
.assertEqual(t
.next_thread(), 'https://elsewhere/posts/4567')
146 if __name__
== '__main__':