Skip to content

Commit

Permalink
Completing initialization refactoring, simplifying initialization.
Browse files Browse the repository at this point in the history
Now initialization does nothing but store some attributes. This is a good thing.
  • Loading branch information
ymendel committed Jun 14, 2008
1 parent ca63f97 commit c1ddebc
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 162 deletions.
7 changes: 1 addition & 6 deletions lib/fsevents/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ def initialize(*paths, &callback)
paths = Dir.pwd if paths.empty?

@allocator = options[:allocator] || OSX::KCFAllocatorDefault
callback = options[:callback]
@context = options[:context] || nil
@paths = [paths].flatten
@since = options[:since] || OSX::KFSEventStreamEventIdSinceNow
@latency = options[:latency] || 1.0
@flags = options[:flags ] || 0

paths = @paths
@stream = OSX.FSEventStreamCreate(allocator, callback, context, paths, since, latency, flags)
raise StreamError, 'Unable to create FSEvents stream.' unless @stream
@flags = options[:flags] || 0
end

def create
Expand Down
157 changes: 1 addition & 156 deletions spec/stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@

describe FSEvents::Stream do
before :each do
@path = "/tmp"
stream = stub('stream')
OSX.stubs(:FSEventStreamCreate).returns(stream)

@path = "/tmp"
@stream = FSEvents::Stream.new(@path)
end

describe 'when initialized' do
before :each do
@stream = stub('stream')
@path = '/tmp'
OSX.stubs(:FSEventStreamCreate).returns(@stream)
end

it 'should accept a path and callback block' do
lambda { FSEvents::Stream.new(@path) {} }.should_not raise_error(ArgumentError)
end
Expand Down Expand Up @@ -123,152 +114,6 @@
FSEvents::Stream.new(@path, @options).flags.should == 0
end
end

it 'should create a new stream' do
OSX.expects(:FSEventStreamCreate).returns(@stream)
FSEvents::Stream.new(@path)
end

describe 'when creating the stream' do
before :each do
@options = {}
[:allocator, :callback, :context, :since, :latency, :flags].each do |opt|
val = stub(opt.to_s)

instance_variable_set("@#{opt}", val)
@options[opt] = val
end

@arg_placeholders = Array.new(7) { anything }
end

it 'should pass the allocator' do
args = @arg_placeholders
args[0] = @allocator
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should pass the callback' do
args = @arg_placeholders
args[1] = @callback
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should pass the context' do
args = @arg_placeholders
args[2] = @context
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should pass the path as an array' do
args = @arg_placeholders
args[3] = [@path]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should pass the paths as-is if given an array of paths' do
other_path = '/other/path'
args = @arg_placeholders
args[3] = [@path, other_path]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new([@path, other_path], @options)
end

it 'should make an array of the paths if given multiple paths' do
other_path = '/other/path'
args = @arg_placeholders
args[3] = [@path, other_path]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, other_path, @options)
end

it 'should pass the since (event ID)' do
args = @arg_placeholders
args[4] = @since
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should pass the latency' do
args = @arg_placeholders
args[5] = @latency
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should pass the flags' do
args = @arg_placeholders
args[6] = @flags
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should default the allocator to KCFAllocatorDefault' do
@options.delete(:allocator)
args = @arg_placeholders
args[0] = OSX::KCFAllocatorDefault
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

# it 'should default the callback' # files changed

it 'should default the context to nil' do
@options.delete(:context)
args = @arg_placeholders
args[2] = nil
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should default the path to the present working directory' do
args = @arg_placeholders
args[3] = [Dir.pwd]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@options)
end

it 'should default the since to KFSEventStreamEventIdSinceNow' do
@options.delete(:since)
args = @arg_placeholders
args[4] = OSX::KFSEventStreamEventIdSinceNow
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should default the latency to 1.0' do
@options.delete(:latency)
args = @arg_placeholders
args[5] = 1.0
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should default the flags to 0' do
@options.delete(:flags)
args = @arg_placeholders
args[6] = 0
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream)
FSEvents::Stream.new(@path, @options)
end

it 'should store the stream' do
FSEvents::Stream.new(@path, @options).stream.should == @stream
end

it 'should raise a StreamError exception if the stream could not be created' do
OSX.stubs(:FSEventStreamCreate).returns(nil)
lambda { FSEvents::Stream.new(@path, @options) }.should raise_error(FSEvents::Stream::StreamError)
end

it 'should not raise a StreamError exception if the stream could be created' do
lambda { FSEvents::Stream.new(@path, @options) }.should_not raise_error(FSEvents::Stream::StreamError)
end
end
end

it 'should create a stream' do
Expand Down

0 comments on commit c1ddebc

Please sign in to comment.