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