public
Description: A process execution library which doesn't suck.
Clone URL: git://github.com/codahale/ropen.git
ropen / spec / ropen / events_spec.rb
100644 51 lines (36 sloc) 1.058 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require File.join(File.dirname(__FILE__), "..", "spec_helper")
 
require "singleton"
 
require "ropen/events"
 
class ThreadSafeStorage
  include Singleton
  attr_reader :stuff
  
  def initialize
    @stuff = []
  end
  
end
 
describe Ropen::Events do
  
  before(:each) do
    @stream = mock(:stream)
    @events = Ropen::Events.new
  end
  
  it "should collect events" do
    callback = lambda { |line| puts line }
    @events.on_output(&callback)
    @events.callbacks.should == [callback]
  end
  
  it "should run collected events on a given stream" do
    output = []
    
    @stream.should_receive(:eof?).and_return(false, false, true)
    @stream.should_receive(:readpartial).with(an_instance_of(Numeric)).and_return("blah", "blee")
    
    @events.on_output do |line|
      output << [1, line]
    end
    
    @events.on_output do |line|
      output << [2, line]
    end
    
    @events.run(@stream)
    @events.finish
    @events.output.should == "blahblee"
    
    output.transpose.last.should == ["blah", "blah", "blee", "blee"]
  end
  
end