Skip to content

Instantly share code, notes, and snippets.

@laszpio
Forked from ngty/someone.rb
Created September 21, 2010 14:30
Show Gist options
  • Save laszpio/589756 to your computer and use it in GitHub Desktop.
Save laszpio/589756 to your computer and use it in GitHub Desktop.
class Someone
class << self
def say
$stdin.each_line do |line|
$stdout << "someone says '#{line}' @ #{Time.now}"
end
end
end
end
require 'rubygems'
require 'rspec'
require 'otaku'
require 'cross-stub'
require File.join(File.dirname(__FILE__),'someone')
describe 'Someone' do
before do
# Cross-process stubbing if required (initializes the stubs cache)
CrossStub.setup :file => '/tmp/crosstub.cache'
# Wrap & execute stdin/stdout manipulation in another process (using otaku service)
Otaku.start do |data|
begin
# Need to explicitely require the definition for Someone (cos this block actually
# runs in another process)
require File.join(File.dirname(__FILE__),'someone')
# Replace stdout & stdin with instances of StringIO
$o_stdout, $o_stdin = $stdout, $stdin
$stdout, $stdin = StringIO.new, StringIO.new
$stdin << data
$stdin.rewind
# Cross-process stubbing if required (refreshes the stubs cache)
require 'cross-stub'
CrossStub.refresh :file => '/tmp/crosstub.cache'
# Running the to-be-tested method
Someone.say
# Grab the stdout dump
$stdout.rewind
$stdout.readlines.join
ensure
# Ensures stdout & stdin are reverted back to the originals
$stdout, $stdin = $o_stdout, $o_stdin
end
end
end
after do
Otaku.stop
CrossStub.clear
end
it 'should say something' do
Otaku.process('hello').should match(/^someone says 'hello'/)
end
it 'should say something else' do
Time.xstub(:now => '2012')
Otaku.process('hello').should == "someone says 'hello' @ 2012"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment