Skip to content

Commit

Permalink
Remove Introspection from RSpec.current_scope
Browse files Browse the repository at this point in the history
My reasoning is thus:

rspec-rails need to do the following condition:
`if RSpec.current_scope == :before_all_hook`

test_prof needs to do the following condition:
`if RSpec.current_scope == :before_all_hook`

amd my little helper needs the following
`unless [:before_each_hook, :example].include?(RSpec.current_scope)`
  • Loading branch information
odinhb committed Jun 20, 2021
1 parent 76f85c1 commit d783bfc
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 47 deletions.
31 changes: 2 additions & 29 deletions lib/rspec/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,37 +129,10 @@ def self.current_example=(example)
RSpec::Support.thread_local_data[:current_example] = example
end

class ScopeSymbol
# @api private
EXAMPLE_CONTEXT = [:before_each_hook, :example, :after_each_hook]

# @api private
# Used whenever you set RSpec.current_scope = :something
def self.wrap(thing)
thing.is_a?(self) ? thing : new(thing)
end

# @private
def initialize(sym)
raise ArgumentError, "I only wrap symbols" unless sym.is_a?(Symbol) || sym.nil?
@sym = sym
end

# Allows comparisons w/ real symbols
def ==(other)
@sym == other
end

# Find out whether you're in the execution context of a single example (either before, during or after)
def in_example_context?
EXAMPLE_CONTEXT.include?(@sym)
end
end

# Set the current scope rspec is executing in
# @api private
def self.current_scope=(scope)
RSpec::Support.thread_local_data[:current_scope] = ScopeSymbol.wrap(scope)
RSpec::Support.thread_local_data[:current_scope] = scope
end
RSpec.current_scope = :suite

Expand All @@ -173,7 +146,7 @@ def self.current_scope=(scope)
# Returns `:example` inside it/example blocks
#
# Returns `nil` after your suite and all hooks are done
# @return [ScopeSymbol/Nil]
# @return [Symbol/Nil]
def self.current_scope
RSpec::Support.thread_local_data[:current_scope]
end
Expand Down
18 changes: 0 additions & 18 deletions spec/rspec/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,58 +134,44 @@
raise "bad current scope: #{RSpec.current_scope.inspect}"
end

if RSpec.current_scope.in_example_context?
raise "this is not an example context"
end

RSpec.configure do |c|
c.before :suite do
expect(RSpec.current_scope).to eq(:before_suite_hook)
expect(RSpec.current_scope.in_example_context?).to eq(false)
end

c.before :all do
expect(RSpec.current_scope).to eq(:before_all_hook)
expect(RSpec.current_scope.in_example_context?).to eq(false)
end

c.before :each do
expect(RSpec.current_scope).to eq(:before_each_hook)
expect(RSpec.current_scope.in_example_context?).to eq(true)
end

c.around :each do |ex|
expect(RSpec.current_scope).to eq(:before_each_hook)
expect(RSpec.current_scope.in_example_context?).to eq(true)
ex.run
expect(RSpec.current_scope.in_example_context?).to eq(true)
expect(RSpec.current_scope).to eq(:after_each_hook)
end

c.after :each do
expect(RSpec.current_scope).to eq(:after_each_hook)
expect(RSpec.current_scope.in_example_context?).to eq(true)
end

c.after :all do
expect(RSpec.current_scope).to eq(:after_all_hook)
expect(RSpec.current_scope.in_example_context?).to eq(false)
end

c.after :suite do
expect(RSpec.current_scope).to eq(:after_suite_hook)
expect(RSpec.current_scope.in_example_context?).to eq(false)
end
end

before :all do
expect(RSpec.current_scope.in_example_context?).to eq(false)
expect(RSpec.current_scope).to eq(:before_all_hook)
end

before :each do
expect(RSpec.current_scope).to eq(:before_each_hook)
expect(RSpec.current_scope.in_example_context?).to eq(true)
end

around :each do |ex|
Expand All @@ -195,23 +181,19 @@
end

after :each do
expect(RSpec.current_scope.in_example_context?).to eq(true)
expect(RSpec.current_scope).to eq(:after_each_hook)
end

after :all do
expect(RSpec.current_scope).to eq(:after_all_hook)
expect(RSpec.current_scope.in_example_context?).to eq(false)
end

it "returns :example inside an example" do
expect(RSpec.current_scope).to eq(:example)
expect(RSpec.current_scope.in_example_context?).to eq(true)
end

it "works for more than one example in a describe block" do
expect(RSpec.current_scope).to eq(:example)
expect(RSpec.current_scope.in_example_context?).to eq(true)
end
end

Expand Down

0 comments on commit d783bfc

Please sign in to comment.