Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
rcov/ruby/1.8/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb | 470 | 314 | 64.26%
|
49.36%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 require "rbconfig" |
2 |
3 module RSpec |
4 module Core |
5 class Configuration |
6 include RSpec::Core::Hooks |
7 |
8 def self.add_setting(name, opts={}) |
9 if opts[:alias] |
10 alias_method name, opts[:alias] |
11 alias_method "#{name}=", "#{opts[:alias]}=" |
12 alias_method "#{name}?", "#{opts[:alias]}?" |
13 else |
14 define_method("#{name}=") {|val| settings[name] = val} |
15 define_method(name) { settings.has_key?(name) ? settings[name] : opts[:default] } |
16 define_method("#{name}?") { send name } |
17 end |
18 end |
19 |
20 add_setting :error_stream |
21 add_setting :output_stream |
22 add_setting :output, :alias => :output_stream |
23 add_setting :out, :alias => :output_stream |
24 add_setting :drb |
25 add_setting :drb_port |
26 add_setting :profile_examples |
27 add_setting :fail_fast |
28 add_setting :run_all_when_everything_filtered |
29 add_setting :filter |
30 add_setting :exclusion_filter |
31 add_setting :filename_pattern, :default => '**/*_spec.rb' |
32 add_setting :files_to_run |
33 add_setting :include_or_extend_modules |
34 add_setting :backtrace_clean_patterns |
35 add_setting :tty |
36 |
37 def initialize |
38 @color_enabled = false |
39 self.include_or_extend_modules = [] |
40 self.files_to_run = [] |
41 self.backtrace_clean_patterns = [ |
42 /\/lib\d*\/ruby\//, |
43 /bin\//, |
44 /gems/, |
45 /spec\/spec_helper\.rb/, |
46 /lib\/rspec\/(core|expectations|matchers|mocks)/ |
47 ] |
48 |
49 filter_run_excluding( |
50 :if => lambda { |value, metadata| metadata.has_key?(:if) && !value }, |
51 :unless => lambda { |value| value } |
52 ) |
53 end |
54 |
55 # :call-seq: |
56 # add_setting(:name) |
57 # add_setting(:name, :default => "default_value") |
58 # add_setting(:name, :alias => :other_setting) |
59 # |
60 # Use this to add custom settings to the RSpec.configuration object. |
61 # |
62 # RSpec.configuration.add_setting :foo |
63 # |
64 # Creates three methods on the configuration object, a setter, a getter, |
65 # and a predicate: |
66 # |
67 # RSpec.configuration.foo=(value) |
68 # RSpec.configuration.foo() |
69 # RSpec.configuration.foo?() # returns true if foo returns anything but nil or false |
70 # |
71 # Intended for extension frameworks like rspec-rails, so they can add config |
72 # settings that are domain specific. For example: |
73 # |
74 # RSpec.configure do |c| |
75 # c.add_setting :use_transactional_fixtures, :default => true |
76 # c.add_setting :use_transactional_examples, :alias => :use_transactional_fixtures |
77 # end |
78 # |
79 # == Options |
80 # |
81 # +add_setting+ takes an optional hash that supports the following |
82 # keys: |
83 # |
84 # :default => "default value" |
85 # |
86 # This sets the default value for the getter and the predicate (which |
87 # will return +true+ as long as the value is not +false+ or +nil+). |
88 # |
89 # :alias => :other_setting |
90 # |
91 # Aliases its setter, getter, and predicate, to those for the |
92 # +other_setting+. |
93 def add_setting(name, opts={}) |
94 self.class.add_setting(name, opts) |
95 end |
96 |
97 def puts(message) |
98 output_stream.puts(message) |
99 end |
100 |
101 def settings |
102 @settings ||= {} |
103 end |
104 |
105 def clear_inclusion_filter # :nodoc: |
106 self.filter = nil |
107 end |
108 |
109 def cleaned_from_backtrace?(line) |
110 backtrace_clean_patterns.any? { |regex| line =~ regex } |
111 end |
112 |
113 # Returns the configured mock framework adapter module |
114 def mock_framework |
115 settings[:mock_framework] ||= begin |
116 require 'rspec/core/mocking/with_rspec' |
117 RSpec::Core::MockFrameworkAdapter |
118 end |
119 end |
120 |
121 # Delegates to mock_framework=(framework) |
122 def mock_with(framework) |
123 self.mock_framework = framework |
124 end |
125 |
126 # Sets the mock framework adapter module. |
127 # |
128 # +framework+ can be a Symbol or a Module. |
129 # |
130 # Given any of :rspec, :mocha, :flexmock, or :rr, configures the named |
131 # framework. |
132 # |
133 # Given :nothing, configures no framework. Use this if you don't use any |
134 # mocking framework to save a little bit of overhead. |
135 # |
136 # Given a Module, includes that module in every example group. The module |
137 # should adhere to RSpec's mock framework adapter API: |
138 # |
139 # setup_mocks_for_rspec |
140 # - called before each example |
141 # |
142 # verify_mocks_for_rspec |
143 # - called after each example. Framework should raise an exception |
144 # when expectations fail |
145 # |
146 # teardown_mocks_for_rspec |
147 # - called after verify_mocks_for_rspec (even if there are errors) |
148 def mock_framework=(framework) |
149 case framework |
150 when Module |
151 settings[:mock_framework] = framework |
152 when String, Symbol |
153 require case framework.to_s |
154 when /rspec/i |
155 'rspec/core/mocking/with_rspec' |
156 when /mocha/i |
157 'rspec/core/mocking/with_mocha' |
158 when /rr/i |
159 'rspec/core/mocking/with_rr' |
160 when /flexmock/i |
161 'rspec/core/mocking/with_flexmock' |
162 else |
163 'rspec/core/mocking/with_absolutely_nothing' |
164 end |
165 settings[:mock_framework] = RSpec::Core::MockFrameworkAdapter |
166 else |
167 end |
168 end |
169 |
170 # Returns the configured expectation framework adapter module(s) |
171 def expectation_frameworks |
172 settings[:expectation_frameworks] ||= begin |
173 require 'rspec/core/expecting/with_rspec' |
174 [RSpec::Core::ExpectationFrameworkAdapter] |
175 end |
176 end |
177 |
178 # Delegates to expect_with=([framework]) |
179 def expectation_framework=(framework) |
180 expect_with([framework]) |
181 end |
182 |
183 # Sets the expectation framework module(s). |
184 # |
185 # +frameworks+ can be :rspec, :stdlib, or both |
186 # |
187 # Given :rspec, configures rspec/expectations. |
188 # Given :stdlib, configures test/unit/assertions |
189 # Given both, configures both |
190 def expect_with(*frameworks) |
191 settings[:expectation_frameworks] = [] |
192 frameworks.each do |framework| |
193 case framework |
194 when Symbol |
195 case framework |
196 when :rspec |
197 require 'rspec/core/expecting/with_rspec' |
198 when :stdlib |
199 require 'rspec/core/expecting/with_stdlib' |
200 else |
201 raise ArgumentError, "#{framework.inspect} is not supported" |
202 end |
203 settings[:expectation_frameworks] << RSpec::Core::ExpectationFrameworkAdapter |
204 end |
205 end |
206 end |
207 |
208 def full_backtrace=(bool) |
209 settings[:backtrace_clean_patterns] = [] |
210 end |
211 |
212 def color_enabled |
213 @color_enabled && output_to_tty? |
214 end |
215 |
216 def color_enabled? |
217 color_enabled |
218 end |
219 |
220 def color_enabled=(bool) |
221 return unless bool |
222 @color_enabled = true |
223 if bool && ::RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ |
224 unless ENV['ANSICON'] |
225 warn "You must use ANSICON 1.31 or later (http://adoxa.110mb.com/ansicon/) to use colour on Windows" |
226 @color_enabled = false |
227 end |
228 end |
229 end |
230 |
231 def libs=(libs) |
232 libs.map {|lib| $LOAD_PATH.unshift lib} |
233 end |
234 |
235 def requires=(paths) |
236 paths.map {|path| require path} |
237 end |
238 |
239 def debug=(bool) |
240 return unless bool |
241 begin |
242 require 'ruby-debug' |
243 Debugger.start |
244 rescue LoadError |
245 raise <<-EOM |
246 |
247 #{'*'*50} |
248 You must install ruby-debug to run rspec with the --debug option. |
249 |
250 If you have ruby-debug installed as a ruby gem, then you need to either |
251 require 'rubygems' or configure the RUBYOPT environment variable with |
252 the value 'rubygems'. |
253 #{'*'*50} |
254 EOM |
255 end |
256 end |
257 |
258 def line_number=(line_number) |
259 filter_run({ :line_number => line_number.to_i }, true) |
260 end |
261 |
262 def full_description=(description) |
263 filter_run({ :full_description => /#{description}/ }, true) |
264 end |
265 |
266 def add_formatter(formatter_to_use, output=nil) |
267 formatter_class = |
268 built_in_formatter(formatter_to_use) || |
269 custom_formatter(formatter_to_use) || |
270 (raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?.") |
271 |
272 formatters << formatter_class.new(output ? File.new(output, 'w') : self.output) |
273 end |
274 |
275 alias_method :formatter=, :add_formatter |
276 |
277 def formatters |
278 @formatters ||= [] |
279 end |
280 |
281 def reporter |
282 @reporter ||= begin |
283 add_formatter('progress') if formatters.empty? |
284 Reporter.new(*formatters) |
285 end |
286 end |
287 |
288 def files_or_directories_to_run=(*files) |
289 self.files_to_run = files.flatten.collect do |file| |
290 if File.directory?(file) |
291 filename_pattern.split(",").collect do |pattern| |
292 Dir["#{file}/#{pattern.strip}"] |
293 end |
294 else |
295 if file =~ /(\:(\d+))$/ |
296 self.line_number = $2 |
297 file.sub($1,'') |
298 else |
299 file |
300 end |
301 end |
302 end.flatten |
303 end |
304 |
305 # E.g. alias_example_to :crazy_slow, :speed => 'crazy_slow' defines |
306 # crazy_slow as an example variant that has the crazy_slow speed option |
307 def alias_example_to(new_name, extra_options={}) |
308 RSpec::Core::ExampleGroup.alias_example_to(new_name, extra_options) |
309 end |
310 |
311 # Define an alias for it_should_behave_like that allows different |
312 # language (like "it_has_behavior" or "it_behaves_like") to be |
313 # employed when including shared examples. |
314 # |
315 # Example: |
316 # |
317 # alias_it_should_behave_like_to(:it_has_behavior, 'has behavior:') |
318 # |
319 # allows the user to include a shared example group like: |
320 # |
321 # describe Entity do |
322 # it_has_behavior 'sortability' do |
323 # let(:sortable) { Entity.new } |
324 # end |
325 # end |
326 # |
327 # which is reported in the output as: |
328 # |
329 # Entity |
330 # has behavior: sortability |
331 # # sortability examples here |
332 def alias_it_should_behave_like_to(new_name, report_label = '') |
333 RSpec::Core::ExampleGroup.alias_it_should_behave_like_to(new_name, report_label) |
334 end |
335 |
336 def filter_run_including(options={}, force_overwrite = false) |
337 if filter and filter[:line_number] || filter[:full_description] |
338 warn "Filtering by #{options.inspect} is not possible since " \ |
339 "you are already filtering by #{filter.inspect}" |
340 else |
341 if force_overwrite |
342 self.filter = options |
343 else |
344 self.filter = (filter || {}).merge(options) |
345 end |
346 end |
347 end |
348 |
349 alias_method :filter_run, :filter_run_including |
350 |
351 def filter_run_excluding(options={}) |
352 self.exclusion_filter = (exclusion_filter || {}).merge(options) |
353 end |
354 |
355 def include(mod, filters={}) |
356 include_or_extend_modules << [:include, mod, filters] |
357 end |
358 |
359 def extend(mod, filters={}) |
360 include_or_extend_modules << [:extend, mod, filters] |
361 end |
362 |
363 def configure_group(group) |
364 modules = { |
365 :include => group.included_modules.dup, |
366 :extend => group.ancestors.dup |
367 } |
368 |
369 include_or_extend_modules.each do |include_or_extend, mod, filters| |
370 next unless filters.empty? || group.apply?(:any?, filters) |
371 group.send(include_or_extend, mod) |
372 end |
373 end |
374 |
375 def configure_mock_framework |
376 RSpec::Core::ExampleGroup.send(:include, mock_framework) |
377 end |
378 |
379 def configure_expectation_framework |
380 expectation_frameworks.each do |framework| |
381 RSpec::Core::ExampleGroup.send(:include, framework) |
382 end |
383 end |
384 |
385 def load_spec_files |
386 files_to_run.map {|f| load File.expand_path(f) } |
387 raise_if_rspec_1_is_loaded |
388 end |
389 |
390 private |
391 |
392 def raise_if_rspec_1_is_loaded |
393 if defined?(Spec) && defined?(Spec::VERSION::MAJOR) && Spec::VERSION::MAJOR == 1 |
394 raise <<-MESSAGE |
395 |
396 #{'*'*80} |
397 You are running rspec-2, but it seems as though rspec-1 has been loaded as |
398 well. This is likely due to a statement like this somewhere in the specs: |
399 |
400 require 'spec' |
401 |
402 Please locate that statement, remove it, and try again. |
403 #{'*'*80} |
404 MESSAGE |
405 end |
406 end |
407 |
408 def output_to_tty? |
409 begin |
410 output_stream.tty? || tty? |
411 rescue NoMethodError |
412 false |
413 end |
414 end |
415 |
416 def built_in_formatter(key) |
417 case key.to_s |
418 when 'd', 'doc', 'documentation', 's', 'n', 'spec', 'nested' |
419 require 'rspec/core/formatters/documentation_formatter' |
420 RSpec::Core::Formatters::DocumentationFormatter |
421 when 'h', 'html' |
422 require 'rspec/core/formatters/html_formatter' |
423 RSpec::Core::Formatters::HtmlFormatter |
424 when 't', 'textmate' |
425 require 'rspec/core/formatters/text_mate_formatter' |
426 RSpec::Core::Formatters::TextMateFormatter |
427 when 'p', 'progress' |
428 require 'rspec/core/formatters/progress_formatter' |
429 RSpec::Core::Formatters::ProgressFormatter |
430 end |
431 end |
432 |
433 def custom_formatter(formatter_ref) |
434 if Class === formatter_ref |
435 formatter_ref |
436 elsif string_const?(formatter_ref) |
437 begin |
438 eval(formatter_ref) |
439 rescue NameError |
440 require path_for(formatter_ref) |
441 eval(formatter_ref) |
442 end |
443 end |
444 end |
445 |
446 def string_const?(str) |
447 str.is_a?(String) && /\A[A-Z][a-zA-Z0-9_:]*\z/ =~ str |
448 end |
449 |
450 def path_for(const_ref) |
451 underscore_with_fix_for_non_standard_rspec_naming(const_ref) |
452 end |
453 |
454 def underscore_with_fix_for_non_standard_rspec_naming(string) |
455 underscore(string).sub(%r{(^|/)r_spec($|/)}, '\\1rspec\\2') |
456 end |
457 |
458 # activesupport/lib/active_support/inflector/methods.rb, line 48 |
459 def underscore(camel_cased_word) |
460 word = camel_cased_word.to_s.dup |
461 word.gsub!(/::/, '/') |
462 word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') |
463 word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') |
464 word.tr!("-", "_") |
465 word.downcase! |
466 word |
467 end |
468 end |
469 end |
470 end |
Generated on Fri Apr 22 17:22:42 -0700 2011 with rcov 0.9.8