spec/lib/wisper_spec.rb in wisper-1.0.1 vs spec/lib/wisper_spec.rb in wisper-1.1.0

- old
+ new

@@ -1,140 +1,33 @@ require 'spec_helper' describe Wisper do - let(:listener) { double('listener') } - let(:publisher) { Object.new.extend(Wisper) } - describe '.add_listener' do - it 'subscribes given listener to all published events' do - listener.should_receive(:this_happened) - listener.should_receive(:so_did_this) - - publisher.add_listener(listener) - - publisher.send(:broadcast, 'this_happened') - publisher.send(:broadcast, 'so_did_this') + it 'includes Wisper::Publisher for backwards compatibility' do + silence_warnings do + publisher_class = Class.new { include Wisper } + publisher_class.ancestors.should include Wisper::Publisher end - - describe ':on argument' do - it 'subscribes given listener to a single event' do - listener.should_receive(:this_happened) - listener.stub(:so_did_this) - listener.should_not_receive(:so_did_this) - - listener.respond_to?(:so_did_this).should be_true - - publisher.add_listener(listener, :on => 'this_happened') - - publisher.send(:broadcast, 'this_happened') - publisher.send(:broadcast, 'so_did_this') - end - - it 'subscribes given listener to many events' do - listener.should_receive(:this_happened) - listener.should_receive(:and_this) - listener.stub(:so_did_this) - listener.should_not_receive(:so_did_this) - - listener.respond_to?(:so_did_this).should be_true - - publisher.add_listener(listener, :on => ['this_happened', 'and_this']) - - publisher.send(:broadcast, 'this_happened') - publisher.send(:broadcast, 'so_did_this') - publisher.send(:broadcast, 'and_this') - end - end - - describe ':with argument' do - it 'sets method to call listener with on event' do - listener.should_receive(:different_method).twice - - publisher.add_listener(listener, :with => :different_method) - - publisher.send(:broadcast, 'this_happened') - publisher.send(:broadcast, 'so_did_this') - end - end - - it 'returns publisher so methods can be chained' do - publisher.add_listener(listener, :on => 'so_did_this').should == publisher - end end - describe '.add_block_listener' do - let(:insider) { double('insider') } + it '.with_listeners subscribes listeners to all broadcast events for the duration of block' do + publisher = publisher_class.new + listener = double('listener') - it 'subscribes given block to all events' do - insider.should_receive(:it_happened).twice + listener.should_receive(:im_here) + listener.should_not_receive(:not_here) - publisher.add_block_listener do - insider.it_happened - end - - publisher.send(:broadcast, 'something_happened') - publisher.send(:broadcast, 'and_so_did_this') + Wisper.with_listeners(listener) do + publisher.send(:broadcast, 'im_here') end - describe ':on argument' do - it '.add_block_listener subscribes block to an event' do - insider.should_not_receive(:it_happened).once - - publisher.add_block_listener(:on => 'something_happened') do - insider.it_happened - end - - publisher.send(:broadcast, 'something_happened') - publisher.send(:broadcast, 'and_so_did_this') - end - end - - it 'returns publisher so methods can be chained' do - publisher.add_block_listener(:on => 'this_thing_happened') do - end.should == publisher - end + publisher.send(:broadcast, 'not_here') end +end - describe '.on (alternative block syntax)' do - it 'subscribes given block to an event' do - insider = double('insider') - insider.should_receive(:it_happened) - - publisher.on(:something_happened) do - insider.it_happened - end - - publisher.send(:broadcast, 'something_happened') - end - end - - describe '.broadcast' do - it 'does not publish events which cannot be responded to' do - listener.should_not_receive(:so_did_this) - listener.stub(:respond_to?, false) - - publisher.add_listener(listener, :on => 'so_did_this') - - publisher.send(:broadcast, 'so_did_this') - end - - describe ':event argument' do - it 'is indifferent to string and symbol' do - listener.should_receive(:this_happened).twice - - publisher.add_listener(listener) - - publisher.send(:broadcast, 'this_happened') - publisher.send(:broadcast, :this_happened) - end - - it 'is indifferent to dasherized and underscored strings' do - listener.should_receive(:this_happened).twice - - publisher.add_listener(listener) - - publisher.send(:broadcast, 'this_happened') - publisher.send(:broadcast, 'this-happened') - end - end - end +# prevents deprecation warning showing up in spec output +def silence_warnings + original_verbosity = $VERBOSE + $VERBOSE = nil + yield + $VERBOSE = original_verbosity end