Sha256: 1c522463be7abeee38bc0a7555f59fda01029de4ab5497e18e75c767b154e571

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

module Coulda
  # A factory for Test::Unit::TestCase test methods
  class Scenario
    attr_reader :name, :test_class
    attr_accessor :statements

    def initialize(name, my_feature, &block)
      raise Exception.new("Scenario must have a name") unless name 
      @name = name
      @statements = []
      @my_feature = my_feature
      create_and_provision_test_method_using &block
    end

    # Predicate indicating if the Scenario was provided with an example
    def pending?
      statements.empty? || has_pending_statements?
    end
    
    private

    def create_and_provision_test_method_using(&block)
      collect_scenario_statements_from &block
      define_test_method_using do
        self.class.current_scenario.statements.each do |stmt|
          if stmt[:method]
            if stmt[:block]
              raise Exception.new "Passing a block to a method called-by-name is currently unhandle"
            else
              self.__send__(stmt[:method])
            end
          elsif stmt[:block]
            self.instance_eval &(stmt[:block])
          else
            pending self.class.current_scenario, stmt
            break
          end
        end
      end
    end

    def collect_scenario_statements_from(&block)
      @my_feature.current_scenario = self
      if block_given?
        @my_feature.instance_eval &block 
      end
    end

    def define_test_method_using(&block)
      scenario = self
      @my_feature.send(:define_method, "test_#{@name.downcase.super_custom_underscore}") do
        self.class.current_scenario = scenario
        self.instance_eval &block if block
      end
    end

    def has_pending_statements?
      statements.find { |s| s[:block].nil? && s[:method].nil? }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
coulda-0.6.1 lib/coulda/scenario.rb
coulda-0.6.0 lib/coulda/scenario.rb
coulda-0.5.5 lib/coulda/scenario.rb
coulda-0.5.3 lib/coulda/scenario.rb
coulda-0.5.2 lib/coulda/scenario.rb
coulda-0.5.1 lib/coulda/scenario.rb