surveyor 0.20.0 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/.gitignore +26 -0
  2. data/Gemfile +11 -0
  3. data/Gemfile.rails_version +13 -0
  4. data/README.md +40 -22
  5. data/Rakefile +92 -29
  6. data/app/helpers/survey_form_builder.rb +37 -0
  7. data/app/helpers/surveyor_helper.rb +9 -9
  8. data/app/views/partials/_answer.html.haml +1 -1
  9. data/app/views/partials/_question.html.haml +3 -3
  10. data/app/views/partials/_question_group.html.haml +2 -2
  11. data/app/views/partials/_section.html.haml +1 -1
  12. data/app/views/surveyor/edit.html.haml +11 -3
  13. data/app/views/surveyor/new.html.haml +8 -1
  14. data/ci-exec.sh +50 -0
  15. data/config/routes.rb +9 -13
  16. data/cucumber.yml +10 -0
  17. data/doc/question types.png +0 -0
  18. data/features/redcap_parser.feature +1 -1
  19. data/features/step_definitions/common_setps.rb +3 -0
  20. data/features/step_definitions/parser_steps.rb +8 -1
  21. data/features/step_definitions/surveyor_steps.rb +33 -9
  22. data/features/step_definitions/web_steps.rb +85 -165
  23. data/features/support/env.rb +40 -46
  24. data/features/support/paths.rb +4 -4
  25. data/features/support/selectors.rb +39 -0
  26. data/features/surveyor.feature +63 -13
  27. data/features/surveyor_dependencies.feature +289 -0
  28. data/features/surveyor_parser.feature +29 -20
  29. data/lib/formtastic/surveyor_builder.rb +9 -9
  30. data/lib/generators/surveyor/custom_generator.rb +17 -0
  31. data/lib/generators/surveyor/install_generator.rb +50 -0
  32. data/{generators/extend_surveyor/templates/extensions → lib/generators/surveyor/templates/app/controllers}/surveyor_controller.rb +0 -0
  33. data/{generators/extend_surveyor/templates/extensions → lib/generators/surveyor/templates/app/views/layouts}/surveyor_custom.html.erb +0 -0
  34. data/{generators/surveyor/templates → lib/generators/surveyor/templates/config}/locales/surveyor_en.yml +0 -0
  35. data/{generators/surveyor/templates → lib/generators/surveyor/templates/config}/locales/surveyor_es.yml +0 -0
  36. data/{generators/surveyor/templates → lib/generators/surveyor/templates/config}/locales/surveyor_he.yml +0 -0
  37. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_api_ids.rb +0 -0
  38. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_correct_answer_id_to_questions.rb +0 -0
  39. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_default_value_to_answers.rb +0 -0
  40. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_display_order_to_surveys.rb +0 -0
  41. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_display_type_to_answers.rb +0 -0
  42. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_index_to_response_sets.rb +0 -0
  43. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_index_to_surveys.rb +0 -0
  44. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_section_id_to_responses.rb +0 -0
  45. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/add_unique_indicies.rb +0 -0
  46. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_answers.rb +0 -0
  47. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_dependencies.rb +0 -0
  48. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_dependency_conditions.rb +0 -0
  49. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_question_groups.rb +0 -0
  50. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_questions.rb +0 -0
  51. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_response_sets.rb +0 -0
  52. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_responses.rb +0 -0
  53. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_survey_sections.rb +0 -0
  54. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_surveys.rb +0 -0
  55. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_validation_conditions.rb +0 -0
  56. data/{generators/surveyor/templates → lib/generators/surveyor/templates/db}/migrate/create_validations.rb +0 -0
  57. data/{generators/surveyor/templates/assets/images → lib/generators/surveyor/templates/public/images/surveyor}/next.gif +0 -0
  58. data/{generators/surveyor/templates/assets/images → lib/generators/surveyor/templates/public/images/surveyor}/prev.gif +0 -0
  59. data/lib/generators/surveyor/templates/public/javascripts/surveyor/jquery.blockUI.js +499 -0
  60. data/{generators/surveyor/templates/assets/javascripts → lib/generators/surveyor/templates/public/javascripts/surveyor}/jquery.surveyor.js +47 -4
  61. data/{generators/surveyor/templates/assets/javascripts → lib/generators/surveyor/templates/public/javascripts/surveyor}/jquery.tools.min.js +0 -0
  62. data/{generators/surveyor/templates/assets → lib/generators/surveyor/templates/public}/stylesheets/sass/custom.sass +0 -0
  63. data/{generators/surveyor/templates/assets → lib/generators/surveyor/templates/public}/stylesheets/sass/surveyor.sass +2 -2
  64. data/{generators/surveyor/templates/assets/stylesheets → lib/generators/surveyor/templates/public/stylesheets/surveyor}/dateinput.css +0 -0
  65. data/{generators/surveyor/templates/assets/stylesheets → lib/generators/surveyor/templates/public/stylesheets/surveyor}/reset.css +0 -0
  66. data/{generators/surveyor/templates/assets/stylesheets → lib/generators/surveyor/templates/public/stylesheets/surveyor}/results.css +0 -0
  67. data/{generators/extend_surveyor/templates/EXTENDING_SURVEYOR → lib/generators/surveyor/templates/surveys/EXTENDING_SURVEYOR.md} +0 -0
  68. data/{generators → lib/generators}/surveyor/templates/surveys/kitchen_sink_survey.rb +0 -0
  69. data/{generators → lib/generators}/surveyor/templates/surveys/quiz.rb +0 -0
  70. data/lib/surveyor.rb +7 -0
  71. data/lib/surveyor/engine.rb +12 -0
  72. data/lib/surveyor/models/answer_methods.rb +0 -1
  73. data/lib/surveyor/models/dependency_condition_methods.rb +1 -3
  74. data/lib/surveyor/models/question_methods.rb +0 -1
  75. data/lib/surveyor/models/response_methods.rb +2 -2
  76. data/lib/surveyor/models/response_set_methods.rb +30 -14
  77. data/lib/surveyor/models/survey_methods.rb +2 -1
  78. data/lib/surveyor/models/survey_section_methods.rb +1 -2
  79. data/lib/surveyor/parser.rb +9 -7
  80. data/lib/surveyor/redcap_parser.rb +10 -7
  81. data/lib/surveyor/surveyor_controller_methods.rb +45 -11
  82. data/lib/surveyor/version.rb +3 -0
  83. data/lib/tasks/surveyor_tasks.rake +2 -2
  84. data/rails/init.rb +0 -1
  85. data/spec/controllers/surveyor_controller_spec.rb +81 -35
  86. data/spec/helpers/surveyor_helper_spec.rb +7 -1
  87. data/spec/models/dependency_condition_spec.rb +76 -63
  88. data/spec/models/response_set_spec.rb +52 -47
  89. data/spec/models/response_spec.rb +19 -16
  90. data/spec/spec_helper.rb +21 -44
  91. data/surveyor.gemspec +25 -176
  92. metadata +302 -77
  93. data/CHANGELOG +0 -335
  94. data/VERSION +0 -1
  95. data/ci-env.sh +0 -29
  96. data/generators/extend_surveyor/extend_surveyor_generator.rb +0 -21
  97. data/generators/surveyor/surveyor_generator.rb +0 -69
  98. data/generators/surveyor/templates/README +0 -10
  99. data/generators/surveyor/templates/tasks/surveyor.rb +0 -4
  100. data/hudson.rakefile +0 -33
  101. data/init_testbed.rakefile +0 -55
  102. data/testbed/Gemfile +0 -13
