chiark / gitweb /
Tests of Ctrl-K.
authorSimon Tatham <anakin@pobox.com>
Thu, 14 Dec 2023 18:20:24 +0000 (18:20 +0000)
committerSimon Tatham <anakin@pobox.com>
Thu, 14 Dec 2023 18:20:24 +0000 (18:20 +0000)
So I can refactor it.

cursesclient.py

index ad4a56661ac167b517f5958b7891672f9aadfa59..7c7d45b32736c618b0487612af94d588c69afb26 100644 (file)
@@ -1093,6 +1093,13 @@ class Composer(Activity, EditorCommon):
             reply_status = self.cc.get_status_by_id(reply_id)
             self.visibility = reply_status['visibility']
 
+    def layout(self, wrapwidth):
+        # Layout during rendering, abstracted into its own function
+        # for standalone testing.
+        self.dtext = self.DisplayText(self.text)
+        self.dtext.layout(wrapwidth, self.char_limit, self.url_cost)
+        self.cy, self.cx = self.dtext.yx[self.point]
+
     def render(self):
         y = 0
 
@@ -1116,16 +1123,13 @@ class Composer(Activity, EditorCommon):
             self.cc.print_at(y, 0, line)
             y += 1
 
-        self.dtext = self.DisplayText(self.text)
-        self.dtext.layout(self.cc.scr_w - 1, self.char_limit, self.url_cost)
+        self.layout(self.cc.scr_w - 1)
 
         ytop = y
-
         for line in self.dtext.lines:
             self.cc.print_at(y, 0, line)
             y += 1
 
-        self.cy, self.cx = self.dtext.yx[self.point]
         self.cc.move_cursor_to(self.cy + ytop, self.cx)
 
     def word_boundary(self, pos):
@@ -1361,3 +1365,40 @@ class testComposerLayout(unittest.TestCase):
         self.assertEqual(t.yx, ([(0,i) for i in range(8)] +
                                 [(1,i) for i in range(2)]))
 
+    def testCtrlY(self):
+        def setup(point):
+            class FakeCC:
+                def get(self, str): return {}
+
+            t = Composer(FakeCC(), "abc def ghi jkl\nmno pqr stu vwx")
+            t.point = point
+            t.layout(10)
+            self.assertEqual(t.dtext.lines, [text.ColouredString("abc def "),
+                                             text.ColouredString("ghi jkl"),
+                                             text.ColouredString("mno pqr "),
+                                             text.ColouredString("stu vwx")])
+            return t
+
+        # On the last line of a paragraph: delete up to the next
+        # newline, but not including it
+        t = setup(12)
+        t.handle_key(ctrl('k'))
+        self.assertEqual(t.text, "abc def ghi \nmno pqr stu vwx")
+
+        # Same, but the paragraph in question is at the end of the
+        # buffer and has no trailing newline. Do the same, without
+        # tripping over the newline
+        t = setup(28)
+        t.handle_key(ctrl('k'))
+        self.assertEqual(t.text, "abc def ghi jkl\nmno pqr stu ")
+
+        # Sitting on a newline: delete just the newline itself
+        t = setup(15)
+        t.handle_key(ctrl('k'))
+        self.assertEqual(t.text, "abc def ghi jklmno pqr stu vwx")
+
+        # At the very end of the buffer: do nothing, including not
+        # crashing
+        t = setup(31)
+        t.handle_key(ctrl('k'))
+        self.assertEqual(t.text, "abc def ghi jkl\nmno pqr stu vwx")