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