@@ -1,5 +1,5 @@
1
1
  - renderer = g.renderer
2
- - f.inputs q_text(g), :id => "g_#{g.id}", :class => "g_#{renderer} #{g.css_class(@response_set)}" do
2
+ = f.inputs q_text(g), :id => "g_#{g.id}", :class => "g_#{renderer} #{g.css_class(@response_set)}" do
3
3
  %li.help= g.help_text
4
4
  - case renderer
5
5
  - when :grid
@@ -21,7 +21,7 @@
21
21
  - if q.pick == "one"
22
22
  - r = response_for(@response_set, q, nil, g)
23
23
  - i = response_idx # increment the response index since the answer partial skips for q.pick == one
24
- - f.semantic_fields_for i, r do |ff|
24
+ = f.semantic_fields_for i, r do |ff|
25
25
  = ff.quiet_input :question_id
26
26
  = ff.quiet_input :id unless r.new_record?
27
27
  %th= q.split_text(:pre)
@@ -1,4 +1,4 @@
1
- - div_for @section, :class => @section.custom_class do
1
+ = div_for @section, :class => @section.custom_class do
2
2
  %span.title= @section.title
3
3
  - qs ||= []
4
4
  - (questions = @section.questions).each_with_index do |q, i|
@@ -3,14 +3,22 @@
3
3
  .surveyor_flash
4
4
  = flash_messages(types)
5
5
  .close
6
- - semantic_form_for(:r, @response_set, :url => update_my_survey_path, :html => {:method => :put, :id => "survey_form", :class => @survey.custom_class}) do |f|
7
- = render 'partials/section_menu' unless @sections.size < 3
6
+ = semantic_form_for(@response_set, :as => :r, :url => update_my_survey_path, :html => {:method => :put, :id => "survey_form", :class => @survey.custom_class}) do |f|
7
+
8
+ = hidden_field_tag :surveyor_javascript_enabled, false
9
+
10
+ = render 'partials/section_menu' # unless @sections.size < 3
8
11
  .survey_title= @survey.title
9
12
  .previous_section= previous_section
10
13
  = render 'partials/dependents' unless @dependents.empty?
11
- - if @response_set.no_responses_for_section?(@section) # cache if response_set has no responses for current section
14
+ - if false #@response_set.no_responses_for_section?(@section) # cache if response_set has no responses for current section
12
15
  = cache("section_#{@section.id}"){ render "/partials/section", :f => f }
13
16
  - else # no cache
14
17
  = render "/partials/section", :f => f
15
18
  .next_section= next_section
16
19
  %br
20
+
21
+ :javascript
22
+ $(document).ready(function() {
23
+ $("input#surveyor_javascript_enabled").val(true);
24
+ });
@@ -10,10 +10,17 @@
10
10
  - unless @surveys.empty?
11
11
  - @surveys.each do |survey|
12
12
  %li
13
- - form_tag(take_survey_path(:survey_code => survey.access_code)) do
13
+ = form_tag(take_survey_path(:survey_code => survey.access_code)) do
14
+ = hidden_field_tag :surveyor_javascript_enabled, false
15
+
14
16
  = survey.title
15
17
  &nbsp;
16
18
  = submit_tag( t('surveyor.take_it') )
17
19
  - else
18
20
  %li
19
21
  No surveys
