]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Strip all  
[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 from abc import ABC, abstractmethod
9 import unittest
10 import io
11 import subprocess
12
13 import paperdoorknob
14
15 from testing.fakeserver import FakeGlowficServer
16 from fetch import DirectFetcher, FakeFetcher, Fetcher
17 from spec import Spec
18 from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
19
20 TIMEOUT = 8
21
22
23 class BaseTestProcess(ABC):
24
25 @abstractmethod
26 def url(self) -> str:
27 raise NotImplementedError()
28
29 @abstractmethod
30 def fetcher(self) -> Fetcher:
31 raise NotImplementedError()
32
33 def testReplies(self) -> None:
34 replies = paperdoorknob.replies(
35 paperdoorknob.clean(
36 paperdoorknob.parse(
37 self.fetcher().fetch(
38 self.url()))))
39 assert [r.text.strip() for r in replies] == [
40 "This is glowfic",
41 "You sure?",
42 "Pretty sure."]
43
44 def testProcess(self) -> None:
45 buf = io.BytesIO()
46 spec = Spec(
47 self.url(),
48 self.fetcher(),
49 lambda x: x,
50 PandocTexifier('pandoc'),
51 buf)
52 paperdoorknob.process(spec)
53 assert buf.getvalue() == b'''\\documentclass{article}
54 \\begin{document}
55 This is glowfic
56 You \\emph{sure}?
57 Pretty sure.
58 \\end{document}
59 '''
60
61 def testDirectTexifier(self) -> None:
62 texifier = VerifyingTexifier(
63 PandocTexifier('pandoc'), DirectTexifier())
64 buf = io.BytesIO()
65 spec = Spec(self.url(), self.fetcher(), lambda x: x, texifier, buf)
66 paperdoorknob.process(spec)
67
68 def testPDF(self) -> None:
69 with open("test.tex", 'wb') as out:
70 spec = Spec(
71 self.url(),
72 self.fetcher(),
73 lambda x: x,
74 PandocTexifier('pandoc'),
75 out)
76 paperdoorknob.process(spec)
77 subprocess.run(['pdflatex', 'test.tex'],
78 stdin=subprocess.DEVNULL, check=True)
79
80
81 class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
82
83 def setUp(self) -> None:
84 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
85 self._server = self.enterContext(FakeGlowficServer())
86 self._port = self._server.port()
87
88 def url(self) -> str:
89 return f"http://localhost:{self._port}"
90
91 def fetcher(self) -> Fetcher:
92 return self._fetcher
93
94
95 class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
96
97 def url(self) -> str:
98 return 'fic'
99
100 def fetcher(self) -> Fetcher:
101 with open('testdata/this-is-glowfic.html', 'rb') as f:
102 return FakeFetcher({'fic': f.read(9999)})
103
104
105 if __name__ == '__main__':
106 unittest.main()