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