Skip to content

Commit

Permalink
Add undo/redo textarea widget features tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-obrebski committed Oct 11, 2024
1 parent cb549ed commit ff41a5e
Showing 1 changed file with 68 additions and 14 deletions.
82 changes: 68 additions & 14 deletions test/library/gui/widgets.TextArea.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2619,15 +2619,21 @@ function test.render_text_set_by_api()
screen:dismiss()
end

function test.undo_keyboard_changes()
function test.undo_redo_keyboard_changes()
local text_area, screen, window, widget = arrange_textarea({w=80})

local text = table.concat({
'Lorem ipsum dolor sit amet. ',
}, '\n')

simulate_input_text(text)
function reset_text()
text_area:setText(text)
text_area:setCursor(#text + 1)
end

reset_text()

-- undo single char
simulate_input_text('A')

expect.eq(read_rendered_text(text_area), text .. 'A_')
Expand All @@ -2636,7 +2642,12 @@ function test.undo_keyboard_changes()

expect.eq(read_rendered_text(text_area), text .. '_')

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), text .. 'A_')

-- undo fast written text as group
reset_text()
simulate_input_text('123')

expect.eq(read_rendered_text(text_area), text .. '123_')
Expand All @@ -2645,7 +2656,12 @@ function test.undo_keyboard_changes()

expect.eq(read_rendered_text(text_area), text .. '_')

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), text .. '123_')

-- undo cut feature
reset_text()
simulate_input_text('123')

expect.eq(read_rendered_text(text_area), text .. '123_')
Expand All @@ -2659,50 +2675,75 @@ function test.undo_keyboard_changes()

expect.eq(read_rendered_text(text_area), text .. '123_')

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), '_')

-- undo paste feature
reset_text()

simulate_input_keys('CUSTOM_CTRL_V')

expect.eq(read_rendered_text(text_area), text .. '123' .. text .. '123_')
expect.eq(read_rendered_text(text_area), text .. text .. '123_')

simulate_input_keys('CUSTOM_CTRL_Z')

expect.eq(read_rendered_text(text_area), text .. '123_')
expect.eq(read_rendered_text(text_area), text .. '_')

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), text .. text .. '123_')

-- undo enter
reset_text()

simulate_input_keys('SELECT')

expect.eq(read_rendered_text(text_area), text .. '123\n_')
expect.eq(read_rendered_text(text_area), text .. '\n_')

simulate_input_keys('CUSTOM_CTRL_Z')

expect.eq(read_rendered_text(text_area), text .. '123_')
expect.eq(read_rendered_text(text_area), text .. '_')

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), text .. '\n_')

-- undo backspace
reset_text()

simulate_input_keys('STRING_A000')

expect.eq(read_rendered_text(text_area), text .. '12_')
expect.eq(read_rendered_text(text_area), text:sub(1, #text - 1) .. '_')

simulate_input_keys('CUSTOM_CTRL_Z')

expect.eq(read_rendered_text(text_area), text .. '123_')
expect.eq(read_rendered_text(text_area), text .. '_')

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), text:sub(1, #text - 1) .. '_')

-- undo line delete
reset_text()

simulate_input_keys('CUSTOM_CTRL_U')

expect.eq(read_rendered_text(text_area), '_')

simulate_input_keys('CUSTOM_CTRL_Z')

expect.eq(read_rendered_text(text_area), text .. '123_')
expect.eq(read_rendered_text(text_area), text .. '_')

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), '_')

-- undo delete rest of line
reset_text()

text_area:setCursor(5)
local expected_text = text:sub(1, 4) .. '_' .. text:sub(6, #text) .. '123'
local expected_text = text:sub(1, 4) .. '_' .. text:sub(6, #text)
expect.eq(read_rendered_text(text_area), expected_text)

simulate_input_keys('CUSTOM_CTRL_K')
Expand All @@ -2713,7 +2754,12 @@ function test.undo_keyboard_changes()

expect.eq(read_rendered_text(text_area), expected_text)

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), text:sub(1, 4) .. '_')

-- undo delete char
reset_text()

text_area:setCursor(5)
expect.eq(read_rendered_text(text_area), expected_text)
Expand All @@ -2722,17 +2768,22 @@ function test.undo_keyboard_changes()

expect.eq(
read_rendered_text(text_area),
text:sub(1, 4) .. '_' .. text:sub(7, #text) .. '123'
text:sub(1, 4) .. '_' .. text:sub(7, #text)
)

simulate_input_keys('CUSTOM_CTRL_Z')

expect.eq(read_rendered_text(text_area), expected_text)

-- undo delete last word
simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(
read_rendered_text(text_area),
text:sub(1, 4) .. '_' .. text:sub(7, #text)
)

text_area:setText(text)
text_area:setCursor(#text + 1)
-- undo delete last word
reset_text()

simulate_input_keys('CUSTOM_CTRL_W')
expect.eq(read_rendered_text(text_area), 'Lorem ipsum dolor sit _')
Expand All @@ -2741,5 +2792,8 @@ function test.undo_keyboard_changes()

expect.eq(read_rendered_text(text_area), text .. '_')

simulate_input_keys('CUSTOM_CTRL_Y')

expect.eq(read_rendered_text(text_area), 'Lorem ipsum dolor sit _')
screen:dismiss()
end

0 comments on commit ff41a5e

Please sign in to comment.