]>
Commit | Line | Data |
---|---|---|
b25a2f90 SW |
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 | ||
38621839 | 8 | from abc import ABC, abstractmethod |
b25a2f90 | 9 | import unittest |
36ae1d5f | 10 | import io |
07f9b178 | 11 | import subprocess |
23f31879 | 12 | |
b25a2f90 | 13 | import paperdoorknob |
23f31879 | 14 | |
41b11505 | 15 | from testing.fakeserver import FakeGlowficServer |
8be20b9d | 16 | from domfilter import ApplyDOMFilters |
38621839 | 17 | from fetch import DirectFetcher, FakeFetcher, Fetcher |
d2a41ff4 | 18 | from glowfic import ContentOnlyLayout, BelowIconLayout |
4b7d905e | 19 | from images import FakeImageStore |
23f31879 | 20 | from spec import Spec |
f1dec720 | 21 | from texify import DirectTexifier, PandocTexifier, VerifyingTexifier |
b25a2f90 | 22 | |
b25a2f90 SW |
23 | TIMEOUT = 8 |
24 | ||
25 | ||
38621839 SW |
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() | |
b25a2f90 | 35 | |
36ae1d5f | 36 | def testProcess(self) -> None: |
38621839 SW |
37 | buf = io.BytesIO() |
38 | spec = Spec( | |
39 | self.url(), | |
40 | self.fetcher(), | |
4b7d905e | 41 | FakeImageStore(), |
929db576 | 42 | lambda x: x, |
8be20b9d | 43 | lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), |
d2a41ff4 | 44 | ContentOnlyLayout(PandocTexifier('pandoc')), |
e10b5b6f | 45 | 'margin=20mm', |
38621839 SW |
46 | buf) |
47 | paperdoorknob.process(spec) | |
48 | assert buf.getvalue() == b'''\\documentclass{article} | |
d2a41ff4 | 49 | \\usepackage{graphicx} |
357f37be | 50 | \\usepackage{wrapstuff} |
e10b5b6f | 51 | \\usepackage[margin=20mm]{geometry} |
07f9b178 SW |
52 | \\begin{document} |
53 | This is glowfic | |
85bcacb0 | 54 | |
357f37be | 55 | \\wrapstuffclear\\vspace{-.5\\ht\\strutbox}\\noindent\\hrulefill |
85bcacb0 | 56 | |
07f9b178 | 57 | You \\emph{sure}? |
85bcacb0 | 58 | |
357f37be | 59 | \\wrapstuffclear\\vspace{-.5\\ht\\strutbox}\\noindent\\hrulefill |
85bcacb0 | 60 | |
07f9b178 SW |
61 | Pretty sure. |
62 | \\end{document} | |
38621839 | 63 | ''' |
07f9b178 | 64 | |
f1dec720 SW |
65 | def testDirectTexifier(self) -> None: |
66 | texifier = VerifyingTexifier( | |
67 | PandocTexifier('pandoc'), DirectTexifier()) | |
38621839 | 68 | buf = io.BytesIO() |
8be20b9d SW |
69 | spec = Spec( |
70 | self.url(), | |
71 | self.fetcher(), | |
4b7d905e | 72 | FakeImageStore(), |
8be20b9d SW |
73 | lambda x: x, |
74 | lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), | |
d2a41ff4 | 75 | ContentOnlyLayout(texifier), |
e10b5b6f | 76 | None, |
8be20b9d | 77 | buf) |
38621839 | 78 | paperdoorknob.process(spec) |
f1dec720 | 79 | |
07f9b178 | 80 | def testPDF(self) -> None: |
38621839 SW |
81 | with open("test.tex", 'wb') as out: |
82 | spec = Spec( | |
83 | self.url(), | |
84 | self.fetcher(), | |
4b7d905e | 85 | FakeImageStore(), |
929db576 | 86 | lambda x: x, |
8be20b9d | 87 | lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), |
c62e8d40 | 88 | BelowIconLayout(PandocTexifier('pandoc'), 20), |
e10b5b6f | 89 | None, |
38621839 SW |
90 | out) |
91 | paperdoorknob.process(spec) | |
23f31879 SW |
92 | subprocess.run(['pdflatex', 'test.tex'], |
93 | stdin=subprocess.DEVNULL, check=True) | |
36ae1d5f | 94 | |
b25a2f90 | 95 | |
38621839 SW |
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 | ||
b25a2f90 SW |
120 | if __name__ == '__main__': |
121 | unittest.main() |