[前][次][番号順一覧][スレッド一覧]

ruby-changes:64965

From: Takashi <ko1@a...>
Date: Wed, 20 Jan 2021 18:03:57 +0900 (JST)
Subject: [ruby-changes:64965] 328df00712 (master): [ruby/irb] Split test files for IRB::Color and IRB::ColorPrinter

https://git.ruby-lang.org/ruby.git/commit/?id=328df00712

From 328df00712650720e9e31a52c76b1f7fa2f8be7f Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Wed, 20 Jan 2021 01:00:32 -0800
Subject: [ruby/irb] Split test files for IRB::Color and IRB::ColorPrinter

https://github.com/ruby/irb/commit/d95e8daab3

diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb
index 50dfc22..ea689b3 100644
--- a/test/irb/test_color.rb
+++ b/test/irb/test_color.rb
@@ -1,7 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L1
 # frozen_string_literal: false
 require 'test/unit'
 require 'irb/color'
-require 'irb/color_printer'
 require 'rubygems'
 require 'stringio'
 
@@ -166,23 +165,6 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L165
       end
     end
 
-    IRBTestColorPrinter = Struct.new(:a)
-
-    def test_color_printer
-      unless ripper_lexer_scan_supported?
-        skip 'Ripper::Lexer#scan is supported in Ruby 2.7+'
-      end
-      {
-        1 => "#{BLUE}#{BOLD}1#{CLEAR}\n",
-        IRBTestColorPrinter.new('test') => "#{GREEN}#<struct TestIRB::TestColor::IRBTestColorPrinter#{CLEAR} a#{GREEN}=#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{RED}test#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}\n",
-        Ripper::Lexer.new('1').scan => "[#{GREEN}#<Ripper::Lexer::Elem:#{CLEAR} on_int@1:0 END token: #{RED}#{BOLD}\"#{CLEAR}#{RED}1#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}]\n",
-        Class.new{define_method(:pretty_print){|q| q.text("[__FILE__, __LINE__, __ENCODING__]")}}.new => "[#{CYAN}#{BOLD}__FILE__#{CLEAR}, #{CYAN}#{BOLD}__LINE__#{CLEAR}, #{CYAN}#{BOLD}__ENCODING__#{CLEAR}]\n",
-      }.each do |object, result|
-        actual = with_term { IRB::ColorPrinter.pp(object, '') }
-        assert_equal(result, actual, "Case: IRB::ColorPrinter.pp(#{object.inspect}, '')")
-      end
-    end
-
     def test_inspect_colorable
       {
         1 => true,
@@ -215,10 +197,6 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L197
       Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
     end
 
-    def ripper_lexer_scan_supported?
-      Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
-    end
-
     def with_term
       stdout = $stdout
       io = StringIO.new
diff --git a/test/irb/test_color_printer.rb b/test/irb/test_color_printer.rb
new file mode 100644
index 0000000..b42048c
--- /dev/null
+++ b/test/irb/test_color_printer.rb
@@ -0,0 +1,71 @@ https://github.com/ruby/ruby/blob/trunk/test/irb/test_color_printer.rb#L1
+# frozen_string_literal: false
+require 'test/unit'
+require 'irb/color_printer'
+require 'rubygems'
+require 'stringio'
+
+module TestIRB
+  class TestColorPrinter < Test::Unit::TestCase
+    CLEAR     = "\e[0m"
+    BOLD      = "\e[1m"
+    UNDERLINE = "\e[4m"
+    REVERSE   = "\e[7m"
+    RED       = "\e[31m"
+    GREEN     = "\e[32m"
+    YELLOW    = "\e[33m"
+    BLUE      = "\e[34m"
+    MAGENTA   = "\e[35m"
+    CYAN      = "\e[36m"
+
+    def setup
+      @get_screen_size = Reline.method(:get_screen_size)
+      Reline.instance_eval { undef :get_screen_size }
+      def Reline.get_screen_size
+        [36, 80]
+      end
+    end
+
+    def teardown
+      Reline.instance_eval { undef :get_screen_size }
+      Reline.define_singleton_method(:get_screen_size, @get_screen_size)
+    end
+
+    IRBTestColorPrinter = Struct.new(:a)
+
+    def test_color_printer
+      unless ripper_lexer_scan_supported?
+        skip 'Ripper::Lexer#scan is supported in Ruby 2.7+'
+      end
+      {
+        1 => "#{BLUE}#{BOLD}1#{CLEAR}\n",
+        IRBTestColorPrinter.new('test') => "#{GREEN}#<struct TestIRB::TestColorPrinter::IRBTestColorPrinter#{CLEAR} a#{GREEN}=#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{RED}test#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}\n",
+        Ripper::Lexer.new('1').scan => "[#{GREEN}#<Ripper::Lexer::Elem:#{CLEAR} on_int@1:0 END token: #{RED}#{BOLD}\"#{CLEAR}#{RED}1#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}]\n",
+        Class.new{define_method(:pretty_print){|q| q.text("[__FILE__, __LINE__, __ENCODING__]")}}.new => "[#{CYAN}#{BOLD}__FILE__#{CLEAR}, #{CYAN}#{BOLD}__LINE__#{CLEAR}, #{CYAN}#{BOLD}__ENCODING__#{CLEAR}]\n",
+      }.each do |object, result|
+        actual = with_term { IRB::ColorPrinter.pp(object, '') }
+        assert_equal(result, actual, "Case: IRB::ColorPrinter.pp(#{object.inspect}, '')")
+      end
+    end
+
+    private
+
+    def ripper_lexer_scan_supported?
+      Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
+    end
+
+    def with_term
+      stdout = $stdout
+      io = StringIO.new
+      def io.tty?; true; end
+      $stdout = io
+
+      env = ENV.to_h.dup
+      ENV['TERM'] = 'xterm-256color'
+
+      yield
+    ensure
+      $stdout = stdout
+      ENV.replace(env) if env
+    end
+  end
+end
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]