Skip to content

Commit

Permalink
Better transport type exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bcotton committed Dec 20, 2007
1 parent bdd4b58 commit 2e17557
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
3 changes: 0 additions & 3 deletions lib/spec/distributed/distributed_spec_runner.rb
@@ -1,7 +1,5 @@
module Spec
module Distributed
class NoSuchTransportException < ArgumentError; end

class DistributedSpecRunner < Spec::Runner::ExampleGroupRunner
def initialize(options, args="")
super(options)
Expand All @@ -10,7 +8,6 @@ def initialize(options, args="")

def process_args(args)
manager_class = TransportManager.manager_for(args)
raise NoSuchTransportException.new("No such transport_type #{args}") unless manager_class
@transport_manager = manager_class.new
end

Expand Down
13 changes: 9 additions & 4 deletions lib/spec/distributed/transport_manager.rb
@@ -1,5 +1,6 @@
module Spec
module Distributed
class NoSuchTransportException < ArgumentError; end
class TransportManager

def self.inherited(klass)
Expand All @@ -8,19 +9,23 @@ def self.inherited(klass)
end

def self.subclasses_by_type
@subclasses_by_type ||= @subclasses.inject({}) do |hash, klass|
@subclasses.inject({}) do |hash, klass|
begin
hash[klass.transport_type] = klass
rescue NoMethodError => e
@subclasses.delete(klass)
raise e
# munch
end
hash
end
end

def self.manager_for(transport_type)
subclasses_by_type[transport_type]
transport_types = subclasses_by_type.keys
manager = subclasses_by_type[transport_type]
if manager.nil?
raise NoSuchTransportException.new("No known transport_type #{transport_type}. Known transport_types are #{transport_types.join(' ' )}")
end
manager
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/spec/distributed/transport_manager_spec.rb
Expand Up @@ -5,10 +5,10 @@ module Distributed
describe TransportManager do

# this passed under autotest, but fails under 'rake spec'
# it "should force subclasses to implement #transport_type" do
# Class.new(TransportManager)
# lambda { TransportManager.manager_for("foo") }.should raise_error(NoMethodError)
# end
it "should force subclasses to implement #transport_type" do
Class.new(TransportManager)
lambda { TransportManager.manager_for("foo") }.should raise_error(NoSuchTransportException, /No known transport_type/)
end

it "should map distribution methods to classes" do
Class.new(TransportManager) do
Expand Down

0 comments on commit 2e17557

Please sign in to comment.