Skip to content

Commit

Permalink
Refactored so that we do not have multiple examples anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
baphled committed Jul 22, 2010
1 parent 33ee40b commit 755d8e3
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 51 deletions.
12 changes: 6 additions & 6 deletions app/models/feature.rb
Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions app/models/story.rb
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions app/views/common/_example_table.html.erb
@@ -1,21 +1,21 @@
<% if not story.examples.empty? %>
<% if not story.example.blank? %>
<hr />
<table class="ui-tabs ui-widget ui-widget-content ui-corner-all">
<thead>
<tr>
<td colspan="<%=story.examples.first.actions.size%>"><b>Examples:</b> <%=h story.examples.first.heading %></td>
<td colspan="<%=story.example.actions.size%>"><b>Examples:</b> <%=h story.example.heading %></td>
</tr>
<tr>
<% story.examples.first.actions.each do |action| %>
<% story.example.actions.each do |action| %>
<td><b><%= action.title%></b></td>
<% end %>
</tr>
</thead>
<tbody>
<% story.examples.first.actions.first.items.each_with_index do |item, index| %>
<% story.example.actions.first.items.each_with_index do |item, index| %>
<tr class="<%= (index.even?)? 'even' : 'odd' %>">
<% story.examples.first.actions.each_with_index do |action, width_index| %>
<td><%= story.examples.first.actions[width_index].items[index].title %></td>
<% story.example.actions.each_with_index do |action, width_index| %>
<td><%= story.example.actions[width_index].items[index].title %></td>
<% end %>
</tr>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion 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| -%>
Expand Down
3 changes: 1 addition & 2 deletions features/enhanced/form_submission_via_ajax.feature
Expand Up @@ -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 <item>
And the <item> has more than 5 <assoc>
Expand Down Expand Up @@ -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 <item>
When I am on the "<page>" page
Expand Down
7 changes: 0 additions & 7 deletions features/plain/feature_import.feature
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions features/step_definitions/feature_file_steps.rb
Expand Up @@ -67,31 +67,31 @@
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
@file.export.stories.first.steps.should_not be_empty
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
Expand Down
6 changes: 1 addition & 5 deletions features/step_definitions/feature_import_steps.rb
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/stories_steps.rb
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/feature_file.rb
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/models/feature_file_spec.rb
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions spec/models/feature_spec.rb
Expand Up @@ -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 == '&nbsp;'
@feature.export.should contain "| |"
Expand Down

0 comments on commit 755d8e3

Please sign in to comment.