diff --git a/.gitignore b/.gitignore index 268eac3..5dafbeb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .DS_Store input.rb +errors/ +plans/ diff --git a/CHANGELOG.md b/CHANGELOG.md index fa99169..f611325 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/base.rb b/lib/base.rb index 7e7dcba..d955007 100644 --- a/lib/base.rb +++ b/lib/base.rb @@ -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 diff --git a/lib/text.rb b/lib/text.rb index bb26498..6182c3f 100644 --- a/lib/text.rb +++ b/lib/text.rb @@ -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 diff --git a/publish b/publish index f8f24f0..90e72b4 100755 --- a/publish +++ b/publish @@ -1,3 +1,3 @@ rm game_state*.txt cd .. -./dragonruby-publish dr-input +./dragonruby-publish "$@" dr-input diff --git a/tests/tests.rb b/tests/tests.rb index 2f5d53d..f338a94 100644 --- a/tests/tests.rb +++ b/tests/tests.rb @@ -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) @@ -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