Skip to content

Commit

Permalink
replace array in config instead of clearing it
Browse files Browse the repository at this point in the history
- this is only really necessary for rspec-core's own specs, in order to
  be able to interact with multiple instances of Configuration (which
  consumers won't need to do)
  • Loading branch information
dchelimsky committed Jul 5, 2010
1 parent 3b1e67e commit b2c73ee
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 75 deletions.
8 changes: 4 additions & 4 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.add_setting(name, opts={})
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) { settings.has_key?(name) ? settings[name] : opts[:default] }
define_method("#{name}?") { !!(send name) }
end
end
Expand Down Expand Up @@ -40,7 +40,7 @@ def self.add_setting(name, opts={})
/bin\/spec/,
/lib\/rspec\/(core|expectations|matchers|mocks)/
]

# :call-seq:
# add_setting(:name)
# add_setting(:name, :default => "default_value")
Expand Down Expand Up @@ -100,7 +100,7 @@ def cleaned_from_backtrace?(line)
end

def mock_with(mock_framework)
self.mock_framework = mock_framework
settings[:mock_framework] = mock_framework
end

def require_mock_framework_adapter
Expand All @@ -119,7 +119,7 @@ def require_mock_framework_adapter
end

def full_backtrace=(bool)
backtrace_clean_patterns.clear
settings[:backtrace_clean_patterns] = []
end

def color_enabled=(bool)
Expand Down
46 changes: 31 additions & 15 deletions spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,22 @@ def you_call_this_a_blt?

context "with no filter" do
it "includes the given module into each example group" do
config.include(InstanceLevelMethods)

RSpec.configure do |c|
c.include(InstanceLevelMethods)
end

group = ExampleGroup.describe('does like, stuff and junk', :magic_key => :include) { }
group.should_not respond_to(:you_call_this_a_blt?)
group.new.you_call_this_a_blt?.should == "egad man, where's the mayo?!?!?"
end

end

context "with a filter" do
it "includes the given module into each matching example group" do
config.include(InstanceLevelMethods, :magic_key => :include)

RSpec.configure do |c|
c.include(InstanceLevelMethods, :magic_key => :include)
end

group = ExampleGroup.describe('does like, stuff and junk', :magic_key => :include) { }
group.should_not respond_to(:you_call_this_a_blt?)
group.new.you_call_this_a_blt?.should == "egad man, where's the mayo?!?!?"
Expand All @@ -169,7 +172,10 @@ def that_thing
end

it "should extend the given module into each matching example group" do
config.extend(ThatThingISentYou, :magic_key => :extend)
RSpec.configure do |c|
c.extend(ThatThingISentYou, :magic_key => :extend)
end

group = ExampleGroup.describe(ThatThingISentYou, :magic_key => :extend) { }
group.should respond_to(:that_thing)
end
Expand Down Expand Up @@ -304,18 +310,18 @@ def that_thing
end

describe "full_backtrace=" do
before do
@backtrace_clean_patterns = config.backtrace_clean_patterns
end

after do
config.backtrace_clean_patterns = @backtrace_clean_patterns
end

it "clears the backtrace clean patterns" do
config.full_backtrace = true
config.backtrace_clean_patterns.should == []
end

it "doesn't impact other instances of config" do
config_1 = Configuration.new
config_2 = Configuration.new

config_1.full_backtrace = true
config_2.backtrace_clean_patterns.should_not be_empty
end
end

describe "debug=true" do
Expand Down Expand Up @@ -384,10 +390,20 @@ def that_thing
config.custom_option?.should be_true
end

it "can be overridden" do
it "can be overridden with a truthy value" do
config.custom_option = "a new value"
config.custom_option.should eq("a new value")
end

it "can be overridden with nil" do
config.custom_option = nil
config.custom_option.should eq(nil)
end

it "can be overridden with false" do
config.custom_option = false
config.custom_option.should eq(false)
end
end
end

