|
3 | 3 | expect(include("a")).to be_diffable
|
4 | 4 | end
|
5 | 5 |
|
| 6 | + shared_examples_for "a Hash target" do |
| 7 | + def build_target(hsh) |
| 8 | + hsh |
| 9 | + end |
| 10 | + |
| 11 | + it 'passes if target has the expected as a key' do |
| 12 | + expect(build_target(:key => 'value')).to include(:key) |
| 13 | + end |
| 14 | + |
| 15 | + it "fails if target does not include expected" do |
| 16 | + expect { |
| 17 | + expect(build_target(:key => 'value')).to include(:other) |
| 18 | + }.to fail_matching(%Q|expected {:key => "value"} to include :other|) |
| 19 | + end |
| 20 | + |
| 21 | + it "fails if target doesn't have a key and we expect nil" do |
| 22 | + expect { |
| 23 | + expect(build_target({})).to include(:something => nil) |
| 24 | + }.to fail_matching(%Q|expected {} to include {:something => nil}|) |
| 25 | + end |
| 26 | + |
| 27 | + it 'works even when an entry in the hash overrides #send' do |
| 28 | + hash = build_target(:key => 'value') |
| 29 | + def hash.send; :sent; end |
| 30 | + expect(hash).to include(hash) |
| 31 | + end |
| 32 | + |
| 33 | + it 'provides a valid diff' do |
| 34 | + allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false) |
| 35 | + |
| 36 | + expect { |
| 37 | + expect(build_target(:foo => 1, :bar => 2)).to include(:foo => 1, :bar => 3) |
| 38 | + }.to fail_including(dedent(<<-END)) |
| 39 | + |Diff: |
| 40 | + |@@ -1,3 +1,3 @@ |
| 41 | + |-:bar => 3, |
| 42 | + |+:bar => 2, |
| 43 | + | :foo => 1, |
| 44 | + END |
| 45 | + end |
| 46 | + end |
| 47 | + |
6 | 48 | describe "expect(...).to include(with_one_arg)" do
|
7 | 49 | it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do
|
8 | 50 | let(:matcher) { include(2) }
|
|
86 | 128 | end
|
87 | 129 |
|
88 | 130 | context "for a hash target" do
|
89 |
| - it 'passes if target has the expected as a key' do |
90 |
| - expect({:key => 'value'}).to include(:key) |
91 |
| - end |
92 |
| - |
93 |
| - it "fails if target does not include expected" do |
94 |
| - expect { |
95 |
| - expect({:key => 'value'}).to include(:other) |
96 |
| - }.to fail_matching(%Q|expected {:key => "value"} to include :other|) |
97 |
| - end |
98 |
| - |
99 |
| - it "fails if target doesn't have a key and we expect nil" do |
100 |
| - expect { |
101 |
| - expect({}).to include(:something => nil) |
102 |
| - }.to fail_matching(%Q|expected {} to include {:something => nil}|) |
103 |
| - end |
| 131 | + it_behaves_like "a Hash target" |
| 132 | + end |
104 | 133 |
|
105 |
| - it 'works even when an entry in the hash overrides #send' do |
106 |
| - hash = { :key => 'value' } |
107 |
| - def hash.send; :sent; end |
108 |
| - expect(hash).to include(hash) |
| 134 | + context "for a target that can pass for a hash" do |
| 135 | + def build_target(hsh) |
| 136 | + PseudoHash.new(hsh) |
109 | 137 | end
|
110 | 138 |
|
111 |
| - it 'provides a valid diff' do |
112 |
| - allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false) |
113 |
| - |
114 |
| - expect { |
115 |
| - expect(:foo => 1, :bar => 2).to include(:foo => 1, :bar => 3) |
116 |
| - }.to fail_including(dedent(<<-END)) |
117 |
| - |Diff: |
118 |
| - |@@ -1,3 +1,3 @@ |
119 |
| - |-:bar => 3, |
120 |
| - |+:bar => 2, |
121 |
| - | :foo => 1, |
122 |
| - END |
123 |
| - end |
| 139 | + around do |example| |
| 140 | + in_sub_process_if_possible do |
| 141 | + require 'delegate' |
124 | 142 |
|
125 |
| - context 'that overrides #send' do |
126 |
| - it 'still works' do |
127 |
| - array = [1, 2] |
128 |
| - def array.send; :sent; end |
| 143 | + class PseudoHash < SimpleDelegator |
| 144 | + end |
129 | 145 |
|
130 |
| - expect(array).to include(*array) |
| 146 | + example.run |
131 | 147 | end
|
132 | 148 | end
|
| 149 | + |
| 150 | + it_behaves_like "a Hash target" |
133 | 151 | end
|
134 | 152 | end
|
135 | 153 |
|
|
0 commit comments