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