Class: RSpec::Core::Configuration
- Inherits:
-
Object
- Object
- RSpec::Core::Configuration
- Includes:
- Hooks
- Defined in:
- lib/rspec/core/configuration.rb
Defined Under Namespace
Classes: MustBeConfiguredBeforeExampleGroupsError
Constant Summary collapse
- CONDITIONAL_FILTERS =
{ :if => lambda { |value, | .has_key?(:if) && !value }, :unless => lambda { |value| value } }
- DEFAULT_BACKTRACE_PATTERNS =
[ /\/lib\d*\/ruby\//, /org\/jruby\//, /bin\//, /gems/, /spec\/spec_helper\.rb/, /lib\/rspec\/(core|expectations|matchers|mocks)/ ]
Class Method Summary collapse
Instance Method Summary collapse
- #add_formatter(formatter_to_use, path = nil) ⇒ Object (also: #formatter=)
- #add_location(file_path, line_numbers) ⇒ Object
-
#add_setting(name, opts = {}) ⇒ Object
:call-seq: add_setting(:name) add_setting(:name, :default => “default_value”) add_setting(:name, :alias => :other_setting).
-
#alias_example_to(new_name, *args) ⇒ Object
E.g.
-
#alias_it_should_behave_like_to(new_name, report_label = '') ⇒ Object
Define an alias for it_should_behave_like that allows different language (like “it_has_behavior” or “it_behaves_like”) to be employed when including shared examples.
- #cleaned_from_backtrace?(line) ⇒ Boolean
- #clear_inclusion_filter ⇒ Object
- #color_enabled ⇒ Object
- #color_enabled=(bool) ⇒ Object
- #color_enabled? ⇒ Boolean
- #command ⇒ Object
- #configure_expectation_framework ⇒ Object
- #configure_group(group) ⇒ Object
- #configure_mock_framework ⇒ Object
- #debug=(bool) ⇒ Object
- #exclusion_filter ⇒ Object
- #exclusion_filter=(filter) ⇒ Object
-
#expect_with(*frameworks) ⇒ Object
Sets the expectation framework module(s).
-
#expectation_framework=(framework) ⇒ Object
Delegates to expect_with().
-
#expectation_frameworks ⇒ Object
Returns the configured expectation framework adapter module(s).
- #extend(mod, *args) ⇒ Object
- #files_or_directories_to_run=(*files) ⇒ Object
- #filter_run_excluding(*args) ⇒ Object
- #filter_run_including(*args) ⇒ Object (also: #filter_run)
- #formatters ⇒ Object
- #full_backtrace=(true_or_false) ⇒ Object
- #full_description=(description) ⇒ Object
- #get_files_to_run(files) ⇒ Object
- #include(mod, *args) ⇒ Object
- #inclusion_filter ⇒ Object
- #inclusion_filter=(filter) ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #libs=(libs) ⇒ Object
-
#line_numbers=(line_numbers) ⇒ Object
Run examples defined on
line_numbers
in all files to run. - #load_spec_files ⇒ Object
-
#mock_framework ⇒ Object
Returns the configured mock framework adapter module.
-
#mock_framework=(framework) ⇒ Object
Sets the mock framework adapter module.
-
#mock_with(framework) ⇒ Object
Delegates to mock_framework=(framework).
- #puts(message) ⇒ Object
- #reporter ⇒ Object
- #requires=(paths) ⇒ Object
- #reset ⇒ Object
- #settings ⇒ Object
Methods included from Hooks
#after, #around, #before, #find_hook, #hooks, #run_hook, #run_hook!, #run_hook_filtered
Methods included from MetadataHashBuilder::Common
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
61 62 63 64 65 66 67 |
# File 'lib/rspec/core/configuration.rb', line 61 def initialize @color_enabled = false self.include_or_extend_modules = [] self.files_to_run = [] self.backtrace_clean_patterns = DEFAULT_BACKTRACE_PATTERNS.dup self.exclusion_filter = CONDITIONAL_FILTERS.dup end |
Class Method Details
.add_setting(name, opts = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rspec/core/configuration.rb', line 11 def self.add_setting(name, opts={}) if opts[:alias] alias_method name, opts[:alias] alias_method "#{name}=", "#{opts[:alias]}=" alias_method "#{name}?", "#{opts[:alias]}?" else define_method("#{name}=") {|val| settings[name] = val} define_method(name) { settings.has_key?(name) ? settings[name] : opts[:default] } define_method("#{name}?") { send name } end end |
Instance Method Details
#add_formatter(formatter_to_use, path = nil) ⇒ Object Also known as: formatter=
298 299 300 301 302 303 304 305 |
# File 'lib/rspec/core/configuration.rb', line 298 def add_formatter(formatter_to_use, path=nil) formatter_class = built_in_formatter(formatter_to_use) || custom_formatter(formatter_to_use) || (raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?.") formatters << formatter_class.new(path ? file_at(path) : output) end |
#add_location(file_path, line_numbers) ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'lib/rspec/core/configuration.rb', line 285 def add_location(file_path, line_numbers) # Filter locations is a hash of expanded paths to arrays of line numbers # to match against. # filter_locations = ((self.filter || {})[:locations] ||= {}) (filter_locations[File.(file_path)] ||= []).push(*line_numbers) filter_run({ :locations => filter_locations }) end |
#add_setting(name, opts = {}) ⇒ Object
:call-seq:
add_setting(:name)
add_setting(:name, :default => "default_value")
add_setting(:name, :alias => :other_setting)
Use this to add custom settings to the RSpec.configuration object.
RSpec.configuration.add_setting :foo
Creates three methods on the configuration object, a setter, a getter, and a predicate:
RSpec.configuration.foo=(value)
RSpec.configuration.foo()
RSpec.configuration.foo?() # returns true if foo returns anything but nil or false
Intended for extension frameworks like rspec-rails, so they can add config settings that are domain specific. For example:
RSpec.configure do |c|
c.add_setting :use_transactional_fixtures, :default => true
c.add_setting :use_transactional_examples, :alias => :use_transactional_fixtures
end
Options
add_setting
takes an optional hash that supports the following keys:
:default => "default value"
This sets the default value for the getter and the predicate (which will return true
as long as the value is not false
or nil
).
:alias => :other_setting
Aliases its setter, getter, and predicate, to those for the other_setting
.
112 113 114 |
# File 'lib/rspec/core/configuration.rb', line 112 def add_setting(name, opts={}) self.class.add_setting(name, opts) end |
#alias_example_to(new_name, *args) ⇒ Object
E.g. alias_example_to :crazy_slow, :speed => ‘crazy_slow’ defines crazy_slow as an example variant that has the crazy_slow speed option
355 356 357 358 |
# File 'lib/rspec/core/configuration.rb', line 355 def alias_example_to(new_name, *args) = (args) RSpec::Core::ExampleGroup.alias_example_to(new_name, ) end |
#alias_it_should_behave_like_to(new_name, report_label = '') ⇒ Object
Define an alias for it_should_behave_like that allows different language (like “it_has_behavior” or “it_behaves_like”) to be employed when including shared examples.
Example:
alias_it_should_behave_like_to(:it_has_behavior, 'has behavior:')
allows the user to include a shared example group like:
describe Entity do
it_has_behavior 'sortability' do
let(:sortable) { Entity.new }
end
end
which is reported in the output as:
Entity
has behavior: sortability
# sortability examples here
381 382 383 |
# File 'lib/rspec/core/configuration.rb', line 381 def alias_it_should_behave_like_to(new_name, report_label = '') RSpec::Core::ExampleGroup.alias_it_should_behave_like_to(new_name, report_label) end |
#cleaned_from_backtrace?(line) ⇒ Boolean
128 129 130 |
# File 'lib/rspec/core/configuration.rb', line 128 def cleaned_from_backtrace?(line) backtrace_clean_patterns.any? { |regex| line =~ regex } end |
#clear_inclusion_filter ⇒ Object
124 125 126 |
# File 'lib/rspec/core/configuration.rb', line 124 def clear_inclusion_filter self.inclusion_filter = nil end |
#color_enabled ⇒ Object
232 233 234 |
# File 'lib/rspec/core/configuration.rb', line 232 def color_enabled @color_enabled && output_to_tty? end |
#color_enabled=(bool) ⇒ Object
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/rspec/core/configuration.rb', line 240 def color_enabled=(bool) return unless bool @color_enabled = true if bool && ::RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ unless ENV['ANSICON'] warn "You must use ANSICON 1.31 or later (http://adoxa.110mb.com/ansicon/) to use colour on Windows" @color_enabled = false end end end |
#color_enabled? ⇒ Boolean
236 237 238 |
# File 'lib/rspec/core/configuration.rb', line 236 def color_enabled? color_enabled end |
#command ⇒ Object
326 327 328 |
# File 'lib/rspec/core/configuration.rb', line 326 def command $0.split(File::SEPARATOR).last end |
#configure_expectation_framework ⇒ Object
452 453 454 455 456 |
# File 'lib/rspec/core/configuration.rb', line 452 def configure_expectation_framework expectation_frameworks.each do |framework| RSpec::Core::ExampleGroup.send(:include, framework) end end |
#configure_group(group) ⇒ Object
441 442 443 444 445 446 |
# File 'lib/rspec/core/configuration.rb', line 441 def configure_group(group) include_or_extend_modules.each do |include_or_extend, mod, filters| next unless filters.empty? || group.any_apply?(filters) group.send(include_or_extend, mod) end end |
#configure_mock_framework ⇒ Object
448 449 450 |
# File 'lib/rspec/core/configuration.rb', line 448 def configure_mock_framework RSpec::Core::ExampleGroup.send(:include, mock_framework) end |
#debug=(bool) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/rspec/core/configuration.rb', line 259 def debug=(bool) return unless bool begin require 'ruby-debug' Debugger.start rescue LoadError => e raise <<-EOM #{'*'*50} #{e.} If you have it installed as a ruby gem, then you need to either require 'rubygems' or configure the RUBYOPT environment variable with the value 'rubygems'. #{e.backtrace.join("\n")} #{'*'*50} EOM end end |
#exclusion_filter ⇒ Object
391 392 393 |
# File 'lib/rspec/core/configuration.rb', line 391 def exclusion_filter settings[:exclusion_filter] || {} end |
#exclusion_filter=(filter) ⇒ Object
386 387 388 |
# File 'lib/rspec/core/configuration.rb', line 386 def exclusion_filter=(filter) settings[:exclusion_filter] = filter end |
#expect_with(*frameworks) ⇒ Object
Sets the expectation framework module(s).
frameworks
can be :rspec, :stdlib, or both
Given :rspec, configures rspec/expectations. Given :stdlib, configures test/unit/assertions Given both, configures both
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/rspec/core/configuration.rb', line 208 def expect_with(*frameworks) assert_no_example_groups_defined(:expect_with) settings[:expectation_frameworks] = [] frameworks.each do |framework| case framework when Symbol case framework when :rspec require 'rspec/core/expecting/with_rspec' self.expecting_with_rspec = true when :stdlib require 'rspec/core/expecting/with_stdlib' else raise ArgumentError, "#{framework.inspect} is not supported" end settings[:expectation_frameworks] << RSpec::Core::ExpectationFrameworkAdapter end end end |
#expectation_framework=(framework) ⇒ Object
Delegates to expect_with()
197 198 199 |
# File 'lib/rspec/core/configuration.rb', line 197 def expectation_framework=(framework) expect_with([framework]) end |
#expectation_frameworks ⇒ Object
Returns the configured expectation framework adapter module(s)
191 192 193 194 |
# File 'lib/rspec/core/configuration.rb', line 191 def expectation_frameworks expect_with :rspec unless settings[:expectation_frameworks] settings[:expectation_frameworks] end |
#extend(mod, *args) ⇒ Object
436 437 438 439 |
# File 'lib/rspec/core/configuration.rb', line 436 def extend(mod, *args) filters = (args) include_or_extend_modules << [:extend, mod, filters] end |
#files_or_directories_to_run=(*files) ⇒ Object
320 321 322 323 324 |
# File 'lib/rspec/core/configuration.rb', line 320 def files_or_directories_to_run=(*files) files = files.flatten files << default_path if command == 'rspec' && default_path && files.empty? self.files_to_run = get_files_to_run(files) end |
#filter_run_excluding(*args) ⇒ Object
426 427 428 429 |
# File 'lib/rspec/core/configuration.rb', line 426 def filter_run_excluding(*args) = (args) self.exclusion_filter = (exclusion_filter || {}).merge() end |
#filter_run_including(*args) ⇒ Object Also known as: filter_run
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/rspec/core/configuration.rb', line 403 def filter_run_including(*args) force_overwrite = if args.last.is_a?(Hash) || args.last.is_a?(Symbol) false else args.pop end = (args) if inclusion_filter and inclusion_filter[:line_numbers] || inclusion_filter[:full_description] warn "Filtering by #{.inspect} is not possible since " \ "you are already filtering by #{inclusion_filter.inspect}" else if force_overwrite self.inclusion_filter = else self.inclusion_filter = (inclusion_filter || {}).merge() end end end |
#formatters ⇒ Object
309 310 311 |
# File 'lib/rspec/core/configuration.rb', line 309 def formatters @formatters ||= [] end |
#full_backtrace=(true_or_false) ⇒ Object
228 229 230 |
# File 'lib/rspec/core/configuration.rb', line 228 def full_backtrace=(true_or_false) settings[:backtrace_clean_patterns] = true_or_false ? [] : DEFAULT_BACKTRACE_PATTERNS end |
#full_description=(description) ⇒ Object
294 295 296 |
# File 'lib/rspec/core/configuration.rb', line 294 def full_description=(description) filter_run({ :full_description => /#{description}/ }, true) end |
#get_files_to_run(files) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/rspec/core/configuration.rb', line 330 def get_files_to_run(files) patterns = pattern.split(",") files.map do |file| if File.directory?(file) patterns.map do |pattern| if pattern =~ /^#{file}/ Dir[pattern.strip] else Dir["#{file}/{#{pattern.strip}}"] end end else if file =~ /^(.*?)((?:\:\d+)+)$/ path, lines = $1, $2[1..-1].split(":").map{|n| n.to_i} add_location path, lines path else file end end end.flatten end |
#include(mod, *args) ⇒ Object
431 432 433 434 |
# File 'lib/rspec/core/configuration.rb', line 431 def include(mod, *args) filters = (args) include_or_extend_modules << [:include, mod, filters] end |
#inclusion_filter ⇒ Object
399 400 401 |
# File 'lib/rspec/core/configuration.rb', line 399 def inclusion_filter settings[:inclusion_filter] || {} end |
#inclusion_filter=(filter) ⇒ Object
395 396 397 |
# File 'lib/rspec/core/configuration.rb', line 395 def inclusion_filter=(filter) settings[:inclusion_filter] = filter end |
#libs=(libs) ⇒ Object
251 252 253 |
# File 'lib/rspec/core/configuration.rb', line 251 def libs=(libs) libs.map {|lib| $LOAD_PATH.unshift lib} end |
#line_numbers=(line_numbers) ⇒ Object
Run examples defined on line_numbers
in all files to run.
281 282 283 |
# File 'lib/rspec/core/configuration.rb', line 281 def line_numbers=(line_numbers) filter_run({ :line_numbers => line_numbers.map{|l| l.to_i} }, true) end |
#load_spec_files ⇒ Object
458 459 460 461 |
# File 'lib/rspec/core/configuration.rb', line 458 def load_spec_files files_to_run.map {|f| load File.(f) } raise_if_rspec_1_is_loaded end |
#mock_framework ⇒ Object
Returns the configured mock framework adapter module
133 134 135 136 137 138 |
# File 'lib/rspec/core/configuration.rb', line 133 def mock_framework settings[:mock_framework] ||= begin require 'rspec/core/mocking/with_rspec' RSpec::Core::MockFrameworkAdapter end end |
#mock_framework=(framework) ⇒ Object
Sets the mock framework adapter module.
framework
can be a Symbol or a Module.
Given any of :rspec, :mocha, :flexmock, or :rr, configures the named framework.
Given :nothing, configures no framework. Use this if you don’t use any mocking framework to save a little bit of overhead.
Given a Module, includes that module in every example group. The module should adhere to RSpec’s mock framework adapter API:
setup_mocks_for_rspec
- called before each example
verify_mocks_for_rspec
- called after each example. Framework should raise an exception
when expectations fail
teardown_mocks_for_rspec
- called after verify_mocks_for_rspec (even if there are errors)
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rspec/core/configuration.rb', line 167 def mock_framework=(framework) assert_no_example_groups_defined(:mock_framework) case framework when Module settings[:mock_framework] = framework when String, Symbol require case framework.to_s when /rspec/i 'rspec/core/mocking/with_rspec' when /mocha/i 'rspec/core/mocking/with_mocha' when /rr/i 'rspec/core/mocking/with_rr' when /flexmock/i 'rspec/core/mocking/with_flexmock' else 'rspec/core/mocking/with_absolutely_nothing' end settings[:mock_framework] = RSpec::Core::MockFrameworkAdapter else end end |
#mock_with(framework) ⇒ Object
Delegates to mock_framework=(framework)
141 142 143 |
# File 'lib/rspec/core/configuration.rb', line 141 def mock_with(framework) self.mock_framework = framework end |
#puts(message) ⇒ Object
116 117 118 |
# File 'lib/rspec/core/configuration.rb', line 116 def puts() output_stream.puts() end |
#reporter ⇒ Object
313 314 315 316 317 318 |
# File 'lib/rspec/core/configuration.rb', line 313 def reporter @reporter ||= begin add_formatter('progress') if formatters.empty? Reporter.new(*formatters) end end |
#requires=(paths) ⇒ Object
255 256 257 |
# File 'lib/rspec/core/configuration.rb', line 255 def requires=(paths) paths.map {|path| require path} end |
#reset ⇒ Object
69 70 71 72 |
# File 'lib/rspec/core/configuration.rb', line 69 def reset @reporter = nil @formatters.clear if @formatters end |
#settings ⇒ Object
120 121 122 |
# File 'lib/rspec/core/configuration.rb', line 120 def settings @settings ||= {} end |