Skip to content

Commit

Permalink
[ruby/reline] Refactoring Reline::Key.match? and add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
osyo-manga authored and matzbot committed Oct 2, 2021
1 parent abc0304 commit b8327fb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lib/reline.rb
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions 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

0 comments on commit b8327fb

Please sign in to comment.