|
1 |
| -require 'spec_helper' |
| 1 | +require 'spec_helper_unit' |
2 | 2 |
|
3 | 3 | describe RSpec::Puppet::FunctionMatchers::Run do
|
4 |
| - let (:wrapper) { double("function wrapper") } |
| 4 | + subject(:matcher) { described_class.new } |
5 | 5 |
|
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) } |
34 | 7 |
|
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 |
42 | 14 | end
|
| 15 | + end |
43 | 16 |
|
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 |
47 | 21 | end
|
48 | 22 | end
|
| 23 | + end |
| 24 | + end |
49 | 25 |
|
50 |
| - context "and_raise_error(ArgumentError)" do |
51 |
| - before(:each) { subject.and_raise_error(ArgumentError) } |
| 26 | + describe '#with_params' do |
52 | 27 |
|
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 |
57 | 29 |
|
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 } } |
64 | 33 |
|
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 |
69 | 35 | end
|
70 | 36 |
|
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 |
90 | 39 |
|
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) |
95 | 43 | end
|
96 | 44 | end
|
97 | 45 | end
|
|
0 commit comments