Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

pending declared in example stops execution

  • Loading branch information...
commit 745115cc4a42fa1da727f4004a261300abf5ecbf 1 parent 0481d70
@dchelimsky dchelimsky authored
View
3  features/pending/pending_examples.feature
@@ -16,14 +16,13 @@ Feature: pending examples
And I should see "Not Yet Implemented"
And I should see "example_without_block_spec.rb:2"
-@wip
Scenario: pending any arbitary reason, with no block
Given a file named "pending_without_block_spec.rb" with:
"""
describe "an example" do
it "is implemented but waiting" do
pending("something else getting finished")
- this should not get executed
+ this_should_not_get_executed
end
end
"""
View
124 lib/rspec/core/example.rb
@@ -4,41 +4,77 @@ class Example
attr_reader :metadata, :example_block
- def example_group
- @example_group_class
+ def self.delegate_to_metadata(*keys)
+ keys.each do |key|
+ define_method(key) {@metadata[key]}
+ end
end
- alias_method :behaviour, :example_group
+ delegate_to_metadata :description, :full_description, :execution_result, :file_path, :pending
+
+ alias_method :inspect, :full_description
+ alias_method :to_s, :full_description
def initialize(example_group_class, desc, options, example_block=nil)
@example_group_class, @options, @example_block = example_group_class, options, example_block
@metadata = @example_group_class.metadata.for_example(desc, options)
end
- def description
- @metadata[:description]
+ def example_group
+ @example_group_class
end
- def record_results(results={})
- @metadata[:execution_result].update(results)
- end
+ alias_method :behaviour, :example_group
- def execution_result
- @metadata[:execution_result]
- end
+ def run(example_group_instance, reporter)
+ @example_group_instance = example_group_instance
+ @example_group_instance.running_example = self
- def file_path
- @metadata[:file_path] || example_group.file_path
- end
+ run_started
- def inspect
- @metadata[:full_description]
- end
+ exception = nil
- def to_s
- inspect
+ begin
+ run_before_each
+
+ pending_message = catch(:pending_declared_in_example) do
+ if @example_group_class.around_eachs.empty?
+ @example_group_instance.instance_eval(&example_block) unless pending
+ else
+ @example_group_class.around_eachs.first.call(AroundProxy.new(self, &example_block))
+ end
+ end
+ rescue Exception => e
+ exception = e
+ end
+
+ assign_auto_description
+
+ begin
+ run_after_each
+ rescue Exception => e
+ exception ||= e
+ ensure
+ @example_group_instance.running_example = nil
+ end
+
+ if exception
+ run_failed(reporter, exception)
+ false
+ elsif String === pending_message
+ run_pending(reporter, pending_message)
+ true
+ elsif pending
+ run_pending(reporter, 'Not Yet Implemented')
+ true
+ else
+ run_passed(reporter)
+ true
+ end
end
+ private
+
def run_started
record_results :started_at => Time.now
end
@@ -47,8 +83,7 @@ def run_passed(reporter=nil)
run_finished reporter, 'passed'
end
- def run_pending(reporter=nil)
- message = metadata[:execution_result][:pending_message] || 'Not Yet Implemented'
+ def run_pending(reporter, message)
run_finished reporter, 'pending', :pending_message => message
end
@@ -82,53 +117,10 @@ def assign_auto_description
end
end
- def runnable?
- !metadata[:pending]
- end
-
- def run(example_group_instance, reporter)
- @example_group_instance = example_group_instance
- @example_group_instance.running_example = self
-
- run_started
-
- all_systems_nominal = true
- exception_encountered = nil
-
- begin
- run_before_each
- if @example_group_class.around_eachs.empty?
- @example_group_instance.instance_eval(&example_block) if runnable?
- else
- @example_group_class.around_eachs.first.call(AroundProxy.new(self, &example_block))
- end
- rescue Exception => e
- exception_encountered = e
- all_systems_nominal = false
- end
-
- assign_auto_description
-
- begin
- run_after_each
- rescue Exception => e
- exception_encountered ||= e
- all_systems_nominal = false
- ensure
- @example_group_instance.running_example = nil
- end
-
- if exception_encountered
- run_failed(reporter, exception_encountered)
- else
- runnable? ? run_passed(reporter) : run_pending(reporter)
- end
-
- all_systems_nominal
+ def record_results(results={})
+ execution_result.update(results)
end
-
end
-
end
end
View
15 lib/rspec/core/example_group.rb
@@ -196,10 +196,10 @@ def self.run(reporter)
# Runs all examples, returning true only if all of them pass
def self.run_examples(instance, reporter)
examples_to_run.map do |example|
- success = example.run(instance, reporter)
+ result = example.run(instance, reporter)
instance.__reset__
before_all_ivars.each {|k, v| instance.instance_variable_set(k, v)}
- success
+ result
end.all?
end
@@ -225,19 +225,18 @@ def __reset__
__memoized.clear
end
- def pending(message = nil)
+ def pending(message = 'No reason given')
running_example.metadata[:pending] = true
- running_example.metadata[:execution_result][:pending_message] = message if message
+ running_example.metadata[:execution_result][:pending_message] = message
if block_given?
begin
result = yield
- rescue Exception => e
- end
- if result
running_example.metadata[:pending] = false
- raise Rspec::Core::PendingExampleFixedError.new
+ rescue Exception => e
end
+ raise Rspec::Core::PendingExampleFixedError.new if result
end
+ throw :pending_declared_in_example, message
end
end
View
27 spec/rspec/core/example_spec.rb
@@ -1,44 +1,46 @@
require 'spec_helper'
describe Rspec::Core::Example, :parent_metadata => 'sample' do
-
- before do
- example_group = stub('example_group',
+ let(:example_group) do
+ stub('example_group',
:metadata => Rspec::Core::Metadata.new.process(
'group description',
:caller => ['foo_spec.rb:37']
)
).as_null_object
- @example = Rspec::Core::Example.new(example_group, 'example description', {}, (lambda {}))
+ end
+
+ let(:example) do
+ Rspec::Core::Example.new(example_group, 'example description', {}, (lambda {}))
end
describe "attr readers" do
it "should have one for the parent example group" do
- @example.should respond_to(:example_group)
+ example.should respond_to(:example_group)
end
it "should have one for it's description" do
- @example.should respond_to(:description)
+ example.should respond_to(:description)
end
it "should have one for it's metadata" do
- @example.should respond_to(:metadata)
+ example.should respond_to(:metadata)
end
it "should have one for it's block" do
- @example.should respond_to(:example_block)
+ example.should respond_to(:example_block)
end
end
describe '#inspect' do
it "should return 'group description - description'" do
- @example.inspect.should == 'group description example description'
+ example.inspect.should == 'group description example description'
end
end
describe '#to_s' do
it "should return #inspect" do
- @example.to_s.should == @example.inspect
+ example.to_s.should == example.inspect
end
end
@@ -60,9 +62,8 @@
end
describe "#run" do
- pending "should run after(:each) when the example fails"
-
- pending "should run after(:each) when the example raises an Exception"
+ it "should run after(:each) when the example fails"
+ it "should run after(:each) when the example raises an Exception"
end
end
View
76 spec/rspec/core/pending_example_spec.rb
@@ -1,19 +1,85 @@
require 'spec_helper'
-Rspec::Matchers.define :be_pending do
+Rspec::Matchers.define :be_pending_with do |message|
match do |example|
- example.metadata[:pending]
+ example.metadata[:pending] && example.metadata[:execution_result][:pending_message] == message
+ end
+
+ failure_message_for_should do |example|
+ "expected example to pending with #{message.inspect}, got #{example.metadata[:execution_result][:pending_message].inspect}"
end
end
describe "an example" do
context "with no block" do
- it "is listed as pending" do
+ it "is listed as pending with 'Not Yet Implemented'" do
group = Rspec::Core::ExampleGroup.create('group') do
it "has no block"
end
- group.run(stub('reporter').as_null_object)
- group.examples.first.should be_pending
+ example = group.examples.first
+ example.run(group.new, stub.as_null_object)
+ example.should be_pending_with('Not Yet Implemented')
+ end
+ end
+
+ context "with no args" do
+ it "is listed as pending with 'No reason given'" do
+ group = Rspec::Core::ExampleGroup.create('group') do
+ it "does something" do
+ pending
+ end
+ end
+ example = group.examples.first
+ example.run(group.new, stub.as_null_object)
+ example.should be_pending_with('No reason given')
+ end
+ end
+
+ context "with a message" do
+ it "is listed as pending with the supplied message" do
+ group = Rspec::Core::ExampleGroup.create('group') do
+ it "does something" do
+ pending("just because")
+ end
+ end
+ example = group.examples.first
+ example.run(group.new, stub.as_null_object)
+ example.should be_pending_with('just because')
+ end
+ end
+
+ context "with a block" do
+ context "that fails" do
+ it "is listed as pending with the supplied message" do
+ group = Rspec::Core::ExampleGroup.create('group') do
+ it "does something" do
+ pending("just because") do
+ 3.should == 4
+ end
+ end
+ end
+ example = group.examples.first
+ example.run(group.new, stub.as_null_object)
+ example.should be_pending_with('just because')
+ end
+ end
+
+ context "that passes" do
+ it "raises a PendingExampleFixedError" do
+ group = Rspec::Core::ExampleGroup.create('group') do
+ it "does something" do
+ pending("just because") do
+ 3.should == 3
+ end
+ end
+ end
+ example = group.examples.first
+ example.run(group.new, stub.as_null_object)
+ example.metadata[:pending].should be_false
+ example.metadata[:execution_result][:status].should == 'failed'
+ example.metadata[:execution_result][:exception_encountered].should be_a(Rspec::Core::PendingExampleFixedError)
+ end
end
end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.