Module: Spec::Example::ExampleGroupMethods

Includes:
BeforeAndAfterHooks, PredicateMatchers, Subject::ExampleGroupMethods
Included in:
ExampleGroup, SharedExampleGroup, Test::Unit::TestCase
Defined in:
lib/spec/example/example_group_methods.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PredicateMatchers

#define_methods_from_predicate_matchers, #predicate_matchers

Methods included from Subject::ExampleGroupMethods

#subject

Methods included from BeforeAndAfterHooks

#after_all_parts, #after_each_parts, after_suite_parts, #after_suite_parts, #append_after, #append_before, #before_all_parts, #before_each_parts, before_suite_parts, #before_suite_parts, #prepend_after, #prepend_before

Class Attribute Details

.matcher_classObject

Returns the value of attribute matcher_class.



6
7
8
# File 'lib/spec/example/example_group_methods.rb', line 6

def matcher_class
  @matcher_class
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/spec/example/example_group_methods.rb', line 13

def options
  @options
end

#spec_pathObject (readonly)

Returns the value of attribute spec_path.



13
14
15
# File 'lib/spec/example/example_group_methods.rb', line 13

def spec_path
  @spec_path
end

Instance Method Details

#backtraceObject

Provides the backtrace up to where this example_group was declared.



21
22
23
# File 'lib/spec/example/example_group_methods.rb', line 21

def backtrace
  @backtrace
end

#describe(*args, &example_group_block) ⇒ Object Also known as: context

Makes the describe/it syntax available from a class. For example:

class StackSpec < Spec::ExampleGroup
  describe Stack, "with no elements"

  before
    @stack = Stack.new
  end

  it "should raise on pop" do
    lambda{ @stack.pop }.should raise_error
  end
end


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spec/example/example_group_methods.rb', line 48

def describe(*args, &example_group_block)
  if example_group_block
    Spec::Example::add_spec_path_to(args)
    options = args.last
    if options[:shared]
      ExampleGroupFactory.create_shared_example_group(*args, &example_group_block)
    else
      subclass(*args, &example_group_block)
    end
  else
    set_description(*args)
  end
end

#described_classObject



127
128
129
# File 'lib/spec/example/example_group_methods.rb', line 127

def described_class
  @described_class ||= Class === described_type ? described_type : nil
end

#described_typeObject



123
124
125
# File 'lib/spec/example/example_group_methods.rb', line 123

def described_type
  @described_type ||= description_parts.reverse.find {|part| part.is_a?(Module)}
end

#descriptionObject



119
120
121
# File 'lib/spec/example/example_group_methods.rb', line 119

def description
  @description ||= build_description_from(*description_parts) || to_s
end

#description_argsObject



131
132
133
# File 'lib/spec/example/example_group_methods.rb', line 131

def description_args
  @description_args ||= []
end

#description_partsObject

:nodoc:



135
136
137
138
139
# File 'lib/spec/example/example_group_methods.rb', line 135

def description_parts #:nodoc:
  @description_parts ||= example_group_hierarchy.inject([]) do |parts, example_group_class|
    [parts << example_group_class.description_args].flatten
  end
end

#example(description = nil, options = {}, backtrace = nil, &implementation) ⇒ Object Also known as: it, specify

Creates an instance of the current example group class and adds it to a collection of examples of the current example group.



72
73
74
75
76
77
# File 'lib/spec/example/example_group_methods.rb', line 72

def example(description=nil, options={}, backtrace=nil, &implementation)
  example_description = ExampleDescription.new(description, options, backtrace || caller(0)[1])
  example_descriptions << example_description
  example_implementations[example_description] = implementation || pending_implementation
  example_description
end

#example_descriptionsObject

:nodoc:



141
142
143
# File 'lib/spec/example/example_group_methods.rb', line 141

def example_descriptions # :nodoc:
  @example_descriptions ||= []
end

#example_group_backtraceObject

Deprecated - use backtrace()



26
27
28
29
30
31
32
# File 'lib/spec/example/example_group_methods.rb', line 26

