Module: Spec::Example::ExampleGroupMethods

Includes:
ArgsAndOptions, 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

Attributes included from Subject::ExampleGroupMethods

#explicit_subject_block

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArgsAndOptions

#add_options, #args_and_options, #set_location

Methods included from PredicateMatchers

#define_methods_from_predicate_matchers, #predicate_matchers

Methods included from Subject::ExampleGroupMethods

#its, #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

#locationObject (readonly)

Returns the value of attribute location


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

def location
  @location
end

Class Method Details

.build_description_from(*args) ⇒ Object


8
9
10
11
12
13
14
# File 'lib/spec/example/example_group_methods.rb', line 8

def build_description_from(*args)
  text = args.inject("") do |description, arg|
    description << " " unless (description == "" || arg.to_s =~ /^(\s|\.|#)/)
    description << arg.to_s
  end
  text == "" ? nil : text
end

Instance Method Details

#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

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

def describe(*args, &example_group_block)
  raise Spec::Example::NoDescriptionError.new("example group", caller(0)[1]) if args.empty?
  if example_group_block
    options = add_options(args)
    set_location(options, caller(0)[1])
    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


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

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

#described_typeObject


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

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

#descriptionObject


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

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

#description_argsObject


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

def description_args
  @description_args ||= []
end

#description_partsObject

:nodoc:


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

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_proxy = ExampleProxy.new(description, options, backtrace || caller(0)[1])
  example_proxies << example_proxy
  example_implementations[example_proxy] = implementation || pending_implementation
  example_proxy
end

#example_group_hierarchyObject


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

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

#example_implementationsObject

:nodoc:


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

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

#example_proxiesObject

:nodoc:


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

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

#examples(run_options = nil) ⇒ Object

:nodoc:


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

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

#include_constants_in(mod) ⇒ Object


164
165
166
# File 'lib/spec/example/example_group_methods.rb', line 164

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

#inherited(klass) ⇒ Object

:nodoc:


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

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

#let(name, &block) ⇒ Object


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

def let(name, &block)
  define_method name do
    @assignments ||= {}
    @assignments[name] ||= instance_eval(&block)
  end
end

#let!(name, &block) ⇒ Object


175
176
177
178
# File 'lib/spec/example/example_group_methods.rb', line 175

def let!(name, &block)
  let(name, &block)
  before { __send__(name) }
end

#nested_descriptionsObject


160
161
162
# File 'lib/spec/example/example_group_methods.rb', line 160

def nested_descriptions
  example_group_hierarchy.nested_descriptions
end

#notify(reporter) ⇒ Object

:nodoc:


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

def notify(reporter) # :nodoc:
  reporter.example_group_started(ExampleGroupProxy.new(self))
end

#number_of_examplesObject

:nodoc:


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

def number_of_examples #:nodoc:
  example_proxies.length
end

#optionsObject

:nodoc:


24
25
26
# File 'lib/spec/example/example_group_methods.rb', line 24

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

#pending_implementationObject


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

def pending_implementation
  lambda {|*args| raise(Spec::Example::NotYetImplementedError) }
end

#run(run_options) ⇒ Object


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

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  = run_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


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

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

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

Use this to temporarily disable an example.


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

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