]> git.scottworley.com Git - paperdoorknob/blame_incremental - paperdoorknob_test.py
Less space between icon and post-separation line
[paperdoorknob] / paperdoorknob_test.py
... / ...
CommitLineData
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
8from abc import ABC, abstractmethod
9import unittest
10import io
11import subprocess
12
13import paperdoorknob
14
15from testing.fakeserver import FakeGlowficServer
16from domfilter import ApplyDOMFilters
17from fetch import DirectFetcher, FakeFetcher, Fetcher
18from glowfic import ContentOnlyLayout, BelowIconLayout
19from images import FakeImageStore
20from spec import Spec
21from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
22
23TIMEOUT = 8
24
25
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()
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{wrapstuff}
51\\usepackage[margin=20mm]{geometry}
52\\begin{document}
53This is glowfic
54
55\\wrapstuffclear\\vspace{-.5\\ht\\strutbox}\\noindent\\hrulefill
56
57You \\emph{sure}?
58
59\\wrapstuffclear\\vspace{-.5\\ht\\strutbox}\\noindent\\hrulefill
60
61Pretty sure.
62\\end{document}
63'''
64
65 def testDirectTexifier(self) -> None:
66 texifier = VerifyingTexifier(
67 PandocTexifier('pandoc'), DirectTexifier())
68 buf = io.BytesIO()
69 spec = Spec(
70 self.url(),
71 self.fetcher(),
72 FakeImageStore(),
73 lambda x: x,
74 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
75 ContentOnlyLayout(texifier),
76 None,
77 buf)
78 paperdoorknob.process(spec)
79
80 def testPDF(self) -> None:
81 with open("test.tex", 'wb') as out:
82 spec = Spec(
83 self.url(),
84 self.fetcher(),
85 FakeImageStore(),
86 lambda x: x,
87 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
88 BelowIconLayout(PandocTexifier('pandoc'), 20),
89 None,
90 out)
91 paperdoorknob.process(spec)
92 subprocess.run(['pdflatex', 'test.tex'],
93 stdin=subprocess.DEVNULL, check=True)
94
95
96class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
97
98 def setUp(self) -> None:
99 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
100 self._server = self.enterContext(FakeGlowficServer())
101 self._port = self._server.port()
102
103 def url(self) -> str:
104 return f"http://localhost:{self._port}"
105
106 def fetcher(self) -> Fetcher:
107 return self._fetcher
108
109
110class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
111
112 def url(self) -> str:
113 return 'fic'
114
115 def fetcher(self) -> Fetcher:
116 with open('testdata/this-is-glowfic.html', 'rb') as f:
117 return FakeFetcher({'fic': f.read(9999)})
118
119
120if __name__ == '__main__':
121 unittest.main()