diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-07-10 03:35:03 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-07-10 03:35:03 +0000 |
commit | 25f037377c9a537f624ba0f4d8e058fae84071db (patch) | |
tree | d7b58938085c2bc89011832f05bfeff3ee3babbc /sample | |
parent | 9804e68f973716d31d1bdfbc132b801d22327772 (diff) | |
download | ruby-25f037377c9a537f624ba0f4d8e058fae84071db.tar.gz |
sample/test.rb: Progress::Rotator
* sample/test.rb (Progress): refactor to separate Rotator.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36353 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample')
-rwxr-xr-x | sample/test.rb | 122 |
1 files changed, 70 insertions, 52 deletions
diff --git a/sample/test.rb b/sample/test.rb index fac4c45183..0e59f27bc0 100755 --- a/sample/test.rb +++ b/sample/test.rb @@ -4,69 +4,87 @@ $testnum=0 $ntest=0 $failed = 0 -PROGRESS = Object.new -PROGRESS.instance_eval do - @color = nil - @quiet = nil - ARGV.each do |arg| - case arg - when /\A--color(?:=(?:always|(auto)|(never)|(.*)))?\z/ - warn "unknown --color argument: #$3" if $3 - @color = $1 ? nil : !$2 - when /\A-(q|-quiet)\z/ - @quiet = true +class Progress + def initialize + @color = nil + @quiet = nil + ARGV.each do |arg| + case arg + when /\A--color(?:=(?:always|(auto)|(never)|(.*)))?\z/ + warn "unknown --color argument: #$3" if $3 + @color = $1 ? nil : !$2 + when /\A-(q|-quiet)\z/ + @quiet = true + end + end + @tty = STDERR.tty? && /dumb/ !~ ENV["TERM"] + case @color + when nil + @color = @tty end + if @color + # dircolors-like style + colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:]*)/)] : {} + @passed = "\e[#{colors["pass"] || "32"}m" + @failed = "\e[#{colors["fail"] || "31"}m" + @reset = "\e[m" + else + @passed = @failed = @reset = "" + end + extend(Rotator) if @tty end - @count = 0 - @rotator = %w[- \\ | /] - @bs = "\b" * @rotator[0].size - @tty = STDERR.tty? && /dumb/ !~ ENV["TERM"] - case @color - when nil - @color = @tty - when true - @tty = true + + def passed_string + "." end - if @color - # dircolors-like style - colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:]*)/)] : {} - @passed = "\e[#{colors["pass"] || "32"}m" - @failed = "\e[#{colors["fail"] || "31"}m" - @reset = "\e[m" - else - @passed = @failed = @reset = "" + def failed_string + "#{@failed}F#{@reset}" end - - if @tty - def self.pass - STDERR.print "#{@bs}#{@rotator[(@count += 1) % @rotator.size]}" - end - def self.fail - @ok = false - STDERR.print "#{@bs}#{@failed}F#{@reset}#{@rotator[@count % @rotator.size]}" - end - def self.init - @ok = true - STDERR.print " " - end - def self.finish - STDERR.print "#{@bs}#{' ' * @bs.size}#{@bs}#{@passed}#{@ok ? 'OK' : ''} #{$testnum}#{@reset}" - STDERR.print @quiet ? "\r\e[2K\r" : "\n" + def init_string + end + def finish_string + if @quiet + "\n" + else + "#{@passed}#{@ok ? 'OK' : ''} #{$testnum}#{@reset}\n" end - else - def self.pass - STDERR.print "." + end + def pass + STDERR.print passed_string + end + def fail + @ok = false + STDERR.print failed_string + end + def init + @ok = true + STDERR.print init_string + end + def finish + STDERR.print finish_string + end + + module Rotator + ROTATOR = %w[- \\ | /] + BS = "\b" * ROTATOR[0].size + def passed_string + "#{BS}#{ROTATOR[(@count += 1) % ROTATOR.size]}" end - def self.fail - STDERR.print "F" + def failed_string + "#{BS}#{super}#{ROTATOR[@count % ROTATOR.size]}" end - def self.init + def init_string + @count = 0 + " " end - def self.finish - STDERR.puts + def finish_string + s = "#{BS}#{' ' * BS.size}#{BS}#{super}" + s.gsub!(/\n/, "\r\e[2K\r") if @quiet + s end end end +PROGRESS = Progress.new def test_check(what) unless $ntest.zero? |