lodestone (owner)

Fork Of

Revisions

gist: 189912 Download_button fork
public
Public Clone URL: git://gist.github.com/189912.git
Embed All Files: show embed
fast_context.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module ShouldaContextExtensions
  def self.included(base)
    base.class_eval do
      alias_method_chain :build, :fast_context
      alias_method_chain :am_subcontext?, :fast_context
    end
  end
 
  def fast_context(name, &blk)
    @fast_subcontexts ||= []
    @fast_subcontexts << Shoulda::FastContext.new(name, self, &blk)
  end
 
  def build_with_fast_context
    build_without_fast_context
    @fast_subcontexts ||= []
    @fast_subcontexts.each {|f| f.build }
  end
 
  def am_subcontext_with_fast_context?
    parent.is_a?(Shoulda::Context) || parent.is_a?(Shoulda::FastContext)
  end
end
 
module Shoulda
  class FastContext < Context
    def create_test_from_should_hash
      test_name = ["test:", full_name, "should", "run_fast"].flatten.join(' ').to_sym
 
      if test_unit_class.instance_methods.include?(test_name.to_s)
        warn " * WARNING: '#{test_name}' is already defined"
      end
 
      context = self
      test_unit_class.send(:define_method, test_name) do
        @shoulda_context = context
        begin
          context.run_parent_setup_blocks(self)
          context.shoulds.each {|s| s[:before].bind(self).call if s[:before] }
          context.run_current_setup_blocks(self)
 
          context.shoulds.each {|should| should[:block].bind(self).call }
        ensure
          context.run_all_teardown_blocks(self)
        end
      end
    end
 
    def build
      create_test_from_should_hash
      subcontexts.each {|context| context.build }
 
      @fast_subcontexts ||= []
      @fast_subcontexts.each {|f| f.build }
 
      print_should_eventuallys
    end
  end
end
 
class ActionController::TestCase
  def self.fast_context(name, &blk)
    if Shoulda.current_context
      Shoulda.current_context.fast_context(name, &blk)
    else
      context = Shoulda::FastContext.new(name, self, &blk)
      context.build
    end
  end
end
 
Shoulda::Context.send :include, ShouldaContextExtensions