Expand Down
24 changes: 22 additions & 2 deletions spec/rspec/core/formatters/html_formatted-1.8.7.html
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,17 @@ <h1>RSpec Code Examples</h1>
<span class="failed_spec_name">fails</span>
<div class="failure" id="failure_0">
<div class="message"><pre>RSpec::Core::PendingExampleFixedError</pre></div>
<div class="backtrace"><pre>./spec/rspec/core/resources/formatter_specs.rb:19</pre></div>
<div class="backtrace"><pre>./spec/rspec/core/resources/formatter_specs.rb:19
./spec/rspec/core/formatters/html_formatter_spec.rb:22
./spec/rspec/core/formatters/html_formatter_spec.rb:43
./spec/rspec/core/formatters/html_formatter_spec.rb:43:in `open'
./spec/rspec/core/formatters/html_formatter_spec.rb:43
./spec/rspec/core/formatters/html_formatter_spec.rb:42:in `chdir'
./spec/rspec/core/formatters/html_formatter_spec.rb:42
./spec/spec_helper.rb:73
./spec/spec_helper.rb:49:in `instance_eval'
./spec/spec_helper.rb:49:in `sandboxed'
./spec/spec_helper.rb:73</pre></div>
<pre class="ruby"><code><span class="linenum">11</span> <span class="keyword">rescue</span> <span class="constant">Exception</span> <span class="punct">=&gt;</span> <span class="ident">e</span>
<span class="linenum">12</span> <span class="keyword">end</span>
<span class="offending"><span class="linenum">13</span> <span class="keyword">raise</span> <span class="constant">RSpec</span><span class="punct">::</span><span class="constant">Core</span><span class="punct">::</span><span class="constant">PendingExampleFixedError</span><span class="punct">.</span><span class="ident">new</span> <span class="keyword">if</span> <span class="ident">result</span></span>
Expand Down Expand Up @@ -243,7 +253,17 @@ <h1>RSpec Code Examples</h1>

