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