Skip to content

Commit

Permalink
Move capture to Kernel. [#5641 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
krekoten authored and josevalim committed Sep 18, 2010
1 parent 76266a8 commit d4fa120
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
19 changes: 19 additions & 0 deletions activesupport/lib/active_support/core_ext/kernel/reporting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ def suppress(*exception_classes)
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
5 changes: 5 additions & 0 deletions activesupport/test/core_ext/kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def test_class_eval
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
Expand Down
20 changes: 1 addition & 19 deletions railties/lib/rails/generators/test_case.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -65,25 +66,6 @@ def self.destination(path)
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:
#
Expand Down
14 changes: 1 addition & 13 deletions railties/test/railties/engine_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d4fa120

Please sign in to comment.