Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
input.rb
errors/
plans/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.0.25 - 26 September 2025

* Fixed a crashing bug when assigning `nil` to an input. (thanks @vinnydiehl on Discord for reporting)
* Fixed a related bug when initializing the input with a non-string, non-falsey value.

# v0.0.24 - 9 March 2025

* Fixed a bug that caused `delete_back` to result in negative `selection_start`/`selection_end` for an empty input. (thanks @Artem on Discord for reporting and sending the PR)
Expand Down
9 changes: 5 additions & 4 deletions lib/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ def blur
end

def value=(text)
text = text[0, @max_length] if @max_length
@value.replace(text)
@selection_start = @selection_start.lesser(text.length)
@selection_end = @selection_end.lesser(text.length)
val = text.to_s
val = val[0, @max_length] if @max_length
@value.replace(val)
@selection_start = @selection_start.lesser(val.length)
@selection_end = @selection_end.lesser(val.length)
end

def size_enum
Expand Down
2 changes: 1 addition & 1 deletion lib/text.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Input
class Text < Base
def initialize(**params)
@value = TextValue.new(params[:value] || '')
@value = TextValue.new(params[:value].to_s)
super
end

Expand Down
2 changes: 1 addition & 1 deletion publish
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rm game_state*.txt
cd ..
./dragonruby-publish dr-input
./dragonruby-publish "$@" dr-input
67 changes: 47 additions & 20 deletions tests/tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,53 @@ def test_menu_calculates_menu_items_to_show_long(args, assert)
assert.equal! menu.items_to_show(110), [995, 5], "with selected_index: #{menu.selected_index}"
end

# ---------------------- delete_back ----------------------

def test_delete_back(_args, assert)
input = build_text_input('1234567890', 10, 10)
input.delete_back

assert.equal! input.value.to_s, '123456789'
assert.equal! input.selection_end, 9
assert.equal! input.selection_start, 9
end

def test_delete_back_empty_value(_args, assert)
input = build_text_input('', 0, 0)
input.delete_back

assert.equal! input.value.to_s, ''
assert.equal! input.selection_end, 0
assert.equal! input.selection_start, 0
end

# ---------------------- bug fixes -----------------------

def test_assign_text_nil(_args, assert)
input = build_text_input('1234567890', 10, 10)
input.value = nil

assert.equal! input.value.to_s, ''
assert.equal! input.selection_end, 0
assert.equal! input.selection_start, 0
end

def test_create_with_nil_value(_args, assert)
input = build_text_input(nil)

assert.equal! input.value.to_s, ''
assert.equal! input.selection_end, 0
assert.equal! input.selection_start, 0
end

def test_create_with_non_string_non_falsey_value(_args, assert)
input = build_text_input(true)

assert.equal! input.value.to_s, 'true'
assert.equal! input.selection_end, 0
assert.equal! input.selection_start, 0
end

# ---------------------- helper methods ----------------------

def build_text_input(value, selection_start = 0, selection_end = selection_start, **attr)
Expand Down Expand Up @@ -515,23 +562,3 @@ def mouse_up
$args.inputs.mouse.click = nil
$args.inputs.mouse.up = true
end

# ---------------------- delete_back ----------------------

def test_delete_back(_args, assert)
input = build_text_input('1234567890', 10, 10)
input.delete_back

assert.equal! input.value.to_s, '123456789'
assert.equal! input.selection_end, 9
assert.equal! input.selection_start, 9
end

def test_delete_back_empty_value(_args, assert)
input = build_text_input('', 0, 0)
input.delete_back

assert.equal! input.value.to_s, ''
assert.equal! input.selection_end, 0
assert.equal! input.selection_start, 0
end
Loading