class RSpec::Core::Configuration
def self.add_setting(name, opts={})
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
def add_setting(name, opts={})
Aliases its setter, getter, and predicate, to those for the
:alias => :other_setting
will return +true+ as long as the value is not +false+ or +nil+).
This sets the default value for the getter and the predicate (which
:default => "default value"
keys:
+add_setting+ takes an optional hash that supports the following
== Options
end
c.add_setting :use_transactional_examples, :alias => :use_transactional_fixtures
c.add_setting :use_transactional_fixtures, :default => true
RSpec.configure do |c|
settings that are domain specific. For example:
Intended for extension frameworks like rspec-rails, so they can add config
RSpec.configuration.foo?() # returns !!foo
RSpec.configuration.foo()
RSpec.configuration.foo=(value)
and a predicate:
Creates three methods on the configuration object, a setter, a getter,
RSpec.configuration.add_setting :foo
Use this to add custom settings to the RSpec.configuration object.
add_setting(:name, :alias => :other_setting)
add_setting(:name, :default => "default_value")
add_setting(:name)
:call-seq:
def add_setting(name, opts={}) self.class.add_setting(name, opts) end
def alias_example_to(new_name, extra_options={})
E.g. alias_example_to :crazy_slow, :speed => 'crazy_slow' defines
def alias_example_to(new_name, extra_options={}) RSpec::Core::ExampleGroup.alias_example_to(new_name, extra_options) end
def alias_it_should_behave_like_to(new_name, report_label = '')
has behavior: sortability
Entity
which is reported in the output as:
end
end
let(:sortable) { Entity.new }
it_has_behavior 'sortability' do
describe Entity do
allows the user to include a shared example group like:
alias_it_should_behave_like_to(:it_has_behavior, 'has behavior:')
Example:
employed when including shared examples.
language (like "it_has_behavior" or "it_behaves_like") to be
Define an alias for it_should_behave_like that allows different
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
def cleaned_from_backtrace?(line)
def cleaned_from_backtrace?(line) backtrace_clean_patterns.any? { |regex| line =~ regex } end
def clear_inclusion_filter # :nodoc:
def clear_inclusion_filter # :nodoc: self.filter = nil end
def color_enabled=(bool)
def color_enabled=(bool) return unless bool settings[:color_enabled] = true if bool && ::Config::CONFIG['host_os'] =~ /mswin|mingw/ orig_output_stream = settings[:output_stream] begin require 'Win32/Console/ANSI' rescue LoadError warn "You must 'gem install win32console' to use colour on Windows" settings[:color_enabled] = false ensure settings[:output_stream] = orig_output_stream end end end
def configure_mock_framework
def configure_mock_framework require_mock_framework_adapter RSpec::Core::ExampleGroup.send(:include, RSpec::Core::MockFrameworkAdapter) end
def debug=(bool)
def debug=(bool) return unless bool begin require 'ruby-debug' rescue LoadError raise <<-EOM 50} st install ruby-debug to run rspec with the --debug option. have ruby-debug installed as a ruby gem, then you need to either e 'rubygems' or configure the RUBYOPT environment variable with lue 'rubygems'. 50} end end
def extend(mod, filters={})
def extend(mod, filters={}) include_or_extend_modules << [:extend, mod, filters] end
def files_or_directories_to_run=(*files)
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
def filter_run_excluding(options={})
def filter_run_excluding(options={}) self.exclusion_filter = options end
def filter_run_including(options={})
def filter_run_including(options={}) # TODO (DC 2010-07-03) this should probably warn when the unless clause returns true self.filter = options unless filter and filter[:line_number] || filter[:full_description] end
def find_modules(group)
def find_modules(group) include_or_extend_modules.select do |include_or_extend, mod, filters| group.all_apply?(filters) end end
def formatter
def formatter @formatter ||= formatter_class.new(output) end
def formatter=(formatter_to_use)
def formatter=(formatter_to_use) if string_const?(formatter_to_use) && (class_name = eval(formatter_to_use)).is_a?(Class) formatter_class = class_name elsif formatter_to_use.is_a?(Class) formatter_class = formatter_to_use else formatter_class = case formatter_to_use.to_s when 'd', 'doc', 'documentation', 's', 'n', 'spec', 'nested' RSpec::Core::Formatters::DocumentationFormatter when 'h', 'html' RSpec::Core::Formatters::HtmlFormatter when 't', 'textmate' RSpec::Core::Formatters::TextMateFormatter when 'p', 'progress' RSpec::Core::Formatters::ProgressFormatter else raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?." end end self.formatter_class = formatter_class end
def full_backtrace=(bool)
def full_backtrace=(bool) settings[:backtrace_clean_patterns] = [] end
def full_description=(description)
def full_description=(description) filter_run :full_description => /#{description}/ end
def include(mod, filters={})
def include(mod, filters={}) include_or_extend_modules << [:include, mod, filters] end
def libs=(libs)
def libs=(libs) libs.map {|lib| $LOAD_PATH.unshift lib} end
def line_number=(line_number)
def line_number=(line_number) filter_run :line_number => line_number.to_i end
def mock_with(mock_framework)
def mock_with(mock_framework) settings[:mock_framework] = mock_framework end
def puts(message)
def puts(message) output_stream.puts(message) end
def require_files_to_run
def require_files_to_run files_to_run.map {|f| require File.expand_path(f) } end
def require_mock_framework_adapter
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
def requires=(paths)
def requires=(paths) paths.map {|path| require path} end
def settings
def settings @settings ||= {} end
def string_const?(str)
def string_const?(str) str.is_a?(String) && /\A[A-Z][a-zA-Z0-9_:]*\z/ =~ str end