22
+
23
+ :javascript
24
+ $(document).ready(function() {
25
+ $("input#surveyor_javascript_enabled").val(true);
26
+ });
data/ci-exec.sh ADDED
@@ -0,0 +1,50 @@
1
+ #!/bin/bash -xe
2
+
3
+ BUNDLER_VERSION=1.1.rc
4
+ GEMSET=surveyor
5
+
6
+ export TMPDIR="`pwd`/tmp"
7
+ mkdir -p tmp
8
+
9
+ if [ -n "${RAILS_VERSION}" ]; then
10
+ GEMSET="${GEMSET}-${RAILS_VERSION}"
11
+ fi
12
+
13
+ if [ -z $CI_RUBY ]; then
14
+ echo "CI_RUBY must be set"
15
+ exit 1
16
+ fi
17
+
18
+ set +xe
19
+ echo "Initializing RVM"
20
+ source ~/.rvm/scripts/rvm
21
+ set -xe
22
+
23
+ # On the overnight build, reinstall all gems
24
+ if [ `date +%H` -lt 5 ]; then
25
+ set +xe
26
+ echo "Purging gemset to verify that all deps can still be installed"
27
+ rvm --force $CI_RUBY gemset delete $GEMSET
28
+ set -xe
29
+ fi
30
+
31
+ RVM_CONFIG="${CI_RUBY}@${GEMSET}"
32
+ set +xe
33
+ echo "Switching to ${RVM_CONFIG}"
34
+ rvm use $RVM_CONFIG
35
+ set -xe
36
+
37
+ which ruby
38
+ ruby -v
39
+
40
+ set +e
41
+ gem list -i bundler -v $BUNDLER_VERSION
42
+ if [ $? -ne 0 ]; then
43
+ set -e
44
+ gem install bundler -v $BUNDLER_VERSION
45
+ fi
46
+ set -e
47
+
48
+ bundle _${BUNDLER_VERSION}_ update
49
+
50
+ bundle _${BUNDLER_VERSION}_ exec rake ci:all --trace
data/config/routes.rb CHANGED
@@ -1,13 +1,9 @@
1
- ActionController::Routing::Routes.draw do |map|
2
- map.with_options :controller => 'results' do |r|
3
- r.results "results", :conditions => {:method => :get}, :action => "index"
4
- r.result "results/:survey_code", :conditions => {:method => :get}, :action => "show"
5
- end
6
- map.with_options :controller => 'surveyor' do |s|
7
- s.available_surveys "surveys", :conditions => {:method => :get}, :action => "new" # GET survey list
8
- s.take_survey "surveys/:survey_code", :conditions => {:method => :post}, :action => "create" # Only POST of survey to create
9
- s.view_my_survey "surveys/:survey_code/:response_set_code.:format", :conditions => {:method => :get}, :action => "show", :format => "html" # GET viewable/printable? survey
10
- s.edit_my_survey "surveys/:survey_code/:response_set_code/take", :conditions => {:method => :get}, :action => "edit" # GET editable survey
11
- s.update_my_survey "surveys/:survey_code/:response_set_code", :conditions => {:method => :put}, :action => "update" # PUT edited survey
12
- end
13
- end
1
+ Rails.application.routes.draw do
2
+ match 'results', :to => 'results#index', :as => 'results', :via => :get
3
+ match 'results/:survey_code', :to => 'results#show', :as => 'result', :via => :get
4
+ match 'surveys', :to => 'surveyor#new', :as => 'available_surveys', :via => :get
5
+ match 'surveys/:survey_code', :to => 'surveyor#create', :as => 'take_survey', :via => :post
6
+ match 'surveys/:survey_code/:response_set_code', :to => 'surveyor#show', :as => 'view_my_survey', :via => :get
7
+ match 'surveys/:survey_code/:response_set_code/take', :to => 'surveyor#edit', :as => 'edit_my_survey', :via => :get
8
+ match 'surveys/:survey_code/:response_set_code', :to => 'surveyor#update', :as => 'update_my_survey', :via => :put
9
+ end
data/cucumber.yml ADDED
@@ -0,0 +1,10 @@
1
+ <%
2
+ rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
3
+ rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
4
+ std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
5
+ %>
6
+ default: <%= std_opts %> features
7
+ wip: --tags @wip:3 --wip features
8
+ rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
9
+ ci: --tags ~@wip --tags ~@javascript --format html --out reports/cucumber.html --format junit --out reports/cucumber-xml --format pretty --strict features
10
+ ci_wip: --wip --tags @wip --tags ~@javascript --format html --out reports/cucumber_wip.html --format pretty features
Binary file
@@ -39,7 +39,7 @@ Feature: Survey creation
39
39
  ||
40
40
  And there should be 2 answers with:
41
41
  ||
42
- @focus
42
+
43
43
  Scenario: with different whitespace
44
44
  Given I parse redcap file "redcap_whitespace.csv"
45
45
  Then there should be 1 survey with:
@@ -0,0 +1,3 @@
1
+ When /^I wait (\d+) seconds$/ do |wait_seconds|
2
+ sleep(wait_seconds.to_i)
3
+ end
@@ -3,7 +3,7 @@ Given /^I parse$|^the survey$/ do |string|
3
3
  end
4
4
 
5
5
  Given /^I parse redcap file "([^"]*)"$/ do |name|
6
- Surveyor::RedcapParser.parse File.read(File.join(RAILS_ROOT, '..', 'features', 'support', name)), name
6
+ Surveyor::RedcapParser.parse File.read(File.join(Rails.root, '..', 'features', 'support', name)), name
7
7
  end
8
8
 
9
9
  Then /^there should be (\d+) survey(?:s?) with:$/ do |x, table|
@@ -13,6 +13,13 @@ Then /^there should be (\d+) survey(?:s?) with:$/ do |x, table|
13
13
  end
14
14
  end
15
15
 
16
+ Then /^there should be (\d+) section(?:s?) with:$/ do |x, table|
17
+ SurveySection.count.should == x.to_i
18
+ table.hashes.each do |hash|
19
+ SurveySection.find(:first, :conditions => hash).should_not be_nil
20
+ end
21
+ end
22
+
16
23
  Then /^there should be (\d+) question groups with:$/ do |x, table|
17
24
  QuestionGroup.count.should == x.to_i
18
25
  table.hashes.each do |hash|
@@ -17,7 +17,7 @@ Then /^there should be (\d+) response set with (\d+) responses? with:$/ do |rs_n
17
17
  Response.first(:conditions => hash.merge({:answer_id => answer.id})).should_not be_nil
