[ruby/reline] Refactoring Reline::Key.match? and add test.

https://github.com/ruby/reline/commit/90e8999ae4
This commit is contained in:
manga_osyo 2021-10-02 08:44:22 +09:00 коммит произвёл git
Родитель abc0304cb2
Коммит b8327fb8b1
2 изменённых файлов: 62 добавлений и 13 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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