about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-06-02 18:52:49 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-06-02 18:52:49 -0700
commit51f4f13ecc39316c0115f617729ca699e13893e3 (patch)
treeca7b0b129927a57d18ee3523d2e0b74f58ee4dcc
parent283c4e58046410ca44bd5d5e7480d2d14e721976 (diff)
downloadlines.love-51f4f13ecc39316c0115f617729ca699e13893e3.tar.gz
test harness now supports copy/paste
-rw-r--r--app.lua10
-rw-r--r--text.lua71
2 files changed, 78 insertions, 3 deletions
diff --git a/app.lua b/app.lua
index ab22311..d7f3c72 100644
--- a/app.lua
+++ b/app.lua
@@ -178,6 +178,14 @@ function App.screen.draw(obj, x,y)
   end
 end
 
+App.clipboard = ''
+function App.getClipboardText()
+  return App.clipboard
+end
+function App.setClipboardText(s)
+  App.clipboard = s
+end
+
 function App.run_after_textinput(t)
   App.textinput(t)
   App.screen.contents = {}
@@ -259,4 +267,6 @@ function App.disable_tests()
   App.screen.draw = love.graphics.draw
   App.width = function(text) return text:getWidth() end
   App.open_for_writing = function(filename) return io.open(filename, 'w') end
+  App.getClipboardText = love.system.getClipboardText
+  App.setClipboardText = love.system.setClipboardText
 end
diff --git a/text.lua b/text.lua
index a502994..7cc89f5 100644
--- a/text.lua
+++ b/text.lua
@@ -322,6 +322,71 @@ function test_edit_wrapping_text()
   App.screen.check(y, 'ghij', 'F - test_edit_wrapping_text/screen:3')
 end
 
+function test_insert_newline()
+  io.write('\ntest_insert_newline')
+  -- display a few lines with cursor on bottom line
+  App.screen.init{width=25+30, height=60}
+  Lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Line_width = App.screen.width
+  Cursor1 = {line=1, pos=2}
+  Screen_top1 = {line=1, pos=1}
+  Screen_bottom1 = {}
+  Zoom = 1
+  local screen_top_margin = 15  -- pixels
+  local line_height = math.floor(15*Zoom)  -- pixels
+  App.draw()
+  local y = screen_top_margin
+  App.screen.check(y, 'abc', 'F - test_insert_newline/baseline/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'def', 'F - test_insert_newline/baseline/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'ghi', 'F - test_insert_newline/baseline/screen:3')
+  -- after hitting the enter key the screen scrolls down
+  App.run_after_keychord('return')
+  check_eq(Screen_top1.line, 1, 'F - test_insert_newline/screen_top')
+  check_eq(Cursor1.line, 2, 'F - test_insert_newline/cursor:line')
+  check_eq(Cursor1.pos, 1, 'F - test_insert_newline/cursor:pos')
+  y = screen_top_margin
+  App.screen.check(y, 'a', 'F - test_insert_newline/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'bc', 'F - test_insert_newline/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'def', 'F - test_insert_newline/screen:3')
+end
+
+function test_insert_from_clipboard()
+  io.write('\ntest_insert_from_clipboard')
+  -- display a few lines with cursor on bottom line
+  App.screen.init{width=25+30, height=60}
+  Lines = load_array{'abc', 'def', 'ghi', 'jkl'}
+  Line_width = App.screen.width
+  Cursor1 = {line=1, pos=2}
+  Screen_top1 = {line=1, pos=1}
+  Screen_bottom1 = {}
+  Zoom = 1
+  local screen_top_margin = 15  -- pixels
+  local line_height = math.floor(15*Zoom)  -- pixels
+  App.draw()
+  local y = screen_top_margin
+  App.screen.check(y, 'abc', 'F - test_insert_from_clipboard/baseline/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'def', 'F - test_insert_from_clipboard/baseline/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'ghi', 'F - test_insert_from_clipboard/baseline/screen:3')
+  -- after hitting the enter key the screen scrolls down
+  App.clipboard = 'xy\nz'
+  App.run_after_keychord('M-v')
+  check_eq(Screen_top1.line, 1, 'F - test_insert_from_clipboard/screen_top')
+  check_eq(Cursor1.line, 2, 'F - test_insert_from_clipboard/cursor:line')
+  check_eq(Cursor1.pos, 2, 'F - test_insert_from_clipboard/cursor:pos')
+  y = screen_top_margin
+  App.screen.check(y, 'axy', 'F - test_insert_from_clipboard/screen:1')
+  y = y + line_height
+  App.screen.check(y, 'zbc', 'F - test_insert_from_clipboard/screen:2')
+  y = y + line_height
+  App.screen.check(y, 'def', 'F - test_insert_from_clipboard/screen:3')
+end
+
 function test_move_cursor_using_mouse()
   io.write('\ntest_move_cursor_using_mouse')
   App.screen.init{width=50, height=60}
@@ -1362,17 +1427,17 @@ function Text.keychord_pressed(chord)
   elseif chord == 'M-c' then
     local s = Text.selection()
     if s then
-      love.system.setClipboardText(s)
+      App.setClipboardText(s)
     end
   elseif chord == 'M-x' then
     local s = Text.cut_selection()
     if s then
-      love.system.setClipboardText(s)
+      App.setClipboardText(s)
     end
   elseif chord == 'M-v' then
     local before_line = Cursor1.line
     local before = snapshot(before_line)
-    local s = love.system.getClipboardText()
+    local s = App.getClipboardText()
     for _,code in utf8.codes(s) do
       local c = utf8.char(code)
       if c == '\n' then