18
18
  elsif
19
19
  Response.first(:conditions => hash).should_not be_nil
20
- end
20
+ end
21
21
  end
22
22
  end
23
23
  end
@@ -34,7 +34,11 @@ Then /^question "([^"]*)" should have a dependency with rule "([^"]*)"$/ do |qr,
34
34
  end
35
35
 
36
36
  Then /^the element "([^"]*)" should have the class "([^"]*)"$/ do |selector, css_class|
37
- response.should have_selector(selector, :class => css_class)
37
+ page.should have_selector(selector, :class => css_class)
38
+ end
39
+
40
+ Then /^the element "([^"]*)" should exist$/ do |selector|
41
+ page.should have_selector(selector)
38
42
  end
39
43
 
40
44
  Then /^the survey should be complete$/ do
@@ -42,7 +46,7 @@ Then /^the survey should be complete$/ do
42
46
  end
43
47
 
44
48
  Then /^a dropdown should exist with the options "([^"]*)"$/ do |options_text|
45
- response.should have_selector('select')
49
+ page.should have_selector('select')
46
50
  options = options_text.split(',').collect(&:strip)
47
51
  within "select" do |select|
48
52
  options.each do |o|
@@ -52,16 +56,15 @@ Then /^a dropdown should exist with the options "([^"]*)"$/ do |options_text|
52
56
  end
53
57
 
54
58
  Then /^there should be (\d+) checkboxes$/ do |count|
55
- response.should have_selector('input[type=checkbox]', :count => count.to_i)
56
-
59
+ page.should have_selector('input[type=checkbox]', :count => count.to_i)
57
60
  end
58
61
 
59
62
  Then /^there should be (\d+) text areas$/ do |count|
60
- response.should have_selector('textarea', :count => count.to_i)
63
+ page.should have_selector('textarea', :count => count.to_i)
61
64
  end
62
65
 
63
66
  Then /^the question "([^"]*)" should be triggered$/ do |text|
64
- response.should have_selector %(fieldset[name="#{text}"][class!="q_hidden"])
67
+ page.should have_selector %(fieldset[name="#{text}"][class!="q_hidden"])
65
68
  end
66
69
 
67
70
  Then /^there should be (\d+) response with answer "([^"]*)"$/ do |count, answer_text|
@@ -73,11 +76,32 @@ Then /^there should be (\d+) datetime responses with$/ do |count, table|
73
76
  Response.count.should == count.to_i
74
77
  table.hashes.each do |hash|
75
78
  if hash.keys == ["datetime_value"]
76
- Response.find_by_datetime_value(DateTime.parse(hash["datetime_value"])).should_not be_blank
79
+ dtv = DateTime.parse( hash["datetime_value"].size == 8 ? "0001-01-01 #{hash['datetime_value']}" : hash["datetime_value"])
80
+ Response.all.one?{|x| x.datetime_value == dtv}.should be_true
77
81
  end
78
82
  end
79
83
  end
80
84
 
81
85
  Then /^I should see the image "([^"]*)"$/ do |src|
82
- response.should have_selector %(img[src^="#{src}"])
86
+ page.should have_selector %(img[src^="#{src}"])
83
87
  end
