]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
Use LaTeX packages: longtable and booktabs
[paperdoorknob] / paperdoorknob_test.py
CommitLineData
b25a2f90
SW
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
38621839 8from abc import ABC, abstractmethod
b25a2f90 9import unittest
36ae1d5f 10import io
4aa87092 11import re
07f9b178 12import subprocess
23f31879 13
b25a2f90 14import paperdoorknob
23f31879 15
41b11505 16from testing.fakeserver import FakeGlowficServer
8be20b9d 17from domfilter import ApplyDOMFilters
38621839 18from fetch import DirectFetcher, FakeFetcher, Fetcher
d2a41ff4 19from glowfic import ContentOnlyLayout, BelowIconLayout
4b7d905e 20from images import FakeImageStore
23f31879 21from spec import Spec
f1dec720 22from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
b25a2f90 23
b25a2f90
SW
24TIMEOUT = 8
25
26
38621839
SW
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()
b25a2f90 36
36ae1d5f 37 def testProcess(self) -> None:
38621839
SW
38 buf = io.BytesIO()
39 spec = Spec(
40 self.url(),
41 self.fetcher(),
4b7d905e 42 FakeImageStore(),
929db576 43 lambda x: x,
8be20b9d 44 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
d2a41ff4 45 ContentOnlyLayout(PandocTexifier('pandoc')),
e10b5b6f 46 'margin=20mm',
38621839
SW
47 buf)
48 paperdoorknob.process(spec)
4aa87092
SW
49 assert re.match(br'''\\documentclass{article}
50(\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry}
07f9b178
SW
51\\begin{document}
52This is glowfic
85bcacb0 53
4aa87092 54You \\emph{sure}\?
85bcacb0 55
07f9b178 56Pretty sure.
23dabdf5 57
07f9b178 58\\end{document}
4aa87092 59''', buf.getvalue())
07f9b178 60
f1dec720
SW
61 def testDirectTexifier(self) -> None:
62 texifier = VerifyingTexifier(
63 PandocTexifier('pandoc'), DirectTexifier())
38621839 64 buf = io.BytesIO()
8be20b9d
SW
65 spec = Spec(
66 self.url(),
67 self.fetcher(),
4b7d905e 68 FakeImageStore(),
8be20b9d
SW
69 lambda x: x,
70 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
d2a41ff4 71 ContentOnlyLayout(texifier),
e10b5b6f 72 None,
8be20b9d 73 buf)
38621839 74 paperdoorknob.process(spec)
f1dec720 75
07f9b178 76 def testPDF(self) -> None:
38621839
SW
77 with open("test.tex", 'wb') as out:
78 spec = Spec(
79 self.url(),
80 self.fetcher(),
4b7d905e 81 FakeImageStore(),
929db576 82 lambda x: x,
8be20b9d 83 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
c62e8d40 84 BelowIconLayout(PandocTexifier('pandoc'), 20),
e10b5b6f 85 None,
38621839
SW
86 out)
87 paperdoorknob.process(spec)
23f31879
SW
88 subprocess.run(['pdflatex', 'test.tex'],
89 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 90
b25a2f90 91
38621839
SW
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:
1452f8d3 113 return FakeFetcher({'fic?view=flat': f.read(9999)})
38621839
SW
114
115
b25a2f90
SW
116if __name__ == '__main__':
117 unittest.main()