pickle 0.1.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/.gitignore +2 -0
  2. data/History.txt +165 -0
  3. data/License.txt +20 -0
  4. data/README.rdoc +205 -0
  5. data/Rakefile +116 -0
  6. data/Todo.txt +4 -0
  7. data/VERSION +1 -0
  8. data/features/app/app.rb +112 -0
  9. data/features/app/blueprints.rb +11 -0
  10. data/features/app/factories.rb +23 -0
  11. data/features/app/views/notifier/email.erb +1 -0
  12. data/features/app/views/notifier/user_email.erb +6 -0
  13. data/features/email/email.feature +38 -0
  14. data/features/generator/generators.feature +54 -0
  15. data/features/path/models_page.feature +44 -0
  16. data/features/path/named_route_page.feature +10 -0
  17. data/features/pickle/create_from_active_record.feature +20 -0
  18. data/features/pickle/create_from_factory_girl.feature +43 -0
  19. data/features/pickle/create_from_machinist.feature +38 -0
  20. data/features/step_definitions/email_steps.rb +50 -0
  21. data/features/step_definitions/extra_email_steps.rb +7 -0
  22. data/features/step_definitions/fork_steps.rb +4 -0
  23. data/features/step_definitions/generator_steps.rb +42 -0
  24. data/features/step_definitions/path_steps.rb +14 -0
  25. data/features/step_definitions/pickle_steps.rb +41 -0
  26. data/features/support/env.rb +34 -0
  27. data/features/support/paths.rb +46 -0
  28. data/garlic.rb +36 -0
  29. data/init.rb +0 -0
  30. data/lib/pickle.rb +26 -0
  31. data/lib/pickle/adapter.rb +87 -0
  32. data/lib/pickle/config.rb +48 -0
  33. data/lib/pickle/email.rb +36 -0
  34. data/lib/pickle/email/parser.rb +18 -0
  35. data/lib/pickle/email/world.rb +13 -0
  36. data/lib/pickle/parser.rb +65 -0
  37. data/lib/pickle/parser/matchers.rb +87 -0
  38. data/lib/pickle/path.rb +44 -0
  39. data/lib/pickle/path/world.rb +5 -0
  40. data/lib/pickle/session.rb +151 -0
  41. data/lib/pickle/session/parser.rb +24 -0
  42. data/lib/pickle/version.rb +6 -0
  43. data/lib/pickle/world.rb +9 -0
  44. data/pickle.gemspec +107 -0
  45. data/rails_generators/pickle/pickle_generator.rb +41 -0
  46. data/rails_generators/pickle/templates/email_steps.rb +50 -0
  47. data/rails_generators/pickle/templates/env.rb +14 -0
  48. data/rails_generators/pickle/templates/paths.rb +20 -0
  49. data/rails_generators/pickle/templates/pickle_steps.rb +41 -0
  50. data/spec/lib/pickle_adapter_spec.rb +164 -0
  51. data/spec/lib/pickle_config_spec.rb +97 -0
  52. data/spec/lib/pickle_email_parser_spec.rb +49 -0
  53. data/spec/lib/pickle_email_spec.rb +131 -0
  54. data/spec/lib/pickle_parser_matchers_spec.rb +70 -0
  55. data/spec/lib/pickle_parser_spec.rb +154 -0
  56. data/spec/lib/pickle_path_spec.rb +77 -0
  57. data/spec/lib/pickle_session_spec.rb +337 -0
  58. data/spec/lib/pickle_spec.rb +24 -0
  59. data/spec/spec_helper.rb +38 -0
  60. metadata +122 -0
