Skip to content

Commit

Permalink
describe at runtime configuration of global exposure
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Nov 21, 2013
1 parent 0e10c03 commit 18da5ee
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
14 changes: 4 additions & 10 deletions features/configuration/enable_global_dsl.feature
Expand Up @@ -14,31 +14,25 @@ Feature: Global namespace DSL
Then the output should contain "1 example, 0 failures"

Scenario: when exposing globally is disabled top level dsl no longer works
Given a file named "config.rb" with:
"""ruby
RSpec.configure { |c| c.expose_globally = false }
"""
Given a file named "spec/example_spec.rb" with:
"""ruby
RSpec.configure { |c| c.expose_globally = false }
describe "specs here" do
it "passes" do
end
end
"""
When I run `rspec -r ./config.rb`
When I run `rspec`
Then the output should contain "undefined method `describe'"

Scenario: regardless of setting
Given a file named "config.rb" with:
"""ruby
RSpec.configure { |c| c.expose_globally = true }
"""
Given a file named "spec/example_spec.rb" with:
"""ruby
RSpec.configure { |c| c.expose_globally = true }
RSpec.describe "specs here" do
it "passes" do
end
end
"""
When I run `rspec -r ./config.rb`
When I run `rspec`
Then the output should contain "1 example, 0 failures"
41 changes: 41 additions & 0 deletions lib/rspec/core/dsl.rb
Expand Up @@ -18,6 +18,47 @@ def self.describe(*args, &example_group_block)

module Core
module DSL

class << self
# @private
attr_accessor :top_level
end

# @private
def self.exposed_globally?
@exposed_globally ||= false
end

# Add's the describe method to Module and the top level binding
def self.expose_globally!
return if exposed_globally?

to_define = proc do
def describe(*args, &block)
::RSpec.describe(*args, &block)
end
end

top_level.instance_eval(&to_define)
Module.class_exec(&to_define)
@exposed_globally = true
end

def self.remove_globally!
return unless exposed_globally?

to_undefine = proc do
undef describe
end

top_level.instance_eval(&to_undefine)
Module.class_exec(&to_undefine)
@exposed_globally = false
end

end
end
end

# cature main without an eval
::RSpec::Core::DSL.top_level = self
29 changes: 29 additions & 0 deletions lib/rspec/core/shared_example_group.rb
Expand Up @@ -56,6 +56,35 @@ def shared_example_groups
end
end

# @private
def self.exposed_globally?
@exposed_globally ||= false
end

def self.expose_globally!
return if exposed_globally?

Core::DSL.top_level.instance_eval(&definitions)
Module.class_exec(&definitions)
@exposed_globally = true
end

def self.remove_globally!
return unless exposed_globally?

to_undefine = proc do
undef shared_examples
undef shared_context
undef share_examples_for
undef shared_examples_for
undef shared_example_groups
end

Core::DSL.top_level.instance_eval(&to_undefine)
Module.class_exec(&to_undefine)
@exposed_globally = false
end

end

def self.registry
Expand Down

0 comments on commit 18da5ee

Please sign in to comment.