]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
ImageStore
[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
07f9b178 11import subprocess
23f31879 12
b25a2f90 13import paperdoorknob
23f31879 14
41b11505 15from testing.fakeserver import FakeGlowficServer
8be20b9d 16from domfilter import ApplyDOMFilters
38621839 17from fetch import DirectFetcher, FakeFetcher, Fetcher
23f31879 18from spec import Spec
f1dec720 19from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
b25a2f90 20
b25a2f90
SW
21TIMEOUT = 8
22
23
38621839
SW
24class BaseTestProcess(ABC):
25
26 @abstractmethod
27 def url(self) -> str:
28 raise NotImplementedError()
29
30 @abstractmethod
31 def fetcher(self) -> Fetcher:
32 raise NotImplementedError()
b25a2f90 33
a2d42468 34 def testReplies(self) -> None:
8be20b9d
SW
35 replies = list(paperdoorknob.replies(
36 paperdoorknob.parse(self.fetcher().fetch(self.url()))))
37 for r in replies:
38 ApplyDOMFilters('NoEdit,NoFooter', r)
38621839
SW
39 assert [r.text.strip() for r in replies] == [
40 "This is glowfic",
41 "You sure?",
42 "Pretty sure."]
136277e3 43
36ae1d5f 44 def testProcess(self) -> None:
38621839
SW
45 buf = io.BytesIO()
46 spec = Spec(
47 self.url(),
48 self.fetcher(),
929db576 49 lambda x: x,
8be20b9d 50 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
38621839 51 PandocTexifier('pandoc'),
e10b5b6f 52 'margin=20mm',
38621839
SW
53 buf)
54 paperdoorknob.process(spec)
55 assert buf.getvalue() == b'''\\documentclass{article}
e10b5b6f 56\\usepackage[margin=20mm]{geometry}
07f9b178
SW
57\\begin{document}
58This is glowfic
59You \\emph{sure}?
60Pretty sure.
61\\end{document}
38621839 62'''
07f9b178 63
f1dec720
SW
64 def testDirectTexifier(self) -> None:
65 texifier = VerifyingTexifier(
66 PandocTexifier('pandoc'), DirectTexifier())
38621839 67 buf = io.BytesIO()
8be20b9d
SW
68 spec = Spec(
69 self.url(),
70 self.fetcher(),
71 lambda x: x,
72 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
73 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(),
929db576 83 lambda x: x,
8be20b9d 84 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
38621839 85 PandocTexifier('pandoc'),
e10b5b6f 86 None,
38621839
SW
87 out)
88 paperdoorknob.process(spec)
23f31879
SW
89 subprocess.run(['pdflatex', 'test.tex'],
90 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 91
b25a2f90 92
38621839
SW
93class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
94
95 def setUp(self) -> None:
96 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
97 self._server = self.enterContext(FakeGlowficServer())
98 self._port = self._server.port()
99
100 def url(self) -> str:
101 return f"http://localhost:{self._port}"
102
103 def fetcher(self) -> Fetcher:
104 return self._fetcher
105
106
107class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
108
109 def url(self) -> str:
110 return 'fic'
111
112 def fetcher(self) -> Fetcher:
113 with open('testdata/this-is-glowfic.html', 'rb') as f:
114 return FakeFetcher({'fic': f.read(9999)})
115
116
b25a2f90
SW
117if __name__ == '__main__':
118 unittest.main()