Sha256: c936eff7579d323ade3e63872160836baf0333dc160afeebe976827f57dd5c06

Contents?: true

Size: 1.8 KB

Versions: 16

Compression:

Stored size: 1.8 KB

Contents

module Spec
  module Matchers
    
    class ThrowSymbol #:nodoc:
      def initialize(expected=nil)
        @expected = expected
        @actual = nil
      end
      
      def matches?(given_proc)
        begin
          given_proc.call
        rescue NameError => e
          raise e unless e.message =~ /uncaught throw/
          @actual = e.name.to_sym
        ensure
          if @expected.nil?
            return @actual.nil? ? false : true
          else
            return @actual == @expected
          end
        end
      end

      def failure_message
        if @actual
          "expected #{expected}, got #{@actual.inspect}"
        else
          "expected #{expected} but nothing was thrown"
        end
      end
      
      def negative_failure_message
        if @expected
          "expected #{expected} not to be thrown"
        else
          "expected no Symbol, got :#{@actual}"
        end
      end
      
      def description
        "throw #{expected}"
      end
      
      private
      
        def expected
          @expected.nil? ? "a Symbol" : @expected.inspect
        end
      
    end
 
    # :call-seq:
    #   should throw_symbol()
    #   should throw_symbol(:sym)
    #   should_not throw_symbol()
    #   should_not throw_symbol(:sym)
    #
    # Given a Symbol argument, matches if the given proc throws the specified Symbol.
    #
    # Given no argument, matches if a proc throws any Symbol.
    #
    # == Examples
    #
    #   lambda { do_something_risky }.should throw_symbol
    #   lambda { do_something_risky }.should throw_symbol(:that_was_risky)
    #
    #   lambda { do_something_risky }.should_not throw_symbol
    #   lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
    def throw_symbol(sym=nil)
      Matchers::ThrowSymbol.new(sym)
    end
  end
end

Version data entries

16 entries across 16 versions & 5 rubygems

Version Path
dchelimsky-rspec-1.1.10 lib/spec/matchers/throw_symbol.rb
merb-core-1.1.3 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/matchers/throw_symbol.rb
merb-core-1.1.2 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/matchers/throw_symbol.rb
merb-core-1.1.1 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/matchers/throw_symbol.rb
merb-core-1.1.0 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/matchers/throw_symbol.rb
merb-core-1.1.0.rc1 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/matchers/throw_symbol.rb
merb-core-1.1.0.pre spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/matchers/throw_symbol.rb
mack-0.8.2 lib/gems/rspec-1.1.11/lib/spec/matchers/throw_symbol.rb
rspec-1.1.10 lib/spec/matchers/throw_symbol.rb
rspec-1.1.6 lib/spec/matchers/throw_symbol.rb
rspec-1.1.11 lib/spec/matchers/throw_symbol.rb
rspec-1.1.7 lib/spec/matchers/throw_symbol.rb
rspec-1.1.9 lib/spec/matchers/throw_symbol.rb
rspec-1.1.8 lib/spec/matchers/throw_symbol.rb
typo-5.1.98 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
typo-5.2 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb