]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
baf79457a78f24499cc4f920dca4fe4576221a3d
[paperdoorknob] / paperdoorknob_test.py
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
8 import unittest
9 import io
10 import subprocess
11
12 import paperdoorknob
13
14 from testing.fakeserver import FakeGlowficServer
15 from fetch import DirectFetcher
16 from spec import Spec
17 from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
18
19 TIMEOUT = 8
20
21
22 class TestPaperDoorknob(unittest.TestCase):
23 def setUp(self) -> None:
24 self._server = self.enterContext(FakeGlowficServer())
25 self._port = self._server.port()
26
27 def testReplies(self) -> None:
28 with DirectFetcher(TIMEOUT) as f:
29 replies = paperdoorknob.replies(
30 paperdoorknob.clean(
31 paperdoorknob.parse(
32 f.fetch(f"http://localhost:{self._port}"))))
33 self.assertEqual([r.text.strip() for r in replies],
34 ["This is glowfic", "You sure?", "Pretty sure."])
35
36 def testProcess(self) -> None:
37 with DirectFetcher(TIMEOUT) as f:
38 buf = io.BytesIO()
39 spec = Spec(
40 f"http://localhost:{self._port}",
41 f,
42 PandocTexifier('pandoc'),
43 buf)
44 paperdoorknob.process(spec)
45 self.assertEqual(buf.getvalue(), b'''\\documentclass{article}
46 \\begin{document}
47 This is glowfic
48 You \\emph{sure}?
49 Pretty sure.
50 \\end{document}
51 ''')
52
53 def testDirectTexifier(self) -> None:
54 texifier = VerifyingTexifier(
55 PandocTexifier('pandoc'), DirectTexifier())
56 with DirectFetcher(TIMEOUT) as f:
57 buf = io.BytesIO()
58 spec = Spec(f"http://localhost:{self._port}", f, texifier, buf)
59 paperdoorknob.process(spec)
60
61 def testPDF(self) -> None:
62 with DirectFetcher(TIMEOUT) as f:
63 with open("test.tex", 'wb') as out:
64 spec = Spec(
65 f"http://localhost:{self._port}",
66 f,
67 PandocTexifier('pandoc'),
68 out)
69 paperdoorknob.process(spec)
70 subprocess.run(['pdflatex', 'test.tex'],
71 stdin=subprocess.DEVNULL, check=True)
72
73
74 if __name__ == '__main__':
75 unittest.main()