Skip to content

Commit

Permalink
Change Reline.add_dialog_proc(name, nil) to properly remove dialog_proc
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Apr 13, 2023
1 parent 127a84d commit 4d3ccb0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,13 @@ def dig_perfect_match_proc=(p)

DialogProc = Struct.new(:dialog_proc, :context)
def add_dialog_proc(name_sym, p, context = nil)
raise ArgumentError unless p.respond_to?(:call) or p.nil?
raise ArgumentError unless name_sym.instance_of?(Symbol)
@dialog_proc_list[name_sym] = DialogProc.new(p, context)
if p.nil?
@dialog_proc_list.delete(name_sym)
else
raise ArgumentError unless p.respond_to?(:call)
@dialog_proc_list[name_sym] = DialogProc.new(p, context)
end
end

def dialog_proc(name_sym)
Expand Down
4 changes: 4 additions & 0 deletions test/reline/test_reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ def test_add_dialog_proc
d = Reline.dialog_proc(:test_proc)
assert_equal(dummy_proc_2, d.dialog_proc)

Reline.add_dialog_proc(:test_proc, nil)
assert_nil(Reline.dialog_proc(:test_proc))

l = lambda {}
Reline.add_dialog_proc(:test_lambda, l)
d = Reline.dialog_proc(:test_lambda)
Expand All @@ -331,6 +334,7 @@ def test_add_dialog_proc
assert_raise(ArgumentError) { Reline.add_dialog_proc(:error, 42) }
assert_raise(ArgumentError) { Reline.add_dialog_proc(:error, 'hoge') }
assert_raise(ArgumentError) { Reline.add_dialog_proc('error', proc {} ) }
assert_raise(ArgumentError) { Reline.add_dialog_proc('error', nil) }

dummy = DummyCallbackObject.new
Reline.add_dialog_proc(:dummy, dummy)
Expand Down

0 comments on commit 4d3ccb0

Please sign in to comment.