]>
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 |
23f31879 | 18 | from spec import Spec |
f1dec720 | 19 | from texify import DirectTexifier, PandocTexifier, VerifyingTexifier |
b25a2f90 | 20 | |
b25a2f90 SW |
21 | TIMEOUT = 8 |
22 | ||
23 | ||
38621839 SW |
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() | |
b25a2f90 | 33 | |
a2d42468 | 34 | def testReplies(self) -> None: |
8be20b9d SW |
35 | replies = list(paperdoorknob.replies( |
36 | paperdoorknob.parse(self.fetcher().fetch(self.url())))) | |
37 | for r in replies: | |
38 | ApplyDOMFilters('NoEdit,NoFooter', r) | |
38621839 SW |
39 | assert [r.text.strip() for r in replies] == [ |
40 | "This is glowfic", | |
41 | "You sure?", | |
42 | "Pretty sure."] | |
136277e3 | 43 | |
36ae1d5f | 44 | def testProcess(self) -> None: |
38621839 SW |
45 | buf = io.BytesIO() |
46 | spec = Spec( | |
47 | self.url(), | |
48 | self.fetcher(), | |
929db576 | 49 | lambda x: x, |
8be20b9d | 50 | lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), |
38621839 SW |
51 | PandocTexifier('pandoc'), |
52 | buf) | |
53 | paperdoorknob.process(spec) | |
54 | assert buf.getvalue() == b'''\\documentclass{article} | |
07f9b178 SW |
55 | \\begin{document} |
56 | This is glowfic | |
57 | You \\emph{sure}? | |
58 | Pretty sure. | |
59 | \\end{document} | |
38621839 | 60 | ''' |
07f9b178 | 61 | |
f1dec720 SW |
62 | def testDirectTexifier(self) -> None: |
63 | texifier = VerifyingTexifier( | |
64 | PandocTexifier('pandoc'), DirectTexifier()) | |
38621839 | 65 | buf = io.BytesIO() |
8be20b9d SW |
66 | spec = Spec( |
67 | self.url(), | |
68 | self.fetcher(), | |
69 | lambda x: x, | |
70 | lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), | |
71 | texifier, | |
72 | buf) | |
38621839 | 73 | paperdoorknob.process(spec) |
f1dec720 | 74 | |
07f9b178 | 75 | def testPDF(self) -> None: |
38621839 SW |
76 | with open("test.tex", 'wb') as out: |
77 | spec = Spec( | |
78 | self.url(), | |
79 | self.fetcher(), | |
929db576 | 80 | lambda x: x, |
8be20b9d | 81 | lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), |
38621839 SW |
82 | PandocTexifier('pandoc'), |
83 | out) | |
84 | paperdoorknob.process(spec) | |
23f31879 SW |
85 | subprocess.run(['pdflatex', 'test.tex'], |
86 | stdin=subprocess.DEVNULL, check=True) | |
36ae1d5f | 87 | |
b25a2f90 | 88 | |
38621839 SW |
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 | ||
b25a2f90 SW |
113 | if __name__ == '__main__': |
114 | unittest.main() |