lib/coulda/feature.rb in coulda-0.4.7 vs lib/coulda/feature.rb in coulda-0.5.0

- old
+ new

@@ -1,64 +1,58 @@ -module Coulda - # The Feature class is composed of an intent and a series of zero or more Scenarios that describe the behavior - # that satisfies the intent. Capturing intent is a key feature of Coulda. Intent defines the business value - # the Feature is attempting to fulfill. It serves as a reminder for developers (and customers) as to why this - # Feature was implemented and why this spec was written. - class Feature - include Test::Unit::Assertions +module Test + module Unit + class TestCase + include Coulda - attr_reader :scenarios, :name - attr_accessor :current_scenario, :test_instance + def self.scenarios + @scenarios ||= [] + @scenarios + end - def initialize(name, opts = {}) - World.register_feature(self) - @name = name - @scenarios = [] - Scenario.testcase_class = opts[:testcase_class] if opts[:testcase_class] - end + def self.current_scenario=(scenario) + @current_scenario = scenario + end - %w[in_order_to as_a i_want_to].each do |intent| - eval <<-HERE - # An intent specifier - def #{intent}(val = nil) - val ? @#{intent} = val : @#{intent} - end - HERE - end + def self.current_scenario + @current_scenario + end - %w[Given When Then And].each do |stmt| - eval <<-HERE - # Specifies a prereqisite, event, or expectation. May be used in any order within the spec - def #{stmt}(text, &block) - @current_scenario.statements << { :type => :#{stmt}, :text => text } - @current_scenario.steps << block if block - end - HERE - end + %w[in_order_to as_a i_want_to].each do |intent| + eval <<-HERE + # An intent specifier + def self.#{intent}(val = nil) + val ? @#{intent} = val : @#{intent} + end + HERE + end - # Creates a Scenario instance and adds it to the Feature - def Scenario(scenario_name, &block) - @scenarios << scenario = Scenario.new(scenario_name, self, &block) - scenario - end + %w[Given When Then And].each do |stmt| + eval <<-HERE + # Specifies a prereqisite, event, or expectation. May be used in any order within the spec + def self.#{stmt}(text, &block) + step = nil + if block_given? + current_scenario.steps << step = block + end + caller[0] =~ (/(.*):(.*)(:in)?/) + current_scenario.statements << { :type => :#{stmt}, :text => text, :block => step, :file => $1, :line => $2} + end + HERE + end - # Accessor to the Scenario-created TestCases - def tests - @scenarios.collect { |s| s.test_class } - end - - - # Raises an error if only some of the intent is captured (i.e., in_order_to and as_a without i_want_to) - def assert_presence_of_intent - presence = %w[in_order_to as_a i_want_to].map { |intent| instance_variable_get("@#{intent}") } - if presence.any? { |p| p } && !presence.all? { |p| p } - raise SyntaxError.new("Must have all or none of in_order, as_a, and i_want_to called in a Feature") + # Creates a Scenario instance and adds it to the Feature + def self.Scenario(scenario_name, &block) + @scenarios ||=[] + @scenarios << scenario = Scenario.new(scenario_name, self, &block) + scenario end - end - def method_missing(name, *args) - if test_instance - test_instance.__send__(name, *args) + # Raises an error if only some of the intent is captured (i.e., in_order_to and as_a without i_want_to) + def self.assert_presence_of_intent + presence = %w[in_order_to as_a i_want_to].map { |intent| instance_variable_get("@#{intent}") } + if presence.any? { |p| p } && !presence.all? { |p| p } + raise SyntaxError.new("Must have all or none of in_order, as_a, and i_want_to called in a Feature") + end end end end end