@@ -0,0 +1,131 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe Pickle::Email do
4
+ include Pickle::Session
5
+ include Pickle::Email
6
+ include Pickle::Email::Parser
7
+
8
+ before do
9
+ @email1 = mock("Email 1")
10
+ @email2 = mock("Email 2")
11
+ ActionMailer::Base.stub!(:deliveries).and_return([@email1, @email2])
12
+ end
13
+
14
+ describe "#emails" do
15
+ it "should return ordered deliveries" do
16
+ emails.should == [@email1, @email2]
17
+ end
18
+
19
+ describe "(after)" do
20
+ before do
21
+ emails
22
+ end
23
+
24
+ it "#email('the email') should return the last delivery" do
25
+ email('the email').should == @email2
26
+ end
27
+
28
+ it "#email('the 1st email') should return the first delivery" do
29
+ email('the 1st email').should == @email1
30
+ end
31
+
32
+ it "#email('the first email') should return the first delivery" do
33
+ email('the first email').should == @email1
34
+ end
35
+
36
+ it "#email('the 2nd email') should return the second delivery" do
37
+ email('the 2nd email').should == @email2
38
+ end
39
+
40
+ it "#email('the last email') should return the second delivery" do
41
+ email('the last email').should == @email2
42
+ end
43
+
44
+ it "#email2('the 3rd email') should be nil" do
45
+ email('the 3rd email').should == nil
46
+ end
47
+ end
48
+
49
+ describe "when email1 is to fred & joe, and email2 is to joe" do
50
+ before do
51
+ @email1.stub!(:to).and_return(['fred@gmail.com', 'joe@gmail.com'])
52
+ @email2.stub!(:to).and_return('joe@gmail.com')
53
+ end
54
+
55
+ it "#emails('to: \"fred@gmail.com\"') should just return email1" do
56
+ emails('to: "fred@gmail.com"').should == [@email1]
57
+ end
58
+
59
+ describe "after #emails('to: \"fred@gmail.com\"')" do
60
+ before do
61
+ emails('to: "fred@gmail.com"')
62
+ end
63
+
64
+ it "#email('first') should be #email('last')" do
65
+ email('first email').should == email('last email')
66
+ email('first email').should == @email1
67
+ end
68
+
69
+ it "#email('the email', 'to: \"blah\") should be nil" do
70
+ email('the email', 'to: "blah"').should == nil
71
+ end
72
+
73
+ it "#email('the email', 'to: \"fred@gmail.com\") should be email1" do
74
+ email('the email', 'to: "fred@gmail.com"').should == @email1
75
+ end
76
+ end
77
+
78
+ it "#emails('to: \"joe@gmail.com\"') should return both emails" do
79
+ emails('to: "joe@gmail.com"').should == [@email1, @email2]
80
+ end
81
+
82
+ describe "and emails have subjects 'email1', 'email2'" do
83
+ before do
84
+ @email1.stub!(:subject).and_return('email1')
85
+ @email2.stub!(:subject).and_return('email2')
86
+ end
87
+
88
+ it "#emails('to: \"joe@gmail.com\", subject: \"email1\"') should return email1" do
89
+ emails('to: "joe@gmail.com", subject: "email1"').should == [@email1]
90
+ end
91
+
92
+ it "#emails('to: \"fred@gmail.com\", subject: \"email2\"') should return empty array" do
93
+ emails('to: "fred@gmail.com", subject: "email2"').should == []
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "#save_and_open_emails" do
100
+ before do
101
+ stub!(:open_in_browser)
102
+ stub!(:emails).and_return(["Contents of Email 1"])
103
+ @now = "2008-01-01".to_time
104
+ Time.stub!(:now).and_return(@now)
105
+ end
106
+
107
+ it "should call #emails to get emails" do
108
+ should_receive(:emails).and_return([])
109
+ save_and_open_emails
110
+ end
111
+
112
+ describe "when emails have been already been found" do
113
+ before { @emails = [] }
114
+
115
+ it "should not call #emails" do
116
+ should_not_receive(:emails)
117
+ save_and_open_emails
118
+ end
119
+ end
120
+
121
+ it "should create a file in Rails/tmp with the emails in it" do
122
+ save_and_open_emails
123
+ File.read("#{RAILS_ROOT}/tmp/webrat-email-#{@now.to_i}.html").should == "<h1>Email 1</h1><pre>Contents of Email 1</pre><hr />"
124
+ end
125
+
126
+ it "should call open_in_browser on created tmp file" do
127
+ should_receive(:open_in_browser).with("#{RAILS_ROOT}/tmp/webrat-email-#{@now.to_i}.html")
128
+ save_and_open_emails
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,70 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe Pickle::Parser::Matchers do
4
+ include Pickle::Parser::Matchers
5
+
6
+ describe "(config: [factories: user, car, fast_car] [predicates: name, status, fancy?, super_fancy?]" do
7
+ def config
8
+ @config ||= Pickle::Config.new do |c|
9
+ c.factories = {
10
+ 'user' => mock('factory'),
11
+ 'car' => mock('factory'),
12
+ 'fast_car' => mock('factory')
13
+ }
14
+ c.predicates = %w(name status fancy? super_fancy?)
15
+ end
16
+ end
17
+
18
+ describe "Match atoms" do
19
+ def self.atom_should_match(atom, strings)
20
+ Array(strings).each do |string|
21
+ it "#{atom} should match '#{string}'" do
22
+ string.should match(/^#{send atom}$/)
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.atom_should_not_match(atom, strings)
28
+ Array(strings).each do |string|
29
+ it "#{atom} should NOT match '#{string}'" do
30
+ string.should_not match(/^#{send atom}$/)
31
+ end
32
+ end
33
+ end
34
+
35
+ atom_should_match :match_ordinal, ['1st', '2nd', '23rd', '104th']
36
+ atom_should_not_match :match_ordinal, ['1', '2']
37
+
38
+ atom_should_match :match_index, ['first', 'last', '23rd', '104th']
39
+ atom_should_not_match :match_index, ['1', '2', 'foo']
40
+
41
+ atom_should_match :match_label, [': "gday"', ': "gday mate"']
42
+ atom_should_not_match :match_label, [': "gday""', ': gday']
43
+
44
+ atom_should_match :match_field, ['foo: "this is the life"', 'bar_man: "and so is this"', 'boolean: false', 'boolean: true', 'numeric: 10', 'numeric: 12.5']
45
+ atom_should_not_match :match_field, ['foo bar: "this aint workin"']
46
+
47
+ atom_should_match :match_fields, ['foo: "bar"', 'foo: "bar", baz: "bah"']
48
+ atom_should_not_match :match_fields, ['foo bar: "baz"', 'email: "a", password: "b", and password_confirmation: "c"']
49
+
50
+ atom_should_match :match_model, ['a user', '1st fast car', 'the 23rd fast_car', 'the user: "fred flinstone"']
51
+ atom_should_not_match :match_model, ['a giraffe', 'a 1st faster car: "jim"', 'an event created']
52
+
53
+ atom_should_match :match_predicate, ['name', 'status', 'fancy', 'super fancy', 'super_fancy']
54
+ atom_should_not_match :match_predicate, ['nameo', 'increment', 'not a predicate']
55
+
56
+ atom_should_match :match_factory, ['user', 'fast car', 'fast_car', 'car']
57
+ atom_should_not_match :match_factory, ['users', 'faster car', 'event created']
58
+
59
+ atom_should_match :match_plural_factory, ['users', 'fast cars']
60
+ atom_should_not_match :match_plural_factory, ['usereres', 'fasts cars']
61
+ end
62
+ end
63
+
64
+ describe "capture methods" do
65
+ it "capture_field should == '(' + match_field + ')'" do
66
+ should_receive(:match_field).and_return('MATCH_FIELD')
67
+ capture_field.should == '(MATCH_FIELD)'
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,154 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe Pickle::Parser do
4
+ before do
5
+ @parser = Pickle::Parser.new(:config => Pickle::Config.new)
6
+ end
7
+
8
+ it "should raise error when created with no config" do
9
+ lambda{ Pickle::Parser.new }.should raise_error(ArgumentError)
10
+ end
11
+
12
+ describe 'misc regexps' do
13
+ describe '/^#{capture_model} exists/' do
14
+ before do
15
+ @regexp = /^(#{@parser.capture_model}) exists$/
16
+ end
17
+
18
+ it "should match 'a user exists'" do
19
+ 'a user exists'.should match(@regexp)
20
+ end
21
+
22
+ it "should caputure 'a user' from 'a user exists'" do
23
+ 'a user exists'.match(@regexp)[1].should == 'a user'
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#parse_field' do
29
+ it "should return {'a' => 'b'} for 'a: \"b\"'" do
30
+ @parser.parse_field('a: "b"').should == {'a' => 'b'}
31
+ end
32
+
33
+ it "should raise error for invalid field 'a : b'" do
34
+ lambda { @parser.parse_field('a : b') }.should raise_error(ArgumentError)
35
+ end
36
+ end
37
+
38
+ describe '#parse_fields' do
39
+ it 'should return {} for blank argument' do
40
+ @parser.parse_fields(nil).should == {}
41
+ @parser.parse_fields('').should == {}
42
+ end
43
+
44
+ it 'should raise error for invalid argument' do
45
+ lambda { @parser.parse_fields('foo foo') }.should raise_error(ArgumentError)
46
+ end
47
+
48
+ it '(\'foo: "bar"\') should == { "foo" => "bar"}' do
49
+ @parser.parse_fields('foo: "bar"').should == { "foo" => "bar"}
50
+ end
51
+
52
+ it '("bool: true") should == { "bool" => true}' do
53
+ @parser.parse_fields('bool: true').should == {"bool" => true}
54
+ end
55
+
56
+ it '("bool: false") should == { "bool" => false}' do
57
+ @parser.parse_fields('bool: false').should == {"bool" => false}
58
+ end
59
+
60
+ it '("int: 10") should == { "int" => 10 }' do
61
+ @parser.parse_fields('int: 10').should == {"int" => 10}
62
+ end
63
+
64
+ it '("float: 10.1") should == { "float" => 10.1 }' do
65
+ @parser.parse_fields('float: 10.1').should == {"float" => 10.1}
66
+ end
67
+
68
+ it '(\'foo: "bar", bar_man: "wonga wonga", gump: 123\') should == {"foo" => "bar", "bar_man" => "wonga wonga", "gump" => 123}' do
69
+ @parser.parse_fields('foo: "bar", bar_man: "wonga wonga", gump: 123').should == {"foo" => "bar", "bar_man" => "wonga wonga", "gump" => 123}
70
+ end
71
+ end
72
+
73
+ describe '#parse_model' do
74
+ it '("a user") should == ["user", ""]' do
75
+ @parser.parse_model("a user").should == ["user", ""]
76
+ end
77
+
78
+ it '("the user") should == ["user", ""]' do
79
+ @parser.parse_model("the user").should == ["user", ""]
80
+ end
81
+
82
+ it '("1 fast car") should == ["fast_car", ""]' do
83
+ @parser.parse_model("1 fast car").should == ["fast_car", ""]
84
+ end
85
+
86
+ it '(\'an user: "jim jones"\') should == ["user", "jim_jones"]' do
87
+ @parser.parse_model('an user: "jim jones"').should == ["user", "jim_jones"]
88
+ end
89
+
90
+ it '(\'that fast car: "herbie"\') should == ["fast_car", "herbie"]' do
91
+ @parser.parse_model('that fast car: "herbie"').should == ["fast_car", "herbie"]
92
+ end
93
+
94
+ it '(\'the 12th user\') should == ["user", 11]' do
95
+ @parser.parse_model('the 12th user').should == ["user", 11]
96
+ end
97
+
98
+ it '(\'the last user\') should == ["user", -1]' do
99
+ @parser.parse_model('the last user').should == ["user", -1]
100
+ end
101
+
102
+ it '("the first user") should == ["user", 0]' do
103
+ @parser.parse_model('the first user').should == ["user", 0]
104
+ end
105
+
106
+ it '("the 1st user") should == ["user", 0]' do
107
+ @parser.parse_model('the 1st user').should == ["user", 0]
108
+ end
109
+ end
110
+
111
+ describe "#parse_index" do
112
+ it '("1st") should == 0' do
113
+ @parser.parse_index("1st").should == 0
114
+ end
115
+
116
+ it '("24th") should == 23' do
117
+ @parser.parse_index("24th").should == 23
118
+ end
119
+ it '("first") should == 0' do
120
+ @parser.parse_index("first").should == 0
121
+ end
122
+
123
+ it '("last") should == -1' do
124
+ @parser.parse_index("last").should == -1
125
+ end
126
+ end
127
+
128
+ describe "customised mappings" do
129
+ describe "config maps 'I|myself' to 'user: \"me\"'" do
130
+ before do
131
+ @config = Pickle::Config.new do |c|
132
+ c.map 'I', 'myself', :to => 'user: "me"'
133
+ end
134
+ @parser = Pickle::Parser.new(:config => @config)
135
+ end
136
+
137
+ it "'I' should match /\#{match_model}/" do
138
+ 'I'.should match(/#{@parser.match_model}/)
139
+ end
140
+
141
+ it "'myself' should match /\#{match_model}/" do
142
+ 'myself'.should match(/#{@parser.match_model}/)
143
+ end
144
+
145
+ it "parse_model('I') should == ['user', 'me']" do
146
+ @parser.parse_model('I').should == ["user", "me"]
147
+ end
148
+
149
+ it "parse_model('myself') should == ['user', 'me']" do
150
+ @parser.parse_model('myself').should == ["user", "me"]
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe Pickle::Path do
4
+ include Pickle::Path
5
+
6
+ describe "#path_to_pickle, when the model doesn't exist" do
7
+ before do
8
+ stub!(:model!).and_raise("foo")
9
+ end
10
+ it "('that user', :extra => 'new comment') should raise the error raised by model!" do
11
+ lambda { path_to_pickle "that user", "new comment" }.should raise_error("foo")
12
+ end
13
+
14
+ end
15
+
16
+ describe "#path_to_pickle" do
17
+ before do
18
+ stub!(:model!).and_return(@user = mock_model(User))
19
+ end
20
+ it "('a user', 'the user: \"fred\"') should retrieve 'a user', and 'the user: \"fred\"' models" do
21
+ should_receive(:model!).with('a user')
22
+ should_receive(:model!).with('the user: "fred"')
23
+ stub!(:user_user_path).and_return('the path')
24
+ path_to_pickle 'a user', 'the user: "fred"'
25
+ end
26
+
27
+ it "('a user', :action => 'foo') should return foo_user_path(<user>)" do
28
+ should_receive(:foo_user_path).with(@user).and_return('the path')
29
+ path_to_pickle('a user', :action => 'foo').should == 'the path'
30
+ end
31
+
32
+ it "('a user', :action => 'foo') should raise informative error if foo_user_path not defined" do
33
+ should_receive(:foo_user_path).with(@user).and_raise(NoMethodError)
34
+ lambda { path_to_pickle('a user', :action => 'foo') }.should raise_error(Exception, /Could not figure out a path for/)
35
+ end
36
+
37
+ it "('a user', :segment => 'foo') should return user_foo_path(<user>)" do
38
+ should_receive(:user_foo_path).with(@user).and_return('the path')
39
+ path_to_pickle('a user', :segment => 'foo').should == 'the path'
40
+ end
41
+
42
+ it "('a user', :segment => 'foo') should raise informative error if foo_user_path not defined" do
43
+ should_receive(:user_foo_path).with(@user).and_raise(NoMethodError)
44
+ lambda { path_to_pickle('a user', :segment => 'foo') }.should raise_error(Exception, /Could not figure out a path for/)
45
+ end
46
+
47
+ it "('a user', :action => 'new', :segment => 'comment') should return new_user_comment_path(<user>)" do
48
+ should_receive(:new_user_comment_path).with(@user).and_return('the path')
49
+ path_to_pickle('a user', :segment => 'comment', :action => 'new').should == 'the path'
50
+ end
51
+
52
+ it "('a user', :action => 'new', :segment => 'comment') should raise informative error if new_user_comment_path not defined" do
53
+ should_receive(:new_user_comment_path).with(@user).and_raise(NoMethodError)
54
+ lambda { path_to_pickle('a user', :action => 'new', :segment => 'comment') }.should raise_error(Exception, /Could not figure out a path for/)
55
+ end
56
+
57
+ it "('a user', :extra => 'new comment') should return new_user_comment_path(<user>)" do
58
+ should_receive(:new_user_comment_path).with(@user).and_return('the path')
59
+ path_to_pickle('a user', :extra => 'new comment').should == 'the path'
60
+ end
61
+
62
+ it "('a user', :extra => 'new comment') should raise informative error if new_user_comment_path not defined" do
63
+ should_receive(:new_user_comment_path).with(@user).and_raise(NoMethodError)
64
+ lambda { path_to_pickle('a user', :extra => 'new comment') }.should raise_error(Exception, /Could not figure out a path for/)
65
+ end
66
+
67
+ describe "(private API)" do
68
+ it "('a user', :extra => 'new ish comment') should try combinations of 'new', 'ish', 'comment'" do
69
+ should_receive(:pickle_path_for_models_action_segment).with([@user], '', 'new_ish_comment').once
70
+ should_receive(:pickle_path_for_models_action_segment).with([@user], 'new', 'ish_comment').once
71
+ should_receive(:pickle_path_for_models_action_segment).with([@user], 'new_ish', 'comment').once
72
+ should_receive(:pickle_path_for_models_action_segment).with([@user], 'new_ish_comment', '').once
73
+ lambda { path_to_pickle('a user', :extra => 'new ish comment') }.should raise_error(Exception, /Could not figure out a path for/)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,337 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe Pickle::Session do
4
+ include Pickle::Session
5
+
6
+ describe "Pickle::Session proxy missing methods to parser", :shared => true do
7
+ it "should forward to pickle_parser it responds_to them" do
8
+ @it.pickle_parser.should_receive(:parse_model)
9
+ @it.parse_model
10
+ end
11
+
12
+ it "should raise error if pickle_parser don't know about em" do
13
+ lambda { @it.parse_infinity }.should raise_error
14
+ end
15
+ end
16
+
17
+ describe "including Pickle::Session" do
18
+ before do
19
+ @it = self
20
+ end
21
+
22
+ it_should_behave_like "Pickle::Session proxy missing methods to parser"
23
+ end
24
+
25
+ describe "extending Pickle::Session" do
26
+ before do
27
+ @it = Object.new
28
+ @it.extend Pickle::Session
29
+ end
30
+
31
+ it_should_behave_like "Pickle::Session proxy missing methods to parser"
32
+ end
33
+
34
+ describe "after storing a single user", :shared => true do
35
+ it "created_models('user') should be array containing the original user" do
36
+ created_models('user').should == [@user]
37
+ end
38
+
39
+ describe "the original user should be retrievable with" do
40
+ it "created_model('the user')" do
41
+ created_model('the user').should == @user
42
+ end
43
+
44
+ it "created_model('1st user')" do
45
+ created_model('1st user').should == @user
46
+ end
47
+
48
+ it "created_model('last user')" do
49
+ created_model('last user').should == @user
50
+ end
51
+ end
52
+
53
+ describe "(found from db)" do
54
+ before do
55
+ @user.stub!(:id).and_return(100)
56
+ @user.class.should_receive(:find).with(100).and_return(@user_from_db = @user.dup)
57
+ end
58
+
59
+ it "models('user') should be array containing user" do
60
+ models('user').should == [@user_from_db]
61
+ end
62
+
63
+ describe "user should be retrievable with" do
64
+ it "model('the user')" do
65
+ model('the user').should == @user_from_db
66
+ end
67
+
68
+ it "model('1st user')" do
69
+ model('1st user').should == @user_from_db
70
+ end
71
+
72
+ it "model('last user')" do
73
+ model('last user').should == @user_from_db
74
+ end
75
+
76
+ it "model!('last user')" do
77
+ model('last user').should == @user_from_db
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ describe "#create_model" do
84
+ before do
85
+ @user = mock_model(User)
86
+ Factory.stub!(:create).and_return(@user)
87
+ end
88
+
89
+ describe "('a user')" do
90
+ def do_create_model
91
+ create_model('a user')
92
+ end
93
+
94
+ it "should call Factory.create('user', {})" do
95
+ Factory.should_receive(:create).with('user', {}).and_return(@user)
96
+ do_create_model
97
+ end
98
+
99
+ describe "after create," do
100
+ before { do_create_model }
101
+
102
+ it_should_behave_like "after storing a single user"
103
+ end
104
+ end
105
+
106
+ describe "('1 user', 'foo: \"bar\", baz: \"bing bong\"')" do
107
+ def do_create_model
108
+ create_model('1 user', 'foo: "bar", baz: "bing bong"')
109
+ end
110
+
111
+ it "should call Factory.create('user', {'foo' => 'bar', 'baz' => 'bing bong'})" do
112
+ Factory.should_receive(:create).with('user', {'foo' => 'bar', 'baz' => 'bing bong'}).and_return(@user)
113
+ do_create_model
114
+ end
115
+
116
+ describe "after create," do
117
+ before { do_create_model }
118
+
119
+ it_should_behave_like "after storing a single user"
120
+ end
121
+ end
122
+
123
+ describe "('an user: \"fred\")" do
124
+ def do_create_model
125
+ create_model('an user: "fred"')
126
+ end
127
+
128
+ it "should call Factory.create('user', {})" do
129
+ Factory.should_receive(:create).with('user', {}).and_return(@user)
130
+ do_create_model
131
+ end
132
+
133
+ describe "after create," do
134
+ before { do_create_model }
135
+
136
+ it_should_behave_like "after storing a single user"
137
+
138
+ it "created_model('the user: \"fred\"') should retrieve the user" do
139
+ created_model('the user: "fred"').should == @user
140
+ end
141
+
142
+ it "created_model?('the user: \"shirl\"') should be false" do
143
+ created_model?('the user: "shirl"').should == false
144
+ end
145
+
146
+ it "model?('the user: \"shirl\"') should be false" do
147
+ model?('the user: "shirl"').should == false
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ describe '#find_model' do
154
+ before do
155
+ @user = mock_model(User)
156
+ User.stub!(:find).and_return(@user)
157
+ end
158
+
159
+ def do_find_model
160
+ find_model('a user', 'hair: "pink"')
161
+ end
162
+
163
+ it "should call User.find :first, :conditions => {'hair' => 'pink'}" do
164
+ User.should_receive(:find).with(:first, :conditions => {'hair' => 'pink'}).and_return(@user)
165
+ do_find_model
166
+ end
167
+
168
+ describe "after find," do
169
+ before { do_find_model }
170
+
171
+ it_should_behave_like "after storing a single user"
172
+ end
173
+ end
174
+
175
+ describe "#find_models" do
176
+ before do
177
+ @user = mock_model(User)
178
+ User.stub!(:find).and_return([@user])
179
+ end
180
+
181
+ def do_find_models
182
+ find_models('user', 'hair: "pink"')
183
+ end
184
+
185
+ it "should call User.find :all, :conditions => {'hair' => 'pink'}" do
186
+ User.should_receive(:find).with(:all, :conditions => {'hair' => 'pink'}).and_return([@user])
187
+ do_find_models
188
+ end
189
+
190
+ describe "after find," do
191
+ before { do_find_models }
192
+
193
+ it_should_behave_like "after storing a single user"
194
+ end
195
+ end
196
+
197
+ describe 'creating \'a super admin: "fred"\', then \'a user: "shirl"\', \'then 1 super_admin\'' do
198
+ before do
199
+ @user = @fred = mock_model(User)
200
+ @shirl = mock_model(User)
201
+ @noname = mock_model(User)
202
+ Factory.stub!(:create).and_return(@fred, @shirl, @noname)
203
+ end
204
+
205
+ def do_create_users
206
+ create_model('a super admin: "fred"')
207
+ create_model('a user: "shirl"')
208
+ create_model('1 super_admin')
209
+ end
210
+
211
+ it "should call Factory.create with <'super_admin'>, <'user'>, <'super_admin'>" do
212
+ Factory.should_receive(:create).with('super_admin', {}).twice
213
+ Factory.should_receive(:create).with('user', {}).once
214
+ do_create_users
215
+ end
216
+
217
+ describe "after create," do
218
+ before do
219
+ do_create_users
220
+ end
221
+
222
+ it "created_models('user') should == [@fred, @shirl, @noname]" do
223
+ created_models('user').should == [@fred, @shirl, @noname]
224
+ end
225
+
226
+ it "created_models('super_admin') should == [@fred, @noname]" do
227
+ created_models('super_admin').should == [@fred, @noname]
228
+ end
229
+
230
+ describe "#created_model" do
231
+ it "'that user' should be @noname (the last user created - as super_admins are users)" do
232
+ created_model('that user').should == @noname
233
+ end
234
+
235
+ it "'the super admin' should be @noname (the last super admin created)" do
236
+ created_model('that super admin').should == @noname
237
+ end
238
+
239
+ it "'the 1st super admin' should be @fred" do
240
+ created_model('the 1st super admin').should == @fred
241
+ end
242
+
243
+ it "'the first user' should be @fred" do
244
+ created_model('the first user').should == @fred
245
+ end
246
+
247
+ it "'the 2nd user' should be @shirl" do
248
+ created_model('the 2nd user').should == @shirl
249
+ end
250
+
251
+ it "'the last user' should be @noname" do
252
+ created_model('the last user').should == @noname
253
+ end
254
+
255
+ it "'the user: \"fred\" should be @fred" do
256
+ created_model('the user: "fred"').should == @fred
257
+ end
258
+
259
+ it "'the user: \"shirl\" should be @shirl" do
260
+ created_model('the user: "shirl"').should == @shirl
261
+ end
262
+ end
263
+ end
264
+ end
265
+
266
+ describe "when 'the user: \"me\"' exists and there is a mapping from 'I', 'myself' => 'user: \"me\"" do
267
+ before do
268
+ @user = mock_model(User)
269
+ User.stub!(:find).and_return(@user)
270
+ Factory.stub!(:create).and_return(@user)
271
+ self.pickle_parser = Pickle::Parser.new(:config => Pickle::Config.new {|c| c.map 'I', 'myself', :to => 'user: "me"'})
272
+ create_model('the user: "me"')
273
+ end
274
+
275
+ it 'model("I") should return the user' do
276
+ model('I').should == @user
277
+ end
278
+
279
+ it 'model("myself") should return the user' do
280
+ model('myself').should == @user
281
+ end
282
+
283
+ it "#parser.parse_fields 'author: user \"JIM\"' should raise Error, as model deos not refer" do
284
+ lambda { pickle_parser.parse_fields('author: user "JIM"') }.should raise_error
285
+ end
286
+
287
+ it "#parser.parse_fields 'author: the user' should return {\"author\" => <user>}" do
288
+ pickle_parser.parse_fields('author: the user').should == {"author" => @user}
289
+ end
290
+
291
+ it "#parser.parse_fields 'author: myself' should return {\"author\" => <user>}" do
292
+ pickle_parser.parse_fields('author: myself').should == {"author" => @user}
293
+ end
294
+
295
+ it "#parser.parse_fields 'author: the user, approver: I, rating: \"5\"' should return {'author' => <user>, 'approver' => <user>, 'rating' => '5'}" do
296
+ pickle_parser.parse_fields('author: the user, approver: I, rating: "5"').should == {'author' => @user, 'approver' => @user, 'rating' => '5'}
297
+ end
298
+
299
+ it "#parser.parse_fields 'author: user: \"me\", approver: \"\"' should return {'author' => <user>, 'approver' => \"\"}" do
300
+ pickle_parser.parse_fields('author: user: "me", approver: ""').should == {'author' => @user, 'approver' => ""}
301
+ end
302
+ end
303
+
304
+ describe "convert_models_to_attributes(ar_class, :user => <a user>)" do
305
+ before do
306
+ @user = mock_model(User)
307
+ end
308
+
309
+ describe "(when ar_class has column 'user_id')" do
310
+ before do
311
+ @ar_class = mock('ActiveRecord', :column_names => ['user_id'])
312
+ end
313
+
314
+ it "should return {'user_id' => <the user.id>}" do
315
+ convert_models_to_attributes(@ar_class, :user => @user).should == {'user_id' => @user.id}
316
+ end
317
+ end
318
+
319
+ describe "(when ar_class has columns 'user_id', 'user_type')" do
320
+ before do
321
+ @ar_class = mock('ActiveRecord', :column_names => ['user_id', 'user_type'])
322
+ end
323
+
324
+ it "should return {'user_id' => <the user.id>, 'user_type' => <the user.type>}" do
325
+ convert_models_to_attributes(@ar_class, :user => @user).should == {'user_id' => @user.id, 'user_type' => @user.class.name}
326
+ end
327
+ end
328
+ end
329
+
330
+ it "#model!('unknown') should raise informative error message" do
331
+ lambda { model!('unknown') }.should raise_error("Can't find pickle model: 'unknown' in this scenario")
332
+ end
333
+
334
+ it "#created_model!('unknown') should raise informative error message" do
335
+ lambda { created_model!('unknown') }.should raise_error("Can't find pickle model: 'unknown' in this scenario")
336
+ end
337
+ end