def example_group_backtrace
  Kernel.warn <<-WARNING
ExampleGroupMethods#example_group_backtrace is deprecated and will be removed
from a future version. Please use ExampleGroupMethods#backtrace instead.
WARNING
  backtrace
end

#example_group_hierarchyObject



157
158
159
# File 'lib/spec/example/example_group_methods.rb', line 157

def example_group_hierarchy
  @example_group_hierarchy ||= ExampleGroupHierarchy.new(self)
end

#example_implementationsObject

:nodoc:



145
146
147
# File 'lib/spec/example/example_group_methods.rb', line 145

def example_implementations # :nodoc:
  @example_implementations ||= {}
end

#examples(run_options = nil) ⇒ Object

:nodoc:



149
150
151
# File 'lib/spec/example/example_group_methods.rb', line 149

def examples(run_options=nil) #:nodoc:
  (run_options && run_options.reverse) ? example_descriptions.reverse : example_descriptions
end

#filtered_description(filter) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/spec/example/example_group_methods.rb', line 161

def filtered_description(filter)
  build_description_from(
    *nested_descriptions.collect do |description|
      description =~ filter ? $1 : description
    end
  )
end

#include_constants_in(mod) ⇒ Object



173
174
175
# File 'lib/spec/example/example_group_methods.rb', line 173

def include_constants_in(mod)
  include mod if (Spec::Ruby.version.to_f >= 1.9) & (Module === mod) & !(Class === mod)
end

#inherited(klass) ⇒ Object

:nodoc:



15
16
17
18
# File 'lib/spec/example/example_group_methods.rb', line 15

def inherited(klass) # :nodoc:
  super
  ExampleGroupFactory.register_example_group(klass)
end

#it_should_behave_like(*shared_example_groups) ⇒ Object

Use this to pull in examples from shared example groups.



64
65
66
67
68
# File 'lib/spec/example/example_group_methods.rb', line 64

def it_should_behave_like(*shared_example_groups)
  shared_example_groups.each do |group|
    include_shared_example_group(group)
  end
end

#nested_descriptionsObject



169
170
171
# File 'lib/spec/example/example_group_methods.rb', line 169

def nested_descriptions
  example_group_hierarchy.nested_descriptions
end

#notify(listener) ⇒ Object

:nodoc:



115
116
117
# File 'lib/spec/example/example_group_methods.rb', line 115

def notify(listener) # :nodoc:
  listener.add_example_group(self)
end

#number_of_examplesObject

:nodoc:



153
154
155
# File 'lib/spec/example/example_group_methods.rb', line 153

def number_of_examples #:nodoc:
  example_descriptions.length
end

#pending_implementationObject



79
80
81
82
# File 'lib/spec/example/example_group_methods.rb', line 79

def pending_implementation
  error = Spec::Example::NotYetImplementedError.new(caller)
  lambda { raise(error) }
end

#run(run_options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/spec/example/example_group_methods.rb', line 95

def run(run_options)
  examples = examples_to_run(run_options)
  notify(run_options.reporter) unless examples.empty?
  return true if examples.empty?
  return dry_run(examples, run_options) if run_options.dry_run?

  define_methods_from_predicate_matchers

  success, before_all_instance_variables = run_before_all(run_options)
  success, after_all_instance_variables  = execute_examples(success, before_all_instance_variables, examples, run_options)
  success                                = run_after_all(success, after_all_instance_variables, run_options)
end

#set_description(*args) ⇒ Object



108
109
110
111
112
113
# File 'lib/spec/example/example_group_methods.rb', line 108

def set_description(*args)
  @description_args, @options = Spec::Example.args_and_options(*args)
  @backtrace = caller(1)
  @spec_path = File.expand_path(options[:spec_path]) if options[:spec_path]
  self
end

#xexample(description = nil, opts = {}, &block) ⇒ Object Also known as: xit, xspecify

Use this to temporarily disable an example.



88
89
90
# File 'lib/spec/example/example_group_methods.rb', line 88

def xexample(description=nil, opts={}, &block)
  Kernel.warn("Example disabled: #{description}")
end