]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
Support strikethrough
[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),
131deef1 45 lambda x: x,
d2a41ff4 46 ContentOnlyLayout(PandocTexifier('pandoc')),
e10b5b6f 47 'margin=20mm',
38621839
SW
48 buf)
49 paperdoorknob.process(spec)
4aa87092
SW
50 assert re.match(br'''\\documentclass{article}
51(\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry}
07f9b178
SW
52\\begin{document}
53This is glowfic
85bcacb0 54
4aa87092 55You \\emph{sure}\?
85bcacb0 56
07f9b178 57Pretty sure.
23dabdf5 58
07f9b178 59\\end{document}
4aa87092 60''', buf.getvalue())
07f9b178 61
f1dec720
SW
62 def testDirectTexifier(self) -> None:
63 texifier = VerifyingTexifier(
64 PandocTexifier('pandoc'), DirectTexifier())
38621839 65 buf = io.BytesIO()
8be20b9d
SW
66 spec = Spec(
67 self.url(),
68 self.fetcher(),
4b7d905e 69 FakeImageStore(),
8be20b9d
SW
70 lambda x: x,
71 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
131deef1 72 lambda x: x,
d2a41ff4 73 ContentOnlyLayout(texifier),
e10b5b6f 74 None,
8be20b9d 75 buf)
38621839 76 paperdoorknob.process(spec)
f1dec720 77
07f9b178 78 def testPDF(self) -> None:
38621839
SW
79 with open("test.tex", 'wb') as out:
80 spec = Spec(
81 self.url(),
82 self.fetcher(),
4b7d905e 83 FakeImageStore(),
929db576 84 lambda x: x,
8be20b9d 85 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
131deef1 86 lambda x: x,
c62e8d40 87 BelowIconLayout(PandocTexifier('pandoc'), 20),
e10b5b6f 88 None,
38621839
SW
89 out)
90 paperdoorknob.process(spec)
23f31879
SW
91 subprocess.run(['pdflatex', 'test.tex'],
92 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 93
b25a2f90 94
38621839
SW
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:
1452f8d3 116 return FakeFetcher({'fic?view=flat': f.read(9999)})
38621839
SW
117
118
b25a2f90
SW
119if __name__ == '__main__':
120 unittest.main()