sandwich 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -1,20 +1,20 @@
1
1
  require 'pp'
2
2
 
3
- Then Sandwich.regexps[:show_source] do
3
+ Then /^show me the source$/ do
4
4
  puts page.body
5
5
  end
6
6
 
7
- Then Sandwich.regexps[:show_all] do |targets|
7
+ Then /^show me all (.+)$/ do |targets|
8
8
  targets.split(/,/).map do |target|
9
- target.strip.singularize.classify.constantize.all.map { |item| pp item }
9
+ target.strip.underscore.singularize.classify.constantize.all.map { |item| pp item }
10
10
  end
11
11
  end
12
12
 
13
- Then Sandwich.regexps[:show_source_snippet] do |selector|
13
+ Then /^show me the source for #{q}$/ do |selector|
14
14
  puts locate(selector).html
15
15
  end
16
16
 
17
- Then Sandwich.regexps[:show_page_text] do
17
+ Then /^show me the page text$/ do
18
18
  IO.popen("html2text", "w+") do |io|
19
19
  io.puts response.body
20
20
  io.close_write
@@ -2,33 +2,51 @@ require 'sandwich/ext/string'
2
2
  require 'sandwich/cucumber/world'
3
3
  require 'sandwich/regexps'
4
4
 
5
- Given Sandwich.regexps[:model] do |total, what|
5
+ Given /^(an?|the|\d+) (.+) exists?$/ do |total, what|
6
6
  class_for(what).tap do |model|
7
7
  total.times { model.make }
8
8
  end
9
9
  end
10
10
 
11
- Given Sandwich.regexps[:no_model] do |what|
12
- class_for(what).delete_all
11
+ Transform /(an?|\d+) (.+) #{q}$/ do |total, what, value|
12
+ model = class_for(what)
13
+ r = total.times.map { model.make(model.default_attribute => value) }
14
+
15
+ r.length == 1 ? r.first : r
13
16
  end
14
17
 
15
- Given Sandwich.regexps[:model_with_attribute] do |total, what, attr, value|
16
- class_for(what).tap do |model|
17
- total.times { model.make(attr => value) }
18
- end
18
+ Transform /the (.+) (?:with|in) (.+) #{q}$/ do |what, attr, value|
19
+ class_for(what).find(:first, :conditions => {attr => value})
20
+ end
21
+
22
+ Transform /the (.+) #{q}$/ do |what, value|
23
+ klass = class_for(what)
24
+ klass.find(:first, :conditions => {klass.default_attribute => value})
25
+ end
26
+
27
+ Given /^(.+) belonging to (.+)$/ do |child, parent|
28
+ child.send("#{parent.class.name.underscore}=", parent)
29
+ end
30
+
31
+ Given /^(.+) has (.+)$/ do |parent, child|
32
+ parent.send(child.class.name.underscore.pluralize) << child
33
+ end
34
+
35
+ Given /^no (.+) exists?$/ do |what|
36
+ class_for(what).delete_all
19
37
  end
20
38
 
21
- Given Sandwich.regexps[:no_model_with_attribute] do |what, attr, value|
39
+ Given /^no (.+) (?:with|in) (.+) #{q} exists?$/ do |what, attr, value|
22
40
  class_for(what).delete_all
23
41
  end
24
42
 
25
- Given Sandwich.regexps[:model_with_attributes] do |total, what, table|
43
+ Given /^(an?|the|\d+) (.+) with the following attributes:$/ do |total, what, table|
26
44
  class_for(what).tap do |model|
27
45
  total.times { model.make(Hash[*table.raw.map { |k, v| [k.underscore, v] }.flatten]) }
28
46
  end
29
47
  end
30
48
 
31
- Given Sandwich.regexps[:models_with_attributes] do |what, table|
49
+ Given /^the following (.+):$/ do |what, table|
32
50
  class_for(what.singularize).tap do |model|
33
51
  table.hashes.each do |h|
34
52
  model.make(Hash[*h.each_pair.map { |(k, v)| [k.underscore, v] }.flatten])
@@ -1,13 +1,13 @@
1
1
  require 'sandwich/regexps'
2
2
 
3
- When Sandwich.regexps[:fill_within_table] do |column, ordinal, path, value|
4
- within(selector_for(path)) do
5
- _, column_index = all("th").each_with_index.find { |e, i| e.text == column }
6
- locate(:xpath, "//tr[#{ordinal.to_i}]/td[#{column_index + 1}]/input").set(value)
7
- end
3
+ When /^I fill in the #{q} row, #{q} column in (.+?) with #{q}$/ do |row, column, path, value|
4
+ selector = xpath(selector_for(path))
5
+ _, column_index = page.all(:xpath, "#{selector}//th").each_with_index.find { |e, i| e.text == column }
6
+ _, row_index = page.all(:xpath, "#{selector}//tr").each_with_index.find { |e, i| e.text.include?(row) }
7
+ page.locate(:xpath, "((#{selector}//tr)[#{row_index + 1}]/td)[#{column_index + 1}]/input").set(value)
8
8
  end
9
9
 
10
- Then Sandwich.regexps[:find_within_row] do |value, ordinal, path|
10
+ Then /^I should see #{q} in the (\w+) data row of (.+)$/ do |value, ordinal, path|
11
11
  within(selector_for(path)) do
