File: passing_argument_matchers_spec.rb

package info (click to toggle)
ruby-spy 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 360 kB
  • sloc: ruby: 3,101; makefile: 2
file content (140 lines) | stat: -rw-r--r-- 4,523 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require 'spec_helper'

module Spy
  describe "Matchers", :broken do
    before(:each) do
      @double = double('double')
      Spy.on(Kernel, :warn)
    end

    after(:each) do
      @double.rspec_verify
    end

    context "handling argument matchers" do
      it "accepts true as boolean()" do
        @double.should_receive(:random_call).with(boolean())
        @double.random_call(true)
      end

      it "accepts false as boolean()" do
        @double.should_receive(:random_call).with(boolean())
        @double.random_call(false)
      end

      it "accepts fixnum as kind_of(Numeric)" do
        @double.should_receive(:random_call).with(kind_of(Numeric))
        @double.random_call(1)
      end

      it "accepts float as an_instance_of(Numeric)" do
        @double.should_receive(:random_call).with(kind_of(Numeric))
        @double.random_call(1.5)
      end

      it "accepts fixnum as instance_of(Fixnum)" do
        @double.should_receive(:random_call).with(instance_of(Fixnum))
        @double.random_call(1)
      end

      it "does NOT accept fixnum as instance_of(Numeric)" do
        @double.should_not_receive(:random_call).with(instance_of(Numeric))
        @double.random_call(1)
      end

      it "does NOT accept float as instance_of(Numeric)" do
        @double.should_not_receive(:random_call).with(instance_of(Numeric))
        @double.random_call(1.5)
      end

      it "accepts string as anything()" do
        @double.should_receive(:random_call).with("a", anything(), "c")
        @double.random_call("a", "whatever", "c")
      end

      it "matches duck type with one method" do
        @double.should_receive(:random_call).with(duck_type(:length))
        @double.random_call([])
      end

      it "matches duck type with two methods" do
        @double.should_receive(:random_call).with(duck_type(:abs, :div))
        @double.random_call(1)
      end

      it "matches no args against any_args()" do
        @double.should_receive(:random_call).with(any_args)
        @double.random_call()
      end

      it "matches one arg against any_args()" do
        @double.should_receive(:random_call).with(any_args)
        @double.random_call("a string")
      end

      it "matches no args against no_args()" do
        @double.should_receive(:random_call).with(no_args)
        @double.random_call()
      end

      it "matches hash with hash_including same hash" do
        @double.should_receive(:random_call).with(hash_including(:a => 1))
        @double.random_call(:a => 1)
      end
    end

    context "handling block matchers" do
      it "matches arguments against RSpec expectations" do
        @double.should_receive(:random_call).with {|arg1, arg2, arr, *rest|
          expect(arg1).to eq 5
          expect(arg2).to have_at_least(3).characters
          expect(arg2).to have_at_most(10).characters
          expect(arr.map {|i| i * 2}).to eq [2,4,6]
          expect(rest).to eq [:fee, "fi", 4]
        }
        @double.random_call 5, "hello", [1,2,3], :fee, "fi", 4
      end

      it "does not eval the block as the return value" do
        eval_count = 0
        @double.should_receive(:msg).with {|a| eval_count += 1}
        @double.msg(:ignore)
        expect(eval_count).to eq(1)
      end
    end

    context "handling non-matcher arguments" do
      it "matches non special symbol (can be removed when deprecated symbols are removed)" do
        @double.should_receive(:random_call).with(:some_symbol)
        @double.random_call(:some_symbol)
      end

      it "matches string against regexp" do
        @double.should_receive(:random_call).with(/bcd/)
        @double.random_call("abcde")
      end

      it "matches regexp against regexp" do
        @double.should_receive(:random_call).with(/bcd/)
        @double.random_call(/bcd/)
      end

      it "matches against a hash submitted and received by value" do
        @double.should_receive(:random_call).with(:a => "a", :b => "b")
        @double.random_call(:a => "a", :b => "b")
      end

      it "matches against a hash submitted by reference and received by value" do
        opts = {:a => "a", :b => "b"}
        @double.should_receive(:random_call).with(opts)
        @double.random_call(:a => "a", :b => "b")
      end

      it "matches against a hash submitted by value and received by reference" do
        opts = {:a => "a", :b => "b"}
        @double.should_receive(:random_call).with(:a => "a", :b => "b")
        @double.random_call(opts)
      end
    end
  end
end