Skip to content

Commit 101977b

Browse files
Two bug fixes: assigning nil to input.value and initializing with non-string, non-falsey value (#33)
* Two bug fixes: assigning nil to input.value and initializing with non-string, non-falsey value * Add test for create with nil value
1 parent 15e3621 commit 101977b

6 files changed

Lines changed: 61 additions & 26 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.DS_Store
22
input.rb
3+
errors/
4+
plans/

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v0.0.25 - 26 September 2025
2+
3+
* Fixed a crashing bug when assigning `nil` to an input. (thanks @vinnydiehl on Discord for reporting)
4+
* Fixed a related bug when initializing the input with a non-string, non-falsey value.
5+
16
# v0.0.24 - 9 March 2025
27

38
* 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)

lib/base.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@ def blur
148148
end
149149

150150
def value=(text)
151-
text = text[0, @max_length] if @max_length
152-
@value.replace(text)
153-
@selection_start = @selection_start.lesser(text.length)
154-
@selection_end = @selection_end.lesser(text.length)
151+
val = text.to_s
152+
val = val[0, @max_length] if @max_length
153+
@value.replace(val)
154+
@selection_start = @selection_start.lesser(val.length)
155+
@selection_end = @selection_end.lesser(val.length)
155156
end
156157

157158
def size_enum

lib/text.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Input
22
class Text < Base
33
def initialize(**params)
4-
@value = TextValue.new(params[:value] || '')
4+
@value = TextValue.new(params[:value].to_s)
55
super
66
end
77

publish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
rm game_state*.txt
22
cd ..
3-
./dragonruby-publish dr-input
3+
./dragonruby-publish "$@" dr-input

tests/tests.rb

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,53 @@ def test_menu_calculates_menu_items_to_show_long(args, assert)
435435
assert.equal! menu.items_to_show(110), [995, 5], "with selected_index: #{menu.selected_index}"
436436
end
437437

438+
# ---------------------- delete_back ----------------------
439+
440+
def test_delete_back(_args, assert)
441+
input = build_text_input('1234567890', 10, 10)
442+
input.delete_back
443+
444+
assert.equal! input.value.to_s, '123456789'
445+
assert.equal! input.selection_end, 9
446+
assert.equal! input.selection_start, 9
447+
end
448+
449+
def test_delete_back_empty_value(_args, assert)
450+
input = build_text_input('', 0, 0)
451+
input.delete_back
452+
453+
assert.equal! input.value.to_s, ''
454+
assert.equal! input.selection_end, 0
455+
assert.equal! input.selection_start, 0
456+
end
457+
458+
# ---------------------- bug fixes -----------------------
459+
460+
def test_assign_text_nil(_args, assert)
461+
input = build_text_input('1234567890', 10, 10)
462+
input.value = nil
463+
464+
assert.equal! input.value.to_s, ''
465+
assert.equal! input.selection_end, 0
466+
assert.equal! input.selection_start, 0
467+
end
468+
469+
def test_create_with_nil_value(_args, assert)
470+
input = build_text_input(nil)
471+
472+
assert.equal! input.value.to_s, ''
473+
assert.equal! input.selection_end, 0
474+
assert.equal! input.selection_start, 0
475+
end
476+
477+
def test_create_with_non_string_non_falsey_value(_args, assert)
478+
input = build_text_input(true)
479+
480+
assert.equal! input.value.to_s, 'true'
481+
assert.equal! input.selection_end, 0
482+
assert.equal! input.selection_start, 0
483+
end
484+
438485
# ---------------------- helper methods ----------------------
439486

440487
def build_text_input(value, selection_start = 0, selection_end = selection_start, **attr)
@@ -515,23 +562,3 @@ def mouse_up
515562
$args.inputs.mouse.click = nil
516563
$args.inputs.mouse.up = true
517564
end
518-
519-
# ---------------------- delete_back ----------------------
520-
521-
def test_delete_back(_args, assert)
522-
input = build_text_input('1234567890', 10, 10)
523-
input.delete_back
524-
525-
assert.equal! input.value.to_s, '123456789'
526-
assert.equal! input.selection_end, 9
527-
assert.equal! input.selection_start, 9
528-
end
529-
530-
def test_delete_back_empty_value(_args, assert)
531-
input = build_text_input('', 0, 0)
532-
input.delete_back
533-
534-
assert.equal! input.value.to_s, ''
535-
assert.equal! input.selection_end, 0
536-
assert.equal! input.selection_start, 0
537-
end

0 commit comments

Comments
 (0)