88
+
89
+ Then /^(\d+) responses should exist$/ do |response_count|
90
+ Response.count.should == response_count.to_i
91
+ end
92
+
93
+ Then /the element "([^\"]*)" should be hidden$/ do |selector|
94
+ wait_until do
95
+ its_hidden = page.evaluate_script("$('#{selector}').is(':hidden');")
96
+ its_not_in_dom = page.evaluate_script("$('#{selector}').length == 0;")
97
+ (its_hidden || its_not_in_dom).should be_true
98
+ end
99
+ end
100
+
101
+ Then /the element "([^\"]*)" should not be hidden$/ do |selector|
102
+ wait_until do
103
+ its_not_hidden = page.evaluate_script("$('#{selector}').is(':not(:hidden)');")
104
+ its_in_dom = page.evaluate_script("$('#{selector}').length > 0;")
105
+ (its_not_hidden && its_in_dom).should be_true
106
+ end
107
+ end
@@ -1,16 +1,45 @@
1
- # IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
2
- # It is recommended to regenerate this file in the future when you upgrade to a
3
- # newer version of cucumber-rails. Consider adding your own code to a new file
4
- # instead of editing this one. Cucumber will automatically load all features/**/*.rb
5
- # files.
1
+ # TL;DR: YOU SHOULD DELETE THIS FILE
2
+ #
3
+ # This file was generated by Cucumber-Rails and is only here to get you a head start
4
+ # These step definitions are thin wrappers around the Capybara/Webrat API that lets you
5
+ # visit pages, interact with widgets and make assertions about page content.
6
+ #
7
+ # If you use these step definitions as basis for your features you will quickly end up
8
+ # with features that are:
9
+ #
10
+ # * Hard to maintain
11
+ # * Verbose to read
12
+ #
13
+ # A much better approach is to write your own higher level step definitions, following
14
+ # the advice in the following blog posts:
15
+ #
16
+ # * http://benmabey.com/2008/05/19/imperative-vs-declarative-scenarios-in-user-stories.html
17
+ # * http://dannorth.net/2011/01/31/whose-domain-is-it-anyway/
18
+ # * http://elabs.se/blog/15-you-re-cuking-it-wrong
19
+ #
6
20
 
7
21
 
8
22
  require 'uri'
9
23
  require 'cgi'
10
24
  require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
25
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "selectors"))
11
26
 
12
- # Commonly used webrat steps
13
- # http://github.com/brynary/webrat
27
+ module WithinHelpers
28
+ def with_scope(locator)
29
+ locator ? within(*selector_for(locator)) { yield } : yield
30
+ end
31
+ end
32
+ World(WithinHelpers)
33
+
34
+ # Single-line step scoper
35
+ When /^(.*) within (.*[^:])$/ do |step, parent|
36
+ with_scope(parent) { When step }
37
+ end
38
+
39
+ # Multi-line step scoper
40
+ When /^(.*) within (.*[^:]):$/ do |step, parent, table_or_string|
41
+ with_scope(parent) { When "#{step}:", table_or_string }
42
+ end
14
43
 
15
44
  Given /^(?:|I )am on (.+)$/ do |page_name|
16
45
  visit path_to(page_name)
@@ -28,28 +57,14 @@ When /^(?:|I )follow "([^"]*)"$/ do |link|
28
57
  click_link(link)
29
58
  end
30
59
 
31
- When /^(?:|I )follow "([^"]*)" within "([^"]*)"$/ do |link, parent|
32
- click_link_within(parent, link)
33
- end
34
-
35
60
  When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
36
- field = resolve_regexp(field)
37
61
  fill_in(field, :with => value)
38
62
  end
39
63
 
40
64
  When /^(?:|I )fill in "([^"]*)" for "([^"]*)"$/ do |value, field|
41
- field = resolve_regexp(field)
42
65
  fill_in(field, :with => value)
43
66
  end
44
67
 
45
- def resolve_regexp(expr)
46
- if expr =~ /\/.*\//
47
- Regexp.new(expr[1..-2])
48
- else
49
- expr
50
- end
51
- end
52
-
53
68
  # Use this to fill in an entire form with data from a table. Example:
54
69
  #
55
70
  # When I fill in the following:
@@ -71,53 +86,6 @@ When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
71
86
  select(value, :from => field)
72
87
  end
73
88
 
74
- # Use this step in conjunction with Rail's datetime_select helper. For example:
75
- # When I select "December 25, 2008 10:00" as the date and time
76
- When /^(?:|I )select "([^"]*)" as the date and time$/ do |time|
77
- select_datetime(time)
78
- end
79
-
80
- # Use this step when using multiple datetime_select helpers on a page or
81
- # you want to specify which datetime to select. Given the following view:
82
- # <%= f.label :preferred %><br />
83
- # <%= f.datetime_select :preferred %>
84
- # <%= f.label :alternative %><br />
85
- # <%= f.datetime_select :alternative %>
86
- # The following steps would fill out the form:
87
- # When I select "November 23, 2004 11:20" as the "Preferred" date and time
88
- # And I select "November 25, 2004 10:30" as the "Alternative" date and time
89
- When /^(?:|I )select "([^"]*)" as the "([^"]*)" date and time$/ do |datetime, datetime_label|
90
- select_datetime(datetime, :from => datetime_label)
91
- end
92
-
93
- # Use this step in conjunction with Rail's time_select helper. For example:
94
- # When I select "2:20PM" as the time
95
- # Note: Rail's default time helper provides 24-hour time-- not 12 hour time. Webrat
96
- # will convert the 2:20PM to 14:20 and then select it.
97
- When /^(?:|I )select "([^"]*)" as the time$/ do |time|
98
- select_time(time)
99
- end
100
-
101
- # Use this step when using multiple time_select helpers on a page or you want to
102
- # specify the name of the time on the form. For example:
103
- # When I select "7:30AM" as the "Gym" time
104
- When /^(?:|I )select "([^"]*)" as the "([^"]*)" time$/ do |time, time_label|
105
- select_time(time, :from => time_label)
106
- end
107
-
108
- # Use this step in conjunction with Rail's date_select helper. For example:
109
- # When I select "February 20, 1981" as the date
110
- When /^(?:|I )select "([^"]*)" as the date$/ do |date|
111
- select_date(date)
112
- end
113
-
114
- # Use this step when using multiple date_select helpers on one page or
115
- # you want to specify the name of the date on the form. For example:
116
- # When I select "April 26, 1982" as the "Date of Birth" date
117
- When /^(?:|I )select "([^"]*)" as the "([^"]*)" date$/ do |date, date_label|
118
- select_date(date, :from => date_label)
119
- end
120
-
121
89
  When /^(?:|I )check "([^"]*)"$/ do |field|
