templater 0.1.1 → 0.1.2

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,47 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '#files' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ @generator_class.class_eval do
8
+ def source_root
9
+ '/tmp/source'
10
+ end
11
+ end
12
+ end
13
+
14
+ it "should return all files" do
15
+ @generator_class.file(:blah1, 'blah.rb')
16
+ @generator_class.file(:blah2, 'blah2.rb')
17
+
18
+ instance = @generator_class.new('/tmp')
19
+
20
+ instance.files[0].name.should == :blah1
21
+ instance.files[1].name.should == :blah2
22
+ end
23
+
24
+ it "should not return files with an option that does not match." do
25
+ @generator_class.option :framework, :default => :rails
26
+
27
+ @generator_class.file(:merb, 'blah.rb', :framework => :merb)
28
+ @generator_class.file(:rails, 'blah2.rb', :framework => :rails)
29
+ @generator_class.file(:none, 'blah2.rb')
30
+
31
+ instance = @generator_class.new('/tmp')
32
+
33
+ instance.files[0].name.should == :rails
34
+ instance.files[1].name.should == :none
35
+
36
+ instance.framework = :merb
37
+ instance.files[0].name.should == :merb
38
+ instance.files[1].name.should == :none
39
+
40
+ instance.framework = :rails
41
+ instance.files[0].name.should == :rails
42
+ instance.files[1].name.should == :none
43
+
44
+ instance.framework = nil
45
+ instance.files[0].name.should == :none
46
+ end
47
+ end
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.generators' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+
8
+ @g1 = Class.new(Templater::Generator)
9
+ @g2 = Class.new(Templater::Generator)
10
+ @g3 = Class.new(Templater::Generator)
11
+ @g4 = Class.new(Templater::Generator)
12
+
13
+ @manifold = mock('a manifold')
14
+ @manifold.stub!(:generator).with(:monkey).and_return(@g1)
15
+ @manifold.stub!(:generator).with(:blah).and_return(@g2)
16
+ @manifold.stub!(:generator).with(:duck).and_return(@g3)
17
+ @manifold.stub!(:generator).with(:llama).and_return(@g4)
18
+ @manifold.stub!(:generator).with(:i_dont_exist).and_return(nil)
19
+
20
+ @generator_class.manifold = @manifold
21
+ @g1.manifold = @manifold
22
+ @g2.manifold = @manifold
23
+ @g3.manifold = @manifold
24
+ @g4.manifold = @manifold
25
+ end
26
+
27
+ it "should return [self] when no manifold or invocations exist" do
28
+ @generator_class.manifold = nil
29
+ @generator_class.generators.should == [@generator_class]
30
+ end
31
+
32
+ it "should return [self] when only invocations exist" do
33
+ @generator_class.manifold = nil
34
+ @generator_class.invoke(:monkey)
35
+ @generator_class.invoke(:blah)
36
+ @generator_class.generators.should == [@generator_class]
37
+ end
38
+
39
+ it "should return a list of invoked generators" do
40
+ @generator_class.invoke(:monkey)
41
+ @generator_class.invoke(:blah)
42
+
43
+ @generator_class.generators.should == [@generator_class, @g1, @g2]
44
+ end
45
+
46
+ it "should return a list of invoked generators recursively" do
47
+ @generator_class.invoke(:monkey)
48
+ @generator_class.invoke(:blah)
49
+ @g1.invoke(:duck)
50
+ @g3.invoke(:llama)
51
+
52
+ @generator_class.generators.should == [@generator_class, @g1, @g3, @g4, @g2]
53
+ end
54
+
55
+ it "should ignore invocations that do not exist in the manifold" do
56
+ @generator_class.invoke(:monkey)
57
+ @generator_class.invoke(:blah)
58
+ @g1.invoke(:duck)
59
+ @g3.invoke(:i_dont_exist)
60
+
61
+ @generator_class.generators.should == [@generator_class, @g1, @g3, @g2]
62
+ end
63
+ end
@@ -0,0 +1,81 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.glob!' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ @generator_class.stub!(:source_root).and_return(template_path('glob'))
8
+ end
9
+
10
+ it "should add templates and files in the source_root based on if their extensions are in the template_extensions array" do
11
+ @generator_class.glob!()
12
+
13
+ @instance = @generator_class.new('/tmp/destination')
14
+
15
+ @instance.template(:arg_js).source.should == template_path('glob/arg.js')
16
+ @instance.template(:arg_js).destination.should == "/tmp/destination/arg.js"
17
+
18
+ @instance.template(:test_rb).source.should == template_path('glob/test.rb')
19
+ @instance.template(:test_rb).destination.should == "/tmp/destination/test.rb"
20
+
21
+ @instance.file(:subfolder_jessica_alba_jpg).source.should == template_path('glob/subfolder/jessica_alba.jpg')
22
+ @instance.file(:subfolder_jessica_alba_jpg).destination.should == '/tmp/destination/subfolder/jessica_alba.jpg'
23
+ end
24
+
25
+ it "should add templates and files with a different template_extensions array" do
26
+ @generator_class.glob!(nil, %w(jpg))
27
+
28
+ @instance = @generator_class.new('/tmp/destination')
29
+
30
+ @instance.file(:arg_js).source.should == template_path('glob/arg.js')
31
+ @instance.file(:arg_js).destination.should == "/tmp/destination/arg.js"
32
+
33
+ @instance.file(:test_rb).source.should == template_path('glob/test.rb')
34
+ @instance.file(:test_rb).destination.should == "/tmp/destination/test.rb"
35
+
36
+ @instance.template(:subfolder_jessica_alba_jpg).source.should == template_path('glob/subfolder/jessica_alba.jpg')
37
+ @instance.template(:subfolder_jessica_alba_jpg).destination.should == '/tmp/destination/subfolder/jessica_alba.jpg'
38
+ end
39
+
40
+ it "should add README and other stuff without an extension as templates if in the template_extensions array" do
41
+ @generator_class.glob!(nil, %w(README))
42
+
43
+ @instance = @generator_class.new('/tmp/destination')
44
+
45
+ @instance.template(:readme).source.should == template_path('glob/README')
46
+ @instance.template(:readme).destination.should == "/tmp/destination/README"
47
+ end
48
+
49
+ it "should glob in a subdirectory" do
50
+ @generator_class.stub!(:source_root).and_return(template_path(""))
51
+ @generator_class.glob!('glob', %w(jpg))
52
+
53
+ @instance = @generator_class.new('/tmp/destination')
54
+
55
+ @instance.file(:glob_arg_js).source.should == template_path('glob/arg.js')
56
+ @instance.file(:glob_arg_js).destination.should == "/tmp/destination/glob/arg.js"
57
+
58
+ @instance.file(:glob_test_rb).source.should == template_path('glob/test.rb')
59
+ @instance.file(:glob_test_rb).destination.should == "/tmp/destination/glob/test.rb"
60
+
61
+ @instance.template(:glob_subfolder_jessica_alba_jpg).source.should == template_path('glob/subfolder/jessica_alba.jpg')
62
+ @instance.template(:glob_subfolder_jessica_alba_jpg).destination.should == '/tmp/destination/glob/subfolder/jessica_alba.jpg'
63
+ end
64
+
65
+ it "should add only the given templates and files" do
66
+ @generator_class.glob!()
67
+
68
+ @instance = @generator_class.new('/tmp/destination')
69
+
70
+ @instance.templates.map { |t| t.name }.should == [
71
+ :arg_js,
72
+ :readme,
73
+ :subfolder_monkey_rb,
74
+ :test_rb
75
+ ]
76
+ @instance.files.map { |f| f.name }.should == [
77
+ :subfolder_jessica_alba_jpg
78
+ ]
79
+ end
80
+
81
+ end
@@ -0,0 +1,64 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '#invocations' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ @generator_class.class_eval do
8
+ def source_root
9
+ '/tmp/source'
10
+ end
11
+ end
12
+
13
+ @generator1 = mock('a generator for merb')
14
+ @instance1 = mock('an instance of the generator for merb')
15
+ @generator1.stub!(:new).and_return(@instance1)
16
+ @generator2 = mock('a generator for rails')
17
+ @instance2 = mock('an instance of the generator for rails')
18
+ @generator2.stub!(:new).and_return(@instance2)
19
+ @generator3 = mock('a generator for both')
20
+ @instance3 = mock('an instance of the generator for both')
21
+ @generator3.stub!(:new).and_return(@instance3)
22
+
23
+ @manifold = mock('a manifold')
24
+ @manifold.stub!(:generator).with(:merb).and_return(@generator1)
25
+ @manifold.stub!(:generator).with(:rails).and_return(@generator2)
26
+ @manifold.stub!(:generator).with(:both).and_return(@generator3)
27
+
28
+ @generator_class.stub!(:manifold).and_return(@manifold)
29
+ end
30
+
31
+ it "should return all invocations" do
32
+ @generator_class.invoke(:merb)
33
+ @generator_class.invoke(:rails)
34
+
35
+ instance = @generator_class.new('/tmp')
36
+
37
+ instance.invocations[0].should == @instance1
38
+ instance.invocations[1].should == @instance2
39
+ end
40
+
41
+ it "should not return templates with an option that does not match." do
42
+ @generator_class.option :framework, :default => :rails
43
+
44
+ @generator_class.invoke(:merb, :framework => :merb)
45
+ @generator_class.invoke(:rails, :framework => :rails)
46
+ @generator_class.invoke(:both)
47
+
48
+ instance = @generator_class.new('/tmp')
49
+
50
+ instance.invocations[0].should == @instance2
51
+ instance.invocations[1].should == @instance3
52
+
53
+ instance.framework = :merb
54
+ instance.invocations[0].should == @instance1
55
+ instance.invocations[1].should == @instance3
56
+
57
+ instance.framework = :rails
58
+ instance.invocations[0].should == @instance2
59
+ instance.invocations[1].should == @instance3
60
+
61
+ instance.framework = nil
62
+ instance.invocations[0].should == @instance3
63
+ end
64
+ end
@@ -0,0 +1,109 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.invoke' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ @generator_class.first_argument :test1
8
+ @generator_class.second_argument :test2
9
+
10
+ @invoked_generator = mock('an invoked generator')
11
+ @invoked_instance = mock('an instance of the invoked generator')
12
+ @invoked_generator.stub!(:new).and_return(@invoked_instance)
13
+
14
+ @manifold = mock('a manifold')
15
+ @manifold.stub!(:generator).with(:test).and_return(@invoked_generator)
16
+ end
17
+
18
+ it "should add nothing if there is no manifold" do
19
+ @generator_class.invoke(:test)
20
+ @instance = @generator_class.new('/tmp', {}, 'test', 'argument')
21
+
22
+ @instance.invocations.should be_empty
23
+ end
24
+
25
+ describe "with no block" do
26
+
27
+ before(:each) do
28
+ @generator_class.stub!(:manifold).and_return(@manifold)
29
+ end
30
+
31
+ it "should return the instantiaded template" do
32
+ @generator_class.invoke(:test)
33
+ @instance = @generator_class.new('/tmp', {}, 'test', 'argument')
34
+
35
+ @instance.invocations.first.should == @invoked_instance
36
+ end
37
+
38
+ it "should ask the manifold for the generator" do
39
+ @generator_class.should_receive(:manifold).at_least(:once).and_return(@manifold)
40
+ @manifold.should_receive(:generator).with(:test).and_return(@invoked_generator)
41
+ @generator_class.invoke(:test)
42
+ @instance = @generator_class.new('/tmp', {}, 'test', 'argument')
43
+
44
+ @instance.invocations.first.should == @invoked_instance
45
+ end
46
+
47
+ it "should instantiate the generator with the correct arguments" do
48
+ @invoked_generator.should_receive(:new).with('/tmp', {}, 'test', 'argument').and_return(@invoked_instance)
49
+ @generator_class.invoke(:test)
50
+ @instance = @generator_class.new('/tmp', {}, 'test', 'argument')
51
+
52
+ @instance.invocations.first.should == @invoked_instance
53
+ end
54
+
55
+ end
56
+
57
+ describe "with a block" do
58
+
59
+ before(:each) do
60
+ @generator_class.stub!(:manifold).and_return(@manifold)
61
+ end
62
+
63
+ it "should pass the generator class to the block and return the result of it" do
64
+ @generator_class.invoke(:test) do |generator|
65
+ generator.new(destination_root, options, 'blah', 'monkey', some_method)
66
+ end
67
+ @instance = @generator_class.new('/tmp', {}, 'test', 'argument')
68
+
69
+ @instance.should_receive(:some_method).and_return('da')
70
+ @invoked_generator.should_receive(:new).with('/tmp', {}, 'blah', 'monkey', 'da').and_return(@invoked_instance)
71
+
72
+ @instance.invocations.first.should == @invoked_instance
73
+ end
74
+
75
+ it "should ask the manifold for the generator" do
76
+ @generator_class.should_receive(:manifold).at_least(:once).and_return(@manifold)
77
+ @manifold.should_receive(:generator).with(:test).and_return(@invoked_generator)
78
+
79
+ @generator_class.invoke(:test) do |generator|
80
+ generator.new(destination_root, options, 'blah', 'monkey')
81
+ end
82
+
83
+ @instance = @generator_class.new('/tmp', {}, 'test', 'argument')
84
+ @instance.invocations.first.should == @invoked_instance
85
+ end
86
+ end
87
+ end
88
+
89
+
90
+
91
+ describe Templater::Generator, '#invoke!' do
92
+
93
+ before do
94
+ @generator_class = Class.new(Templater::Generator)
95
+ end
96
+
97
+ it "should invoke all templates" do
98
+ template1 = mock('a template')
99
+ template2 = mock('another template')
100
+
101
+ instance = @generator_class.new('/tmp')
102
+
103
+ instance.should_receive(:templates).and_return([template1, template2])
104
+ template1.should_receive(:invoke!)
105
+ template2.should_receive(:invoke!)
106
+
107
+ instance.invoke!
108
+ end
109
+ end
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.option' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ end
8
+
9
+ it "should add accessors" do
10
+ @generator_class.option(:test)
11
+
12
+ instance = @generator_class.new('/tmp')
13
+
14
+ instance.test = "monkey"
15
+ instance.test.should == "monkey"
16
+
17
+ end
18
+
19
+ it "should preset a default value" do
20
+ @generator_class.option(:test, :default => 'elephant')
21
+
22
+ instance = @generator_class.new('/tmp')
23
+
24
+ instance.test.should == "elephant"
25
+ end
26
+
27
+ it "should allow overwriting of default values" do
28
+ @generator_class.option(:test, :default => 'elephant')
29
+
30
+ instance = @generator_class.new('/tmp')
31
+
32
+ instance.test.should == "elephant"
33
+ instance.test = "monkey"
34
+ instance.test.should == "monkey"
35
+ end
36
+
37
+ it "should allow passing in of options on generator creation" do
38
+ @generator_class.option(:test, :default => 'elephant')
39
+
40
+ instance = @generator_class.new('/tmp', { :test => 'freebird' })
41
+
42
+ instance.test.should == "freebird"
43
+ instance.test = "monkey"
44
+ instance.test.should == "monkey"
45
+ end
46
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '#source_root' do
4
+ it "try to retrieve the source root from the class" do
5
+ @generator_class = Class.new(Templater::Generator)
6
+ @generator_class.should_receive(:source_root).and_return('/tmp/source')
7
+
8
+ instance = @generator_class.new('/tmp')
9
+ instance.source_root.should == '/tmp/source'
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.source_root' do
4
+ it "should raise an error, complaining that source_root must be overridden" do
5
+ @generator_class = Class.new(Templater::Generator)
6
+ lambda { @generator_class.source_root }.should raise_error(Templater::SourceNotSpecifiedError)
7
+ end
8
+ end
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '#template' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ @generator_class.class_eval do
8
+ def source_root
9
+ '/tmp/source'
10
+ end
11
+ end
12
+ end
13
+
14
+ it "should find a template by name" do
15
+ @generator_class.template(:blah1, 'blah.rb')
16
+ @generator_class.template(:blah2, 'blah2.rb')
17
+
18
+ instance = @generator_class.new('/tmp')
19
+
20
+ instance.template(:blah1).name.should == :blah1
21
+ instance.template(:blah1).source.should == '/tmp/source/blah.rbt'
22
+ instance.template(:blah1).destination.should == '/tmp/blah.rb'
23
+ end
24
+
25
+ it "should not return a template with an option that does not match." do
26
+ @generator_class.option :framework, :default => :rails
27
+
28
+ @generator_class.template(:merb, 'blah.rb', :framework => :merb)
29
+ @generator_class.template(:rails, 'blah2.rb', :framework => :rails)
30
+ @generator_class.template(:none, 'blah2.rb')
31
+
32
+ instance = @generator_class.new('/tmp')
33
+
34
+ instance.template(:rails).name.should == :rails
35
+ instance.template(:merb).should be_nil
36
+ instance.template(:none).name.should == :none
37
+
38
+ instance.framework = :merb
39
+ instance.template(:rails).should be_nil
40
+ instance.template(:merb).name.should == :merb
41
+ instance.template(:none).name.should == :none
42
+
43
+ instance.framework = nil
44
+ instance.template(:rails).should be_nil
45
+ instance.template(:merb).should be_nil
46
+ instance.template(:none).name.should == :none
47
+ end
48
+ end