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