(compared using ==)
</pre></div>
<div class="backtrace"><pre>./spec/rspec/core/resources/formatter_specs.rb:34</pre></div>
<div class="backtrace"><pre>./spec/rspec/core/resources/formatter_specs.rb:34
./spec/rspec/core/formatters/html_formatter_spec.rb:22
./spec/rspec/core/formatters/html_formatter_spec.rb:43
./spec/rspec/core/formatters/html_formatter_spec.rb:43:in `open'
./spec/rspec/core/formatters/html_formatter_spec.rb:43
./spec/rspec/core/formatters/html_formatter_spec.rb:42:in `chdir'
./spec/rspec/core/formatters/html_formatter_spec.rb:42
./spec/spec_helper.rb:73
./spec/spec_helper.rb:49:in `instance_eval'
./spec/spec_helper.rb:49:in `sandboxed'
./spec/spec_helper.rb:73</pre></div>
<pre class="ruby"><code><span class="linenum">27</span> <span class="keyword">end</span>
<span class="linenum">28</span>
<span class="offending"><span class="linenum">29</span> <span class="keyword">raise</span><span class="punct">(</span><span class="constant">RSpec</span><span class="punct">::</span><span class="constant">Expectations</span><span class="punct">::</span><span class="constant">ExpectationNotMetError</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">message</span><span class="punct">))</span></span>
Expand Down
101 changes: 47 additions & 54 deletions spec/rspec/core/formatters/html_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,72 +1,65 @@
# require 'spec_helper'
require 'spec_helper'
require 'nokogiri'

begin # See rescue all the way at the bottom
module RSpec
module Core
module Formatters
describe HtmlFormatter do
let(:jruby?) { ::RUBY_PLATFORM == 'java' }
let(:root) { File.expand_path("#{File.dirname(__FILE__)}/../../../..") }
let(:suffix) { jruby? ? '-jruby' : '' }

require 'nokogiri' # Needed to compare generated with wanted HTML
require 'rspec/core/formatters/html_formatter'

module RSpec
module Core
module Formatters
describe HtmlFormatter do
let(:jruby?) { ::RUBY_PLATFORM == 'java' }
let(:root) { File.expand_path("#{File.dirname(__FILE__)}/../../../..") }
let(:suffix) { jruby? ? '-jruby' : '' }

let(:expected_file) do
"#{File.dirname(__FILE__)}/html_formatted-#{::RUBY_VERSION}#{suffix}.html"
end
let(:expected_file) do
"#{File.dirname(__FILE__)}/html_formatted-#{::RUBY_VERSION}#{suffix}.html"
end

let(:generated_html) do
# TODO (DC 2010-07-03)- in rspec-1 this was all done in memory -
# doing this by shelling out is a HOG, but some things need to be
# untangled between config options, config, world, and runner to
# get it to work right.
seconds = /\d+\.\d+ seconds/
html = `rspec spec/rspec/core/resources/formatter_specs.rb --format html`
html.gsub seconds, 'x seconds'
end
let(:generated_html) do
options = RSpec::Core::ConfigurationOptions.new(
%w[spec/rspec/core/resources/formatter_specs.rb --format html]
)
options.parse_options
err, out = StringIO.new, StringIO.new
command_line = RSpec::Core::CommandLine.new(options)
command_line.run(err, out)
out.string.gsub /\d+\.\d+ seconds/, 'x seconds'
end

let(:expected_html) do
raise "There is no HTML file with expected content for this platform: #{expected_file}" unless File.file?(expected_file)
File.read(expected_file)
end
let(:expected_html) do
raise "There is no HTML file with expected content for this platform: #{expected_file}" unless File.file?(expected_file)
File.read(expected_file)
end

# Uncomment this line temporarily in order to overwrite the expected with actual.
# Use with care!!!
# describe "file generator" do
# it "generates a new comparison file" do
# Dir.chdir(root) do
# File.open(expected_file, 'w') {|io| io.write(generated_html)}
# end
# Uncomment this group temporarily in order to overwrite the expected
# with actual. Use with care!!!
# describe "file generator" do
# it "generates a new comparison file" do
# Dir.chdir(root) do
# File.open(expected_file, 'w') {|io| io.write(generated_html)}
# end
# end
# end

it "should produce HTML identical to the one we designed manually" do
Dir.chdir(root) do
actual_doc = Nokogiri::HTML(generated_html)
actual_backtraces = actual_doc.search("div.backtrace").collect {|e| e.at("pre").inner_html}
actual_doc.css("div.backtrace").remove
it "should produce HTML identical to the one we designed manually" do
Dir.chdir(root) do
actual_doc = Nokogiri::HTML(generated_html)
actual_backtraces = actual_doc.search("div.backtrace").collect {|e| e.at("pre").inner_html}
actual_doc.css("div.backtrace").remove

expected_doc = Nokogiri::HTML(expected_html)
expected_backtraces = expected_doc.search("div.backtrace").collect {|e| e.at("pre").inner_html}
expected_doc.search("div.backtrace").remove
expected_doc = Nokogiri::HTML(expected_html)
expected_backtraces = expected_doc.search("div.backtrace").collect {|e| e.at("pre").inner_html}
expected_doc.search("div.backtrace").remove

actual_doc.inner_html.should == expected_doc.inner_html
actual_doc.inner_html.should == expected_doc.inner_html

expected_backtraces.each_with_index do |expected_line, i|
expected_path, expected_line_number, expected_suffix = expected_line.split(':')
actual_path, actual_line_number, actual_suffix = actual_backtraces[i].split(':')
File.expand_path(actual_path).should == File.expand_path(expected_path)
actual_line_number.should == expected_line_number
end
expected_backtraces.each_with_index do |expected_line, i|
expected_path, expected_line_number, expected_suffix = expected_line.split(':')
actual_path, actual_line_number, actual_suffix = actual_backtraces[i].split(':')
File.expand_path(actual_path).should == File.expand_path(expected_path)
actual_line_number.should == expected_line_number
end
end
end
end
end
end

rescue LoadError
warn "nokogiri not loaded -- skipping HtmlFormatter specs"
end

0 comments on commit b2c73ee

Please sign in to comment.