]> git.scottworley.com Git - paperdoorknob/blobdiff - glowfic_test.py
Test emit()
[paperdoorknob] / glowfic_test.py
index 68debf91cc82a719bec4f3f2ed391db6dc25f5e6..befd0b57a584d6a18624dbd48b7352b17dafa053 100644 (file)
@@ -4,10 +4,12 @@
 # under the terms of the GNU General Public License as published by the
 # Free Software Foundation, version 3.
 
-
+from io import BytesIO
 from sys import stderr
 import unittest
 
+from typing import Optional
+
 from fetch import FakeFetcher
 from images import FakeImageStore
 from glowfic import makeChunk, Thread
@@ -15,9 +17,10 @@ from spec import Spec
 from texify import PandocTexifier
 
 
-def spec_for_testing(html: bytes) -> Spec:
-    return Spec('test',
-                FakeFetcher({'test': html, 'test?view=flat': html}),
+def spec_for_testing(html: bytes, outbuf: Optional[BytesIO] = None) -> Spec:
+    return Spec('https://fake/test',
+                FakeFetcher({'https://fake/test': html,
+                             'https://fake/test?view=flat': html}),
                 FakeImageStore(),
                 lambda x: x,
                 lambda x: None,
@@ -26,7 +29,7 @@ def spec_for_testing(html: bytes) -> Spec:
                 20,
                 b'',
                 None,
-                stderr.buffer,
+                stderr.buffer if outbuf is None else outbuf,
                 lambda x: None)
 
 
@@ -87,6 +90,21 @@ class TestMakeChunk(unittest.TestCase):
         self.assertEqual(
             PandocTexifier("pandoc").texify(c.character), b'{Keltham}\n')
 
+    def testMinimal(self) -> None:
+        t = Thread(spec_for_testing(b'''
+            <html><body>
+              <div class="post-container post-post">
+                <div class="post-content">Just content</div>
+              </div>
+            </body></html>'''))
+        c = makeChunk(next(iter(t.chunkDOMs())), FakeImageStore())
+        self.assertIsNone(c.icon)
+        self.assertIsNone(c.character)
+        self.assertIsNone(c.screen_name)
+        self.assertIsNone(c.author)
+        self.assertEqual(str(c.content),
+                         '<div class="post-content">Just content</div>')
+
 
 class TestThread(unittest.TestCase):
 
@@ -102,7 +120,7 @@ class TestThread(unittest.TestCase):
             </body></html>'''))
         self.assertEqual(t.title(), 'Teh Story!')
 
-    def testNextThread(self) -> None:
+    def testNextThreadRelative(self) -> None:
         t = Thread(spec_for_testing(b'''
             <html><body>
               <div class="post-navheader">
@@ -112,7 +130,39 @@ class TestThread(unittest.TestCase):
               </div>
               <div class="post-container post-post">The "post"</div>
             </body></html>'''))
-        self.assertEqual(t.next_thread(), '/posts/4567')
+        self.assertEqual(t.next_thread(), 'https://fake/posts/4567')
+
+    def testNextThreadAbsolute(self) -> None:
+        t = Thread(spec_for_testing(b'''
+            <html><body>
+              <div class="post-navheader">
+                <a class="view-button-link" href="https://elsewhere/posts/4567"><div class="view-button">Next Post &raquo;</div>
+                </a><a class="view-button-link" href="https://elsewhere/posts/4321"><div class="view-button float-none">&laquo; Previous Post</div>
+                </a>
+              </div>
+              <div class="post-container post-post">The "post"</div>
+            </body></html>'''))
+        self.assertEqual(t.next_thread(), 'https://elsewhere/posts/4567')
+
+
+class TestEmit(unittest.TestCase):
+
+    def testEmit(self) -> None:
+        buf = BytesIO()
+        Thread(spec_for_testing(b'''
+            <html><body>
+              <div class="post-container post-post">
+                <div class="post-content">A</div>
+              </div>
+              <div class="flat-post-replies">
+                <div class="post-container post-reply">
+                  <div class="post-content">B</div>
+                </div>
+              </div>
+            </body></html>''', buf)).emit()
+        self.assertEqual(buf.getvalue(), rb'''\glowhead{}{}{}{}A
+\glowhead{}{}{}{}B
+''')
 
 
 if __name__ == '__main__':