mrflip-pickle 0.1.13

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.
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe Pickle::Email::Parser do
4
+ include Pickle::Parser::Matchers
5
+ include Pickle::Email::Parser
6
+
7
+ describe "#match_email" do
8
+ it "should match 'the email'" do
9
+ 'the email'.should match(/^#{match_email}$/)
10
+ end
11
+
12
+ it "should match 'the first email'" do
13
+ 'the first email'.should match(/^#{match_email}$/)
14
+ end
15
+
16
+ it "should match 'the last email'" do
17
+ 'the last email'.should match(/^#{match_email}$/)
18
+ end
19
+
20
+ it "should match 'the 3rd email'" do
21
+ 'the 3rd email'.should match(/^#{match_email}$/)
22
+ end
23
+
24
+ it "should match 'an email'" do
25
+ 'an email'.should match(/^#{match_email}$/)
26
+ end
27
+ end
28
+
29
+ it "#capture_email should just capture match_email" do
30
+ capture_email.should == "(#{match_email})"
31
+ end
32
+
33
+ describe "#capture_index_in_email" do
34
+ it "should extract the '2nd' from 'the 2nd email'" do
35
+ match = 'the 2nd email'.match(/^#{capture_index_in_email}$/)
36
+ match[1].should == '2nd'
37
+ end
38
+
39
+ it "should extract nil from 'the email'" do
40
+ match = 'the email'.match(/^#{capture_index_in_email}$/)
41
+ match[1].should == nil
42
+ end
43
+
44
+ it "should extract the 'last' from 'the last email'" do
45
+ match = 'the last email'.match(/^#{capture_index_in_email}$/)
46
+ match[1].should == 'last'
47
+ end
48
+ end
49
+ end
@@ -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