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