]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
New dependency: wrapfig TeX package
[paperdoorknob] / paperdoorknob_test.py
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
8 from abc import ABC, abstractmethod
9 import unittest
10 import io
11 import subprocess
12
13 import paperdoorknob
14
15 from testing.fakeserver import FakeGlowficServer
16 from domfilter import ApplyDOMFilters
17 from fetch import DirectFetcher, FakeFetcher, Fetcher
18 from images import FakeImageStore
19 from spec import Spec
20 from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
21
22 TIMEOUT = 8
23
24
25 class BaseTestProcess(ABC):
26
27 @abstractmethod
28 def url(self) -> str:
29 raise NotImplementedError()
30
31 @abstractmethod
32 def fetcher(self) -> Fetcher:
33 raise NotImplementedError()
34
35 def testProcess(self) -> None:
36 buf = io.BytesIO()
37 spec = Spec(
38 self.url(),
39 self.fetcher(),
40 FakeImageStore(),
41 lambda x: x,
42 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
43 PandocTexifier('pandoc'),
44 'margin=20mm',
45 buf)
46 paperdoorknob.process(spec)
47 assert buf.getvalue() == b'''\\documentclass{article}
48 \\usepackage{wrapfig}
49 \\usepackage[margin=20mm]{geometry}
50 \\begin{document}
51 This is glowfic
52 You \\emph{sure}?
53 Pretty sure.
54 \\end{document}
55 '''
56
57 def testDirectTexifier(self) -> None:
58 texifier = VerifyingTexifier(
59 PandocTexifier('pandoc'), DirectTexifier())
60 buf = io.BytesIO()
61 spec = Spec(
62 self.url(),
63 self.fetcher(),
64 FakeImageStore(),
65 lambda x: x,
66 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
67 texifier,
68 None,
69 buf)
70 paperdoorknob.process(spec)
71
72 def testPDF(self) -> None:
73 with open("test.tex", 'wb') as out:
74 spec = Spec(
75 self.url(),
76 self.fetcher(),
77 FakeImageStore(),
78 lambda x: x,
79 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
80 PandocTexifier('pandoc'),
81 None,
82 out)
83 paperdoorknob.process(spec)
84 subprocess.run(['pdflatex', 'test.tex'],
85 stdin=subprocess.DEVNULL, check=True)
86
87
88 class 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
102 class 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
112 if __name__ == '__main__':
113 unittest.main()