Skip to content

Commit

Permalink
Modified the way pickle handles predicates to match rspec eg should b…
Browse files Browse the repository at this point in the history
…e_empty => empty? == true and should have_charisma => has_charisma? == true. This avoids the ugly: Then a user should be has_charisma. #railscamp7
  • Loading branch information
Michael MacDonald authored and ianwhite committed Apr 27, 2010
1 parent bc5d7ab commit 163edc5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
16 changes: 12 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ You can refer to other models in the fields
"Given <b>n models</b> exist with <b>fields</b>", examples:

Given 10 users exist with activated: false

"Given the following <b>models</b> exist:", examples:

Given the following users exist
| name | activated |
| Fred | false |
| Ethel | true |

==== Then steps

Expand Down Expand Up @@ -221,13 +228,14 @@ Many-to-one assocs: "Then <b>a model</b> should be [in|one of] <b>other model</b
===== Asserting predicate methods

"Then <b>a model</b> should [be|have] [a|an] <b>predicate</b>", e.g.

Then the user should have a status # => user.status?.should == true
Then the car: "batmobile" should be fast # => car.fast?.should == true

Then the user should have a status # => user.status.should be_present
Then the user should have a stale password # => user.should have_stale_password
Then the car: "batmobile" should be fast # => car.should be_fast

"Then <b>a model</b> should not [be|have] [a|an] <b>predicate</b>", e.g.

Then person: "fred" should not be childless # => fred.childless?.should == false
Then person: "fred" should not be childless # => fred.should_not be_childless

=== Regexps for use in your own steps

Expand Down
1 change: 1 addition & 0 deletions features/app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
create_table :users, :force => true do |t|
t.string :name, :status, :email
t.decimal :attitude_score, :precision => 4, :scale => 2
t.boolean :has_stale_password, :default => false
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion features/pickle/create_from_active_record.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ Feature: I can easily create models from my blueprints

@wip
Scenario: I create a user, and see if it looks right
Given a user exists with name: "Fred"
Given a user exists with name: "Fred", has_stale_password: true
Then the user should not have a status
And the user should have a stale password
And the user's name should be "Fred"

Scenario: I create a user, and see if it looks right
Expand Down
2 changes: 1 addition & 1 deletion lib/pickle/parser/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def match_model
end

def match_predicate
"(?:#{config.predicates.map{|m| m.to_s.sub(/\?$/,'').gsub('_','[_ ]')}.join('|')})"
"(?:#{config.predicates.map{|m| m.sub(/^has_/,'').sub(/\?$/,'').gsub('_','[_ ]')}.join('|')})"
end

# create capture analogues of match methods
Expand Down
12 changes: 10 additions & 2 deletions rails_generators/pickle/templates/pickle_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@

# assert model.predicate?
Then(/^#{capture_model} should (?:be|have) (?:an? )?#{capture_predicate}$/) do |name, predicate|
model!(name).should send("be_#{predicate.gsub(' ', '_')}")
if model!(name).respond_to?("has_#{predicate.gsub(' ', '_')}")
model!(name).should send("have_#{predicate.gsub(' ', '_')}")
else
model!(name).should send("be_#{predicate.gsub(' ', '_')}")
end
end

# assert not model.predicate?
Then(/^#{capture_model} should not (?:be|have) (?:an? )?#{capture_predicate}$/) do |name, predicate|
model!(name).should_not send("be_#{predicate.gsub(' ', '_')}")
if model!(name).respond_to?("has_#{predicate.gsub(' ', '_')}")
model!(name).should_not send("have_#{predicate.gsub(' ', '_')}")
else
model!(name).should_not send("be_#{predicate.gsub(' ', '_')}")
end
end

# model.attribute.should eql(value)
Expand Down
8 changes: 4 additions & 4 deletions spec/pickle/parser/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
describe Pickle::Parser::Matchers do
include Pickle::Parser::Matchers

describe "(config: [factories: user, car, fast_car] [predicates: name, status, fancy?, super_fancy?]" do
describe "(config: [factories: user, car, fast_car] [predicates: name, status, fancy?, super_fancy?, has_style?, has_super_style?]" do
def config
@config ||= Pickle::Config.new do |c|
c.factories = {
'user' => mock('factory'),
'car' => mock('factory'),
'fast_car' => mock('factory')
}
c.predicates = %w(name status fancy? super_fancy?)
c.predicates = %w(name status fancy? super_fancy? has_style? has_super_style?)
end
end

Expand Down Expand Up @@ -50,8 +50,8 @@ def self.atom_should_not_match(atom, strings)
atom_should_match :match_model, ['a user', '1st fast car', 'the 23rd fast_car', 'the user: "fred flinstone"']
atom_should_not_match :match_model, ['a giraffe', 'a 1st faster car: "jim"', 'an event created']

atom_should_match :match_predicate, ['name', 'status', 'fancy', 'super fancy', 'super_fancy']
atom_should_not_match :match_predicate, ['nameo', 'increment', 'not a predicate']
atom_should_match :match_predicate, ['name', 'status', 'fancy', 'super fancy', 'super_fancy', 'style', 'super style', 'super_style']
atom_should_not_match :match_predicate, ['nameo', 'increment', 'not a predicate', 'has style']

atom_should_match :match_factory, ['user', 'fast car', 'fast_car', 'car']
atom_should_not_match :match_factory, ['users', 'faster car', 'event created']
Expand Down

0 comments on commit 163edc5

Please sign in to comment.