Sha256: 260a06b6b9f221889b5480a8c2bd62f44d0be1b692409267dbb819ccdbb5dcaf

Contents?: true

Size: 1.85 KB

Versions: 14

Compression:

Stored size: 1.85 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 = extract_sym_from_name_error(e)
        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
      
        def extract_sym_from_name_error(error)
          return "#{error.message.split("`").last.split("'").first}".to_sym
        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

14 entries across 14 versions & 3 rubygems

Version Path
puppet-0.23.0 test/lib/spec/matchers/throw_symbol.rb
puppet-0.22.4 test/lib/spec/matchers/throw_symbol.rb
puppet-0.23.2 test/lib/spec/matchers/throw_symbol.rb
puppet-0.23.1 test/lib/spec/matchers/throw_symbol.rb
riess-0.0.8 vendor/rspec-0.8.2/lib/spec/matchers/throw_symbol.rb
rspec-0.8.1 lib/spec/matchers/throw_symbol.rb
rspec-0.8.0 lib/spec/matchers/throw_symbol.rb
rspec-0.9.0 lib/spec/matchers/throw_symbol.rb
rspec-0.9.1 lib/spec/matchers/throw_symbol.rb
rspec-0.9.2 lib/spec/matchers/throw_symbol.rb
rspec-0.9.3 lib/spec/matchers/throw_symbol.rb
rspec-0.9.4 lib/spec/matchers/throw_symbol.rb
rspec-1.0.0 lib/spec/matchers/throw_symbol.rb
rspec-0.8.2 lib/spec/matchers/throw_symbol.rb