Permalink
Browse files

Some respect for rspec

  • Loading branch information...
methodmissing committed Mar 5, 2009
1 parent c46d336 commit 02004e03783a49db8d8becbaab3ded14181fb589
Showing with 210 additions and 78 deletions.
  1. +6 −1 Rakefile
  2. +6 −3 em-spec.gemspec
  3. +11 −74 lib/em/spec.rb
  4. +35 −0 lib/em/spec/bacon.rb
  5. +57 −0 lib/em/spec/rspec.rb
  6. +47 −0 test/bacon_spec.rb
  7. +48 −0 test/rspec_spec.rb
View
@@ -10,4 +10,9 @@ task :install => :gem do
sh 'sudo gem install em-spec-*.gem'
end
-task :default => :gem
+task :default => :gem
+
+task :spec do
+ sh 'bacon test/bacon_spec.rb'
+ sh 'spec -f specdoc test/rspec_spec.rb'
+end
View
@@ -10,9 +10,12 @@ spec = Gem::Specification.new do |s|
s.authors = ["Aman Gupta"]
s.add_dependency('eventmachine', '>= 0.12.4')
s.add_dependency('bacon', '>= 1.1.0')
-
- # ruby -rpp -e "pp Dir['{README,{examples,lib,protocol}/**/*.{json,rb,txt,xml}}'].map"
+ s.add_dependency('rspec', '>= 1.1.12')
s.files = ["README",
"lib/em/spec.rb",
- "lib/ext/fiber18.rb"]
+ "lib/em/spec/bacon.rb",
+ "lib/em/spec/rspec.rb",
+ "lib/ext/fiber18.rb",
+ "test/bacon_spec.rb",
+ "test/rspec_spec.rb"]
end
View
@@ -1,85 +1,22 @@
require File.dirname(__FILE__) + '/../ext/fiber18'
-
-require 'bacon'
-
-class Bacon::FiberedContext < Bacon::Context
- def it *args
- super{
- if block_given?
- yield
- Fiber.yield
- end
- }
- end
-
- def done
- EM.next_tick{
- :done.should == :done
- $bacon_fiber.resume if $bacon_fiber
- }
- end
-end
-
+require 'rubygems'
require 'eventmachine'
module EventMachine
+
+ def self.spec_backend=( backend )
+ @spec_backend = backend
+ end
+
+ def self.spec_backend
+ @spec_backend
+ end
+
def self.spec *args, &blk
raise ArgumentError, 'block required' unless block_given?
raise 'EventMachine reactor already running' if EM.reactor_running?
- EM.run{
- Bacon.summary_on_exit
- ($bacon_fiber = Fiber.new{
- Bacon::FiberedContext.new(args.join(' '), &blk).run
- EM.stop_event_loop
- }).resume
- }
+ spec_backend.spec( args, blk )
end
class << self; alias :describe :spec; end
-end
-
-if __FILE__ == $0
-
- describe 'Bacon' do
- should 'work as normal outside EM.describe' do
- 1.should == 1
- end
- end
-
- EM.describe EventMachine do
- should 'work' do
- done
- end
-
- should 'have timers' do
- start = Time.now
-
- EM.add_timer(0.5){
- (Time.now-start).should.be.close 0.5, 0.1
- done
- }
- end
-
- should 'have periodic timers' do
- num = 0
- start = Time.now
-
- timer = EM.add_periodic_timer(0.5){
- if (num += 1) == 2
- (Time.now-start).should.be.close 1.0, 0.1
- EM.__send__ :cancel_timer, timer
- done
- end
- }
- end
-
- should 'have deferrables' do
- defr = EM::DefaultDeferrable.new
- defr.timeout(1)
- defr.errback{
- done
- }
- end
- end
-
end
View
@@ -0,0 +1,35 @@
+module EventMachine
+ module Spec
+ module Bacon
+
+ def self.spec( args, blk )
+ EM.run do
+ ::Bacon.summary_on_exit
+ ($em_spec_fiber = Fiber.new{
+ ::Bacon::FiberedContext.new(args.join(' '), &blk).run
+ EM.stop_event_loop
+ }).resume
+ end
+ end
+
+ end
+ end
+end
+
+class Bacon::FiberedContext < Bacon::Context
+ def it *args
+ super{
+ if block_given?
+ yield
+ Fiber.yield
+ end
+ }
+ end
+
+ def done
+ EM.next_tick{
+ :done.should == :done
+ $em_spec_fiber.resume if $em_spec_fiber
+ }
+ end
+end
View
@@ -0,0 +1,57 @@
+module EventMachine
+ module Spec
+ module Rspec
+
+ def self.spec( args, blk )
+ context(args, &blk)
+ end
+
+ end
+ end
+end
+
+module Spec
+ module Example
+ module ExampleMethods
+
+ alias :original_execute :execute
+ def execute(options, instance_variables)
+ original_execute(options, instance_variables)
+ Fiber.yield
+ end
+
+ def done
+ EM.next_tick{
+ :done.should == :done
+ $em_spec_fiber.resume if $em_spec_fiber
+ }
+ end
+
+ end
+
+ module ExampleGroupMethods
+
+ alias :run_without_em :run
+ def run( run_options )
+ if options[:scope] == EventMachine::Spec::Rspec
+ EM.run do
+ run_in_fiber( run_options )
+ end
+ else
+ run_in_fiber( run_options )
+ end
+ true
+ end
+
+ private
+
+ def run_in_fiber( run_options )
+ ($em_spec_fiber = Fiber.new{
+ run_without_em( run_options )
+ EM.stop_event_loop if EM.reactor_running?
+ }).resume
+ end
+
+ end
+ end
+end
View
@@ -0,0 +1,47 @@
+require File.dirname(__FILE__) + '/../lib/em/spec'
+require 'bacon'
+require File.dirname(__FILE__) + '/../lib/em/spec/bacon'
+
+EM.spec_backend = EventMachine::Spec::Bacon
+
+describe 'Bacon' do
+ should 'work as normal outside EM.describe' do
+ 1.should == 1
+ end
+end
+
+EM.describe EventMachine do
+ should 'work' do
+ done
+ end
+
+ should 'have timers' do
+ start = Time.now
+
+ EM.add_timer(0.5){
+ (Time.now-start).should.be.close 0.5, 0.1
+ done
+ }
+ end
+
+ should 'have periodic timers' do
+ num = 0
+ start = Time.now
+
+ timer = EM.add_periodic_timer(0.5){
+ if (num += 1) == 2
+ (Time.now-start).should.be.close 1.0, 0.1
+ EM.__send__ :cancel_timer, timer
+ done
+ end
+ }
+ end
+
+ should 'have deferrables' do
+ defr = EM::DefaultDeferrable.new
+ defr.timeout(1)
+ defr.errback{
+ done
+ }
+ end
+end
View
@@ -0,0 +1,48 @@
+require File.dirname(__FILE__) + '/../lib/em/spec'
+require 'spec'
+require File.dirname(__FILE__) + '/../lib/em/spec/rspec'
+
+EM.spec_backend = EventMachine::Spec::Rspec
+
+describe 'Rspec' do
+ it 'should work as normal outside EM.describe' do
+ 1.should == 1
+ end
+end
+
+EM.describe EventMachine do
+ it 'should work' do
+ done
+ end
+
+ it 'should have timers' do
+ start = Time.now
+
+ EM.add_timer(0.5){
+ (Time.now-start).should be_close( 0.5, 0.1 )
+ done
+ }
+ end
+
+ it 'should have periodic timers' do
+ num = 0
+ start = Time.now
+
+ timer = EM.add_periodic_timer(0.5){
+ if (num += 1) == 2
+ (Time.now-start).should be_close( 1.0, 0.1 )
+ EM.__send__ :cancel_timer, timer
+ done
+ end
+ }
+ end
+
+ it 'should have deferrables' do
+ defr = EM::DefaultDeferrable.new
+ defr.timeout(1)
+ defr.errback{
+ done
+ }
+ end
+
+end

0 comments on commit 02004e0

Please sign in to comment.