kbaum-pickle 0.2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.gitignore +3 -0
  2. data/History.txt +239 -0
  3. data/License.txt +20 -0
  4. data/README.rdoc +246 -0
  5. data/Rakefile +110 -0
  6. data/Todo.txt +4 -0
  7. data/VERSION +1 -0
  8. data/features/app/app.rb +121 -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 +39 -0
  14. data/features/generator/generators.feature +59 -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 +49 -0
  18. data/features/pickle/create_from_factory_girl.feature +55 -0
  19. data/features/pickle/create_from_machinist.feature +38 -0
  20. data/features/step_definitions/email_steps.rb +55 -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 +46 -0
  24. data/features/step_definitions/path_steps.rb +14 -0
  25. data/features/step_definitions/pickle_steps.rb +73 -0
  26. data/features/support/email.rb +21 -0
  27. data/features/support/env.rb +55 -0
  28. data/features/support/paths.rb +46 -0
  29. data/features/support/pickle.rb +26 -0
  30. data/features/support/pickle_app.rb +4 -0
  31. data/garlic.rb +38 -0
  32. data/init.rb +0 -0
  33. data/lib/pickle/adapter.rb +88 -0
  34. data/lib/pickle/config.rb +48 -0
  35. data/lib/pickle/email/parser.rb +18 -0
  36. data/lib/pickle/email/world.rb +13 -0
  37. data/lib/pickle/email.rb +36 -0
  38. data/lib/pickle/parser/matchers.rb +87 -0
  39. data/lib/pickle/parser.rb +65 -0
  40. data/lib/pickle/path/world.rb +5 -0
  41. data/lib/pickle/path.rb +45 -0
  42. data/lib/pickle/session/parser.rb +34 -0
  43. data/lib/pickle/session.rb +157 -0
  44. data/lib/pickle/version.rb +6 -0
  45. data/lib/pickle/world.rb +9 -0
  46. data/lib/pickle.rb +26 -0
  47. data/pickle.gemspec +110 -0
  48. data/rails_generators/pickle/pickle_generator.rb +40 -0
  49. data/rails_generators/pickle/templates/email.rb +21 -0
  50. data/rails_generators/pickle/templates/email_steps.rb +55 -0
  51. data/rails_generators/pickle/templates/paths.rb +20 -0
  52. data/rails_generators/pickle/templates/pickle.rb +28 -0
  53. data/rails_generators/pickle/templates/pickle_steps.rb +73 -0
  54. data/spec/lib/pickle_adapter_spec.rb +164 -0
  55. data/spec/lib/pickle_config_spec.rb +97 -0
  56. data/spec/lib/pickle_email_parser_spec.rb +49 -0
  57. data/spec/lib/pickle_email_spec.rb +131 -0
  58. data/spec/lib/pickle_parser_matchers_spec.rb +70 -0
  59. data/spec/lib/pickle_parser_spec.rb +154 -0
  60. data/spec/lib/pickle_path_spec.rb +92 -0
  61. data/spec/lib/pickle_session_spec.rb +384 -0
  62. data/spec/lib/pickle_spec.rb +24 -0
  63. data/spec/spec_helper.rb +38 -0
  64. metadata +126 -0