12
12
  page.should have_xpath("//tr[#{ordinal.to_i}]/td", :text => value)
13
13
  end
@@ -0,0 +1,5 @@
1
+ require 'sandwich/regexps'
2
+
3
+ Then /^I should see "([^\"]*)" within (.+)$/ do |text, selector|
4
+ page.should have_xpath(xpath(selector_for(selector)), :text => text)
5
+ end
@@ -3,6 +3,14 @@ module Sandwich
3
3
  def class_for(name)
4
4
  name.underscore.classify.constantize
5
5
  end
6
+
7
+ def xpath(selector)
8
+ if Capybara.default_selector.to_sym == :css
9
+ Capybara::XPath.from_css(selector)
10
+ else
11
+ selector
12
+ end
13
+ end
6
14
  end
7
15
  end
8
16
 
@@ -1,3 +1,4 @@
1
1
  ActiveRecord::Base.class_eval do
2
+ extend Sandwich::Base::ClassMethods
2
3
  include Sandwich::Amounts
3
4
  end
@@ -0,0 +1,9 @@
1
+ module Sandwich
2
+ module Object
3
+ QUOTED_VALUE = '"([^\"]*)"'
4
+
5
+ def q() QUOTED_VALUE end
6
+
7
+ ::Object.send(:include, self)
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Sandwich
2
+ module Base
3
+ module ClassMethods
4
+ def default_attribute(name = nil)
5
+ if name
6
+ @default_attribute = name
7
+ else
8
+ @default_attribute
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sandwich}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David Leal"]
12
- s.date = %q{2010-04-14}
12
+ s.date = %q{2010-04-18}
13
13
  s.description = %q{Yup, cucumber helpers}
14
14
  s.email = %q{david@thedigitalants.com}
15
15
  s.extra_rdoc_files = [
@@ -27,12 +27,14 @@ Gem::Specification.new do |s|
27
27
  "lib/sandwich/cucumber/debug_steps.rb",
28
28
  "lib/sandwich/cucumber/model_steps.rb",
29
29
  "lib/sandwich/cucumber/table_steps.rb",
30
+ "lib/sandwich/cucumber/web_steps.rb",
30
31
  "lib/sandwich/cucumber/world.rb",
31
32
  "lib/sandwich/ext/active_record.rb",
32
33
  "lib/sandwich/ext/capybara.rb",
34
+ "lib/sandwich/ext/object.rb",
33
35
  "lib/sandwich/ext/string.rb",
34
36
  "lib/sandwich/helpers/amounts.rb",
35
- "lib/sandwich/regexps.rb",
37
+ "lib/sandwich/helpers/base.rb",
36
38
  "sandwich.gemspec",
37
39
  "spec/sandwich_spec.rb",
38
40
  "spec/spec.opts",
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - David Leal
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-14 00:00:00 +01:00
17
+ date: 2010-04-18 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -99,12 +99,14 @@ files:
99
99
  - lib/sandwich/cucumber/debug_steps.rb
100
100
  - lib/sandwich/cucumber/model_steps.rb
101
101
  - lib/sandwich/cucumber/table_steps.rb
102
+ - lib/sandwich/cucumber/web_steps.rb
102
103
  - lib/sandwich/cucumber/world.rb
103
104
  - lib/sandwich/ext/active_record.rb
104
105
  - lib/sandwich/ext/capybara.rb
106
+ - lib/sandwich/ext/object.rb
105
107
  - lib/sandwich/ext/string.rb
106
108
  - lib/sandwich/helpers/amounts.rb
107
- - lib/sandwich/regexps.rb
109
+ - lib/sandwich/helpers/base.rb
108
110
  - sandwich.gemspec
109
111
  - spec/sandwich_spec.rb
110
112
  - spec/spec.opts
@@ -1,27 +0,0 @@
1
- module Sandwich
2
- def self.regexps
3
- # TODO extract (a|the|etc) to its own thing, so that it can be overridden
4
-
5
- @regexps ||= {}.tap do |rx|
6
- rx[:model] = /^(an?|the|\d+) (.+) exists?$/
7
- rx[:no_model] = /^no (.+) exists?$/
8
- rx[:model_with_attribute] = /^(an?|the|\d+) (.+) (?:with|in) (.+) #{quoted_value} exists?$/
9
- rx[:no_model_with_attribute] = /^no (.+) (?:with|in) (.+) #{quoted_value} exists?$/
10
- rx[:model_with_attributes] = /^(an?|the|\d+) (.+) exists with the following attributes:$/
11
- rx[:models_with_attributes] = /^the following (.+) exist:$/
12
- rx[:show_source] = /^show me the source$/
13
- rx[:show_all] = /^show me all (.+)$/
14
- rx[:show_source_snippet] = /^show me the source for #{quoted_value}$/
15
- rx[:show_page_text] = /^show me the page text$/
16
- rx[:look_at] = /^I look at (.+)$/
17
- rx[:fill_within_table] = /^I fill in the #{quoted_value} cell for the (\w+) row of (.+?) with #{quoted_value}$/
18
- rx[:find_within_row] = /^I should see #{quoted_value} in the (\w+) data row of (.+)$/
19
- end
20
- end
21
-
22
- private
23
-
24
- def self.quoted_value
25
- '"([^\"]*)"'
26
- end
27
- end