]> git.scottworley.com Git - paperdoorknob/blame_incremental - paperdoorknob_test.py
Support strikethrough
[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 lambda x: x,
46 ContentOnlyLayout(PandocTexifier('pandoc')),
47 'margin=20mm',
48 buf)
49 paperdoorknob.process(spec)
50 assert re.match(br'''\\documentclass{article}
51(\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry}
52\\begin{document}
53This is glowfic
54
55You \\emph{sure}\?
56
57Pretty sure.
58
59\\end{document}
60''', buf.getvalue())
61
62 def testDirectTexifier(self) -> None:
63 texifier = VerifyingTexifier(
64 PandocTexifier('pandoc'), DirectTexifier())
65 buf = io.BytesIO()
66 spec = Spec(
67 self.url(),
68 self.fetcher(),
69 FakeImageStore(),
70 lambda x: x,
71 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
72 lambda x: 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 lambda x: x,
87 BelowIconLayout(PandocTexifier('pandoc'), 20),
88 None,
89 out)
90 paperdoorknob.process(spec)
91 subprocess.run(['pdflatex', 'test.tex'],
92 stdin=subprocess.DEVNULL, check=True)
93
94
95class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
96
97 def setUp(self) -> None:
98 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
99 self._server = self.enterContext(FakeGlowficServer())
100 self._port = self._server.port()
101
102 def url(self) -> str:
103 return f"http://localhost:{self._port}"
104
105 def fetcher(self) -> Fetcher:
106 return self._fetcher
107
108
109class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
110
111 def url(self) -> str:
112 return 'fic'
113
114 def fetcher(self) -> Fetcher:
115 with open('testdata/this-is-glowfic.html', 'rb') as f:
116 return FakeFetcher({'fic?view=flat': f.read(9999)})
117
118
119if __name__ == '__main__':
120 unittest.main()