]>
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 |
4aa87092 | 11 | import re |
07f9b178 | 12 | import subprocess |
23f31879 | 13 | |
b25a2f90 | 14 | import paperdoorknob |
23f31879 | 15 | |
41b11505 | 16 | from testing.fakeserver import FakeGlowficServer |
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, |
4640c55a | 43 | lambda x: None, |
131deef1 | 44 | lambda x: x, |
d2a41ff4 | 45 | ContentOnlyLayout(PandocTexifier('pandoc')), |
e10b5b6f | 46 | 'margin=20mm', |
38621839 SW |
47 | buf) |
48 | paperdoorknob.process(spec) | |
4aa87092 SW |
49 | assert re.match(br'''\\documentclass{article} |
50 | (\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry} | |
07f9b178 SW |
51 | \\begin{document} |
52 | This is glowfic | |
85bcacb0 | 53 | |
4aa87092 | 54 | You \\emph{sure}\? |
85bcacb0 | 55 | |
07f9b178 | 56 | Pretty sure. |
23dabdf5 | 57 | |
07f9b178 | 58 | \\end{document} |
4aa87092 | 59 | ''', buf.getvalue()) |
07f9b178 | 60 | |
f1dec720 SW |
61 | def testDirectTexifier(self) -> None: |
62 | texifier = VerifyingTexifier( | |
63 | PandocTexifier('pandoc'), DirectTexifier()) | |
38621839 | 64 | buf = io.BytesIO() |
8be20b9d SW |
65 | spec = Spec( |
66 | self.url(), | |
67 | self.fetcher(), | |
4b7d905e | 68 | FakeImageStore(), |
8be20b9d | 69 | lambda x: x, |
4640c55a | 70 | lambda x: None, |
131deef1 | 71 | lambda x: x, |
d2a41ff4 | 72 | ContentOnlyLayout(texifier), |
e10b5b6f | 73 | None, |
8be20b9d | 74 | buf) |
38621839 | 75 | paperdoorknob.process(spec) |
f1dec720 | 76 | |
07f9b178 | 77 | def testPDF(self) -> None: |
38621839 SW |
78 | with open("test.tex", 'wb') as out: |
79 | spec = Spec( | |
80 | self.url(), | |
81 | self.fetcher(), | |
4b7d905e | 82 | FakeImageStore(), |
929db576 | 83 | lambda x: x, |
4640c55a | 84 | lambda x: None, |
131deef1 | 85 | lambda x: x, |
c62e8d40 | 86 | BelowIconLayout(PandocTexifier('pandoc'), 20), |
e10b5b6f | 87 | None, |
38621839 SW |
88 | out) |
89 | paperdoorknob.process(spec) | |
23f31879 SW |
90 | subprocess.run(['pdflatex', 'test.tex'], |
91 | stdin=subprocess.DEVNULL, check=True) | |
36ae1d5f | 92 | |
b25a2f90 | 93 | |
38621839 SW |
94 | class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase): |
95 | ||
96 | def setUp(self) -> None: | |
97 | self._fetcher = self.enterContext(DirectFetcher(TIMEOUT)) | |
98 | self._server = self.enterContext(FakeGlowficServer()) | |
99 | self._port = self._server.port() | |
100 | ||
101 | def url(self) -> str: | |
102 | return f"http://localhost:{self._port}" | |
103 | ||
104 | def fetcher(self) -> Fetcher: | |
105 | return self._fetcher | |
106 | ||
107 | ||
108 | class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase): | |
109 | ||
110 | def url(self) -> str: | |
111 | return 'fic' | |
112 | ||
113 | def fetcher(self) -> Fetcher: | |
114 | with open('testdata/this-is-glowfic.html', 'rb') as f: | |
1452f8d3 | 115 | return FakeFetcher({'fic?view=flat': f.read(9999)}) |
38621839 SW |
116 | |
117 | ||
b25a2f90 SW |
118 | if __name__ == '__main__': |
119 | unittest.main() |