ruby-changes:68202
From: manga_osyo <ko1@a...>
Date: Sat, 2 Oct 2021 19:58:57 +0900 (JST)
Subject: [ruby-changes:68202] b8327fb8b1 (master): [ruby/reline] Refactoring Reline::Key.match? and add test.
https://git.ruby-lang.org/ruby.git/commit/?id=b8327fb8b1 From b8327fb8b10615ddb3f5e1978d6d83be465503a9 Mon Sep 17 00:00:00 2001 From: manga_osyo <manga.osyo@g...> Date: Sat, 2 Oct 2021 08:44:22 +0900 Subject: [ruby/reline] Refactoring Reline::Key.match? and add test. https://github.com/ruby/reline/commit/90e8999ae4 --- lib/reline.rb | 22 +++++++----------- test/reline/test_reline_key.rb | 53 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 test/reline/test_reline_key.rb 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 https://github.com/ruby/ruby/blob/trunk/lib/reline.rb#L17 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 @@ https://github.com/ruby/ruby/blob/trunk/test/reline/test_reline_key.rb#L1 +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 -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/