@@ -0,0 +1,164 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe Pickle::Adapter do
4
+ it ".factories should raise NotImplementedError" do
5
+ lambda{ Pickle::Adapter.factories }.should raise_error(NotImplementedError)
6
+ end
7
+
8
+ it "#create should raise NotImplementedError" do
9
+ lambda{ Pickle::Adapter.new.create }.should raise_error(NotImplementedError)
10
+ end
11
+
12
+ describe ".model_classes" do
13
+ before do
14
+ Pickle::Adapter.model_classes = nil
15
+ end
16
+
17
+ if defined?(CGI::Session::ActiveRecordStore::Session)
18
+ it "should not include CGI::Session::ActiveRecordStore::Session" do
19
+ Pickle::Adapter.model_classes.should_not include(CGI::Session::ActiveRecordStore)
20
+ end
21
+ end
22
+
23
+ if defined?(ActiveRecord::SessionStore::Session)
24
+ it "should not include ActiveRecord::SessionStore::Session" do
25
+ Pickle::Adapter.model_classes.should_not include(ActiveRecord::SessionStore::Session)
26
+ end
27
+ end
28
+
29
+ it "should not include classes without a table" do
30
+ klass = Class.new(ActiveRecord::Base)
31
+ Pickle::Adapter.model_classes.should_not include(klass)
32
+ end
33
+
34
+ it "should not include abstract classes without a table" do
35
+ klass = Class.new(ActiveRecord::Base)
36
+ klass.abstract_class = true
37
+ Pickle::Adapter.model_classes.should_not include(klass)
38
+ end
39
+ end
40
+
41
+ describe "adapters: " do
42
+ before do
43
+ @klass1 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('One')}
44
+ @klass2 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('One::Two')}
45
+ @klass3 = returning(Class.new(ActiveRecord::Base)) {|k| k.stub!(:name).and_return('Three')}
46
+ end
47
+
48
+ describe 'ActiveRecord' do
49
+ before do
50
+ Pickle::Adapter::ActiveRecord.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
51
+ end
52
+
53
+ it ".factories should create one for each active record class" do
54
+ Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass1).once
55
+ Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass2).once
56
+ Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass3).once
57
+ Pickle::Adapter::ActiveRecord.factories
58
+ end
59
+
60
+ describe ".new(Class)" do
61
+ before do
62
+ @factory = Pickle::Adapter::ActiveRecord.new(@klass2)
63
+ end
64
+
65
+ it "should have underscored (s/_) name of Class as #name" do
66
+ @factory.name.should == 'one_two'
67
+ end
68
+
69
+ it "#create(attrs) should call Class.create!(attrs)" do
70
+ @klass2.should_receive(:create!).with({:key => "val"})
71
+ @factory.create(:key => "val")
72
+ end
73
+ end
74
+ end
75
+
76
+ describe 'FactoryGirl' do
77
+ before do
78
+ Pickle::Adapter::FactoryGirl.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
79
+ @orig_factories, Factory.factories = Factory.factories, {}
80
+
81
+ Factory.define(:one, :class => @klass1) {}
82
+ Factory.define(:two, :class => @klass2) {}
83
+ @factory1 = Factory.factories[:one]
84
+ @factory2 = Factory.factories[:two]
85
+ end
86
+
87
+ after do
88
+ Factory.factories = @orig_factories
89
+ end
90
+
91
+ it ".factories should create one for each factory" do
92
+ Pickle::Adapter::FactoryGirl.should_receive(:new).with(@factory1).once
93
+ Pickle::Adapter::FactoryGirl.should_receive(:new).with(@factory2).once
94
+ Pickle::Adapter::FactoryGirl.factories
95
+ end
96
+
97
+ describe ".new(factory)" do
98
+ before do
99
+ @factory = Pickle::Adapter::FactoryGirl.new(@factory1)
100
+ end
101
+
102
+ it "should have name of factory_name" do
103
+ @factory.name.should == 'one'
104
+ end
105
+
106
+ it "should have klass of build_class" do
107
+ @factory.klass.should == @klass1
108
+ end
109
+
110
+ it "#create(attrs) should call Factory(<:key>, attrs)" do
111
+ Factory.should_receive(:create).with("one", {:key => "val"})
112
+ @factory.create(:key => "val")
113
+ end
114
+ end
115
+ end
116
+
117
+ describe 'Machinist' do
118
+ before do
119
+ Pickle::Adapter::Machinist.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
120
+
121
+ @klass1.blueprint {}
122
+ @klass3.blueprint {}
123
+ @klass3.blueprint(:special) {}
124
+ end
125
+
126
+ it ".factories should create one for each master blueprint, and special case" do
127
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass1, :master).once
128
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass3, :master).once
129
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass3, :special).once
130
+ Pickle::Adapter::Machinist.factories
131
+ end
132
+
133
+ describe ".new(Class, :master)" do
134
+ before do
135
+ @factory = Pickle::Adapter::Machinist.new(@klass1, :master)
136
+ end
137
+
138
+ it "should have underscored (s/_) name of Class as #name" do
139
+ @factory.name.should == 'one'
140
+ end
141
+
142
+ it "#create(attrs) should call Class.make(:master, attrs)" do
143
+ @klass1.should_receive(:make).with(:master, {:key => "val"})
144
+ @factory.create(:key => "val")
145
+ end
146
+ end
147
+
148
+ describe ".new(Class, :special)" do
149
+ before do
150
+ @factory = Pickle::Adapter::Machinist.new(@klass3, :special)
151
+ end
152
+
153
+ it "should have 'special_<Class name>' as #name" do
154
+ @factory.name.should == 'special_three'
155
+ end
156
+
157
+ it "#create(attrs) should call Class.make(:special, attrs)" do
158
+ @klass3.should_receive(:make).with(:special, {:key => "val"})
159
+ @factory.create(:key => "val")
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,97 @@
1
+ require File.expand_path(File.join(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', :public_instance_methods => ['nope', 'foo?', 'bar?'], :column_names => ['one', 'two'])
58
+ class2 = mock('Class2', :public_instance_methods => ['not', 'foo?', 'faz?'], :column_names => ['two', 'three'])
59
+ Pickle::Adapter.stub!(:model_classes).and_return([class1, class2])
60
+
61
+ @config.predicates.to_set.should == ['foo?', 'faz?', 'bar?', 'one', 'two', 'three'].to_set
62
+ end
63
+
64
+ it "should be overridable" do
65
+ @config.predicates = %w(lame?)
66
+ @config.predicates.should == %w(lame?)
67
+ end
68
+ end
69
+
70
+ describe "#map 'foo', :to => 'faz'" do
71
+ before do
72
+ @config.map 'foo', :to => 'faz'
73
+ end
74
+
75
+ it "should create OpenStruct(search: 'foo', replace: 'faz') mapping" do
76
+ @config.mappings.first.should == OpenStruct.new(:search => 'foo', :replacement => 'faz')
77
+ end
78
+ end
79
+
80
+ describe "#map 'foo', 'bar' :to => 'faz'" do
81
+ before do
82
+ @config.map 'foo', 'bar', :to => 'faz'
83
+ end
84
+
85
+ it "should create 2 mappings" do
86
+ @config.mappings.first.should == OpenStruct.new(:search => 'foo', :replacement => 'faz')
87
+ @config.mappings.last.should == OpenStruct.new(:search => 'bar', :replacement => 'faz')
88
+ end
89
+ end
90
+
91
+ it "#configure(&block) should execiute on self" do
92
+ @config.should_receive(:foo).with(:bar)
93
+ @config.configure do |c|
94
+ c.foo :bar
95
+ end
96
+ end
97
+ end
@@ -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', 'numeric: +10', 'numeric: +12.5', 'numeric: -10', 'numeric: -12.5', 'nil_field: nil']
45
+ atom_should_not_match :match_field, ['foo bar: "this aint workin"', 'not_numeric: --10', 'not_numeric: -ten']
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