pickle-has_many_support 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. data/.gitignore +5 -0
  2. data/History.txt +331 -0
  3. data/License.txt +20 -0
  4. data/README.rdoc +299 -0
  5. data/Rakefile +20 -0
  6. data/Rakefile.d/cucumber.rake +24 -0
  7. data/Rakefile.d/jeweller.rake +19 -0
  8. data/Rakefile.d/rcov.rake +18 -0
  9. data/Rakefile.d/rspec.rake +7 -0
  10. data/Rakefile.d/yard.rake +5 -0
  11. data/Todo.txt +3 -0
  12. data/VERSION +1 -0
  13. data/features/app/app.rb +122 -0
  14. data/features/app/blueprints.rb +11 -0
  15. data/features/app/factories.rb +23 -0
  16. data/features/app/views/notifier/email.erb +1 -0
  17. data/features/app/views/notifier/user_email.erb +6 -0
  18. data/features/email/email.feature +64 -0
  19. data/features/generator/generators.feature +59 -0
  20. data/features/path/models_page.feature +44 -0
  21. data/features/path/named_route_page.feature +10 -0
  22. data/features/pickle/create_from_active_record.feature +76 -0
  23. data/features/pickle/create_from_factory_girl.feature +59 -0
  24. data/features/pickle/create_from_machinist.feature +39 -0
  25. data/features/step_definitions/email_steps.rb +63 -0
  26. data/features/step_definitions/extra_email_steps.rb +7 -0
  27. data/features/step_definitions/fork_steps.rb +4 -0
  28. data/features/step_definitions/generator_steps.rb +46 -0
  29. data/features/step_definitions/path_steps.rb +14 -0
  30. data/features/step_definitions/pickle_steps.rb +100 -0
  31. data/features/support/email.rb +21 -0
  32. data/features/support/env.rb +52 -0
  33. data/features/support/paths.rb +47 -0
  34. data/features/support/pickle.rb +26 -0
  35. data/features/support/pickle_app.rb +4 -0
  36. data/init.rb +0 -0
  37. data/lib/pickle/adapter.rb +127 -0
  38. data/lib/pickle/adapters/active_record.rb +46 -0
  39. data/lib/pickle/adapters/data_mapper.rb +37 -0
  40. data/lib/pickle/config.rb +48 -0
  41. data/lib/pickle/email/parser.rb +18 -0
  42. data/lib/pickle/email/world.rb +13 -0
  43. data/lib/pickle/email.rb +79 -0
  44. data/lib/pickle/parser/matchers.rb +95 -0
  45. data/lib/pickle/parser.rb +71 -0
  46. data/lib/pickle/path/world.rb +5 -0
  47. data/lib/pickle/path.rb +45 -0
  48. data/lib/pickle/session/parser.rb +34 -0
  49. data/lib/pickle/session.rb +195 -0
  50. data/lib/pickle/version.rb +9 -0
  51. data/lib/pickle/world.rb +13 -0
  52. data/lib/pickle.rb +26 -0
  53. data/pickle.gemspec +117 -0
  54. data/rails_generators/pickle/pickle_generator.rb +33 -0
  55. data/rails_generators/pickle/templates/email.rb +21 -0
  56. data/rails_generators/pickle/templates/email_steps.rb +63 -0
  57. data/rails_generators/pickle/templates/paths.rb +47 -0
  58. data/rails_generators/pickle/templates/pickle.rb +28 -0
  59. data/rails_generators/pickle/templates/pickle_steps.rb +100 -0
  60. data/spec/pickle/adapter_spec.rb +163 -0
  61. data/spec/pickle/config_spec.rb +105 -0
  62. data/spec/pickle/email/parser_spec.rb +51 -0
  63. data/spec/pickle/email_spec.rb +160 -0
  64. data/spec/pickle/parser/matchers_spec.rb +74 -0
  65. data/spec/pickle/parser_spec.rb +165 -0
  66. data/spec/pickle/path_spec.rb +101 -0
  67. data/spec/pickle/session_spec.rb +451 -0
  68. data/spec/pickle_spec.rb +24 -0
  69. data/spec/spec_helper.rb +8 -0
  70. metadata +144 -0
