From 755d8e3ddcfd530ba1ea9c5e0e4c8f159544e684 Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Thu, 22 Jul 2010 15:37:38 +0100 Subject: [PATCH] Refactored so that we do not have multiple examples anymore. --- app/models/feature.rb | 12 ++++++------ app/models/story.rb | 7 +++---- app/views/common/_example_table.html.erb | 12 ++++++------ app/views/common/forms/_story_examples.html.erb | 2 +- features/enhanced/form_submission_via_ajax.feature | 3 +-- features/plain/feature_import.feature | 7 ------- features/step_definitions/feature_file_steps.rb | 12 ++++++------ features/step_definitions/feature_import_steps.rb | 6 +----- features/step_definitions/stories_steps.rb | 2 +- lib/feature_file.rb | 12 ++++++------ spec/models/feature_file_spec.rb | 8 ++++---- spec/models/feature_spec.rb | 6 +++--- 12 files changed, 38 insertions(+), 51 deletions(-) diff --git a/app/models/feature.rb b/app/models/feature.rb index 67c05662..eaebcf87 100644 --- a/app/models/feature.rb +++ b/app/models/feature.rb @@ -118,17 +118,17 @@ def is_unique? end def feature_scenarios story - result = (story.examples.first.blank?)? "\n Scenario:" : "\n Scenario Outline:" + result = (story.example.blank?)? "\n Scenario:" : "\n Scenario Outline:" result += " #{story.scenario}\n#{story_titles story}"unless story.steps.blank? end def feature_examples story - if not story.examples.first.nil? - result = "\n\t\tExamples: #{story.examples.first.heading.lstrip}\n" - story.examples.first.actions.each { |action| result += "\t\t| #{action.title}" } + if not story.example.nil? + result = "\n\t\tExamples: #{story.example.heading.lstrip}\n" + story.example.actions.each { |action| result += "\t\t| #{action.title}" } result += " |\n" - story.examples.first.actions.first.items.each_with_index do |item, index| - story.examples.first.actions.each do |action| + story.example.actions.first.items.each_with_index do |item, index| + story.example.actions.each do |action| result += "\t\t| #{action.items[index].title}" end result += " |\n" diff --git a/app/models/story.rb b/app/models/story.rb index 6189363a..726bf40f 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -14,11 +14,10 @@ class Story < ActiveRecord::Base has_many :step_stories has_many :steps, :through => :step_stories - has_many :example_stories - has_many :examples, :through => :example_stories - + belongs_to :example + accepts_nested_attributes_for :steps - accepts_nested_attributes_for :examples + accepts_nested_attributes_for :example def self.formatted_steps steps last = nil diff --git a/app/views/common/_example_table.html.erb b/app/views/common/_example_table.html.erb index 35c5b5e2..8647e926 100644 --- a/app/views/common/_example_table.html.erb +++ b/app/views/common/_example_table.html.erb @@ -1,21 +1,21 @@ -<% if not story.examples.empty? %> +<% if not story.example.blank? %>
- + - <% story.examples.first.actions.each do |action| %> + <% story.example.actions.each do |action| %> <% end %> - <% story.examples.first.actions.first.items.each_with_index do |item, index| %> + <% story.example.actions.first.items.each_with_index do |item, index| %> - <% story.examples.first.actions.each_with_index do |action, width_index| %> - + <% story.example.actions.each_with_index do |action, width_index| %> + <% end %> <% end %> diff --git a/app/views/common/forms/_story_examples.html.erb b/app/views/common/forms/_story_examples.html.erb index 6443c225..a8703324 100644 --- a/app/views/common/forms/_story_examples.html.erb +++ b/app/views/common/forms/_story_examples.html.erb @@ -1,4 +1,4 @@ -<% f.fields_for :examples do |example_f| -%> +<% f.fields_for :example_id do |example_f| -%> <%= example_f.hidden_field :heading -%>   <% example_f.fields_for :actions do |action_f| -%> diff --git a/features/enhanced/form_submission_via_ajax.feature b/features/enhanced/form_submission_via_ajax.feature index 6c362fca..dc9fce8f 100644 --- a/features/enhanced/form_submission_via_ajax.feature +++ b/features/enhanced/form_submission_via_ajax.feature @@ -36,7 +36,6 @@ Feature: Form submission via AJAX | edit story | story | update | | edit step | step | update | - # @TODO Add more pagination related scenarios later Scenario Outline: We should still be able to use the pagination Given there is a And the has more than 5 @@ -109,7 +108,7 @@ Feature: Form submission via AJAX | edit story | story | update | | edit step | step | update | - # @TODO Refactor so we can apply the same scenario to new form submissions & our edge case scenario's' + # @TODO Refactor so we can apply the same scenario to our edge case scenario's Scenario Outline: Displaying the sidebar after form submission Given there is a When I am on the "" page diff --git a/features/plain/feature_import.feature b/features/plain/feature_import.feature index a377023c..662609a8 100644 --- a/features/plain/feature_import.feature +++ b/features/plain/feature_import.feature @@ -211,13 +211,6 @@ Feature: Users should not have to manually input their features if they have alr When I am on "import project" Then the submit button will be disabled for "sample_one" - Scenario: We want to make sure that a scenario's example are imported along with its scenario - Given there is a project - And the project does have a project location - When I am on "import project" - And we click import "most used" - Then the features scenario "Our actions that we want to check states for" should be saved - Scenario: A feature should not be submittable if a duplicate scenario is found in the new feature Given there is a project And the project has a project path diff --git a/features/step_definitions/feature_file_steps.rb b/features/step_definitions/feature_file_steps.rb index ccf8e08e..fc60a374 100644 --- a/features/step_definitions/feature_file_steps.rb +++ b/features/step_definitions/feature_file_steps.rb @@ -67,7 +67,7 @@ end Then /^a scenario outline should be found$/ do - @file.export.stories.first.examples.first.should_not be_nil + @file.export.stories.first.example.should_not be_nil end Then /^each scenario outline should have the expected steps$/ do @@ -75,23 +75,23 @@ end Then /^the scenario outline should precede its examples$/ do - @file.export.stories.first.examples.should_not be_empty + @file.export.stories.first.example.should_not be_blank end Then /^the example should have a list of actions$/ do - @file.export.stories.first.examples.first.actions.should_not be_empty + @file.export.stories.first.example.actions.should_not be_empty end Then /^the scenario outlines example should be found$/ do - @file.export.stories.first.examples.first.should_not be_nil + @file.export.stories.first.example.should_not be_nil end Then /^the actions should only contain "([^\"]*)"$/ do |words| - words.split(',').each_with_index { |word,index| @file.export.stories.first.examples.first.actions[index].title.should == word } + words.split(',').each_with_index { |word,index| @file.export.stories.first.example.actions[index].title.should == word } end Then /^the "([^\"]*)" should have "([^\"]*)" associated to it "([^\"]*)" of times$/ do |action, current_item, amount| - @file.export.stories.first.examples.first.actions.each {|current_action| count = 0; current_action.items.each {|item| (item.title == current_item)? count += 1 :nil; } ; count.to_s.should eql amount if current_action.title == action } + @file.export.stories.first.example.actions.each {|current_action| count = 0; current_action.items.each {|item| (item.title == current_item)? count += 1 :nil; } ; count.to_s.should eql amount if current_action.title == action } end #@TODO refactor to make more meaningful diff --git a/features/step_definitions/feature_import_steps.rb b/features/step_definitions/feature_import_steps.rb index ed622ecd..b33d4fe2 100644 --- a/features/step_definitions/feature_import_steps.rb +++ b/features/step_definitions/feature_import_steps.rb @@ -50,16 +50,12 @@ Then /^the first story should contain its examples$/ do @project.import_features do |feature| if feature[:file] == 'most_used.feature' - response.should contain feature[:feature].stories.first.examples.first + response.should contain feature[:feature].stories.first.example end end response.should contain 'Examples' end -Then /^the features scenario "([^\"]*)" should be saved$/ do |heading| - Example.first.heading.should eql heading -end - Then /^the submit button will be disabled for "([^\"]*)"$/ do |container| response.should have_selector :p, attribute = {:id => "error"} do |error| error.should contain "Feature has duplicate scenario" diff --git a/features/step_definitions/stories_steps.rb b/features/step_definitions/stories_steps.rb index e04f9eaa..22cae0e9 100644 --- a/features/step_definitions/stories_steps.rb +++ b/features/step_definitions/stories_steps.rb @@ -10,11 +10,11 @@ @story = Story.make end +# TODO Make more flexible and speed up Given /^we have a stories with examples$/ do f = FeatureFile.new("#{RAILS_ROOT}/features/plain/enhancements.feature").export @story = f.stories.first @story.save - @story.examples.size.should eql 1 end Given /^it has an associated feature$/ do diff --git a/lib/feature_file.rb b/lib/feature_file.rb index 636f3084..d461f90b 100644 --- a/lib/feature_file.rb +++ b/lib/feature_file.rb @@ -33,12 +33,12 @@ def scenarios str = line.strip.sub('And', scenarios.last.steps.last.first_word) if line.strip =~ /^And/ scenarios.last.steps << Step.find_or_initialize_by_title(:title => str) # need to clean this up elsif line.strip =~ /^Examples:/ - scenarios.last.examples << Example.new(:heading => line.strip.sub(/^Examples:/, '').strip) - elsif (line.strip =~ /^\|\w*|/ and scenarios.last.nil? == false) and scenarios.last.examples.last.nil? == false - if scenarios.last.examples.last.actions.empty? - line.strip.split('|').each { |action| scenarios.last.examples.last.actions << Action.new(:title => action.gsub(/ /,'').strip) unless action.strip.blank?} - elsif not scenarios.last.examples.last.actions.empty? - line.strip.split('|').each_with_index { |item, index| scenarios.last.examples.last.actions[index-1].items << Item.new(:title => item.strip) unless item.empty?} + scenarios.last.example = Example.new(:heading => line.strip.sub(/^Examples:/, '').strip) + elsif (line.strip =~ /^\|\w*|/ and scenarios.last.nil? == false) and scenarios.last.example.nil? == false + if scenarios.last.example.actions.empty? + line.strip.split('|').each { |action| scenarios.last.example.actions << Action.new(:title => action.gsub(/ /,'').strip) unless action.strip.blank?} + elsif not scenarios.last.example.actions.empty? + line.strip.split('|').each_with_index { |item, index| scenarios.last.example.actions[index-1].items << Item.new(:title => item.strip) unless item.empty?} end end end diff --git a/spec/models/feature_file_spec.rb b/spec/models/feature_file_spec.rb index a0734899..e384622c 100644 --- a/spec/models/feature_file_spec.rb +++ b/spec/models/feature_file_spec.rb @@ -29,19 +29,19 @@ end it "should have a list of examples associated to it" do - @feature_file.scenarios.first.examples.should_not be_empty + @feature_file.scenarios.first.example.should_not be_blank end it "should have a list of associated actions" do - @feature_file.scenarios.first.examples.first.actions.should_not be_empty + @feature_file.scenarios.first.example.actions.should_not be_empty end it "should story the actions in the expected format" do - @feature_file.scenarios.last.examples.last.actions.first.title.should == 'page' + @feature_file.scenarios.last.example.actions.first.title.should == 'page' end it "should store the actions items" do - @feature_file.scenarios.last.examples.last.actions.last.items.should_not be_empty + @feature_file.scenarios.last.example.actions.last.items.should_not be_empty end end diff --git a/spec/models/feature_spec.rb b/spec/models/feature_spec.rb index b18041e4..cdc8d3e4 100644 --- a/spec/models/feature_spec.rb +++ b/spec/models/feature_spec.rb @@ -83,17 +83,17 @@ end it "should display the example heading" do - @feature.export.should contain "Examples: #{@feature.stories.first.examples.first.heading}" + @feature.export.should contain "Examples: #{@feature.stories.first.example.heading}" end it "should display the examples action headings" do - @feature.stories.first.examples.first.actions.each do |action| + @feature.stories.first.example.actions.each do |action| @feature.export.should contain "| #{action.title} |" end end it "should have items" do - @feature.stories.first.examples.first.actions.each do |action| + @feature.stories.first.example.actions.each do |action| action.items.each do |item| if item.title == ' ' @feature.export.should contain "| |"
Examples: <%=h story.examples.first.heading %>Examples: <%=h story.example.heading %>
<%= action.title%>
<%= story.examples.first.actions[width_index].items[index].title %><%= story.example.actions[width_index].items[index].title %>