122
90
  check(field)
123
91
  end
@@ -130,140 +98,92 @@ When /^(?:|I )choose "([^"]*)"$/ do |field|
130
98
  choose(field)
131
99
  end
132
100
 
133
- # Adds support for validates_attachment_content_type. Without the mime-type getting
134
- # passed to attach_file() you will get a "Photo file is not one of the allowed file types."
135
- # error message
136
101
  When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"$/ do |path, field|
137
- type = path.split(".")[1]
138
-
139
- case type
140
- when "jpg"
141
- type = "image/jpg"
142
- when "jpeg"
143
- type = "image/jpeg"
144
- when "png"
145
- type = "image/png"
146
- when "gif"
147
- type = "image/gif"
148
- end
149
-
150
- attach_file(field, path, type)
102
+ attach_file(field, File.expand_path(path))
151
103
  end
152
104
 
153
105
  Then /^(?:|I )should see "([^"]*)"$/ do |text|
154
- if response.respond_to? :should
155
- response.should contain(text)
106
+ if page.respond_to? :should
107
+ page.should have_content(text)
156
108
  else
157
- assert_contain text
158
- end
159
- end
160
-
161
- Then /^(?:|I )should see "([^"]*)" within "([^"]*)"$/ do |text, selector|
162
- within(selector) do |content|
163
- if content.respond_to? :should
164
- content.should contain(text)
165
- else
166
- hc = Webrat::Matchers::HasContent.new(text)
167
- assert hc.matches?(content), hc.failure_message
168
- end
109
+ assert page.has_content?(text)
169
110
  end
170
111
  end
171
112
 
172
113
  Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp|
173
114
  regexp = Regexp.new(regexp)
174
- if response.respond_to? :should
175
- response.should contain(regexp)
176
- else
177
- assert_match(regexp, response_body)
178
- end
179
- end
180
115
 
181
- Then /^(?:|I )should see \/([^\/]*)\/ within "([^"]*)"$/ do |regexp, selector|
182
- within(selector) do |content|
183
- regexp = Regexp.new(regexp)
184
- if content.respond_to? :should
185
- content.should contain(regexp)
186
- else
187
- assert_match(regexp, content)
188
- end
116
+ if page.respond_to? :should
117
+ page.should have_xpath('//*', :text => regexp)
118
+ else
119
+ assert page.has_xpath?('//*', :text => regexp)
189
120
  end
190
121
  end
191
122
 
192
123
  Then /^(?:|I )should not see "([^"]*)"$/ do |text|
193
- if response.respond_to? :should_not
194
- response.should_not contain(text)
124
+ if page.respond_to? :should
125
+ page.should have_no_content(text)
195
126
  else
