summaryrefslogtreecommitdiffstats
path: root/test/lib/spec/expectations/should/base.rb
blob: 1be4677e8388395aa5a6504b4a88586b92d05777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Spec
  module Expectations
    module Should
      class Base
        
        #== and =~ will stay after the new syntax
        def ==(expected)
          __delegate_method_missing_to_target "==", "==", expected
        end
        
        def =~(expected)
          __delegate_method_missing_to_target "=~", "=~", expected
        end
        
        #<, <=, >=, > are all implemented in Spec::Matchers::Be
        # and will be removed with 0.9
        deprecated do
          def <(expected)
            __delegate_method_missing_to_target "<", "<", expected
          end
        
          def <=(expected)
            __delegate_method_missing_to_target "<=", "<=", expected
          end
        
          def >=(expected)
            __delegate_method_missing_to_target ">=", ">=", expected
          end
        
          def >(expected)
            __delegate_method_missing_to_target ">", ">", expected
          end
        end

        def default_message(expectation, expected=nil)
          return "expected #{expected.inspect}, got #{@target.inspect} (using #{expectation})" if expectation == '=='
          "expected #{expectation} #{expected.inspect}, got #{@target.inspect}" unless expectation == '=='
        end

        def fail_with_message(message, expected=nil, target=nil)
          Spec::Expectations.fail_with(message, expected, target)
        end
    
        def find_supported_sym(original_sym)
          ["#{original_sym}?", "#{original_sym}s?"].each do |alternate_sym|
            return alternate_sym.to_s if @target.respond_to?(alternate_sym.to_s)
          end
        end

        deprecated do
          def method_missing(original_sym, *args, &block)
            if original_sym.to_s =~ /^not_/
              return Not.new(@target).__send__(sym, *args, &block)
            end
            if original_sym.to_s =~ /^have_/
              return have.__send__(original_sym.to_s[5..-1].to_sym, *args, &block)
            end
            __delegate_method_missing_to_target original_sym, find_supported_sym(original_sym), *args
          end
        end
      end
    end
  end
end