Sha256: b595cf71c2c5ea9fac313e27f312a678b91ed0b1d64d798f5338aef69610c8a3

Contents?: true

Size: 1.78 KB

Versions: 39

Compression:

Stored size: 1.78 KB

Contents

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

39 entries across 39 versions & 7 rubygems

Version Path
jnstq-acts_as_sms-0.1.0 test/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
jnstq-acts_as_sms-0.1.1 test/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
jnstq-acts_as_sms-0.1.3 test/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
jnstq-acts_as_sms-0.1.4 test/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
picolena-0.0.99 app_generators/picolena/templates/vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
picolena-0.1.1 rails_plugins/rspec/lib/spec/matchers/throw_symbol.rb
picolena-0.1.0 rails_plugins/rspec/lib/spec/matchers/throw_symbol.rb
picolena-0.1.4 rails_plugins/rspec/lib/spec/matchers/throw_symbol.rb
picolena-0.1.2 rails_plugins/rspec/lib/spec/matchers/throw_symbol.rb
picolena-0.1.3 rails_plugins/rspec/lib/spec/matchers/throw_symbol.rb
picolena-0.1.5 rails_plugins/rspec/lib/spec/matchers/throw_symbol.rb
pictrails-0.5.0 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
radiant-0.6.5.1 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
radiant-0.6.5 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
radiant-0.6.7 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
radiant-0.6.6 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
radiant-0.6.9 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
radiant-0.6.8 vendor/plugins/rspec/lib/spec/matchers/throw_symbol.rb
rspec-0.0.10 lib/spec/matchers/throw_symbol.rb
rspec-1.1.1 lib/spec/matchers/throw_symbol.rb