Module: Spec::Example::ExampleGroupMethods

Includes:
BeforeAndAfterHooks
Included in:
ExampleGroup, SharedExampleGroup, Test::Unit::TestCase
Defined in:
lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BeforeAndAfterHooks

#after_all_parts, #after_each_parts, #append_after, #append_before, #before_all_parts, #before_each_parts, #prepend_after, #prepend_before, #remove_after, #setup, #teardown

Class Attribute Details

.matcher_classObject

Returns the value of attribute matcher_class.



8
9
10
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 8

def matcher_class
  @matcher_class
end

Instance Attribute Details

#description_optionsObject (readonly) Also known as: options

Returns the value of attribute description_options.



18
19
20
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 18

def description_options
  @description_options
end

#description_textObject (readonly)

Returns the value of attribute description_text.



18
19
20
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 18

def description_text
  @description_text
end

#spec_pathObject (readonly)

Returns the value of attribute spec_path.



18
19
20
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 18

def spec_path
  @spec_path
end

Class Method Details

.description_text(*args) ⇒ Object



10
11
12
13
14
15
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 10

def description_text(*args)
  args.inject("") do |result, arg|
    result << " " unless (result == "" || arg.to_s =~ /^(\s|\.|#)/)
    result << arg.to_s
  end
end

Instance Method Details

#create_nested_example_group(args, example_group_block) ⇒ Object



65
66
67
68
69
70
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 65

def create_nested_example_group(args, example_group_block)
  self.subclass("Subclass") do
    describe(*args)
    module_eval(&example_group_block)
  end
end

#create_shared_example_group(args, example_group_block) ⇒ Object



61
62
63
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 61

def create_shared_example_group(args, example_group_block)
  SharedExampleGroup.new(*args, &example_group_block)
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


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 45

def describe(*args, &example_group_block)
  args << {} unless Hash === args.last
  if example_group_block
    options = args.last
    options[:spec_path] = eval("caller(0)[1]", example_group_block) unless options[:spec_path]
    if options[:shared]
      create_shared_example_group(args, example_group_block)
    else
      create_nested_example_group(args, example_group_block)
    end
  else
    set_description(*args)
  end
end

#described_typeObject



154
155
156
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 154

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

#descriptionObject



145
146
147
148
149
150
151
152
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 145

def description
  result = ExampleGroupMethods.description_text(*description_parts)
  if result.nil? || result == ""
    return to_s
  else
    result
  end
end

#description_argsObject



21
22
23
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 21

def description_args
  @description_args ||= []
end

#description_partsObject

:nodoc:



158
159
160
161
162
163
164
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 158

def description_parts #:nodoc:
  parts = []
  each_ancestor_example_group_class do |example_group_class|
    parts << example_group_class.description_args
  end
  parts.flatten.compact
end

#example(description = nil, options = {}, &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.



114
115
116
117
118
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 114

def example(description=nil, options={}, &implementation)
  e = new(description, options, &implementation)
  example_objects << e
  e
end

#examplesObject

:nodoc:



175
176
177
178
179
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 175

def examples #:nodoc:
  examples = example_objects.dup
  add_method_examples(examples)
  Spec::Runner.options.reverse ? examples.reverse : examples
end

#inherited(klass) ⇒ Object



25
26
27
28
29
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 25

def inherited(klass)
  super
  klass.register {}
  Spec::Runner.register_at_exit_hook
end

#it_should_behave_like(*shared_example_groups) ⇒ Object

Use this to pull in examples from shared example groups. See Spec::Runner for information about shared example groups.



74
75
76
77
78
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 74

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

#number_of_examplesObject

:nodoc:



181
182
183
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 181

def number_of_examples #:nodoc:
  examples.length
end

#predicate_matchersObject

:call-seq:

predicate_matchers[matcher_name] = method_on_object
predicate_matchers[matcher_name] = [method1_on_object, method2_on_object]

Dynamically generates a custom matcher that will match a predicate on your class. RSpec provides a couple of these out of the box:

exist (or state expectations)
  File.should exist("path/to/file")

an_instance_of (for mock argument constraints)
  mock.should_receive(:message).with(an_instance_of(String))

Examples

class Fish
  def can_swim?
    true
  end
end

describe Fish do
  predicate_matchers[:swim] = :can_swim?
  it "should swim" do
    Fish.new.should swim
  end
end


108
109
110
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 108

def predicate_matchers
  @predicate_matchers ||= {:an_instance_of => :is_a?}
end

#register(&registration_binding_block) ⇒ Object



193
194
195
196
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 193

def register(&registration_binding_block)
  @registration_binding_block = registration_binding_block
  Spec::Runner.options.add_example_group self
end

#registration_backtraceObject



202
203
204
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 202

def registration_backtrace
  eval("caller", @registration_binding_block)
end

#resetObject

Only used from RSpec’s own examples



186
187
188
189
190
191
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 186

def reset # :nodoc:
  @before_all_parts = nil
  @after_all_parts = nil
  @before_each_parts = nil
  @after_each_parts = nil
end

#runObject



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 131

def run
  examples = examples_to_run
  reporter.add_example_group(self) unless examples_to_run.empty?
  return true if examples.empty?
  return dry_run(examples) if dry_run?

  plugin_mock_framework
  define_methods_from_predicate_matchers

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

#run_after_each(example) ⇒ Object



212
213
214
215
216
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 212

def run_after_each(example)
  each_ancestor_example_group_class(:superclass_first) do |example_group_class|
    example.eval_each_fail_slow(example_group_class.after_each_parts)
  end
end

#run_before_each(example) ⇒ Object



206
207
208
209
210
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 206

def run_before_each(example)
  each_ancestor_example_group_class do |example_group_class|
    example.eval_each_fail_fast(example_group_class.before_each_parts)
  end
end

#set_description(*args) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 166

def set_description(*args)
  args, options = args_and_options(*args)
  @description_args = args
  @description_options = options
  @description_text = ExampleGroupMethods.description_text(*args)
  @spec_path = File.expand_path(options[:spec_path]) if options[:spec_path]
  self
end

#unregisterObject

:nodoc:



198
199
200
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 198

def unregister #:nodoc:
  Spec::Runner.options.remove_example_group self
end

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

Use this to temporarily disable an example.



124
125
126
# File 'lib/gems/rspec-1.1.11/lib/spec/example/example_group_methods.rb', line 124

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