]>
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 | You \\emph{sure}? | |
55 | Pretty sure. | |
56 | \\end{document} | |
57 | ''' | |
58 | ||
59 | def testDirectTexifier(self) -> None: | |
60 | texifier = VerifyingTexifier( | |
61 | PandocTexifier('pandoc'), DirectTexifier()) | |
62 | buf = io.BytesIO() | |
63 | spec = Spec( | |
64 | self.url(), | |
65 | self.fetcher(), | |
66 | FakeImageStore(), | |
67 | lambda x: x, | |
68 | lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), | |
69 | ContentOnlyLayout(texifier), | |
70 | None, | |
71 | buf) | |
72 | paperdoorknob.process(spec) | |
73 | ||
74 | def testPDF(self) -> None: | |
75 | with open("test.tex", 'wb') as out: | |
76 | spec = Spec( | |
77 | self.url(), | |
78 | self.fetcher(), | |
79 | FakeImageStore(), | |
80 | lambda x: x, | |
81 | lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), | |
82 | BelowIconLayout(PandocTexifier('pandoc')), | |
83 | None, | |
84 | out) | |
85 | paperdoorknob.process(spec) | |
86 | subprocess.run(['pdflatex', 'test.tex'], | |
87 | stdin=subprocess.DEVNULL, check=True) | |
88 | ||
89 | ||
90 | class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase): | |
91 | ||
92 | def setUp(self) -> None: | |
93 | self._fetcher = self.enterContext(DirectFetcher(TIMEOUT)) | |
94 | self._server = self.enterContext(FakeGlowficServer()) | |
95 | self._port = self._server.port() | |
96 | ||
97 | def url(self) -> str: | |
98 | return f"http://localhost:{self._port}" | |
99 | ||
100 | def fetcher(self) -> Fetcher: | |
101 | return self._fetcher | |
102 | ||
103 | ||
104 | class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase): | |
105 | ||
106 | def url(self) -> str: | |
107 | return 'fic' | |
108 | ||
109 | def fetcher(self) -> Fetcher: | |
110 | with open('testdata/this-is-glowfic.html', 'rb') as f: | |
111 | return FakeFetcher({'fic': f.read(9999)}) | |
112 | ||
113 | ||
114 | if __name__ == '__main__': | |
115 | unittest.main() |