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