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,62 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '#actions' 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')
14
+ @instance1 = mock('an instance of the generator')
15
+ @generator1.stub!(:new).and_return(@instance1)
16
+ @generator2 = mock('another generator')
17
+ @instance2 = mock('an instance of another generator')
18
+ @generator2.stub!(:new).and_return(@instance2)
19
+
20
+ @manifold = mock('a manifold')
21
+ @manifold.stub!(:generator).with(:one).and_return(@generator1)
22
+ @manifold.stub!(:generator).with(:two).and_return(@generator2)
23
+ @manifold.stub!(:generator).with(:three).and_return(@generator3)
24
+
25
+ @generator_class.stub!(:manifold).and_return(@manifold)
26
+ end
27
+
28
+ it "should return all templates and files" do
29
+ instance = @generator_class.new('/tmp')
30
+ instance.should_receive(:templates).at_least(:once).and_return(['template1', 'template2'])
31
+ instance.should_receive(:files).at_least(:once).and_return(['file1', 'file2'])
32
+
33
+ instance.actions.should include('template1')
34
+ instance.actions.should include('template2')
35
+ instance.actions.should include('file1')
36
+ instance.actions.should include('file2')
37
+ end
38
+
39
+ it "should return all templates and files recursively for all invocations" do
40
+ instance = @generator_class.new('/tmp')
41
+ instance.should_receive(:templates).at_least(:once).and_return(['template1', 'template2'])
42
+ instance.should_receive(:files).at_least(:once).and_return(['file1', 'file2'])
43
+ instance.should_receive(:empty_directories).at_least(:once).and_return(['public', 'bin'])
44
+ instance.should_receive(:invocations).at_least(:once).and_return([@instance1, @instance2])
45
+
46
+ @instance1.should_receive(:actions).at_least(:once).and_return(['subtemplate1', 'subfile1'])
47
+ @instance2.should_receive(:actions).at_least(:once).and_return(['subtemplate2', 'subfile2'])
48
+
49
+ instance.actions.should include('template1')
50
+ instance.actions.should include('template2')
51
+ instance.actions.should include('subtemplate1')
52
+ instance.actions.should include('subtemplate2')
53
+ instance.actions.should include('file1')
54
+ instance.actions.should include('file2')
55
+ instance.actions.should include('subfile1')
56
+ instance.actions.should include('subfile2')
57
+
58
+ instance.actions.should include('public')
59
+ instance.actions.should include('bin')
60
+ end
61
+
62
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.argument as array' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ @generator_class.argument(0, :monkey)
8
+ @generator_class.argument(1, :llama, :as => :array)
9
+ end
10
+
11
+ it "should allow assignment of arrays" do
12
+ instance = @generator_class.new('/tmp', {}, 'a monkey', %w(an array))
13
+
14
+ instance.monkey.should == 'a monkey'
15
+ instance.llama[0].should == 'an'
16
+ instance.llama[1].should == 'array'
17
+
18
+ instance.llama = %w(another donkey)
19
+ instance.llama[0].should == 'another'
20
+ instance.llama[1].should == 'donkey'
21
+ end
22
+
23
+ it "should convert a single argument to an array" do
24
+ instance = @generator_class.new('/tmp', {}, 'a monkey', 'test')
25
+ instance.llama[0].should == 'test'
26
+ end
27
+
28
+ it "should consume the remaining arguments and convert them to an array" do
29
+ instance = @generator_class.new('/tmp', {}, 'a monkey', 'test', 'silver', 'river')
30
+ instance.llama[0].should == 'test'
31
+ instance.llama[1].should == 'silver'
32
+ instance.llama[2].should == 'river'
33
+ end
34
+
35
+ it "should raise error if the argument is not an array" do
36
+ instance = @generator_class.new('/tmp')
37
+ lambda { instance.llama = :not_an_array }.should raise_error(Templater::MalformattedArgumentError)
38
+ end
39
+
40
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.argument as hash' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ @generator_class.argument(0, :monkey)
8
+ @generator_class.argument(1, :llama, :as => :hash)
9
+ end
10
+
11
+ it "should allow assignment of hashes" do
12
+ instance = @generator_class.new('/tmp', {}, 'a monkey', { :hash => 'blah' })
13
+
14
+ instance.monkey.should == 'a monkey'
15
+ instance.llama[:hash].should == 'blah'
16
+
17
+ instance.llama = { :me_s_a => :hash }
18
+ instance.llama[:me_s_a].should == :hash
19
+ end
20
+
21
+ it "should convert a key/value pair to a hash" do
22
+ instance = @generator_class.new('/tmp', {}, 'a monkey', 'test:unit')
23
+ instance.llama['test'].should == 'unit'
24
+ end
25
+
26
+ it "should consume the remaining arguments and convert them to a hash if they are key/value pairs" do
27
+ instance = @generator_class.new('/tmp', {}, 'a monkey', 'test:unit', 'john:silver', 'river:road')
28
+ instance.llama['test'].should == 'unit'
29
+ instance.llama['john'].should == 'silver'
30
+ instance.llama['river'].should == 'road'
31
+ end
32
+
33
+ it "should raise an error if one of the remaining arguments is not a key/value pair" do
34
+ lambda { @generator_class.new('/tmp', {}, 'a monkey', 'a:llama', 'duck:llama', 'not_a_pair', 'pair:blah') }.should raise_error(Templater::MalformattedArgumentError)
35
+ end
36
+
37
+ it "should raise error if the argument is neither a hash nor a key/value pair" do
38
+ lambda { @generator_class.new('/tmp', {}, 'a monkey', 23) }.should raise_error(Templater::MalformattedArgumentError)
39
+ instance = @generator_class.new('/tmp')
40
+ lambda { instance.llama = :not_a_hash }.should raise_error(Templater::MalformattedArgumentError)
41
+ end
42
+
43
+ end
@@ -0,0 +1,132 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.argument' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ end
8
+
9
+ it "should create accessors" do
10
+ @generator_class.argument(0, :monkey)
11
+
12
+ instance = @generator_class.new('/tmp')
13
+ instance.monkey = 'a test'
14
+ instance.monkey.should == 'a test'
15
+ end
16
+
17
+ it "should pass an initial value to the argument" do
18
+ @generator_class.argument(0, :monkey)
19
+
20
+ instance = @generator_class.new('/tmp', {}, 'i am a monkey')
21
+ instance.monkey.should == 'i am a monkey'
22
+ end
23
+
24
+ it "should create multiple accessors" do
25
+ @generator_class.argument(0, :monkey)
26
+ @generator_class.argument(1, :llama)
27
+ @generator_class.argument(2, :herd)
28
+
29
+ instance = @generator_class.new('/tmp')
30
+ instance.monkey = 'a monkey'
31
+ instance.monkey.should == 'a monkey'
32
+ instance.llama = 'a llama'
33
+ instance.llama.should == 'a llama'
34
+ instance.herd = 'a herd'
35
+ instance.herd.should == 'a herd'
36
+ end
37
+
38
+ it "should pass an initial value to multiple accessors" do
39
+ @generator_class.argument(0, :monkey)
40
+ @generator_class.argument(1, :llama)
41
+ @generator_class.argument(2, :herd)
42
+
43
+ instance = @generator_class.new('/tmp', {}, 'a monkey', 'a llama', 'a herd')
44
+ instance.monkey.should == 'a monkey'
45
+ instance.llama.should == 'a llama'
46
+ instance.herd.should == 'a herd'
47
+ end
48
+
49
+ it "should set a default value for an argument" do
50
+ @generator_class.argument(0, :monkey, :default => 'a revision')
51
+
52
+ instance = @generator_class.new('/tmp')
53
+ instance.monkey.should == 'a revision'
54
+ end
55
+
56
+ it "should allow some syntactic sugar declaration" do
57
+ @generator_class.first_argument(:monkey)
58
+ @generator_class.second_argument(:llama)
59
+ @generator_class.third_argument(:herd)
60
+ @generator_class.fourth_argument(:elephant)
61
+
62
+ instance = @generator_class.new('/tmp', {}, 'a monkey', 'a llama', 'a herd', 'an elephant')
63
+ instance.monkey.should == 'a monkey'
64
+ instance.llama.should == 'a llama'
65
+ instance.herd.should == 'a herd'
66
+ instance.elephant.should == 'an elephant'
67
+ end
68
+
69
+ it "should whine when there are too many arguments" do
70
+ @generator_class.argument(0, :monkey)
71
+ @generator_class.argument(1, :llama)
72
+
73
+ lambda { @generator_class.new('/tmp', {}, 'a monkey', 'a llama', 'a herd') }.should raise_error(Templater::TooManyArgumentsError)
74
+ end
75
+
76
+ it "should assign arguments if an argument is required and that requirement is fullfilled" do
77
+ @generator_class.argument(0, :monkey, :required => true)
78
+ @generator_class.argument(1, :elephant, :required => true)
79
+ @generator_class.argument(2, :llama)
80
+
81
+ instance = @generator_class.new('/tmp', {}, 'enough', 'arguments')
82
+ instance.monkey.should == "enough"
83
+ instance.elephant.should == "arguments"
84
+ instance.llama.should be_nil
85
+ end
86
+
87
+ it "should raise an error when a required argument is not passed" do
88
+ @generator_class.argument(0, :monkey, :required => true)
89
+ @generator_class.argument(1, :elephant, :required => true)
90
+ @generator_class.argument(2, :llama)
91
+
92
+ lambda { @generator_class.new('/tmp', {}, 'too few arguments') }.should raise_error(Templater::TooFewArgumentsError)
93
+ end
94
+
95
+ it "should raise an error if nil is assigned to a require argument" do
96
+ @generator_class.argument(0, :monkey, :required => true)
97
+
98
+ instance = @generator_class.new('/tmp', {}, 'test')
99
+
100
+ lambda { instance.monkey = nil }.should raise_error(Templater::TooFewArgumentsError)
101
+ end
102
+
103
+ it "should assign an argument when a block appended to an argument does not throw :invalid" do
104
+ @generator_class.argument(0, :monkey) do
105
+ 1 + 1
106
+ end
107
+ @generator_class.argument(1, :elephant) do
108
+ false
109
+ end
110
+ @generator_class.argument(2, :llama)
111
+
112
+ instance = @generator_class.new('/tmp', {}, 'blah', 'urgh')
113
+ instance.monkey.should == 'blah'
114
+ instance.elephant.should == 'urgh'
115
+
116
+ instance.monkey = :harr
117
+ instance.monkey.should == :harr
118
+ end
119
+
120
+ it "should raise an error with the throw message, when a block is appended to an argument and throws :invalid" do
121
+ @generator_class.argument(0, :monkey) do
122
+ throw :invalid, 'this is not a valid monkey, bad monkey!'
123
+ end
124
+
125
+ lambda { @generator_class.new('/tmp', {}, 'blah') }.should raise_error(Templater::ArgumentError, 'this is not a valid monkey, bad monkey!')
126
+
127
+ instance = @generator_class.new('/tmp')
128
+
129
+ lambda { instance.monkey = :anything }.should raise_error(Templater::ArgumentError, 'this is not a valid monkey, bad monkey!')
130
+ end
131
+
132
+ end
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.desc' do
4
+ it "should append text when called with an argument, and return it when called with no argument" do
5
+ @generator_class = Class.new(Templater::Generator)
6
+
7
+ @generator_class.desc "some text"
8
+ @generator_class.desc.should == "some text"
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '#destination_root' do
4
+ it "should be remembered" do
5
+ @generator_class = Class.new(Templater::Generator)
6
+ instance = @generator_class.new('/path/to/destination')
7
+ instance.destination_root.should == '/path/to/destination'
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, ".empty_directory" do
4
+ before do
5
+ @generator_class = Class.new(Templater::Generator)
6
+ end
7
+
8
+ it "adds directory path to list of directories that should be created" do
9
+ lambda do
10
+ @generator_class.empty_directory :bin, "bin"
11
+ end.should change(@generator_class.empty_directories, :size)
12
+ end
13
+
14
+ it "calculates directory path relatively to destination root" do
15
+ @generator_class.empty_directory :bin, "bin/swf"
16
+
17
+ @instance = @generator_class.new("/tmp/destination")
18
+ @instance.empty_directory(:bin).destination.should == "/tmp/destination/bin/swf"
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '#file' 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 file by name" 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.file(:blah1).name.should == :blah1
21
+ instance.file(:blah1).source.should == '/tmp/source/blah.rb'
22
+ instance.file(:blah1).destination.should == '/tmp/blah.rb'
23
+ end
24
+
25
+ it "should not return a file with an option that does not match." do
26
+ @generator_class.option :framework, :default => :rails
27
+
28
+ @generator_class.file(:merb, 'blah.rb', :framework => :merb)
29
+ @generator_class.file(:rails, 'blah2.rb', :framework => :rails)
30
+ @generator_class.file(:none, 'blah2.rb')
31
+
32
+ instance = @generator_class.new('/tmp')
33
+
34
+ instance.file(:rails).name.should == :rails
35
+ instance.file(:merb).should be_nil
36
+ instance.file(:none).name.should == :none
37
+
38
+ instance.framework = :merb
39
+ instance.file(:rails).should be_nil
40
+ instance.file(:merb).name.should == :merb
41
+ instance.file(:none).name.should == :none
42
+
43
+ instance.framework = nil
44
+ instance.file(:rails).should be_nil
45
+ instance.file(:merb).should be_nil
46
+ instance.file(:none).name.should == :none
47
+ end
48
+ end
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.file_list' do
4
+
5
+ it "should add a series of files given a list as heredoc" do
6
+ @generator_class = Class.new(Templater::Generator)
7
+
8
+ @generator_class.should_receive(:file).with(:app_model_rb, 'app/model.rb')
9
+ @generator_class.should_receive(:file).with(:spec_model_rb, 'spec/model.rb')
10
+ @generator_class.should_receive(:file).with(:donkey_poo_css, 'donkey/poo.css')
11
+ @generator_class.should_receive(:file).with(:john_smith_file_rb, 'john/smith/file.rb')
12
+
13
+ @generator_class.file_list <<-LIST
14
+ app/model.rb
15
+ spec/model.rb
16
+ donkey/poo.css
17
+ john/smith/file.rb
18
+ LIST
19
+ end
20
+
21
+ it "should add a series of files given a list as array" do
22
+ @generator_class = Class.new(Templater::Generator)
23
+
24
+ @generator_class.should_receive(:file).with(:app_model_rb, 'app/model.rb')
25
+ @generator_class.should_receive(:file).with(:spec_model_rb, 'spec/model.rb')
26
+ @generator_class.should_receive(:file).with(:donkey_poo_css, 'donkey/poo.css')
27
+ @generator_class.should_receive(:file).with(:john_smith_file_rb, 'john/smith/file.rb')
28
+
29
+ @generator_class.file_list(%w(app/model.rb spec/model.rb donkey/poo.css john/smith/file.rb))
30
+ end
31
+
32
+ end
@@ -0,0 +1,53 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Templater::Generator, '.file' do
4
+
5
+ before do
6
+ @generator_class = Class.new(Templater::Generator)
7
+ end
8
+
9
+ it "should add a file with source and destination" do
10
+ @generator_class.file(:my_template, 'path/to/source.rbt', 'path/to/destination.rb')
11
+ @instance = @generator_class.new('/tmp/destination')
12
+
13
+ @instance.stub!(:source_root).and_return('/tmp/source')
14
+
15
+ @instance.file(:my_template).source.should == '/tmp/source/path/to/source.rbt'
16
+ @instance.file(:my_template).destination.should == '/tmp/destination/path/to/destination.rb'
17
+ @instance.file(:my_template).should be_an_instance_of(Templater::File)
18
+ end
19
+
20
+ it "should add a file with source and infer destination " do
21
+ @generator_class.file(:my_template, 'path/to/file.rb')
22
+ @instance = @generator_class.new('/tmp/destination')
23
+
24
+ @instance.stub!(:source_root).and_return('/tmp/source')
25
+
26
+ @instance.file(:my_template).source.should == '/tmp/source/path/to/file.rb'
27
+ @instance.file(:my_template).destination.should == '/tmp/destination/path/to/file.rb'
28
+ @instance.file(:my_template).should be_an_instance_of(Templater::File)
29
+ end
30
+
31
+ it "should add a file and convert an instruction encoded in the destination, but not one encoded in the source" do
32
+ @generator_class.file(:my_template, 'template/%some_method%.rbt', 'template/%another_method%.rb')
33
+ @instance = @generator_class.new('/tmp/destination')
34
+
35
+ @instance.stub!(:source_root).and_return('/tmp/source')
36
+ @instance.should_not_receive(:some_method)
37
+ @instance.should_receive(:another_method).at_least(:once).and_return('beast')
38
+
39
+ @instance.file(:my_template).source.should == '/tmp/source/template/%some_method%.rbt'
40
+ @instance.file(:my_template).destination.should == "/tmp/destination/template/beast.rb"
41
+ @instance.file(:my_template).should be_an_instance_of(Templater::File)
42
+ end
43
+
44
+ it "should add a file and leave an encoded instruction be if it doesn't exist as a method" do
45
+ @generator_class.file(:my_template, 'template/blah.rbt', 'template/%some_method%.rb')
46
+ @instance = @generator_class.new('/tmp/destination')
47
+
48
+ @instance.stub!(:source_root).and_return('/tmp/source')
49
+
50
+ @instance.file(:my_template).destination.should == "/tmp/destination/template/%some_method%.rb"
51
+ @instance.file(:my_template).should be_an_instance_of(Templater::File)
52
+ end
53
+ end