Module: RSpec::ExampleGroups

Extended by:
Support::RecursiveConstMethods
Defined in:
lib/rspec/core/example_group.rb

Overview

Namespace for the example group subclasses generated by top-level `describe`.

Class Method Summary collapse

Class Method Details

.assign_const(group) ⇒ Object



822
823
824
825
826
827
828
# File 'lib/rspec/core/example_group.rb', line 822

def self.assign_const(group)
  base_name   = base_name_for(group)
  const_scope = constant_scope_for(group)
  name        = disambiguate(base_name, const_scope)

  const_scope.const_set(name, group)
end

.base_name_for(group) ⇒ Object



842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# File 'lib/rspec/core/example_group.rb', line 842

def self.base_name_for(group)
  return "Anonymous".dup if group.description.empty?

  # Convert to CamelCase.
  name = ' ' + group.description
  name.gsub!(/[^0-9a-zA-Z]+([0-9a-zA-Z])/) do
    match = ::Regexp.last_match[1]
    match.upcase!
    match
  end

  name.lstrip!                # Remove leading whitespace
  name.gsub!(/\W/, ''.freeze) # JRuby, RBX and others don't like non-ascii in const names

  # Ruby requires first const letter to be A-Z. Use `Nested`
  # as necessary to enforce that.
  name.gsub!(/\A([^A-Z]|\z)/, 'Nested\1'.freeze)

  name
end

.constant_scope_for(group) ⇒ Object



830
831
832
833
834
# File 'lib/rspec/core/example_group.rb', line 830

def self.constant_scope_for(group)
  const_scope = group.superclass
  const_scope = self if const_scope == ::RSpec::Core::ExampleGroup
  const_scope
end

.disambiguate(name, const_scope) ⇒ Object



875
876
877
878
879
880
881
882
883
# File 'lib/rspec/core/example_group.rb', line 875

def self.disambiguate(name, const_scope)
  return name unless const_defined_on?(const_scope, name)

  # Add a trailing number if needed to disambiguate from an existing
  # constant.
  name << "_2"
  name.next! while const_defined_on?(const_scope, name)
  name
end

.remove_all_constantsObject



836
837
838
839
840
# File 'lib/rspec/core/example_group.rb', line 836

def self.remove_all_constants
  constants.each do |constant|
    __send__(:remove_const, constant)
  end
end