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