data/pickle.gemspec ADDED
@@ -0,0 +1,117 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{pickle-has_many_support}
8
+ s.version = "0.3.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ian White"]
12
+ s.date = %q{2010-07-21}
13
+ s.description = %q{Easy model creation and reference in your cucumber features}
14
+ s.email = %q{ian.w.white@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "History.txt",
21
+ "License.txt",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "Rakefile.d/cucumber.rake",
25
+ "Rakefile.d/jeweller.rake",
26
+ "Rakefile.d/rcov.rake",
27
+ "Rakefile.d/rspec.rake",
28
+ "Rakefile.d/yard.rake",
29
+ "Todo.txt",
30
+ "VERSION",
31
+ "features/app/app.rb",
32
+ "features/app/blueprints.rb",
33
+ "features/app/factories.rb",
34
+ "features/app/views/notifier/email.erb",
35
+ "features/app/views/notifier/user_email.erb",
36
+ "features/email/email.feature",
37
+ "features/generator/generators.feature",
38
+ "features/path/models_page.feature",
39
+ "features/path/named_route_page.feature",
40
+ "features/pickle/create_from_active_record.feature",
41
+ "features/pickle/create_from_factory_girl.feature",
42
+ "features/pickle/create_from_machinist.feature",
43
+ "features/step_definitions/email_steps.rb",
44
+ "features/step_definitions/extra_email_steps.rb",
45
+ "features/step_definitions/fork_steps.rb",
46
+ "features/step_definitions/generator_steps.rb",
47
+ "features/step_definitions/path_steps.rb",
48
+ "features/step_definitions/pickle_steps.rb",
49
+ "features/support/email.rb",
50
+ "features/support/env.rb",
51
+ "features/support/paths.rb",
52
+ "features/support/pickle.rb",
53
+ "features/support/pickle_app.rb",
54
+ "init.rb",
55
+ "lib/pickle.rb",
56
+ "lib/pickle/adapter.rb",
57
+ "lib/pickle/adapters/active_record.rb",
58
+ "lib/pickle/adapters/data_mapper.rb",
59
+ "lib/pickle/config.rb",
60
+ "lib/pickle/email.rb",
61
+ "lib/pickle/email/parser.rb",
62
+ "lib/pickle/email/world.rb",
63
+ "lib/pickle/parser.rb",
64
+ "lib/pickle/parser/matchers.rb",
65
+ "lib/pickle/path.rb",
66
+ "lib/pickle/path/world.rb",
67
+ "lib/pickle/session.rb",
68
+ "lib/pickle/session/parser.rb",
69
+ "lib/pickle/version.rb",
70
+ "lib/pickle/world.rb",
71
+ "pickle.gemspec",
72
+ "rails_generators/pickle/pickle_generator.rb",
73
+ "rails_generators/pickle/templates/email.rb",
74
+ "rails_generators/pickle/templates/email_steps.rb",
75
+ "rails_generators/pickle/templates/paths.rb",
76
+ "rails_generators/pickle/templates/pickle.rb",
77
+ "rails_generators/pickle/templates/pickle_steps.rb",
78
+ "spec/pickle/adapter_spec.rb",
79
+ "spec/pickle/config_spec.rb",
80
+ "spec/pickle/email/parser_spec.rb",
81
+ "spec/pickle/email_spec.rb",
82
+ "spec/pickle/parser/matchers_spec.rb",
83
+ "spec/pickle/parser_spec.rb",
84
+ "spec/pickle/path_spec.rb",
85
+ "spec/pickle/session_spec.rb",
86
+ "spec/pickle_spec.rb",
87
+ "spec/spec_helper.rb"
88
+ ]
89
+ s.homepage = %q{http://github.com/kb/pickle/tree}
90
+ s.rdoc_options = ["--charset=UTF-8"]
91
+ s.require_paths = ["lib"]
92
+ s.rubygems_version = %q{1.3.7}
93
+ s.summary = %q{Easy model creation and reference in your cucumber features}
94
+ s.test_files = [
95
+ "spec/pickle/adapter_spec.rb",
96
+ "spec/pickle/config_spec.rb",
97
+ "spec/pickle/email/parser_spec.rb",
98
+ "spec/pickle/email_spec.rb",
99
+ "spec/pickle/parser/matchers_spec.rb",
100
+ "spec/pickle/parser_spec.rb",
101
+ "spec/pickle/path_spec.rb",
102
+ "spec/pickle/session_spec.rb",
103
+ "spec/pickle_spec.rb",
104
+ "spec/spec_helper.rb"
105
+ ]
106
+
107
+ if s.respond_to? :specification_version then
108
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
109
+ s.specification_version = 3
110
+
111
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
112
+ else
113
+ end
114
+ else
115
+ end
116
+ end
117
+
@@ -0,0 +1,33 @@
1
+ class PickleGenerator < Rails::Generator::Base
2
+ def initialize(args, options)
3
+ super(args, options)
4
+ @generate_email_steps = args.include?('email')
5
+ if @generate_path_steps = args.include?('path') || args.include?('paths')
6
+ File.exists?('features/support/paths.rb') or raise "features/support/paths.rb not found, is your cucumber up to date?"
7
+ end
8
+ end
9
+
10
+ def manifest
11
+ record do |m|
12
+ m.directory File.join('features/step_definitions')
13
+ m.directory File.join('features/support')
14
+
15
+ current_pickle = File.exists?('features/support/pickle.rb') ? File.read('features/support/pickle.rb') : ''
16
+ pickle_assigns = {:pickle_path => false, :pickle_email => false}
17
+
18
+ if @generate_path_steps
19
+ pickle_assigns[:pickle_path] = true
20
+ m.template 'paths.rb', File.join('features/support', 'paths.rb')
21
+ end
22
+
23
+ if @generate_email_steps
24
+ pickle_assigns[:pickle_email] = true
25
+ m.template 'email_steps.rb', File.join('features/step_definitions', 'email_steps.rb')
26
+ m.template 'email.rb', File.join('features/support', 'email.rb')
27
+ end
28
+
29
+ m.template 'pickle_steps.rb', File.join('features/step_definitions', 'pickle_steps.rb')
30
+ m.template 'pickle.rb', File.join('features/support', 'pickle.rb'), :assigns => pickle_assigns
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ module EmailHelpers
2
+ # Maps a name to an email address. Used by email_steps
3
+
4
+ def email_for(to)
5
+ case to
6
+
7
+ # add your own name => email address mappings here
8
+
9
+ when /^#{capture_model}$/
10
+ model($1).email
11
+
12
+ when /^"(.*)"$/
13
+ $1
14
+
15
+ else
16
+ to
17
+ end
18
+ end
19
+ end
20
+
21
+ World(EmailHelpers)
@@ -0,0 +1,63 @@
1
+ # this file generated by script/generate pickle email
2
+ #
3
+ # add email mappings in features/support/email.rb
4
+
5
+ ActionMailer::Base.delivery_method = :test
6
+ ActionMailer::Base.perform_deliveries = true
7
+
8
+ Before do
9
+ ActionMailer::Base.deliveries.clear
10
+ end
11
+
12
+ # Clear the deliveries array, useful if your background sends email that you want to ignore
13
+ Given(/^all emails? (?:have|has) been delivered$/) do
14
+ ActionMailer::Base.deliveries.clear
15
+ end
16
+
17
+ Given(/^(\d)+ emails? should be delivered$/) do |count|
18
+ emails.size.should == count.to_i
19
+ end
20
+
21
+ When(/^(?:I|they) follow "([^"]*?)" in #{capture_email}$/) do |link, email_ref|
22
+ visit_in_email(email(email_ref), link)
23
+ end
24
+
25
+ When(/^(?:I|they) click the first link in #{capture_email}$/) do |email_ref|
26
+ click_first_link_in_email(email(email_ref))
27
+ end
28
+
29
+ Then(/^(\d)+ emails? should be delivered to (.*)$/) do |count, to|
30
+ emails("to: \"#{email_for(to)}\"").size.should == count.to_i
31
+ end
32
+
33
+ Then(/^(\d)+ emails? should be delivered with #{capture_fields}$/) do |count, fields|
34
+ emails(fields).size.should == count.to_i
35
+ end
36
+
37
+ Then(/^#{capture_email} should be delivered to (.+)$/) do |email_ref, to|
38
+ email(email_ref, "to: \"#{email_for(to)}\"").should_not be_nil
39
+ end
40
+
41
+ Then(/^#{capture_email} should not be delivered to (.+)$/) do |email_ref, to|
42
+ email(email_ref, "to: \"#{email_for(to)}\"").should be_nil
43
+ end
44
+
45
+ Then(/^#{capture_email} should have #{capture_fields}$/) do |email_ref, fields|
46
+ email(email_ref, fields).should_not be_nil
47
+ end
48
+
49
+ Then(/^#{capture_email} should contain "(.*)"$/) do |email_ref, text|
50
+ email(email_ref).body.should =~ /#{text}/
51
+ end
52
+
53
+ Then(/^#{capture_email} should not contain "(.*)"$/) do |email_ref, text|
54
+ email(email_ref).body.should_not =~ /#{text}/
55
+ end
56
+
57
+ Then(/^#{capture_email} should link to (.+)$/) do |email_ref, page|
58
+ email(email_ref).body.should =~ /#{path_to(page)}/
59
+ end
60
+
61
+ Then(/^show me the emails?$/) do
62
+ save_and_open_emails
63
+ end
@@ -0,0 +1,47 @@
1
+ module NavigationHelpers
2
+ # Maps a name to a path. Used by the
3
+ #
4
+ # When /^I go to (.+)$/ do |page_name|
5
+ #
6
+ # step definition in web_steps.rb
7
+ #
8
+ def path_to(page_name)
9
+ case page_name
10
+
11
+ when /the home\s?page/
12
+ '/'
13
+
14
+ # the following are examples using path_to_pickle
15
+
16
+ when /^#{capture_model}(?:'s)? page$/ # eg. the forum's page
17
+ path_to_pickle $1
18
+
19
+ when /^#{capture_model}(?:'s)? #{capture_model}(?:'s)? page$/ # eg. the forum's post's page
20
+ path_to_pickle $1, $2
21
+
22
+ when /^#{capture_model}(?:'s)? #{capture_model}'s (.+?) page$/ # eg. the forum's post's comments page
23
+ path_to_pickle $1, $2, :extra => $3 # or the forum's post's edit page
24
+
25
+ when /^#{capture_model}(?:'s)? (.+?) page$/ # eg. the forum's posts page
26
+ path_to_pickle $1, :extra => $2 # or the forum's edit page
27
+
28
+ # Add more mappings here.
29
+ # Here is an example that pulls values out of the Regexp:
30
+ #
31
+ # when /^(.*)'s profile page$/i
32
+ # user_profile_path(User.find_by_login($1))
33
+
34
+ else
35
+ begin
36
+ page_name =~ /the (.*) page/
37
+ path_components = $1.split(/\s+/)
38
+ self.send(path_components.push('path').join('_').to_sym)
39
+ rescue Object => e
40
+ raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
41
+ "Now, go and add a mapping in #{__FILE__}"
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ World(NavigationHelpers)
@@ -0,0 +1,28 @@
1
+ # this file generated by script/generate pickle [paths] [email]
2
+ #
3
+ # Make sure that you are loading your factory of choice in your cucumber environment
4
+ #
5
+ # For machinist add: features/support/machinist.rb
6
+ #
7
+ # require 'machinist/active_record' # or your chosen adaptor
8
+ # require File.dirname(__FILE__) + '/../../spec/blueprints' # or wherever your blueprints are
9
+ # Before { Sham.reset } # to reset Sham's seed between scenarios so each run has same random sequences
10
+ #
11
+ # For FactoryGirl add: features/support/factory_girl.rb
12
+ #
13
+ # require 'factory_girl'
14
+ # require File.dirname(__FILE__) + '/../../spec/factories' # or wherever your factories are
15
+ #
16
+ # You may also need to add gem dependencies on your factory of choice in <tt>config/environments/cucumber.rb</tt>
17
+
18
+ require 'pickle/world'
19
+ # Example of configuring pickle:
20
+ #
21
+ # Pickle.configure do |config|
22
+ # config.adapters = [:machinist]
23
+ # config.map 'I', 'myself', 'me', 'my', :to => 'user: "me"'
24
+ # end
25
+ <%- if pickle_path -%>require 'pickle/path/world'
26
+ <%- end -%>
27
+ <%- if pickle_email -%>require 'pickle/email/world'
28
+ <%- end -%>
@@ -0,0 +1,100 @@
1
+ # this file generated by script/generate pickle
2
+
3
+ # create a model
4
+ Given(/^#{capture_model} exists?(?: with #{capture_fields})?$/) do |name, fields|
5
+ create_model(name, fields)
6
+ end
7
+
8
+ # create n models
9
+ Given(/^(\d+) #{capture_plural_factory} exist(?: with #{capture_fields})?$/) do |count, plural_factory, fields|
10
+ count.to_i.times { create_model(plural_factory.singularize, fields) }
11
+ end
12
+
13
+ # create models from a table
14
+ Given(/^the following #{capture_plural_factory} exists?:?$/) do |plural_factory, table|
15
+ create_models_from_table(plural_factory, table)
16
+ end
17
+
18
+ # find a model
19
+ Then(/^#{capture_model} should exist(?: with #{capture_fields})?$/) do |name, fields|
20
+ find_model!(name, fields)
21
+ end
22
+
23
+ # not find a model
24
+ Then(/^#{capture_model} should not exist(?: with #{capture_fields})?$/) do |name, fields|
25
+ find_model(name, fields).should be_nil
26
+ end
27
+
28
+ # find models with a table
29
+ Then(/^the following #{capture_plural_factory} should exists?:?$/) do |plural_factory, table|
30
+ find_models_from_table(plural_factory, table).should_not be_any(&:nil?)
31
+ end
32
+
33
+ # find exactly n models
34
+ Then(/^(\d+) #{capture_plural_factory} should exist(?: with #{capture_fields})?$/) do |count, plural_factory, fields|
35
+ find_models(plural_factory.singularize, fields).size.should == count.to_i
36
+ end
37
+
38
+ # assert equality of models
39
+ Then(/^#{capture_model} should be #{capture_model}$/) do |a, b|
40
+ model!(a).should == model!(b)
41
+ end
42
+
43
+ # assert model is in another model's has_many assoc
44
+ Then(/^#{capture_model} should be (?:in|one of|amongst) #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
45
+ model!(owner).send(association).should include(model!(target))
46
+ end
47
+
48
+ # assert model is not in another model's has_many assoc
49
+ Then(/^#{capture_model} should not be (?:in|one of|amongst) #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
50
+ model!(owner).send(association).should_not include(model!(target))
51
+ end
52
+
53
+ # assert model is another model's has_one/belongs_to assoc
54
+ Then(/^#{capture_model} should be #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
55
+ model!(owner).send(association).should == model!(target)
56
+ end
57
+
58
+ # assert model is not another model's has_one/belongs_to assoc
59
+ Then(/^#{capture_model} should not be #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
60
+ model!(owner).send(association).should_not == model!(target)
61
+ end
62
+
63
+ # assert model.predicate?
64
+ Then(/^#{capture_model} should (?:be|have) (?:an? )?#{capture_predicate}$/) do |name, predicate|
65
+ if model!(name).respond_to?("has_#{predicate.gsub(' ', '_')}")
66
+ model!(name).should send("have_#{predicate.gsub(' ', '_')}")
67
+ else
68
+ model!(name).should send("be_#{predicate.gsub(' ', '_')}")
69
+ end
70
+ end
71
+
72
+ # assert not model.predicate?
73
+ Then(/^#{capture_model} should not (?:be|have) (?:an? )?#{capture_predicate}$/) do |name, predicate|
74
+ if model!(name).respond_to?("has_#{predicate.gsub(' ', '_')}")
75
+ model!(name).should_not send("have_#{predicate.gsub(' ', '_')}")
76
+ else
77
+ model!(name).should_not send("be_#{predicate.gsub(' ', '_')}")
78
+ end
79
+ end
80
+
81
+ # model.attribute.should eql(value)
82
+ # model.attribute.should_not eql(value)
83
+ Then(/^#{capture_model}'s (\w+) (should(?: not)?) be #{capture_value}$/) do |name, attribute, expectation, expected|
84
+ actual_value = model(name).send(attribute)
85
+ expectation = expectation.gsub(' ', '_')
86
+
87
+ case expected
88
+ when 'nil', 'true', 'false'
89
+ actual_value.send(expectation, send("be_#{expected}"))
90
+ when /^[+-]?[0-9_]+(\.\d+)?$/
91
+ actual_value.send(expectation, eql(expected.to_f))
92
+ else
93
+ actual_value.to_s.send(expectation, eql(eval(expected)))
94
+ end
95
+ end
96
+
97
+ # assert size of association
98
+ Then /^#{capture_model} should have (\d+) (\w+)$/ do |name, size, association|
99
+ model!(name).send(association).size.should == size.to_i
100
+ end
@@ -0,0 +1,163 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require 'active_record'
4
+ require 'factory_girl'
5
+ require 'machinist/active_record'
6
+ require 'pickle/adapters/active_record'
7
+
8
+ describe Pickle::Adapter do
9
+ it ".factories should raise NotImplementedError" do
10
+ lambda{ Pickle::Adapter.factories }.should raise_error(NotImplementedError)
11
+ end
12
+
13
+ it "#create should raise NotImplementedError" do
14
+ lambda{ Pickle::Adapter.new.create }.should raise_error(NotImplementedError)
15
+ end
16
+
17
+ describe ".model_classes" do
18
+ before do
19
+ Pickle::Adapter.model_classes = nil
20
+ end
21
+
22
+ it "should only include #suitable_for_pickle classes" do
23
+ klass1 = Class.new(ActiveRecord::Base)
24
+ klass2 = Class.new(ActiveRecord::Base)
25
+ klass3 = Class.new(ActiveRecord::Base)
26
+ klass4 = Class.new(ActiveRecord::Base)
27
+ klass5 = Class.new(ActiveRecord::Base)
28
+ klass6 = Class.new(ActiveRecord::Base)
29
+ [klass1, klass2,klass3,klass4, klass5, klass6].each{|k| k.stub!(:table_exists?).and_return(true)}
30
+
31
+ klass2.stub!(:name).and_return("CGI::Session::ActiveRecordStore::Session")
32
+ klass3.stub!(:name).and_return("ActiveRecord::SessionStore::Session")
33
+ klass4.stub!(:table_exists?).and_return(false)
34
+ klass5.stub!(:abstract_class?).and_return(true)
35
+ Pickle::Adapter.model_classes.should include(klass1, klass6)
36
+ Pickle::Adapter.model_classes.should_not include(klass2, klass3, klass4, klass5)
37
+ end
38
+ end
39
+
40
+ describe "adapters: " do
41
+ before do
42
+ @klass1 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('One')}
43
+ @klass2 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('One::Two')}
44
+ @klass3 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('Three')}
45
+ end
46
+
47
+ describe 'ActiveRecord' do
48
+ before do
49
+ ActiveRecord::Base::PickleAdapter.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
50
+ end
51
+
52
+ it ".factories should create one for each active record class" do
53
+ Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass1).once
54
+ Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass2).once
55
+ Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass3).once
56
+ Pickle::Adapter::ActiveRecord.factories
57
+ end
58
+
59
+ describe ".new(Class)" do
60
+ before do
61
+ @factory = Pickle::Adapter::ActiveRecord.new(@klass2)
62
+ end
63
+
64
+ it "should have underscored (s/_) name of Class as #name" do
65
+ @factory.name.should == 'one_two'
66
+ end
67
+
68
+ it "#create(attrs) should call Class.create!(attrs)" do
69
+ @klass2.should_receive(:create!).with({:key => "val"})
70
+ @factory.create(:key => "val")
71
+ end
72
+ end
73
+ end
74
+
75
+ describe 'FactoryGirl' do
76
+ before do
77
+ Pickle::Adapter::FactoryGirl.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
78
+ @orig_factories, Factory.factories = Factory.factories, {}
79
+
80
+ Factory.define(:one, :class => @klass1) {}
81
+ Factory.define(:two, :class => @klass2) {}
82
+ @factory1 = Factory.factories[:one]
83
+ @factory2 = Factory.factories[:two]
84
+ end
85
+
86
+ after do
87
+ Factory.factories = @orig_factories
88
+ end
89
+
90
+ it ".factories should create one for each factory" do
91
+ Pickle::Adapter::FactoryGirl.should_receive(:new).with(@factory1).once
92
+ Pickle::Adapter::FactoryGirl.should_receive(:new).with(@factory2).once
93
+ Pickle::Adapter::FactoryGirl.factories
94
+ end
95
+
96
+ describe ".new(factory)" do
97
+ before do
98
+ @factory = Pickle::Adapter::FactoryGirl.new(@factory1)
99
+ end
100
+
101
+ it "should have name of factory_name" do
102
+ @factory.name.should == 'one'
103
+ end
104
+
105
+ it "should have klass of build_class" do
106
+ @factory.klass.should == @klass1
107
+ end
108
+
109
+ it "#create(attrs) should call Factory(<:key>, attrs)" do
110
+ Factory.should_receive(:create).with("one", {:key => "val"})
111
+ @factory.create(:key => "val")
112
+ end
113
+ end
114
+ end
115
+
116
+ describe 'Machinist' do
117
+ before do
118
+ Pickle::Adapter::Machinist.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
119
+
120
+ @klass1.blueprint {}
121
+ @klass3.blueprint {}
122
+ @klass3.blueprint(:special) {}
123
+ end
124
+
125
+ it ".factories should create one for each master blueprint, and special case" do
126
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass1, :master).once
127
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass3, :master).once
128
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass3, :special).once
129
+ Pickle::Adapter::Machinist.factories
130
+ end
131
+
132
+ describe ".new(Class, :master)" do
133
+ before do
134
+ @factory = Pickle::Adapter::Machinist.new(@klass1, :master)
135
+ end
136
+
137
+ it "should have underscored (s/_) name of Class as #name" do
138
+ @factory.name.should == 'one'
139
+ end
140
+
141
+ it "#create(attrs) should call Class.make(:master, attrs)" do
142
+ @klass1.should_receive(:make).with(:master, {:key => "val"})
143
+ @factory.create(:key => "val")
144
+ end
145
+ end
146
+
147
+ describe ".new(Class, :special)" do
148
+ before do
149
+ @factory = Pickle::Adapter::Machinist.new(@klass3, :special)
150
+ end
151
+
152
+ it "should have 'special_<Class name>' as #name" do
153
+ @factory.name.should == 'special_three'
154
+ end
155
+
156
+ it "#create(attrs) should call Class.make(:special, attrs)" do
157
+ @klass3.should_receive(:make).with(:special, {:key => "val"})
158
+ @factory.create(:key => "val")
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,105 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Pickle::Config do
4
+ before do
5
+ @config = Pickle::Config.new
6
+ end
7
+
8
+ it "#adapters should default to :machinist, :factory_girl, :active_record" do
9
+ @config.adapters.should == [:machinist, :factory_girl, :active_record]
10
+ end
11
+
12
+ it "#adapter_classes should default to Adapter::Machinist, Adapter::FactoryGirl, Adapter::ActiveRecord" do
13
+ @config.adapter_classes.should == [Pickle::Adapter::Machinist, Pickle::Adapter::FactoryGirl, Pickle::Adapter::ActiveRecord]
14
+ end
15
+
16
+ describe "setting adapters to [:machinist, SomeAdapter]" do
17
+ class SomeAdapter; end
18
+
19
+ before do
20
+ @config.adapters = [:machinist, SomeAdapter]
21
+ end
22
+
23
+ it "#adapter_classes should be Adapter::Machinist, SomeAdapter" do
24
+ @config.adapter_classes.should == [Pickle::Adapter::Machinist, SomeAdapter]
25
+ end
26
+ end
27
+
28
+ describe "#factories" do
29
+ it "should call adaptor.factories for each adaptor" do
30
+ Pickle::Adapter::Machinist.should_receive(:factories).and_return([])
31
+ Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([])
32
+ Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([])
33
+ @config.factories
34
+ end
35
+
36
+ it "should aggregate factories into a hash using factory name as key" do
37
+ Pickle::Adapter::Machinist.should_receive(:factories).and_return([@machinist = mock('machinist', :name => 'machinist')])
38
+ Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([@factory_girl = mock('factory_girl', :name => 'factory_girl')])
39
+ Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([@active_record = mock('active_record', :name => 'active_record')])
40
+ @config.factories.should == {'machinist' => @machinist, 'factory_girl' => @factory_girl, 'active_record' => @active_record}
41
+ end
42
+
43
+ it "should give preference to adaptors first in the list" do
44
+ Pickle::Adapter::Machinist.should_receive(:factories).and_return([@machinist_one = mock('one', :name => 'one')])
45
+ Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([@factory_girl_one = mock('one', :name => 'one'), @factory_girl_two = mock('two', :name => 'two')])
46
+ Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([@active_record_two = mock('two', :name => 'two'), @active_record_three = mock('three', :name => 'three')])
47
+ @config.factories.should == {'one' => @machinist_one, 'two' => @factory_girl_two, 'three' => @active_record_three}
48
+ end
49
+ end
50
+
51
+ it "#mappings should default to []" do
52
+ @config.mappings.should == []
53
+ end
54
+
55
+ describe '#predicates' do
56
+ it "should be list of all non object ? public instance methods + columns methods of Adapter.model_classes" do
57
+ class1 = mock('Class1',
58
+ :public_instance_methods => ['nope', 'foo?', 'bar?'],
59
+ :column_names => ['one', 'two'],
60
+ :const_get => ::ActiveRecord::Base::PickleAdapter
61
+ )
62
+ class2 = mock('Class2',
63
+ :public_instance_methods => ['not', 'foo?', 'faz?'],
64
+ :column_names => ['two', 'three'],
65
+ :const_get => ::ActiveRecord::Base::PickleAdapter
66
+ )
67
+ Pickle::Adapter.stub!(:model_classes).and_return([class1, class2])
68
+
69
+ @config.predicates.to_set.should == ['foo?', 'faz?', 'bar?', 'one', 'two', 'three'].to_set
70
+ end
71
+
72
+ it "should be overridable" do
73
+ @config.predicates = %w(lame?)
74
+ @config.predicates.should == %w(lame?)
75
+ end
76
+ end
77
+
78
+ describe "#map 'foo', :to => 'faz'" do
79
+ before do
80
+ @config.map 'foo', :to => 'faz'
81
+ end
82
+
83
+ it "should create OpenStruct(search: 'foo', replace: 'faz') mapping" do
84
+ @config.mappings.first.should == OpenStruct.new(:search => 'foo', :replacement => 'faz')
85
+ end
86
+ end
87
+
88
+ describe "#map 'foo', 'bar' :to => 'faz'" do
89
+ before do
90
+ @config.map 'foo', 'bar', :to => 'faz'
91
+ end
92
+
93
+ it "should create 2 mappings" do
94
+ @config.mappings.first.should == OpenStruct.new(:search => 'foo', :replacement => 'faz')
95
+ @config.mappings.last.should == OpenStruct.new(:search => 'bar', :replacement => 'faz')
96
+ end
97
+ end
98
+
99
+ it "#configure(&block) should execiute on self" do
100
+ @config.should_receive(:foo).with(:bar)
101
+ @config.configure do |c|
102
+ c.foo :bar
103
+ end
104
+ end
105
+ end