Skip to content
This repository
Newer
Older
100644 95 lines (79 sloc) 2.718 kb
18716c60 »
2010-09-19 Revert "Fix bug with debugger which prevented its use outside rspec e…
1 module RSpec
2 module Core
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
3 # DSL defines methods to group examples, most notably `describe`,
a5a09c10 »
2014-01-14 Cleanup docs a bit.
4 # and exposes them as class methods of {RSpec}. They can also be
5 # exposed globally (on `main` and instances of `Module`) through
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
6 # the {Configuration} option `expose_dsl_globally`.
7 #
8 # By default the methods `describe`, `context` and `example_group`
9 # are exposed. These methods define a named context for one or
10 # more examples. The given block is evaluated in the context of
11 # a generated subclass of {RSpec::Core::ExampleGroup}
12 #
13 # ## Examples:
14 #
15 # RSpec.describe "something" do
16 # context "when something is a certain way" do
17 # it "does something" do
18 # # example code goes here
19 # end
20 # end
21 # end
22 #
23 # @see ExampleGroup
24 # @see ExampleGroup.example_group
3e981117 »
2011-06-01 add some docs and move some things around
25 module DSL
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
26 # @private
27 def self.example_group_aliases
28 @example_group_aliases ||= []
29 end
30
6ea23f2a »
2014-01-22 Ensure example group aliases are exposed globally if so configured.
31 # @private
32 def self.exposed_globally?
33 @exposed_globally ||= false
34 end
35
526ed4e9 »
2014-03-02 Achieve 100% doc coverage
36 # @private
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
37 def self.expose_example_group_alias(name)
38 example_group_aliases << name
39
bf2947d7 »
2014-01-14 Standardize on `__send__` over `send`.
40 (class << RSpec; self; end).__send__(:define_method, name) do |*args, &example_group_block|
1652da5b »
2014-03-17 Remove unnecessary layer of indirection.
41 RSpec.world.register RSpec::Core::ExampleGroup.__send__(name, *args, &example_group_block)
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
42 end
6ea23f2a »
2014-01-22 Ensure example group aliases are exposed globally if so configured.
43
44 expose_example_group_alias_globally(name) if exposed_globally?
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
45 end
46
0005890c »
2013-10-20 describe at runtime configuration of global exposure
47 class << self
48 # @private
49 attr_accessor :top_level
50 end
51
526ed4e9 »
2014-03-02 Achieve 100% doc coverage
52 # Adds the describe method to Module and the top level binding
44f28860 »
2014-03-26 Cleanup some more docs.
53 # @api private
0005890c »
2013-10-20 describe at runtime configuration of global exposure
54 def self.expose_globally!
55 return if exposed_globally?
56
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
57 example_group_aliases.each do |method_name|
6ea23f2a »
2014-01-22 Ensure example group aliases are exposed globally if so configured.
58 expose_example_group_alias_globally(method_name)
0005890c »
2013-10-20 describe at runtime configuration of global exposure
59 end
60
61 @exposed_globally = true
62 end
63
526ed4e9 »
2014-03-02 Achieve 100% doc coverage
64 # Removes the describe method from Module and the top level binding
44f28860 »
2014-03-26 Cleanup some more docs.
65 # @api private
0005890c »
2013-10-20 describe at runtime configuration of global exposure
66 def self.remove_globally!
67 return unless exposed_globally?
68
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
69 example_group_aliases.each do |method_name|
bd90bd2f »
2014-01-22 Cleanup how the DSL is defined.
70 change_global_dsl { undef_method method_name }
0005890c »
2013-10-20 describe at runtime configuration of global exposure
71 end
72
73 @exposed_globally = false
74 end
75
526ed4e9 »
2014-03-02 Achieve 100% doc coverage
76 # @private
6ea23f2a »
2014-01-22 Ensure example group aliases are exposed globally if so configured.
77 def self.expose_example_group_alias_globally(method_name)
bd90bd2f »
2014-01-22 Cleanup how the DSL is defined.
78 change_global_dsl do
79 define_method(method_name) { |*a, &b| ::RSpec.__send__(method_name, *a, &b) }
6ea23f2a »
2014-01-22 Ensure example group aliases are exposed globally if so configured.
80 end
bd90bd2f »
2014-01-22 Cleanup how the DSL is defined.
81 end
6ea23f2a »
2014-01-22 Ensure example group aliases are exposed globally if so configured.
82
526ed4e9 »
2014-03-02 Achieve 100% doc coverage
83 # @private
bd90bd2f »
2014-01-22 Cleanup how the DSL is defined.
84 def self.change_global_dsl(&changes)
85 (class << top_level; self; end).class_exec(&changes)
86 Module.class_exec(&changes)
6ea23f2a »
2014-01-22 Ensure example group aliases are exposed globally if so configured.
87 end
88
18716c60 »
2010-09-19 Revert "Fix bug with debugger which prevented its use outside rspec e…
89 end
90 end
91 end
0005890c »
2013-10-20 describe at runtime configuration of global exposure
92
78a82ada »
2013-12-23 expose example group aliases on RSpec and globally if configured
93 # capture main without an eval
0005890c »
2013-10-20 describe at runtime configuration of global exposure
94 ::RSpec::Core::DSL.top_level = self
Something went wrong with that request. Please try again.