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