summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/reline.rb22
-rw-r--r--test/reline/test_reline_key.rb53
2 files changed, 62 insertions, 13 deletions
diff --git a/lib/reline.rb b/lib/reline.rb
index 9746b35ac5..2f2b017d50 100644
--- a/lib/reline.rb
+++ b/lib/reline.rb
@@ -17,19 +17,15 @@ module Reline
class ConfigEncodingConversionError < StandardError; end
Key = Struct.new('Key', :char, :combined_char, :with_meta) do
- def match?(key)
- if key.instance_of?(Reline::Key)
- (key.char.nil? or char.nil? or char == key.char) and
- (key.combined_char.nil? or combined_char.nil? or combined_char == key.combined_char) and
- (key.with_meta.nil? or with_meta.nil? or with_meta == key.with_meta)
- elsif key.is_a?(Integer) or key.is_a?(Symbol)
- if not combined_char.nil? and combined_char == key
- true
- elsif combined_char.nil? and not char.nil? and char == key
- true
- else
- false
- end
+ def match?(other)
+ case other
+ when Reline::Key
+ (other.char.nil? or char.nil? or char == other.char) and
+ (other.combined_char.nil? or combined_char.nil? or combined_char == other.combined_char) and
+ (other.with_meta.nil? or with_meta.nil? or with_meta == other.with_meta)
+ when Integer, Symbol
+ (combined_char and combined_char == other) or
+ (combined_char.nil? and char and char == other)
else
false
end
diff --git a/test/reline/test_reline_key.rb b/test/reline/test_reline_key.rb
new file mode 100644
index 0000000000..0058fa85ec
--- /dev/null
+++ b/test/reline/test_reline_key.rb
@@ -0,0 +1,53 @@
+require_relative 'helper'
+require "reline"
+
+class Reline::Test < Reline::TestCase
+ def setup
+ end
+
+ def teardown
+ Reline.test_reset
+ end
+
+ def test_match_key
+ assert(Reline::Key.new(1, 2, false).match?(Reline::Key.new(1, 2, false)))
+ assert(Reline::Key.new(1, 2, false).match?(Reline::Key.new(nil, 2, false)))
+ assert(Reline::Key.new(1, 2, false).match?(Reline::Key.new(1, 2, nil)))
+
+ assert(Reline::Key.new(nil, 2, false).match?(Reline::Key.new(nil, 2, false)))
+ assert(Reline::Key.new(1, nil, false).match?(Reline::Key.new(1, nil, false)))
+ assert(Reline::Key.new(1, 2, nil).match?(Reline::Key.new(1, 2, nil)))
+
+ assert(Reline::Key.new(nil, 2, false).match?(Reline::Key.new(nil, 2, false)))
+ assert(Reline::Key.new(1, nil, false).match?(Reline::Key.new(1, nil, false)))
+ assert(Reline::Key.new(1, 2, nil).match?(Reline::Key.new(1, 2, nil)))
+
+ assert(!Reline::Key.new(1, 2, false).match?(Reline::Key.new(3, 1, false)))
+ assert(!Reline::Key.new(1, 2, false).match?(Reline::Key.new(1, 3, false)))
+ assert(!Reline::Key.new(1, 2, false).match?(Reline::Key.new(1, 3, true)))
+ end
+
+ def test_match_integer
+ assert(Reline::Key.new(1, 2, false).match?(2))
+ assert(Reline::Key.new(nil, 2, false).match?(2))
+ assert(Reline::Key.new(1, nil, false).match?(1))
+
+ assert(!Reline::Key.new(1, 2, false).match?(1))
+ assert(!Reline::Key.new(1, nil, false).match?(2))
+ assert(!Reline::Key.new(nil, nil, false).match?(1))
+ end
+
+ def test_match_symbol
+ assert(Reline::Key.new(:key1, :key2, false).match?(:key2))
+ assert(Reline::Key.new(:key1, nil, false).match?(:key1))
+
+ assert(!Reline::Key.new(:key1, :key2, false).match?(:key1))
+ assert(!Reline::Key.new(:key1, nil, false).match?(:key2))
+ assert(!Reline::Key.new(nil, nil, false).match?(:key1))
+ end
+
+ def test_match_other
+ assert(!Reline::Key.new(:key1, 2, false).match?("key1"))
+ assert(!Reline::Key.new(nil, nil, false).match?(nil))
+ end
+end