196
- assert_not_contain(text)
197
- end
198
- end
199
-
200
- Then /^(?:|I )should not see "([^"]*)" within "([^"]*)"$/ do |text, selector|
201
- within(selector) do |content|
202
- if content.respond_to? :should_not
203
- content.should_not contain(text)
204
- else
205
- hc = Webrat::Matchers::HasContent.new(text)
206
- assert !hc.matches?(content), hc.negative_failure_message
207
- end
127
+ assert page.has_no_content?(text)
208
128
  end
209
129
  end
210
130
 
211
131
  Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp|
212
132
  regexp = Regexp.new(regexp)
213
- if response.respond_to? :should_not
214
- response.should_not contain(regexp)
133
+
134
+ if page.respond_to? :should
135
+ page.should have_no_xpath('//*', :text => regexp)
215
136
  else
216
- assert_not_contain(regexp)
137
+ assert page.has_no_xpath?('//*', :text => regexp)
217
138
  end
218
139
  end
219
140
 
220
- Then /^(?:|I )should not see \/([^\/]*)\/ within "([^"]*)"$/ do |regexp, selector|
221
- within(selector) do |content|
222
- regexp = Regexp.new(regexp)
223
- if content.respond_to? :should_not
224
- content.should_not contain(regexp)
141
+ Then /^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/ do |field, parent, value|
142
+ with_scope(parent) do
143
+ field = find_field(field)
144
+ field_value = (field.tag_name == 'textarea') ? field.text : field.value
145
+ if field_value.respond_to? :should
146
+ field_value.should =~ /#{value}/
225
147
  else
226
- assert_no_match(regexp, content)
148
+ assert_match(/#{value}/, field_value)
227
149
  end
228
150
  end
229
151
  end
230
152
 
231
- Then /^the "([^"]*)" field should contain "([^"]*)"$/ do |field, value|
232
- field_value = field_labeled(field).value
233
- if field_value.respond_to? :should
234
- field_value.should =~ /#{value}/
235
- else
236
- assert_match(/#{value}/, field_value)
237
- end
238
- end
239
-
240
- Then /^the "([^"]*)" field should not contain "([^"]*)"$/ do |field, value|
241
- field_value = field_labeled(field).value
242
- if field_value.respond_to? :should_not
243
- field_value.should_not =~ /#{value}/
244
- else
245
- assert_no_match(/#{value}/, field_value)
153
+ Then /^the "([^"]*)" field(?: within (.*))? should not contain "([^"]*)"$/ do |field, parent, value|
154
+ with_scope(parent) do
155
+ field = find_field(field)
156
+ field_value = (field.tag_name == 'textarea') ? field.text : field.value
157
+ if field_value.respond_to? :should_not
158
+ field_value.should_not =~ /#{value}/
159
+ else
160
+ assert_no_match(/#{value}/, field_value)
161
+ end
246
162
  end
247
163
  end
248
164
 
249
- Then /^the "([^"]*)" checkbox should be checked$/ do |label|
250
- field = field_labeled(label)
251
- if field.respond_to? :should
252
- field.should be_checked
253
- else
254
- assert field.checked?
165
+ Then /^the "([^"]*)" checkbox(?: within (.*))? should be checked$/ do |label, parent|
166
+ with_scope(parent) do
167
+ field_checked = find_field(label)['checked']
168
+ if field_checked.respond_to? :should
169
+ field_checked.should be_true
170
+ else
171
+ assert field_checked
172
+ end
255
173
  end
256
174
  end
257
175
 
258
- Then /^the "([^"]*)" checkbox should not be checked$/ do |label|
259
- field = field_labeled(label)
260
- if field.respond_to? :should_not
261
- field.should_not be_checked
262
- else
263
- assert !field.checked?
176
+ Then /^the "([^"]*)" checkbox(?: within (.*))? should not be checked$/ do |label, parent|
177
+ with_scope(parent) do
178
+ field_checked = find_field(label)['checked']
179
+ if field_checked.respond_to? :should
180
+ field_checked.should be_false
181
+ else
182
+ assert !field_checked
183
+ end
264
184
  end
265
185
  end
266
-
186
+
267
187
  Then /^(?:|I )should be on (.+)$/ do |page_name|
268
188
  current_path = URI.parse(current_url).path
269
189
  if current_path.respond_to? :should
@@ -278,7 +198,7 @@ Then /^(?:|I )should have the following query string:$/ do |expected_pairs|
278
198
  actual_params = query ? CGI.parse(query) : {}
279
199
  expected_params = {}
280
200
  expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')}
281
-
201
+
282
202
  if actual_params.respond_to? :should
283
203
  actual_params.should == expected_params
284
204
  else