]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
More structure and tests around splitting the page into chunks' DOMs.
[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
cfd0b634 18from images import ImageStore
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(),
cfd0b634 40 ImageStore('is', self.fetcher()),
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}
e10b5b6f 48\\usepackage[margin=20mm]{geometry}
07f9b178
SW
49\\begin{document}
50This is glowfic
51You \\emph{sure}?
52Pretty sure.
53\\end{document}
38621839 54'''
07f9b178 55
f1dec720
SW
56 def testDirectTexifier(self) -> None:
57 texifier = VerifyingTexifier(
58 PandocTexifier('pandoc'), DirectTexifier())
38621839 59 buf = io.BytesIO()
8be20b9d
SW
60 spec = Spec(
61 self.url(),
62 self.fetcher(),
cfd0b634 63 ImageStore('is', self.fetcher()),
8be20b9d
SW
64 lambda x: x,
65 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
66 texifier,
e10b5b6f 67 None,
8be20b9d 68 buf)
38621839 69 paperdoorknob.process(spec)
f1dec720 70
07f9b178 71 def testPDF(self) -> None:
38621839
SW
72 with open("test.tex", 'wb') as out:
73 spec = Spec(
74 self.url(),
75 self.fetcher(),
cfd0b634 76 ImageStore('is', self.fetcher()),
929db576 77 lambda x: x,
8be20b9d 78 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
38621839 79 PandocTexifier('pandoc'),
e10b5b6f 80 None,
38621839
SW
81 out)
82 paperdoorknob.process(spec)
23f31879
SW
83 subprocess.run(['pdflatex', 'test.tex'],
84 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 85
b25a2f90 86
38621839
SW
87class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
88
89 def setUp(self) -> None:
90 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
91 self._server = self.enterContext(FakeGlowficServer())
92 self._port = self._server.port()
93
94 def url(self) -> str:
95 return f"http://localhost:{self._port}"
96
97 def fetcher(self) -> Fetcher:
98 return self._fetcher
99
100
101class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
102
103 def url(self) -> str:
104 return 'fic'
105
106 def fetcher(self) -> Fetcher:
107 with open('testdata/this-is-glowfic.html', 'rb') as f:
108 return FakeFetcher({'fic': f.read(9999)})
109
110
b25a2f90
SW
111if __name__ == '__main__':
112 unittest.main()