Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit dce099d

Browse files
committed
Remove Introspection from RSpec.current_scope
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)`
1 parent c3aa023 commit dce099d

File tree

2 files changed

+2
-47
lines changed

2 files changed

+2
-47
lines changed

lib/rspec/core.rb

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,37 +129,10 @@ def self.current_example=(example)
129129
RSpec::Support.thread_local_data[:current_example] = example
130130
end
131131

132-
class ScopeSymbol
133-
# @api private
134-
EXAMPLE_CONTEXT = [:before_each_hook, :example, :after_each_hook]
135-
136-
# @api private
137-
# Used whenever you set RSpec.current_scope = :something
138-
def self.wrap(thing)
139-
thing.is_a?(self) ? thing : new(thing)
140-
end
141-
142-
# @private
143-
def initialize(sym)
144-
raise ArgumentError, "I only wrap symbols" unless sym.is_a?(Symbol) || sym.nil?
145-
@sym = sym
146-
end
147-
148-
# Allows comparisons w/ real symbols
149-
def ==(other)
150-
@sym == other
151-
end
152-
153-
# Find out whether you're in the execution context of a single example (either before, during or after)
154-
def in_example_context?
155-
EXAMPLE_CONTEXT.include?(@sym)
156-
end
157-
end
158-
159132
# Set the current scope rspec is executing in
160133
# @api private
161134
def self.current_scope=(scope)
162-
RSpec::Support.thread_local_data[:current_scope] = ScopeSymbol.wrap(scope)
135+
RSpec::Support.thread_local_data[:current_scope] = scope
163136
end
164137
RSpec.current_scope = :suite
165138

@@ -175,7 +148,7 @@ def self.current_scope=(scope)
175148
# Returns `:example` inside it/example blocks
176149
#
177150
# Returns `nil` after your suite and all hooks are done
178-
# @return [ScopeSymbol/Nil]
151+
# @return [Symbol/Nil]
179152
def self.current_scope
180153
RSpec::Support.thread_local_data[:current_scope]
181154
end

spec/rspec/core_spec.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,58 +134,44 @@
134134
raise "bad current scope: #{RSpec.current_scope.inspect}"
135135
end
136136

137-
if RSpec.current_scope.in_example_context?
138-
raise "this is not an example context"
139-
end
140-
141137
RSpec.configure do |c|
142138
c.before :suite do
143139
expect(RSpec.current_scope).to eq(:before_suite_hook)
144-
expect(RSpec.current_scope.in_example_context?).to eq(false)
145140
end
146141

147142
c.before :all do
148143
expect(RSpec.current_scope).to eq(:before_all_hook)
149-
expect(RSpec.current_scope.in_example_context?).to eq(false)
150144
end
151145

152146
c.before :each do
153147
expect(RSpec.current_scope).to eq(:before_each_hook)
154-
expect(RSpec.current_scope.in_example_context?).to eq(true)
155148
end
156149

157150
c.around :each do |ex|
158151
expect(RSpec.current_scope).to eq(:before_each_hook)
159-
expect(RSpec.current_scope.in_example_context?).to eq(true)
160152
ex.run
161-
expect(RSpec.current_scope.in_example_context?).to eq(true)
162153
expect(RSpec.current_scope).to eq(:after_each_hook)
163154
end
164155

165156
c.after :each do
166157
expect(RSpec.current_scope).to eq(:after_each_hook)
167-
expect(RSpec.current_scope.in_example_context?).to eq(true)
168158
end
169159

170160
c.after :all do
171161
expect(RSpec.current_scope).to eq(:after_all_hook)
172-
expect(RSpec.current_scope.in_example_context?).to eq(false)
173162
end
174163

175164
c.after :suite do
176165
expect(RSpec.current_scope).to eq(:after_suite_hook)
177-
expect(RSpec.current_scope.in_example_context?).to eq(false)
178166
end
179167
end
180168

181169
before :all do
182-
expect(RSpec.current_scope.in_example_context?).to eq(false)
183170
expect(RSpec.current_scope).to eq(:before_all_hook)
184171
end
185172

186173
before :each do
187174
expect(RSpec.current_scope).to eq(:before_each_hook)
188-
expect(RSpec.current_scope.in_example_context?).to eq(true)
189175
end
190176

191177
around :each do |ex|
@@ -195,23 +181,19 @@
195181
end
196182

197183
after :each do
198-
expect(RSpec.current_scope.in_example_context?).to eq(true)
199184
expect(RSpec.current_scope).to eq(:after_each_hook)
200185
end
201186

202187
after :all do
203188
expect(RSpec.current_scope).to eq(:after_all_hook)
204-
expect(RSpec.current_scope.in_example_context?).to eq(false)
205189
end
206190

207191
it "returns :example inside an example" do
208192
expect(RSpec.current_scope).to eq(:example)
209-
expect(RSpec.current_scope.in_example_context?).to eq(true)
210193
end
211194

212195
it "works for more than one example in a describe block" do
213196
expect(RSpec.current_scope).to eq(:example)
214-
expect(RSpec.current_scope.in_example_context?).to eq(true)
215197
end
216198
end
217199

0 commit comments

Comments
 (0)