]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
New dependency: wrapfig TeX package
[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
07f9b178 11import subprocess
23f31879 12
b25a2f90 13import paperdoorknob
23f31879 14
41b11505 15from testing.fakeserver import FakeGlowficServer
8be20b9d 16from domfilter import ApplyDOMFilters
38621839 17from fetch import DirectFetcher, FakeFetcher, Fetcher
4b7d905e 18from images import FakeImageStore
23f31879 19from spec import Spec
f1dec720 20from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
b25a2f90 21
b25a2f90
SW
22TIMEOUT = 8
23
24
38621839
SW
25class BaseTestProcess(ABC):
26
27 @abstractmethod
28 def url(self) -> str:
29 raise NotImplementedError()
30
31 @abstractmethod
32 def fetcher(self) -> Fetcher:
33 raise NotImplementedError()
b25a2f90 34
36ae1d5f 35 def testProcess(self) -> None:
38621839
SW
36 buf = io.BytesIO()
37 spec = Spec(
38 self.url(),
39 self.fetcher(),
4b7d905e 40 FakeImageStore(),
929db576 41 lambda x: x,
8be20b9d 42 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
38621839 43 PandocTexifier('pandoc'),
e10b5b6f 44 'margin=20mm',
38621839
SW
45 buf)
46 paperdoorknob.process(spec)
47 assert buf.getvalue() == b'''\\documentclass{article}
8d583d5b 48\\usepackage{wrapfig}
e10b5b6f 49\\usepackage[margin=20mm]{geometry}
07f9b178
SW
50\\begin{document}
51This is glowfic
52You \\emph{sure}?
53Pretty sure.
54\\end{document}
38621839 55'''
07f9b178 56
f1dec720
SW
57 def testDirectTexifier(self) -> None:
58 texifier = VerifyingTexifier(
59 PandocTexifier('pandoc'), DirectTexifier())
38621839 60 buf = io.BytesIO()
8be20b9d
SW
61 spec = Spec(
62 self.url(),
63 self.fetcher(),
4b7d905e 64 FakeImageStore(),
8be20b9d
SW
65 lambda x: x,
66 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
67 texifier,
e10b5b6f 68 None,
8be20b9d 69 buf)
38621839 70 paperdoorknob.process(spec)
f1dec720 71
07f9b178 72 def testPDF(self) -> None:
38621839
SW
73 with open("test.tex", 'wb') as out:
74 spec = Spec(
75 self.url(),
76 self.fetcher(),
4b7d905e 77 FakeImageStore(),
929db576 78 lambda x: x,
8be20b9d 79 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
38621839 80 PandocTexifier('pandoc'),
e10b5b6f 81 None,
38621839
SW
82 out)
83 paperdoorknob.process(spec)
23f31879
SW
84 subprocess.run(['pdflatex', 'test.tex'],
85 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 86
b25a2f90 87
38621839
SW
88class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
89
90 def setUp(self) -> None:
91 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
92 self._server = self.enterContext(FakeGlowficServer())
93 self._port = self._server.port()
94
95 def url(self) -> str:
96 return f"http://localhost:{self._port}"
97
98 def fetcher(self) -> Fetcher:
99 return self._fetcher
100
101
102class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
103
104 def url(self) -> str:
105 return 'fic'
106
107 def fetcher(self) -> Fetcher:
108 with open('testdata/this-is-glowfic.html', 'rb') as f:
109 return FakeFetcher({'fic': f.read(9999)})
110
111
b25a2f90
SW
112if __name__ == '__main__':
113 unittest.main()