Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/output_spec.rb

raw symbols SCIP
1 RSpec.shared_examples "output_to_stream" do |stream_name, matcher_method, helper_module|
2 include helper_module
3 extend helper_module
4
5 it_behaves_like "an RSpec block-only matcher" do
6 let(:matcher) { output(/fo/).send(matcher_method) }
7 def valid_block
8 print_to_stream('foo')
9 end
10 def invalid_block
11 end
12 end
13
14 define_method :matcher do |*args|
15 output(args.first).send(matcher_method)
16 end
17
18 it 'is diffable' do
19 expect(matcher).to be_diffable
20 end
21
22 it 'does not produce warnings when the failure message is accessed first' do
23 expect($VERBOSE).to be_truthy
24 expect { matcher.failure_message }.not_to output.to_stderr
25 end
26
27 context "expect { ... }.to output.#{matcher_method}" do
28 it "passes if the block outputs to #{stream_name}" do
29 expect { print_to_stream 'foo' }.to matcher
30 end
31
32 it "fails if the block does not output to #{stream_name}" do
33 expect {
34 expect {}.to matcher
35 }.to fail_with("expected block to output to #{stream_name}, but did not")
36 end
37 end
38
39 context "expect { ... }.not_to output.#{matcher_method}" do
40 it "passes if the block does not output to #{stream_name}" do
41 expect {}.not_to matcher
42 end
43
44 it "fails if the block outputs to #{stream_name}" do
45 expect {
46 expect { print_to_stream 'foo' }.not_to matcher
47 }.to fail_with("expected block to not output to #{stream_name}, but output \"foo\"")
48 end
49 end
50
51 context "expect { ... }.to output('string').#{matcher_method}" do
52 it "passes if the block outputs that string to #{stream_name}" do
53 expect { print_to_stream 'foo' }.to matcher("foo")
54 end
55
56 it "fails if the block does not output to #{stream_name}" do
57 expect {
58 expect {}.to matcher('foo')
59 }.to fail_with("expected block to output \"foo\" to #{stream_name}, but output nothing")
60 end
61
62 it "fails if the block outputs a different string to #{stream_name}" do
63 expect {
64 expect { print_to_stream 'food' }.to matcher('foo')
65 }.to fail_with("expected block to output \"foo\" to #{stream_name}, but output \"food\"")
66 end
67 end
68
69 context "expect { ... }.to_not output('string').#{matcher_method}" do
70 it "passes if the block outputs a different string to #{stream_name}" do
71 expect { print_to_stream 'food' }.to_not matcher('foo')
72 end
73
74 it "passes if the block does not output to #{stream_name}" do
75 expect {}.to_not matcher('foo')
76 end
77
78 it "fails if the block outputs the same string to #{stream_name}" do
79 expect {
80 expect { print_to_stream 'foo' }.to_not matcher('foo')
81 }.to fail_with("expected block to not output \"foo\" to #{stream_name}, but output \"foo\"")
82 end
83 end
84
85 context "expect { ... }.to output(/regex/).#{matcher_method}" do
86 it "passes if the block outputs a string to #{stream_name} that matches the regex" do
87 expect { print_to_stream 'foo' }.to matcher(/foo/)
88 end
89
90 it "fails if the block does not output to #{stream_name}" do
91 expect {
92 expect {}.to matcher(/foo/)
93 }.to fail_including("expected block to output /foo/ to #{stream_name}, but output nothing\nDiff")
94 end
95
96 it "fails if the block outputs a string to #{stream_name} that does not match" do
97 expect {
98 expect { print_to_stream 'foo' }.to matcher(/food/)
99 }.to fail_including("expected block to output /food/ to #{stream_name}, but output \"foo\"\nDiff")
100 end
101 end
102
103 context "expect { ... }.to_not output(/regex/).#{matcher_method}" do
104 it "passes if the block outputs a string to #{stream_name} that does not match the regex" do
105 expect { print_to_stream 'food' }.to_not matcher(/bar/)
106 end
107
108 it "passes if the block does not output to #{stream_name}" do
109 expect {}.to_not matcher(/foo/)
110 end
111
112 it "fails if the block outputs a string to #{stream_name} that matches the regex" do
113 expect {
114 expect { print_to_stream 'foo' }.to_not matcher(/foo/)
115 }.to fail_including("expected block to not output /foo/ to #{stream_name}, but output \"foo\"\nDiff")
116 end
117 end
118
119 context "expect { ... }.to output(matcher).#{matcher_method}" do
120 it "passes if the block outputs a string to #{stream_name} that passes the given matcher" do
121 expect { print_to_stream 'foo' }.to matcher(a_string_starting_with("f"))
122 end
123
124 it "fails if the block outputs a string to #{stream_name} that does not pass the given matcher" do
125 expect {
126 expect { print_to_stream 'foo' }.to matcher(a_string_starting_with("b"))
127 }.to fail_including("expected block to output a string starting with \"b\" to #{stream_name}, but output \"foo\"\nDiff")
128 end
129 end
130
131 context "expect { ... }.to_not output(matcher).#{matcher_method}" do
132 it "passes if the block does not output a string to #{stream_name} that passes the given matcher" do
133 expect { print_to_stream 'foo' }.to_not matcher(a_string_starting_with("b"))
134 end
135
136 it "fails if the block outputs a string to #{stream_name} that passes the given matcher" do
137 expect {
138 expect { print_to_stream 'foo' }.to_not matcher(a_string_starting_with("f"))
139 }.to fail_including("expected block to not output a string starting with \"f\" to #{stream_name}, but output \"foo\"\nDiff")
140 end
141 end
142 end
143
144 module RSpec
145 module Matchers
146 RSpec.describe "output.to_stderr matcher" do
147 include_examples "output_to_stream", :stderr, :to_stderr, Module.new {
148 def print_to_stream(msg)
149 $stderr.print(msg)
150 end
151 }
152 end
153
154 RSpec.describe "output.to_stdout matcher" do
155 include_examples "output_to_stream", :stdout, :to_stdout, Module.new {
156 def print_to_stream(msg)
157 print(msg)
158 end
159 }
160 end
161
162 RSpec.describe "output.to_stderr_from_any_process matcher" do
163 include_examples "output_to_stream", :stderr, :to_stderr_from_any_process, Module.new {
164 def print_to_stream(msg)
165 if RSpec::Support::OS.windows?
166 system("<nul set /p msg=\"#{msg}\" 1>&2")
167 else
168 system("printf #{msg} 1>&2")
169 end
170 end
171 }
172 end
173
174 RSpec.describe "output.to_stdout_from_any_process matcher" do
175 include_examples "output_to_stream", :stdout, :to_stdout_from_any_process, Module.new {
176 def print_to_stream(msg)
177 if RSpec::Support::OS.windows?
178 system("<nul set /p msg=#{msg}")
179 else
180 system("printf #{msg}")
181 end
182 end
183 }
184 end
185
186 RSpec.describe "output (without `to_stdout` or `to_stderr`)" do
187 it 'raises an error explaining the use is invalid' do
188 expect {
189 expect { print 'foo' }.to output
190 }.to raise_error(/must chain.*to_stdout.*to_stderr/)
191 end
192
193 it 'still provides a description (e.g. when used in a one-liner)' do
194 expect(output("foo").description).to eq('output "foo" to some stream')
195 end
196 end
197
198 RSpec.describe "can capture stdin and stderr" do
199 it "prints diff for both when both fail" do
200 expect {
201 expect {
202 print "foo"; $stderr.print("bar")
203 }.to output(/baz/).to_stdout.and output(/qux/).to_stderr
204 }.to fail_including(
205 'expected block to output /baz/ to stdout, but output "foo"',
206 '...and:',
207 'expected block to output /qux/ to stderr, but output "bar"',
208 'Diff for (output /baz/ to stdout):',
209 '-/baz/',
210 '+"foo"',
211 'Diff for (output /qux/ to stderr):',
212 '-/qux/',
213 '+"bar"'
214 )
215 end
216 end
217 end
218 end