Sha256: 8f386d9436f06729b7a2ec41e3f063de882fd8e50b41cc0447a1dc0a1f0d62bc

Contents?: true

Size: 1.7 KB

Versions: 11

Compression:

Stored size: 1.7 KB

Contents

module Spec
  module Matchers
    
    class ThrowSymbol #:nodoc:
      def initialize(expected=nil)
        @expected = expected
      end
      
      def matches?(proc)
        begin
          proc.call
        rescue NameError => e
          @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 a 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

11 entries across 11 versions & 2 rubygems

Version Path
has_finder-0.1.1 spec/rails/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
has_finder-0.1.2 spec/rails/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
has_finder-0.1.3 spec/rails/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
rspec-1.0.1 lib/spec/matchers/throw_symbol.rb
rspec-1.0.2 lib/spec/matchers/throw_symbol.rb
rspec-1.0.3 lib/spec/matchers/throw_symbol.rb
rspec-1.0.4 lib/spec/matchers/throw_symbol.rb
rspec-1.0.5 lib/spec/matchers/throw_symbol.rb
rspec-1.0.6 lib/spec/matchers/throw_symbol.rb
rspec-1.0.7 lib/spec/matchers/throw_symbol.rb
rspec-1.0.8 lib/spec/matchers/throw_symbol.rb