Class: RSpec::Core::Configuration
- Includes:
- Hooks
- Defined in:
- lib/rspec/core/configuration.rb
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#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, extra_options = {}) ⇒ 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
:nodoc:.
- #color_enabled ⇒ Object
- #color_enabled=(bool) ⇒ Object
- #color_enabled? ⇒ Boolean
- #configure_expectation_framework ⇒ Object
- #configure_group(group) ⇒ Object
- #configure_mock_framework ⇒ Object
- #debug=(bool) ⇒ Object
- #expect_with(expectation_framework) ⇒ Object
- #extend(mod, filters = {}) ⇒ Object
- #files_or_directories_to_run=(*files) ⇒ Object
- #filter_run_excluding(options = {}) ⇒ Object
- #filter_run_including(options = {}, force_overwrite = false) ⇒ Object (also: #filter_run)
- #formatter ⇒ Object
- #formatter=(formatter_to_use) ⇒ Object
- #full_backtrace=(bool) ⇒ Object
- #full_description=(description) ⇒ Object
- #include(mod, filters = {}) ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #libs=(libs) ⇒ Object
- #line_number=(line_number) ⇒ Object
- #load_spec_files ⇒ Object
- #mock_with(mock_framework) ⇒ Object
- #puts(message) ⇒ Object
- #reporter ⇒ Object
- #require_expectation_framework_adapter ⇒ Object
- #require_mock_framework_adapter ⇒ Object
- #requires=(paths) ⇒ Object
- #settings ⇒ Object
Methods included from Hooks
#after, #around, #before, #find_hook, #hooks, #run_hook, #run_hook!, #run_hook_filtered
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rspec/core/configuration.rb', line 39 def initialize @color_enabled = false self.include_or_extend_modules = [] self.files_to_run = [] self.backtrace_clean_patterns = [ /\/lib\d*\/ruby\//, /bin\//, /gems/, /spec\/spec_helper\.rb/, /lib\/rspec\/(core|expectations|matchers|mocks)/ ] filter_run_excluding( :if => lambda { |value, | .has_key?(:if) && !value }, :unless => lambda { |value| value } ) end |
Instance Attribute Details
#formatter_class ⇒ Object
212 213 214 215 216 217 |
# File 'lib/rspec/core/configuration.rb', line 212 def formatter_class @formatter_class ||= begin require 'rspec/core/formatters/progress_formatter' RSpec::Core::Formatters::ProgressFormatter end end |
Class Method Details
.add_setting(name, opts = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rspec/core/configuration.rb', line 8 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_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 !!foo
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
.
95 96 97 |
# File 'lib/rspec/core/configuration.rb', line 95 def add_setting(name, opts={}) self.class.add_setting(name, opts) end |
#alias_example_to(new_name, extra_options = {}) ⇒ 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
253 254 255 |
# File 'lib/rspec/core/configuration.rb', line 253 def alias_example_to(new_name, ={}) 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
278 279 280 |
# File 'lib/rspec/core/configuration.rb', line 278 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
111 112 113 |
# File 'lib/rspec/core/configuration.rb', line 111 def cleaned_from_backtrace?(line) backtrace_clean_patterns.any? { |regex| line =~ regex } end |
#clear_inclusion_filter ⇒ Object
:nodoc:
107 108 109 |
# File 'lib/rspec/core/configuration.rb', line 107 def clear_inclusion_filter # :nodoc: self.filter = nil end |
#color_enabled ⇒ Object
151 152 153 |
# File 'lib/rspec/core/configuration.rb', line 151 def color_enabled @color_enabled && (output_to_tty? || autotest?) end |
#color_enabled=(bool) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rspec/core/configuration.rb', line 159 def color_enabled=(bool) return unless bool @color_enabled = true if bool && ::RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ using_stdout = settings[:output_stream] == $stdout using_stderr = settings[:error_stream] == $stderr begin require 'Win32/Console/ANSI' settings[:output_stream] = $stdout if using_stdout settings[:error_stream] = $stderr if using_stderr rescue LoadError warn "You must 'gem install win32console' to use colour on Windows" @color_enabled = false end end end |
#color_enabled? ⇒ Boolean
155 156 157 |
# File 'lib/rspec/core/configuration.rb', line 155 def color_enabled? !!color_enabled end |
#configure_expectation_framework ⇒ Object
328 329 330 331 |
# File 'lib/rspec/core/configuration.rb', line 328 def configure_expectation_framework require_expectation_framework_adapter RSpec::Core::ExampleGroup.send(:include, RSpec::Core::ExpectationFrameworkAdapter) end |
#configure_group(group) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/rspec/core/configuration.rb', line 309 def configure_group(group) modules = { :include => [] + group.included_modules, :extend => [] + group.ancestors } include_or_extend_modules.each do |include_or_extend, mod, filters| next unless group.apply?(:all?, filters) next if modules[include_or_extend].include?(mod) modules[include_or_extend] << mod group.send(include_or_extend, mod) end end |
#configure_mock_framework ⇒ Object
323 324 325 326 |
# File 'lib/rspec/core/configuration.rb', line 323 def configure_mock_framework require_mock_framework_adapter RSpec::Core::ExampleGroup.send(:include, RSpec::Core::MockFrameworkAdapter) end |
#debug=(bool) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rspec/core/configuration.rb', line 184 def debug=(bool) return unless bool begin require 'ruby-debug' rescue LoadError raise <<-EOM #{'*'*50} You must install ruby-debug to run rspec with the --debug option. If you have ruby-debug installed as a ruby gem, then you need to either require 'rubygems' or configure the RUBYOPT environment variable with the value 'rubygems'. #{'*'*50} EOM end end |
#expect_with(expectation_framework) ⇒ Object
134 135 136 |
# File 'lib/rspec/core/configuration.rb', line 134 def expect_with(expectation_framework) settings[:expectation_framework] = expectation_framework end |
#extend(mod, filters = {}) ⇒ Object
305 306 307 |
# File 'lib/rspec/core/configuration.rb', line 305 def extend(mod, filters={}) include_or_extend_modules << [:extend, mod, filters] end |
#files_or_directories_to_run=(*files) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/rspec/core/configuration.rb', line 234 def files_or_directories_to_run=(*files) self.files_to_run = files.flatten.collect do |file| if File.directory?(file) filename_pattern.split(",").collect do |pattern| Dir["#{file}/#{pattern.strip}"] end else if file =~ /(\:(\d+))$/ self.line_number = $2 file.sub($1,'') else file end end end.flatten end |
#filter_run_excluding(options = {}) ⇒ Object
297 298 299 |
# File 'lib/rspec/core/configuration.rb', line 297 def filter_run_excluding(={}) self.exclusion_filter = (exclusion_filter || {}).merge() end |
#filter_run_including(options = {}, force_overwrite = false) ⇒ Object Also known as: filter_run
282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/rspec/core/configuration.rb', line 282 def filter_run_including(={}, force_overwrite = false) if filter and filter[:line_number] || filter[:full_description] warn "Filtering by #{.inspect} is not possible since " \ "you are already filtering by #{filter.inspect}" else if force_overwrite self.filter = else self.filter = (filter || {}).merge() end end end |
#formatter ⇒ Object
226 227 228 |
# File 'lib/rspec/core/configuration.rb', line 226 def formatter @formatter ||= formatter_class.new(output) end |
#formatter=(formatter_to_use) ⇒ Object
219 220 221 222 223 224 |
# File 'lib/rspec/core/configuration.rb', line 219 def formatter=(formatter_to_use) self.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'?.") end |
#full_backtrace=(bool) ⇒ Object
147 148 149 |
# File 'lib/rspec/core/configuration.rb', line 147 def full_backtrace=(bool) settings[:backtrace_clean_patterns] = [] end |
#full_description=(description) ⇒ Object
206 207 208 |
# File 'lib/rspec/core/configuration.rb', line 206 def full_description=(description) filter_run({ :full_description => /#{description}/ }, true) end |
#include(mod, filters = {}) ⇒ Object
301 302 303 |
# File 'lib/rspec/core/configuration.rb', line 301 def include(mod, filters={}) include_or_extend_modules << [:include, mod, filters] end |
#libs=(libs) ⇒ Object
176 177 178 |
# File 'lib/rspec/core/configuration.rb', line 176 def libs=(libs) libs.map {|lib| $LOAD_PATH.unshift lib} end |
#line_number=(line_number) ⇒ Object
202 203 204 |
# File 'lib/rspec/core/configuration.rb', line 202 def line_number=(line_number) filter_run({ :line_number => line_number.to_i }, true) end |
#load_spec_files ⇒ Object
333 334 335 |
# File 'lib/rspec/core/configuration.rb', line 333 def load_spec_files files_to_run.map {|f| load File.(f) } end |
#mock_with(mock_framework) ⇒ Object
115 116 117 |
# File 'lib/rspec/core/configuration.rb', line 115 def mock_with(mock_framework) settings[:mock_framework] = mock_framework end |
#puts(message) ⇒ Object
99 100 101 |
# File 'lib/rspec/core/configuration.rb', line 99 def puts() output_stream.puts() end |
#reporter ⇒ Object
230 231 232 |
# File 'lib/rspec/core/configuration.rb', line 230 def reporter @reporter ||= Reporter.new(formatter) end |
#require_expectation_framework_adapter ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/rspec/core/configuration.rb', line 138 def require_expectation_framework_adapter require case expectation_framework.to_s when /rspec/i 'rspec/core/expecting/with_rspec' else raise ArgumentError, "#{expectation_framework.inspect} is not supported" end end |
#require_mock_framework_adapter ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rspec/core/configuration.rb', line 119 def require_mock_framework_adapter require case mock_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 end |
#requires=(paths) ⇒ Object
180 181 182 |
# File 'lib/rspec/core/configuration.rb', line 180 def requires=(paths) paths.map {|path| require path} end |
#settings ⇒ Object
103 104 105 |
# File 'lib/rspec/core/configuration.rb', line 103 def settings @settings ||= {} end |