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