forked from cucumber/cucumber-ruby
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Cucumber::Formatter::Interceptor::Pipe class for transparentl…
…y intercepting writes to pipes This will ultimately be used to consume $stderr and $stdout for formatters
- Loading branch information
R. Tyler Croy
committed
Apr 24, 2012
1 parent
143d2b8
commit 4d1bccd
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
module Cucumber | ||
module Formatter | ||
module Interceptor | ||
class Pipe | ||
attr_reader :pipe, :buffer | ||
def initialize(pipe) | ||
@pipe = pipe | ||
@buffer = [] | ||
end | ||
|
||
def write(str) | ||
@buffer << str | ||
return @pipe.write(str) | ||
end | ||
|
||
def method_missing(method, *args, &blk) | ||
@pipe.send(method, *args, &blk) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'spec_helper' | ||
require 'cucumber/formatter/interceptor' | ||
|
||
module Cucumber::Formatter | ||
describe Interceptor::Pipe do | ||
let(:pipe) do | ||
pipe = double('original pipe') | ||
pipe.stub(:instance_of?).and_return(true) | ||
pipe | ||
end | ||
|
||
describe '#write' do | ||
let(:buffer) { 'Some stupid buffer' } | ||
let(:pi) { Interceptor::Pipe.new(pipe) } | ||
|
||
it 'should write arguments to the original pipe' do | ||
pipe.should_receive(:write).with(buffer).and_return(buffer.size) | ||
pi.write(buffer).should == buffer.size | ||
end | ||
|
||
it 'should add the buffer to its stored output' do | ||
pipe.stub(:write) | ||
pi.write(buffer) | ||
pi.buffer.should_not be_empty | ||
pi.buffer.first.should == buffer | ||
end | ||
end | ||
|
||
describe '#method_missing' do | ||
let(:pi) { Interceptor::Pipe.new(pipe) } | ||
|
||
it 'should pass #tty? to the original pipe' do | ||
pipe.should_receive(:tty?).and_return(true) | ||
pi.tty?.should be true | ||
end | ||
end | ||
end | ||
end |