From: Simon Tatham Date: Thu, 14 Dec 2023 18:20:24 +0000 (+0000) Subject: Tests of Ctrl-K. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=5da4b4eb00af880361b0d004f5f0bd32224c5ef3;p=mastodonochrome.git Tests of Ctrl-K. So I can refactor it. --- diff --git a/cursesclient.py b/cursesclient.py index ad4a566..7c7d45b 100644 --- a/cursesclient.py +++ b/cursesclient.py @@ -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")