1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move capture to Kernel. [#5641 state:resolved]

This commit is contained in:
Krekoten' Marjan 2010-09-16 19:03:55 +03:00 committed by José Valim
parent 76266a8184
commit d4fa120671
4 changed files with 26 additions and 32 deletions

View file

@ -59,4 +59,23 @@ module Kernel
raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
end
end
# Captures the given stream and returns it:
#
# stream = capture(:stdout){ puts "Cool" }
# stream # => "Cool\n"
#
def capture(stream)
begin
stream = stream.to_s
eval "$#{stream} = StringIO.new"
yield
result = eval("$#{stream}").string
ensure
eval("$#{stream} = #{stream.upcase}")
end
result
end
alias :silence :capture
end

View file

@ -52,6 +52,11 @@ class KernelTest < Test::Unit::TestCase
class << o; @x = 1; end
assert_equal 1, o.class_eval { @x }
end
def test_capture
assert_equal 'STDERR', capture(:stderr) {$stderr.print 'STDERR'}
assert_equal 'STDOUT', capture(:stdout) {print 'STDOUT'}
end
end
class KernelSuppressTest < Test::Unit::TestCase

View file

@ -1,6 +1,7 @@
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/core_ext/kernel/reporting'
require 'rails/generators'
require 'fileutils'
@ -65,25 +66,6 @@ module Rails
self.destination_root = path
end
# Captures the given stream and returns it:
#
# stream = capture(:stdout){ puts "Cool" }
# stream # => "Cool\n"
#
def capture(stream)
begin
stream = stream.to_s
eval "$#{stream} = StringIO.new"
yield
result = eval("$#{stream}").string
ensure
eval("$#{stream} = #{stream.upcase}")
end
result
end
alias :silence :capture
# Asserts a given file exists. You need to supply an absolute path or a path relative
# to the configured destination:
#

View file

@ -1,22 +1,10 @@
require "isolation/abstract_unit"
require "railties/shared_tests"
require 'stringio'
require 'active_support/core_ext/kernel/reporting'
module RailtiesTest
class EngineTest < Test::Unit::TestCase
# TODO: it's copied from generators/test_case, maybe make a module with such helpers?
def capture(stream)
begin
stream = stream.to_s
eval "$#{stream} = StringIO.new"
yield
result = eval("$#{stream}").string
ensure
eval("$#{stream} = #{stream.upcase}")
end
result
end
include ActiveSupport::Testing::Isolation
include SharedTests