Skip to content

Commit 249d24d

Browse files
rodjekDavidS
authored andcommitted
WIP
1 parent cd37eb5 commit 249d24d

File tree

1 file changed

+27
-79
lines changed

1 file changed

+27
-79
lines changed

spec/unit/matchers/run_spec.rb

Lines changed: 27 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,45 @@
1-
require 'spec_helper'
1+
require 'spec_helper_unit'
22

33
describe RSpec::Puppet::FunctionMatchers::Run do
4-
let (:wrapper) { double("function wrapper") }
4+
subject(:matcher) { described_class.new }
55

6-
before :each do
7-
expect(wrapper).to receive(:call).never
8-
end
9-
10-
it 'should call the wrapper with no params' do
11-
expect(wrapper).to receive(:execute).with(no_args).once
12-
expect(subject.matches?(wrapper)).to be true
13-
end
14-
15-
it 'should not match a wrapper that raises an error' do
16-
expect(wrapper).to receive(:execute).and_raise(StandardError, 'Forced Error').once
17-
expect(subject.matches?(wrapper)).to be false
18-
end
19-
20-
[ [true], [false], [''], ['string'], [nil], [0], [1.1], [[]], ['one', 'two'], [{}], [{ 'key' => 'value' }], [:undef] ].each do |supplied_params|
21-
context "with_params(#{supplied_params.collect { |p| p.inspect }.join(', ')})" do
22-
before(:each) { subject.with_params(*supplied_params) }
23-
24-
it 'should call the wrapper with the supplied params' do
25-
expect(wrapper).to receive(:execute).with(*supplied_params).once
26-
expect(subject.matches?(wrapper)).to be true
27-
end
28-
29-
it 'should not match a wrapper that raises an error' do
30-
expect(wrapper).to receive(:execute).and_raise(StandardError, 'Forced Error').once
31-
expect(subject.matches?(wrapper)).to be false
32-
end
33-
end
6+
let(:wrapper) { test_double(RSpec::Puppet::FunctionExampleGroup::V4FunctionWrapper) }
347

35-
[ true, false, '', 'string', nil, 0, 1.1, [], {}, :undef ].each do |expected_return|
36-
context "and_return(#{expected_return.inspect})" do
37-
before(:each) { subject.and_return(expected_return) }
38-
39-
it 'should match a wrapper that does return the requested value' do
40-
expect(wrapper).to receive(:execute).and_return(expected_return).once
41-
expect(subject.matches?(wrapper)).to be true
8+
describe '#matches?' do
9+
context 'when the function takes no arguments and has no expected return value' do
10+
context 'and returns nothing' do
11+
it 'returns true' do
12+
allow(wrapper).to receive(:execute).with(no_args).once
13+
expect(matcher.matches?(wrapper)).to be_truthy
4214
end
15+
end
4316

44-
it 'should not match a wrapper that does return a different value' do
45-
expect(wrapper).to receive(:execute).and_return(!expected_return).once
46-
expect(subject.matches?(wrapper)).to be false
17+
context 'and raises an exception' do
18+
it 'returns false' do
19+
allow(wrapper).to receive(:execute).with(no_args).and_raise(StandardError, 'Forced Error').once
20+
expect(matcher.matches?(wrapper)).to be_falsey
4721
end
4822
end
23+
end
24+
end
4925

50-
context "and_raise_error(ArgumentError)" do
51-
before(:each) { subject.and_raise_error(ArgumentError) }
26+
describe '#with_params' do
5227

53-
it 'should match a wrapper that raises ArgumentError' do
54-
expect(wrapper).to receive(:execute).and_raise(ArgumentError, 'Forced Error').once
55-
expect(subject.matches?(wrapper)).to be true
56-
end
28+
end
5729

58-
[ true, false, '', 'string', nil, 0, 1.1, [], {}, :undef ].each do |value|
59-
it "should not match a wrapper that returns #{value.inspect}" do
60-
expect(wrapper).to receive(:execute).and_return(value).once
61-
expect(subject.matches?(wrapper)).to be false
62-
end
63-
end
30+
describe '#with_lambda' do
31+
context 'when a lambda is passed to the matcher' do
32+
let(:block) { proc { |r| r + 2 } }
6433

65-
it 'should not match a wrapper that raises a different error' do
66-
expect(wrapper).to receive(:execute).and_raise(StandardError, 'Forced Error').once
67-
expect(subject.matches?(wrapper)).to be false
68-
end
34+
before do
6935
end
7036

71-
context "and_raise_error(ArgumentError, /message/)" do
72-
before(:each) { subject.and_raise_error(ArgumentError, /message/) }
73-
74-
it 'should match a wrapper that raises ArgumentError("with matching message")' do
75-
expect(wrapper).to receive(:execute).and_raise(ArgumentError, 'with matching message').once
76-
expect(subject.matches?(wrapper)).to be true
77-
end
78-
79-
it 'should not match a wrapper that raises a different ArgumentError' do
80-
expect(wrapper).to receive(:execute).and_raise(ArgumentError, 'Forced Error').once
81-
expect(subject.matches?(wrapper)).to be false
82-
end
83-
84-
[ true, false, '', 'string', nil, 0, 1.1, [], {}, :undef ].each do |value|
85-
it "should not match a wrapper that returns #{value.inspect}" do
86-
expect(wrapper).to receive(:execute).and_return(value).once
87-
expect(subject.matches?(wrapper)).to be false
88-
end
89-
end
37+
after do
38+
end
9039

91-
it 'should not match a wrapper that raises a different error' do
92-
expect(wrapper).to receive(:execute).and_raise(StandardError, 'Forced Error').once
93-
expect(subject.matches?(wrapper)).to be false
94-
end
40+
it 'passes the lambda when executing the function' do
41+
matcher.with_lambda { |r| r + 2 }
42+
matcher.matches?(wrapper)
9543
end
9644
end
9745
end

0 commit comments

Comments
 (0)