Skip to content

Instantly share code, notes, and snippets.

@lifo
Created September 20, 2009 17:31
Show Gist options
  • Save lifo/189854 to your computer and use it in GitHub Desktop.
Save lifo/189854 to your computer and use it in GitHub Desktop.

Revisions

  1. lifo revised this gist Sep 29, 2009. 1 changed file with 1 addition and 72 deletions.
    73 changes: 1 addition & 72 deletions fast_context.rb
    Original file line number Diff line number Diff line change
    @@ -1,72 +1 @@
    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 ActiveSupport::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
    http://github.com/lifo/fast_context
  2. lifo revised this gist Sep 21, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fast_context.rb
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ def build
    end
    end

    class ActionController::TestCase
    class ActiveSupport::TestCase
    def self.fast_context(name, &blk)
    if Shoulda.current_context
    Shoulda.current_context.fast_context(name, &blk)
  3. lifo revised this gist Sep 20, 2009. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion fast_context.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@ 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

    @@ -15,6 +16,10 @@ def build_with_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
    @@ -64,4 +69,4 @@ def self.fast_context(name, &blk)
    end
    end

    Shoulda::Context.send :include, ShouldaContextExtensions
    Shoulda::Context.send :include, ShouldaContextExtensions
  4. lifo created this gist Sep 20, 2009.
    67 changes: 67 additions & 0 deletions fast_context.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    module ShouldaContextExtensions
    def self.included(base)
    base.class_eval do
    alias_method_chain :build, :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
    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