]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Specify page geometry
[paperdoorknob] / paperdoorknob_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 abc import ABC, abstractmethod
9 import unittest
10 import io
11 import subprocess
12
13 import paperdoorknob
14
15 from testing.fakeserver import FakeGlowficServer
16 from domfilter import ApplyDOMFilters
17 from fetch import DirectFetcher, FakeFetcher, Fetcher
18 from spec import Spec
19 from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
20
21 TIMEOUT = 8
22
23
24 class BaseTestProcess(ABC):
25
26 @abstractmethod
27 def url(self) -> str:
28 raise NotImplementedError()
29
30 @abstractmethod
31 def fetcher(self) -> Fetcher:
32 raise NotImplementedError()
33
34 def testReplies(self) -> None:
35 replies = list(paperdoorknob.replies(
36 paperdoorknob.parse(self.fetcher().fetch(self.url()))))
37 for r in replies:
38 ApplyDOMFilters('NoEdit,NoFooter', r)
39 assert [r.text.strip() for r in replies] == [
40 "This is glowfic",
41 "You sure?",
42 "Pretty sure."]
43
44 def testProcess(self) -> None:
45 buf = io.BytesIO()
46 spec = Spec(
47 self.url(),
48 self.fetcher(),
49 lambda x: x,
50 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
51 PandocTexifier('pandoc'),
52 'margin=20mm',
53 buf)
54 paperdoorknob.process(spec)
55 assert buf.getvalue() == b'''\\documentclass{article}
56 \\usepackage[margin=20mm]{geometry}
57 \\begin{document}
58 This is glowfic
59 You \\emph{sure}?
60 Pretty sure.
61 \\end{document}
62 '''
63
64 def testDirectTexifier(self) -> None:
65 texifier = VerifyingTexifier(
66 PandocTexifier('pandoc'), DirectTexifier())
67 buf = io.BytesIO()
68 spec = Spec(
69 self.url(),
70 self.fetcher(),
71 lambda x: x,
72 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
73 texifier,
74 None,
75 buf)
76 paperdoorknob.process(spec)
77
78 def testPDF(self) -> None:
79 with open("test.tex", 'wb') as out:
80 spec = Spec(
81 self.url(),
82 self.fetcher(),
83 lambda x: x,
84 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
85 PandocTexifier('pandoc'),
86 None,
87 out)
88 paperdoorknob.process(spec)
89 subprocess.run(['pdflatex', 'test.tex'],
90 stdin=subprocess.DEVNULL, check=True)
91
92
93 class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
94
95 def setUp(self) -> None:
96 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
97 self._server = self.enterContext(FakeGlowficServer())
98 self._port = self._server.port()
99
100 def url(self) -> str:
101 return f"http://localhost:{self._port}"
102
103 def fetcher(self) -> Fetcher:
104 return self._fetcher
105
106
107 class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
108
109 def url(self) -> str:
110 return 'fic'
111
112 def fetcher(self) -> Fetcher:
113 with open('testdata/this-is-glowfic.html', 'rb') as f:
114 return FakeFetcher({'fic': f.read(9999)})
115
116
117 if __name__ == '__main__':
118 unittest.main()