schlick-pickle 0.1.5.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.
@@ -0,0 +1,134 @@
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
+ it ".model_classes should not include CGI::Session::ActiveRecordStore" do
13
+ Pickle::Adapter.model_classes.should_not include(CGI::Session::ActiveRecordStore)
14
+ end
15
+
16
+ describe '::ActiveRecord' do
17
+ before do
18
+ # set up a fake object space
19
+ @klass1 = mock('One', :name => 'One')
20
+ @klass2 = mock('One::Two', :name => 'One::Two')
21
+ Pickle::Adapter::ActiveRecord.stub!(:model_classes).and_return([@klass1, @klass2])
22
+ end
23
+
24
+ describe ".factories" do
25
+ it "should create one for each active record class" do
26
+ Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass1).once
27
+ Pickle::Adapter::ActiveRecord.should_receive(:new).with(@klass2).once
28
+ Pickle::Adapter::ActiveRecord.factories
29
+ end
30
+
31
+ describe ".new(Class)" do
32
+ before do
33
+ @factory = Pickle::Adapter::ActiveRecord.new(@klass2)
34
+ end
35
+
36
+ it "should have underscored (s/_) name of Class as #name" do
37
+ @factory.name.should == 'one_two'
38
+ end
39
+
40
+ it "#create(attrs) should call Class.create!(attrs)" do
41
+ @klass2.should_receive(:create!).with({:key => "val"})
42
+ @factory.create(:key => "val")
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '::FactoryGirl' do
49
+ before do
50
+ # set up a fake object space
51
+ @factory1 = mock('factory1', :factory_name => :one, :build_class => (@class1 = mock('Class1')))
52
+ @factory2 = mock('factory2', :factory_name => :two, :build_class => (@class2 = mock('Class2')))
53
+ Factory.stub!(:factories).and_return(:factory1 => @factory1, :factory2 => @factory2)
54
+ end
55
+
56
+ describe ".factories" do
57
+ it "should create one for each factory" do
58
+ Pickle::Adapter::FactoryGirl.should_receive(:new).with(@factory1).once
59
+ Pickle::Adapter::FactoryGirl.should_receive(:new).with(@factory2).once
60
+ Pickle::Adapter::FactoryGirl.factories
61
+ end
62
+
63
+ describe ".new(factory)" do
64
+ before do
65
+ @factory = Pickle::Adapter::FactoryGirl.new(@factory1)
66
+ end
67
+
68
+ it "should have name of factory_name" do
69
+ @factory.name.should == 'one'
70
+ end
71
+
72
+ it "should have klass of build_class" do
73
+ @factory.klass.should == @class1
74
+ end
75
+
76
+ it "#create(attrs) should call Factory(<:key>, attrs)" do
77
+ Factory.should_receive(:create).with("one", {:key => "val"})
78
+ @factory.create(:key => "val")
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ describe '::Machinist' do
85
+ before do
86
+ # set up a fake object space
87
+ @klass1 = mock('One', :name => 'One', :make => true, :make_unsaved => true)
88
+ @klass1.instance_variable_set('@blueprint', true)
89
+ @klass2 = mock('Two', :name => 'Two')
90
+ @klass3 = mock('Two::Sub', :name => 'Two::Sub', :make_special => true, :make => true, :make_unsaved => true)
91
+ @klass3.instance_variable_set('@blueprint', true)
92
+ Pickle::Adapter::Machinist.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
93
+ end
94
+
95
+ describe ".factories" do
96
+ it "should create one for each machinist make method, except make_unsaved" do
97
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass1, 'make').once
98
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass3, 'make').once
99
+ Pickle::Adapter::Machinist.should_receive(:new).with(@klass3, 'make_special').once
100
+ Pickle::Adapter::Machinist.factories
101
+ end
102
+
103
+ describe ".new(Class, 'make')" do
104
+ before do
105
+ @factory = Pickle::Adapter::Machinist.new(@klass1, 'make')
106
+ end
107
+
108
+ it "should have underscored (s/_) name of Class as #name" do
109
+ @factory.name.should == 'one'
110
+ end
111
+
112
+ it "#create(attrs) should call Class.make(attrs)" do
113
+ @klass1.should_receive(:make).with({:key => "val"})
114
+ @factory.create(:key => "val")
115
+ end
116
+ end
117
+
118
+ describe ".new(Class, 'make_special')" do
119
+ before do
120
+ @factory = Pickle::Adapter::Machinist.new(@klass3, 'make_special')
121
+ end
122
+
123
+ it "should have 'special_<Class name>' as #name" do
124
+ @factory.name.should == 'special_two_sub'
125
+ end
126
+
127
+ it "#create(attrs) should call Class.make_special(attrs)" do
128
+ @klass3.should_receive(:make_special).with({:key => "val"})
129
+ @factory.create(:key => "val")
130
+ end
131
+ end
132
+ end
133
+ end
134
+ 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', :replace => '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', :replace => 'faz')
87
+ @config.mappings.last.should == OpenStruct.new(:search => 'bar', :replace => '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,22 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
+
3
+ describe Pickle::Injector do
4
+ describe ".inject Pickle::Session, :into => <a class>" do
5
+ before do
6
+ klass = Class.new
7
+ Pickle::Injector.inject Pickle::Session, :into => klass
8
+ @object = klass.new
9
+ end
10
+
11
+ it "object should respond_to Pickle:Session methods" do
12
+ @object.should respond_to(:model)
13
+ @object.should respond_to(:create_model)
14
+ @object.should respond_to(:find_model)
15
+ end
16
+
17
+ it "object.model (a pickle method) should call object.pickle_session.model" do
18
+ @object.pickle_session.should_receive(:model).with('a user')
19
+ @object.model('a user')
20
+ end
21
+ end
22
+ 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