]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
Learning TeX: Do Layouts with TeX macros
[paperdoorknob] / paperdoorknob_test.py
CommitLineData
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 8from abc import ABC, abstractmethod
b25a2f90 9import unittest
36ae1d5f 10import io
4aa87092 11import re
07f9b178 12import subprocess
23f31879 13
b25a2f90 14import paperdoorknob
23f31879 15
41b11505 16from testing.fakeserver import FakeGlowficServer
38621839 17from fetch import DirectFetcher, FakeFetcher, Fetcher
37221176 18from glowfic import ContentOnlyLayout, BesideIconLayout
4b7d905e 19from images import FakeImageStore
23f31879 20from spec import Spec
f1dec720 21from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
b25a2f90 22
b25a2f90
SW
23TIMEOUT = 8
24
25
38621839
SW
26class 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,
1fac41bf 48 ContentOnlyLayout,
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)*
1fac41bf
SW
57\\glowhead{}{}{}{}This is \\href{https://glowfic.com}{glowfic}
58\\glowhead{}{}{}{}You \\emph{sure}\?
59\\glowhead{}{}{}{}Pretty sure.
07f9b178 60\\end{document}
4aa87092 61''', buf.getvalue())
07f9b178 62
f1dec720
SW
63 def testDirectTexifier(self) -> None:
64 texifier = VerifyingTexifier(
65 PandocTexifier('pandoc'), DirectTexifier())
38621839 66 buf = io.BytesIO()
8be20b9d
SW
67 spec = Spec(
68 self.url(),
69 self.fetcher(),
4b7d905e 70 FakeImageStore(),
8be20b9d 71 lambda x: x,
4640c55a 72 lambda x: None,
bbfe8e52 73 texifier,
131deef1 74 lambda x: x,
9afdb32a 75 20,
1fac41bf 76 ContentOnlyLayout,
e10b5b6f 77 None,
c594c8bf
SW
78 buf,
79 lambda _: None)
38621839 80 paperdoorknob.process(spec)
f1dec720 81
07f9b178 82 def testPDF(self) -> None:
bbfe8e52 83 texifier = PandocTexifier('pandoc')
38621839
SW
84 with open("test.tex", 'wb') as out:
85 spec = Spec(
86 self.url(),
87 self.fetcher(),
4b7d905e 88 FakeImageStore(),
929db576 89 lambda x: x,
4640c55a 90 lambda x: None,
bbfe8e52 91 texifier,
131deef1 92 lambda x: x,
9afdb32a 93 20,
1fac41bf 94 BesideIconLayout,
e10b5b6f 95 None,
c594c8bf
SW
96 out,
97 lambda _: None)
38621839 98 paperdoorknob.process(spec)
23f31879
SW
99 subprocess.run(['pdflatex', 'test.tex'],
100 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 101
b25a2f90 102
38621839
SW
103class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
104
105 def setUp(self) -> None:
106 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
107 self._server = self.enterContext(FakeGlowficServer())
108 self._port = self._server.port()
109
110 def url(self) -> str:
111 return f"http://localhost:{self._port}"
112
113 def fetcher(self) -> Fetcher:
114 return self._fetcher
115
116
117class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
118
119 def url(self) -> str:
120 return 'fic'
121
122 def fetcher(self) -> Fetcher:
123 with open('testdata/this-is-glowfic.html', 'rb') as f:
1452f8d3 124 return FakeFetcher({'fic?view=flat': f.read(9999)})
38621839
SW
125
126
b25a2f90
SW
127if __name__ == '__main__':
128 unittest.main()