· 8 years ago · May 27, 2018, 01:30 AM
1gem 'pickle', '0.2.11'
2
3run 'bundle install'
4
5file 'features/support/pickle.rb', <<-EOF
6# Make sure that you are loading your factory of choice in your cucumber environment
7#
8# For machinist add: features/support/machinist.rb
9#
10# require 'machinist/active_record' # or your chosen adaptor
11# require File.dirname(__FILE__) + '/../../spec/blueprints' # or wherever your blueprints are
12# Before { Sham.reset } # to reset Sham's seed between scenarios so each run has same random sequences
13#
14# For FactoryGirl add: features/support/factory_girl.rb
15#
16# require 'factory_girl'
17# require File.dirname(__FILE__) + '/../../spec/factories' # or wherever your factories are
18#
19# You may also need to add gem dependencies on your factory of choice in <tt>config/environments/cucumber.rb</tt>
20
21require 'pickle/world'
22# Example of configuring pickle:
23#
24# Pickle.configure do |config|
25# config.adapters = [:machinist]
26# config.map 'I', 'myself', 'me', 'my', :to => 'user: "me"'
27# end
28Pickle.configure do |config|
29 config.adapters = [:machinist]
30end
31EOF
32
33file 'features/step_definitions/pickle_steps.rb', <<-EOF
34# create a model
35Given(/^\#{capture_model} exists?(?: with \#{capture_fields})?$/) do |name, fields|
36 create_model(name, fields)
37end
38
39# create n models
40Given(/^(\d+) \#{capture_plural_factory} exist(?: with \#{capture_fields})?$/) do |count, plural_factory, fields|
41 count.to_i.times { create_model(plural_factory.singularize, fields) }
42end
43
44# create models from a table
45Given(/^the following \#{capture_plural_factory} exists?:?$/) do |plural_factory, table|
46 create_models_from_table(plural_factory, table)
47end
48
49# find a model
50Then(/^\#{capture_model} should exist(?: with \#{capture_fields})?$/) do |name, fields|
51 find_model!(name, fields)
52end
53
54# not find a model
55Then(/^\#{capture_model} should not exist(?: with \#{capture_fields})?$/) do |name, fields|
56 find_model(name, fields).should be_nil
57end
58
59# find models with a table
60Then(/^the following \#{capture_plural_factory} should exists?:?$/) do |plural_factory, table|
61 find_models_from_table(plural_factory, table).should_not be_any(&:nil?)
62end
63
64# find exactly n models
65Then(/^(\d+) \#{capture_plural_factory} should exist(?: with \#{capture_fields})?$/) do |count, plural_factory, fields|
66 find_models(plural_factory.singularize, fields).size.should == count.to_i
67end
68
69# assert equality of models
70Then(/^\#{capture_model} should be \#{capture_model}$/) do |a, b|
71 model!(a).should == model!(b)
72end
73
74# assert model is in another model's has_many assoc
75Then(/^\#{capture_model} should be (?:in|one of|amongst) \#{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
76 model!(owner).send(association).should include(model!(target))
77end
78
79# assert model is not in another model's has_many assoc
80Then(/^\#{capture_model} should not be (?:in|one of|amongst) \#{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
81 model!(owner).send(association).should_not include(model!(target))
82end
83
84# assert model is another model's has_one/belongs_to assoc
85Then(/^\#{capture_model} should be \#{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
86 model!(owner).send(association).should == model!(target)
87end
88
89# assert model is not another model's has_one/belongs_to assoc
90Then(/^\#{capture_model} should not be \#{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
91 model!(owner).send(association).should_not == model!(target)
92end
93
94# assert model.predicate?
95Then(/^\#{capture_model} should (?:be|have) (?:an? )?\#{capture_predicate}$/) do |name, predicate|
96 if model!(name).respond_to?("has_\#{predicate.gsub(' ', '_')}")
97 model!(name).should send("have_\#{predicate.gsub(' ', '_')}")
98 else
99 model!(name).should send("be_\#{predicate.gsub(' ', '_')}")
100 end
101end
102
103# assert not model.predicate?
104Then(/^\#{capture_model} should not (?:be|have) (?:an? )?\#{capture_predicate}$/) do |name, predicate|
105 if model!(name).respond_to?("has_\#{predicate.gsub(' ', '_')}")
106 model!(name).should_not send("have_\#{predicate.gsub(' ', '_')}")
107 else
108 model!(name).should_not send("be_\#{predicate.gsub(' ', '_')}")
109 end
110end
111
112# model.attribute.should eql(value)
113# model.attribute.should_not eql(value)
114Then(/^\#{capture_model}'s (\w+) (should(?: not)?) be \#{capture_value}$/) do |name, attribute, expectation, expected|
115 actual_value = model(name).send(attribute)
116 expectation = expectation.gsub(' ', '_')
117
118 case expected
119 when 'nil', 'true', 'false'
120 actual_value.send(expectation, send("be_\#{expected}"))
121 when /^[+-]?[0-9_]+(\.\d+)?$/
122 actual_value.send(expectation, eql(expected.to_f))
123 else
124 actual_value.to_s.send(expectation, eql(eval(expected)))
125 end
126end
127
128# assert size of association
129Then /^\#{capture_model} should have (\d+) (\w+)$/ do |name, size, association|
130 model!(name).send(association).size.should == size.to_i
131end
132EOF
133
134git :add => '.'
135git :commit => "-am 'Added Pickle for Cucumber'"