generator-spec 0.3.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/README.markdown +3 -3
  2. data/VERSION +1 -1
  3. data/generator-spec.gemspec +11 -6
  4. data/lib/generators/controller/controller_generator.rb +33 -0
  5. data/lib/generators/helper/helper_generator.rb +33 -0
  6. data/lib/generators/{demo/demo_generator.rb → model/model_generator.rb} +2 -2
  7. data/lib/generators/observer/observer_generator.rb +33 -0
  8. data/lib/generators/view/view_generator.rb +34 -0
  9. data/lib/rspec_for_generators/generator_spec_helper.rb +13 -0
  10. data/lib/rspec_for_generators/matchers/content/have_class.rb +17 -6
  11. data/lib/rspec_for_generators/matchers/content/have_subclass.rb +41 -0
  12. data/lib/rspec_for_generators/matchers/file/generate_file.rb +18 -6
  13. data/lib/rspec_for_generators/rails_helpers/rails_controller.rb +2 -2
  14. data/lib/rspec_for_generators/rails_helpers/rails_helper.rb +5 -5
  15. data/lib/rspec_for_generators/rails_helpers/rails_observer.rb +33 -0
  16. data/lib/rspec_for_generators/rails_helpers/rails_view.rb +6 -10
  17. data/spec/rspec_for_generators/controller_generator_spec.rb +47 -0
  18. data/spec/rspec_for_generators/helper_generator_spec.rb +47 -0
  19. data/spec/rspec_for_generators/migration_generator_spec.rb +2 -2
  20. data/spec/rspec_for_generators/model_generator_spec.rb +8 -8
  21. data/spec/rspec_for_generators/observer_generator_spec.rb +47 -0
  22. data/spec/rspec_for_generators/view_generator_spec.rb +50 -0
  23. data/spec/spec_helper.rb +6 -4
  24. metadata +13 -8
  25. data/lib/rspec_for_generators/matchers/migration/have_migration.rb +0 -31
  26. data/tmp/rails/config/routes.rb +0 -2
  27. data/tmp/rails/db/migrations/20100805160101_create_users.rb +0 -14
@@ -95,7 +95,7 @@ describe 'model_generator' do
95
95
 
96
96
  before :each do
97
97
  # define generator to test
98
- RSpec::Generator.setup_generator 'model_generator' do
98
+ setup_generator 'model_generator' do
99
99
  tests Canable::Generators::ModelGenerator
100
100
  end
101
101
  # ensure clean state before run
@@ -108,7 +108,7 @@ describe 'model_generator' do
108
108
  end
109
109
 
110
110
  it "should not work without an existing Account model file" do
111
- RSpec::Generator.with_generator do |g|
111
+ with_generator do |g|
112
112
  name = 'account'
113
113
  g.run_generator %w{account}
114
114
  g.should_not generate_file name, :model
@@ -116,7 +116,7 @@ describe 'model_generator' do
116
116
  end
117
117
 
118
118
  it "should decorate an existing Account model file with 'include Canable:Ables'" do
119
- RSpec::Generator.with_generator do |g|
119
+ with_generator do |g|
120
120
  name = 'account'
121
121
  create_model name
122
122
  g.run_generator %w{account}
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.5
1
+ 0.4.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{generator-spec}
8
- s.version = "0.3.5"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2010-08-05}
12
+ s.date = %q{2010-08-07}
13
13
  s.description = %q{RSpec 2 library to assist in writing generator specs}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,9 +25,13 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "generator-spec.gemspec",
28
- "lib/generators/demo/demo_generator.rb",
28
+ "lib/generators/controller/controller_generator.rb",
29
+ "lib/generators/helper/helper_generator.rb",
29
30
  "lib/generators/migration/migration_generator.rb",
30
31
  "lib/generators/migration/templates/create_users.erb",
32
+ "lib/generators/model/model_generator.rb",
33
+ "lib/generators/observer/observer_generator.rb",
34
+ "lib/generators/view/view_generator.rb",
31
35
  "lib/rspec_for_generators.rb",
32
36
  "lib/rspec_for_generators/fixtures/routes.rb",
33
37
  "lib/rspec_for_generators/generator_spec_helper.rb",
@@ -38,6 +42,7 @@ Gem::Specification.new do |s|
38
42
  "lib/rspec_for_generators/matchers/content/have_class.rb",
39
43
  "lib/rspec_for_generators/matchers/content/have_method.rb",
40
44
  "lib/rspec_for_generators/matchers/content/have_module.rb",
45
+ "lib/rspec_for_generators/matchers/content/have_subclass.rb",
41
46
  "lib/rspec_for_generators/matchers/content/include_module.rb",
42
47
  "lib/rspec_for_generators/matchers/content/inherit_from.rb",
43
48
  "lib/rspec_for_generators/matchers/file/generate_directory.rb",
@@ -48,7 +53,6 @@ Gem::Specification.new do |s|
48
53
  "lib/rspec_for_generators/matchers/helpers/migration.rb",
49
54
  "lib/rspec_for_generators/matchers/migration/have_column.rb",
50
55
  "lib/rspec_for_generators/matchers/migration/have_index.rb",
51
- "lib/rspec_for_generators/matchers/migration/have_migration.rb",
52
56
  "lib/rspec_for_generators/matchers/migration/have_table.rb",
53
57
  "lib/rspec_for_generators/matchers/migration/have_tbl_column.rb",
54
58
  "lib/rspec_for_generators/matchers/migration/have_up_down.rb",
@@ -59,6 +63,7 @@ Gem::Specification.new do |s|
59
63
  "lib/rspec_for_generators/rails_helpers/rails_mailer.rb",
60
64
  "lib/rspec_for_generators/rails_helpers/rails_migration.rb",
61
65
  "lib/rspec_for_generators/rails_helpers/rails_model.rb",
66
+ "lib/rspec_for_generators/rails_helpers/rails_observer.rb",
62
67
  "lib/rspec_for_generators/rails_helpers/rails_orm.rb",
63
68
  "lib/rspec_for_generators/rails_helpers/rails_view.rb",
64
69
  "lib/rspec_for_generators/rails_spec_helper.rb",
@@ -68,10 +73,9 @@ Gem::Specification.new do |s|
68
73
  "spec/rspec_for_generators/helper_generator_spec.rb",
69
74
  "spec/rspec_for_generators/migration_generator_spec.rb",
70
75
  "spec/rspec_for_generators/model_generator_spec.rb",
76
+ "spec/rspec_for_generators/observer_generator_spec.rb",
71
77
  "spec/rspec_for_generators/view_generator_spec.rb",
72
78
  "spec/spec_helper.rb",
73
- "tmp/rails/config/routes.rb",
74
- "tmp/rails/db/migrations/20100805160101_create_users.rb",
75
79
  "wiki/Custom Rails 3 Generators.textile",
76
80
  "wiki/content_matchers.textile",
77
81
  "wiki/file_matchers.textile",
@@ -88,6 +92,7 @@ Gem::Specification.new do |s|
88
92
  "spec/rspec_for_generators/helper_generator_spec.rb",
89
93
  "spec/rspec_for_generators/migration_generator_spec.rb",
90
94
  "spec/rspec_for_generators/model_generator_spec.rb",
95
+ "spec/rspec_for_generators/observer_generator_spec.rb",
91
96
  "spec/rspec_for_generators/view_generator_spec.rb",
92
97
  "spec/spec_helper.rb"
93
98
  ]
@@ -0,0 +1,33 @@
1
+ class ControllerGenerator < Rails::Generators::NamedBase
2
+ desc "Adds method 'control_me' to a Controller"
3
+
4
+ def self.source_root
5
+ @source_root ||= File.expand_path("../templates", __FILE__)
6
+ end
7
+
8
+ def add_helper_method
9
+ if File.exist?(controller_file_name)
10
+ inject_into_file(controller_file_name, controller_method_code, :after => after_txt) if after_txt
11
+ else
12
+ say "#{controller_file_name} does not exist. Please create it first before you can add a controller method to it!", :red
13
+ end
14
+ end
15
+
16
+ protected
17
+
18
+ def after_txt
19
+ "#{class_name}Controller"
20
+ end
21
+
22
+ def controller_file_name
23
+ File.join(Rails.root, "app/controllers/#{file_name}_controller.rb")
24
+ end
25
+
26
+ def controller_method_code
27
+ %Q{
28
+ def control_me
29
+ "Control me please!"
30
+ end
31
+ }
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ class HelperGenerator < Rails::Generators::NamedBase
2
+ desc "Adds method 'help_me' to a Helper"
3
+
4
+ def self.source_root
5
+ @source_root ||= File.expand_path("../templates", __FILE__)
6
+ end
7
+
8
+ def add_helper_method
9
+ if File.exist?(helper_file_name)
10
+ inject_into_file(helper_file_name, helper_method_code, :after => after_txt) if after_txt
11
+ else
12
+ say "#{helper_file_name} does not exist. Please create it first before you can add a helper method to it!", :red
13
+ end
14
+ end
15
+
16
+ protected
17
+
18
+ def after_txt
19
+ "#{class_name}Helper"
20
+ end
21
+
22
+ def helper_file_name
23
+ File.join(Rails.root, "app/helpers/#{file_name}_helper.rb")
24
+ end
25
+
26
+ def helper_method_code
27
+ %Q{
28
+ def help_me
29
+ "Help me please!"
30
+ end
31
+ }
32
+ end
33
+ end
@@ -1,7 +1,7 @@
1
- class DemoGenerator < Rails::Generators::Base
1
+ class ModelGenerator < Rails::Generators::Base
2
2
  desc "Adds Canable::Ables permission system to Model"
3
3
 
4
- argument :name, :type => :string, :default => 'User', :desc => 'Name of model to make Canable:Able', :required => false
4
+ argument :name, :type => :string, :default => 'User', :desc => 'Name of model to make Canable:Able', :required => false
5
5
 
6
6
  def self.source_root
7
7
  @source_root ||= File.expand_path("../../templates", __FILE__)
@@ -0,0 +1,33 @@
1
+ class ObserverGenerator < Rails::Generators::NamedBase
2
+ desc "Adds method 'observe_me' to a Observer"
3
+
4
+ def self.source_root
5
+ @source_root ||= File.expand_path("../templates", __FILE__)
6
+ end
7
+
8
+ def add_observer_method
9
+ if File.exist?(observer_file_name)
10
+ inject_into_file(observer_file_name, observer_method_code, :after => after_txt) if after_txt
11
+ else
12
+ say "#{observer_file_name} does not exist. Please create it first before you can add a observer method to it!", :red
13
+ end
14
+ end
15
+
16
+ protected
17
+
18
+ def after_txt
19
+ "ActiveRecord::Observer"
20
+ end
21
+
22
+ def observer_file_name
23
+ File.join(Rails.root, "app/models/#{file_name}_observer.rb")
24
+ end
25
+
26
+ def observer_method_code
27
+ %Q{
28
+ def observe_me
29
+ "Observe me please!"
30
+ end
31
+ }
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ class ViewGenerator < Rails::Generators::NamedBase
2
+ desc "Adds some view code to existing View"
3
+
4
+ argument :view, :type => :string, :required => true
5
+ argument :ext, :type => :string, :default => 'html.erb'
6
+
7
+ def self.source_root
8
+ @source_root ||= File.expand_path("../templates", __FILE__)
9
+ end
10
+
11
+ def add_helper_method
12
+ if File.exist?(view_file_name)
13
+ inject_into_file(view_file_name, view_code, :after => after_txt) if after_txt
14
+ else
15
+ say "#{view_file_name} does not exist. Please create it first before you can add view code to it!", :red
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def after_txt
22
+ '# view content'
23
+ end
24
+
25
+ def view_file_name
26
+ File.join(Rails.root, "app/views/#{file_name}/#{view}.#{ext}")
27
+ end
28
+
29
+ def view_code
30
+ %Q{
31
+ <%= "Hello You" %>
32
+ }
33
+ end
34
+ end
@@ -93,3 +93,16 @@ module RSpec
93
93
  end # class self
94
94
  end
95
95
  end
96
+
97
+
98
+ module RSpec::Core
99
+ class ExampleGroup
100
+ def with_generator &block
101
+ RSpec::Generator.with_generator &block
102
+ end
103
+
104
+ def setup_generator test_method_name=nil, &block
105
+ RSpec::Generator.setup_generator test_method_name, &block
106
+ end
107
+ end
108
+ end
@@ -1,13 +1,14 @@
1
1
  module RSpec::RubyContentMatchers
2
2
  class HaveClass
3
- attr_reader :klass
3
+ attr_reader :klass, :type
4
4
 
5
- def initialize(klass)
6
- @klass = klass.to_s.camelize
5
+ def initialize(klass, type=nil)
6
+ @klass = klass.to_s.camelize
7
+ @type = type.to_s.camelize if type
7
8
  end
8
9
 
9
10
  def matches?(content)
10
- match_res = (content =~ /class\s+#{klass}\s+(.*)end/m)
11
+ match_res = (content =~ /class\s+#{klass}#{type}\s+(.*)end/m)
11
12
  if block_given? && $1
12
13
  ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
13
14
  yield ruby_content
@@ -26,7 +27,17 @@ module RSpec::RubyContentMatchers
26
27
 
27
28
  end
28
29
 
29
- def have_class(klass)
30
- HaveClass.new(klass)
30
+ def have_class(klass, type = nil)
31
+ HaveClass.new(klass, type)
31
32
  end
33
+
34
+ def have_helper_class(klass)
35
+ HaveClass.new(klass, :helper)
36
+ end
37
+
38
+ def have_controller_class(klass)
39
+ HaveClass.new(klass, :controller)
40
+ end
32
41
  end
42
+
43
+
@@ -0,0 +1,41 @@
1
+ module RSpec::RubyContentMatchers
2
+ class HaveSubclass
3
+ attr_reader :klass, :superclass, :type
4
+
5
+ def initialize(klass, superclass, type=nil)
6
+ @klass = klass.to_s.camelize
7
+ @superclass = superclass.to_s.camelize
8
+ @type = type.to_s.camelize if type
9
+ end
10
+
11
+ def matches?(content)
12
+ match_res = (content =~ /class\s+#{klass}#{type}\s+<\s+#{superclass}(.*)end$/m)
13
+ if block_given? && $1
14
+ ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
15
+ yield ruby_content
16
+ else
17
+ match_res
18
+ end
19
+ end
20
+
21
+ def failure_message
22
+ "Expected there to be the subclass #{klass} of #{superclass}"
23
+ end
24
+
25
+ def negative_failure_message
26
+ "Did no expected there to be the subclass #{klass} of #{superclass}"
27
+ end
28
+ end
29
+
30
+ def have_migration(klass, superclass, type=nil)
31
+ HaveSubclass.new(klass, superclass, type)
32
+ end
33
+
34
+ def have_observer_class(klass)
35
+ HaveSubclass.new(klass, 'ActiveRecord::Observer', :observer)
36
+ end
37
+
38
+ def have_migration(klass)
39
+ HaveSubclass.new(klass, 'ActiveRecord::Migration')
40
+ end
41
+ end
@@ -8,7 +8,7 @@ module RSpec::FileMatchers
8
8
  class GenerateFile
9
9
 
10
10
  def initialize(relative_path, type = nil)
11
- @relative_path = relative_rails_file relative_path, type
11
+ @relative_path = relative_rails_file(relative_path, type)
12
12
  end
13
13
 
14
14
  def matches?(generator, &block)
@@ -33,10 +33,21 @@ module RSpec::FileMatchers
33
33
 
34
34
  protected
35
35
 
36
- def relative_rails_file path, type = nil
37
- return File.join(::Rails.root, base_dir(type), folder(type), "#{path.to_s}.rb") if type
36
+ def relative_rails_file path, type = nil
37
+ f_name = file_name(path, type)
38
+ return File.join(::Rails.root, base_dir(type), folder(type), f_name) if type
38
39
  File.join(::Rails.root, path)
39
40
  end
41
+
42
+ def file_name path, type
43
+ return "#{path}#{postfix(type)}.rb" if !path.include? '.'
44
+ path
45
+ end
46
+
47
+
48
+ def postfix type
49
+ "_#{type}" if [:helper, :controller, :observer].include?(type)
50
+ end
40
51
 
41
52
  def folder type
42
53
  case type
@@ -71,7 +82,8 @@ module RSpec::FileMatchers
71
82
  GenerateFile.new(relative, :model)
72
83
  end
73
84
 
74
- def generate_controller(relative)
85
+ def generate_controller(relative)
86
+ puts "Generate controller: #{relative}"
75
87
  GenerateFile.new(relative, :controller)
76
88
  end
77
89
 
@@ -79,8 +91,8 @@ module RSpec::FileMatchers
79
91
  GenerateFile.new(relative, :helper)
80
92
  end
81
93
 
82
- def generate_view(relative)
83
- GenerateFile.new(relative, :view)
94
+ def generate_view(relative, action, ext)
95
+ GenerateFile.new("#{relative}/#{action}.#{ext}", :view)
84
96
  end
85
97
 
86
98
  def generate_observer(relative)
@@ -7,7 +7,7 @@ module RSpec
7
7
  FileUtils.mkdir_p File.dirname(file)
8
8
  content ||= yield if block_given?
9
9
  File.open(file, 'w') do |f|
10
- f.puts controller_content(content)
10
+ f.puts controller_content(name, content)
11
11
  end
12
12
  end
13
13
  end
@@ -28,7 +28,7 @@ module RSpec
28
28
  end
29
29
 
30
30
  def controller_file_name name
31
- File.join(::Rails.root, "app/controllers/#{name}.rb")
31
+ File.join(::Rails.root, "app/controllers/#{name}_controller.rb")
32
32
  end
33
33
 
34
34
  end
@@ -6,18 +6,18 @@ module RSpec::Rails
6
6
  FileUtils.mkdir_p File.dirname(file)
7
7
  content ||= yield if block_given?
8
8
  File.open(file, 'w') do |f|
9
- f.puts helper_content(content)
9
+ f.puts helper_content(name, content)
10
10
  end
11
11
  end
12
12
  end
13
13
 
14
14
  def helper_content name, content=nil
15
15
  %Q{class #{name.to_s.camelize}Helper
16
- #{content}
17
- end}
16
+ #{content}
17
+ end}
18
18
  end
19
19
 
20
- def remove_model name
20
+ def remove_helper name
21
21
  file = helper_file_name(name)
22
22
  FileUtils.rm_f(file) if File.exist?(file)
23
23
  end
@@ -29,7 +29,7 @@ module RSpec::Rails
29
29
  protected
30
30
 
31
31
  def helper_file_name name
32
- File.join(::Rails.root, "app/helpers/#{name}.rb")
32
+ File.join(::Rails.root, "app/helpers/#{name}_helper.rb")
33
33
  end
34
34
 
35
35
  end
@@ -0,0 +1,33 @@
1
+ module RSpec::Rails
2
+ module Observer
3
+ def create_observer name, content=nil
4
+ file = observer_file_name(name)
5
+ unless File.exist?(file)
6
+ FileUtils.mkdir_p File.dirname(file)
7
+ content ||= yield if block_given?
8
+ File.open(file, 'w') do |f|
9
+ f.puts observer_content(name, content)
10
+ end
11
+ end
12
+ end
13
+
14
+ def observer_content name, content=nil
15
+ %Q{class #{name.to_s.camelize}Observer < ActiveRecord::Observer
16
+ #{content}
17
+ end}
18
+ end
19
+
20
+ def remove_observer name
21
+ file = observer_file_name(name)
22
+ FileUtils.rm_f(file) if File.exist?(file)
23
+ end
24
+
25
+ def remove_observers *names
26
+ names.each{|name| remove_observer name }
27
+ end
28
+
29
+ def observer_file_name name
30
+ File.join(::Rails.root, "app/models/#{name}_observer.rb")
31
+ end
32
+ end
33
+ end
@@ -1,7 +1,7 @@
1
1
  module RSpec::Rails
2
2
  module View
3
- def create_view name, content=nil
4
- file = view_file_name(name)
3
+ def create_view name, action, type, content=nil
4
+ file = view_file_name(name, action, type)
5
5
  unless File.exist?(file)
6
6
  FileUtils.mkdir_p File.dirname(file)
7
7
  content ||= yield if block_given?
@@ -12,17 +12,13 @@ module RSpec::Rails
12
12
  end
13
13
  end
14
14
 
15
- def remove_view name
16
- file = view_file_name(name)
15
+ def remove_view name, action, type
16
+ file = view_file_name(name, action, type)
17
17
  FileUtils.rm_f(file) if File.exist?(file)
18
18
  end
19
-
20
- def remove_views *names
21
- names.each{|name| remove_view name }
22
- end
23
19
 
24
- def view_file_name name
25
- File.join(::Rails.root, "app/views/#{name}.rb")
20
+ def view_file_name name, action, type
21
+ File.join(::Rails.root, "app/views/#{name}/#{action}.#{type}")
26
22
  end
27
23
 
28
24
  end
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require_generator :controller
3
+
4
+ module RSpec::Core
5
+ class ExampleGroup
6
+ include RSpec::Rails::Controller
7
+ end
8
+ end
9
+
10
+ describe 'helper_generator' do
11
+ before :each do
12
+ setup_generator 'controller_generator' do
13
+ tests ControllerGenerator
14
+ end
15
+ remove_controller 'account'
16
+ end
17
+
18
+ after :each do
19
+ remove_controller 'account'
20
+ end
21
+
22
+ it "should not work without an Account controller file" do
23
+ with_generator do |g|
24
+ name = 'account'
25
+ g.run_generator [name]
26
+ g.should_not generate_controller name
27
+ end
28
+ end
29
+
30
+ it "should decorate an existing Account controller file with a 'control_me' method" do
31
+ with_generator do |g|
32
+ name = 'account'
33
+ create_controller name do
34
+ '# controller content'
35
+ end
36
+ g.run_generator [name]
37
+ g.should generate_controller name do |content|
38
+ content.should have_controller_class name do |klass|
39
+ klass.should have_method :control_me
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+
47
+
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require_generator :helper
3
+
4
+ module RSpec::Core
5
+ class ExampleGroup
6
+ include RSpec::Rails::Helper
7
+ end
8
+ end
9
+
10
+ describe 'helper_generator' do
11
+ before :each do
12
+ setup_generator 'helper_generator' do
13
+ tests HelperGenerator
14
+ end
15
+ remove_helper 'account'
16
+ end
17
+
18
+ after :each do
19
+ remove_helper 'account'
20
+ end
21
+
22
+ it "should not work without an Account helper file" do
23
+ with_generator do |g|
24
+ name = 'account'
25
+ g.run_generator [name]
26
+ g.should_not generate_helper name
27
+ end
28
+ end
29
+
30
+ it "should decorate an existing Account helper file with a 'help_me' method" do
31
+ with_generator do |g|
32
+ name = 'account'
33
+ create_helper name do
34
+ '# helper content'
35
+ end
36
+ g.run_generator [name]
37
+ g.should generate_helper name do |content|
38
+ content.should have_helper_class name do |klass|
39
+ klass.should have_method :help_me
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+
47
+
@@ -6,7 +6,7 @@ describe 'migration_generator' do
6
6
  include RSpec::Rails::Migration
7
7
 
8
8
  before :each do
9
- RSpec::Generator.setup_generator 'migration_generator' do
9
+ setup_generator 'migration_generator' do
10
10
  tests MigrationGenerator
11
11
  end
12
12
  end
@@ -15,7 +15,7 @@ describe 'migration_generator' do
15
15
  end
16
16
 
17
17
  it "should generate create_user migration" do
18
- RSpec::Generator.with_generator do |g|
18
+ with_generator do |g|
19
19
  name = 'create_users'
20
20
  remove_migration 'create_users'
21
21
  g.run_generator [name]
@@ -1,12 +1,12 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require_generator :demo
2
+ require_generator :model
3
3
 
4
4
  describe 'model_generator' do
5
5
  include RSpec::Rails::Orm::ActiveRecord
6
6
 
7
7
  before :each do
8
- RSpec::Generator.setup_generator 'model_generator' do
9
- tests DemoGenerator
8
+ setup_generator 'model_generator' do
9
+ tests ModelGenerator
10
10
  end
11
11
  remove_model 'account'
12
12
  end
@@ -16,19 +16,19 @@ describe 'model_generator' do
16
16
  end
17
17
 
18
18
  it "should not work without an Account model file" do
19
- RSpec::Generator.with_generator do |g|
19
+ with_generator do |g|
20
20
  name = 'account'
21
- g.run_generator %w{account}
21
+ g.run_generator [name]
22
22
  g.should_not generate_file name, :model
23
23
  end
24
24
  end
25
25
 
26
26
  it "should decorate an existing Account model file with include Canable:Ables" do
27
- RSpec::Generator.with_generator do |g|
27
+ with_generator do |g|
28
28
  name = 'account'
29
29
  create_model name
30
- g.run_generator %w{account}
31
- g.should generate_file name, :model do |content|
30
+ g.run_generator [name]
31
+ g.should generate_model name do |content|
32
32
  content.should have_class name do |klass|
33
33
  klass.should include_module 'Canable::Ables'
34
34
  end
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require_generator :observer
3
+
4
+ module RSpec::Core
5
+ class ExampleGroup
6
+ include RSpec::Rails::Observer
7
+ end
8
+ end
9
+
10
+ describe 'observer_generator' do
11
+ before :each do
12
+ setup_generator 'observer_generator' do
13
+ tests ObserverGenerator
14
+ end
15
+ remove_observer 'account'
16
+ end
17
+
18
+ after :each do
19
+ remove_observer 'account'
20
+ end
21
+
22
+ it "should not work without an Account observer file" do
23
+ with_generator do |g|
24
+ name = 'account'
25
+ g.run_generator [name]
26
+ g.should_not generate_observer name
27
+ end
28
+ end
29
+
30
+ it "should decorate an existing Account observer file with a 'observe_me' method" do
31
+ with_generator do |g|
32
+ name = 'account'
33
+ create_observer name do
34
+ '# observer content'
35
+ end
36
+ g.run_generator [name]
37
+ g.should generate_observer name do |content|
38
+ content.should have_observer_class name do |klass|
39
+ klass.should have_method :observe_me
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+
47
+
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require_generator :view
3
+
4
+ module RSpec::Core
5
+ class ExampleGroup
6
+ include RSpec::Rails::View
7
+ end
8
+ end
9
+
10
+ describe 'view_generator' do
11
+ before :each do
12
+ setup_generator 'view_generator' do
13
+ tests ViewGenerator
14
+ end
15
+ remove_view :account, :edit, 'html.erb'
16
+ end
17
+
18
+ after :each do
19
+ remove_view :account, :edit, 'html.erb'
20
+ end
21
+
22
+ it "should not work without an Edit Account view file" do
23
+ with_generator do |g|
24
+ name = :account
25
+ action = :edit
26
+ type = 'html.erb'
27
+ g.run_generator [name, action, type].map(&:to_s)
28
+ g.should_not generate_view name, action, type
29
+ end
30
+ end
31
+
32
+ it "should decorate an existing Edit Account view file with some view code" do
33
+ with_generator do |g|
34
+ name = :account
35
+ action = :edit
36
+ type = 'html.erb'
37
+
38
+ create_view name, action, type do
39
+ '# view content'
40
+ end
41
+ g.run_generator [name, action, type].map(&:to_s)
42
+ g.should generate_view name, action, type do |content|
43
+ content.should match /Hello You/
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+
50
+
@@ -2,12 +2,14 @@ require 'rspec'
2
2
  require 'rspec/autorun'
3
3
  require 'rspec_for_generators'
4
4
 
5
- RSpec::Generator.remove_temp_dir = false
5
+ RSpec::Generator.remove_temp_dir = true
6
6
  RSpec::Generator.configure_root_dir __FILE__
7
-
8
- class RSpec::Core::ExampleGroup
9
7
 
10
- end
8
+ # RSpec::Core::ExampleGroup
9
+
10
+ # RSpec.configure do |c|
11
+ # c.extend RSpec::Generator
12
+ # end
11
13
 
12
14
  module Rails
13
15
  def self.logger
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- - 5
9
- version: 0.3.5
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-05 00:00:00 +02:00
17
+ date: 2010-08-07 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -129,9 +129,13 @@ files:
129
129
  - Rakefile
130
130
  - VERSION
131
131
  - generator-spec.gemspec
132
- - lib/generators/demo/demo_generator.rb
132
+ - lib/generators/controller/controller_generator.rb
133
+ - lib/generators/helper/helper_generator.rb
133
134
  - lib/generators/migration/migration_generator.rb
134
135
  - lib/generators/migration/templates/create_users.erb
136
+ - lib/generators/model/model_generator.rb
137
+ - lib/generators/observer/observer_generator.rb
138
+ - lib/generators/view/view_generator.rb
135
139
  - lib/rspec_for_generators.rb
136
140
  - lib/rspec_for_generators/fixtures/routes.rb
137
141
  - lib/rspec_for_generators/generator_spec_helper.rb
@@ -142,6 +146,7 @@ files:
142
146
  - lib/rspec_for_generators/matchers/content/have_class.rb
143
147
  - lib/rspec_for_generators/matchers/content/have_method.rb
144
148
  - lib/rspec_for_generators/matchers/content/have_module.rb
149
+ - lib/rspec_for_generators/matchers/content/have_subclass.rb
145
150
  - lib/rspec_for_generators/matchers/content/include_module.rb
146
151
  - lib/rspec_for_generators/matchers/content/inherit_from.rb
147
152
  - lib/rspec_for_generators/matchers/file/generate_directory.rb
@@ -152,7 +157,6 @@ files:
152
157
  - lib/rspec_for_generators/matchers/helpers/migration.rb
153
158
  - lib/rspec_for_generators/matchers/migration/have_column.rb
154
159
  - lib/rspec_for_generators/matchers/migration/have_index.rb
155
- - lib/rspec_for_generators/matchers/migration/have_migration.rb
156
160
  - lib/rspec_for_generators/matchers/migration/have_table.rb
157
161
  - lib/rspec_for_generators/matchers/migration/have_tbl_column.rb
158
162
  - lib/rspec_for_generators/matchers/migration/have_up_down.rb
@@ -163,6 +167,7 @@ files:
163
167
  - lib/rspec_for_generators/rails_helpers/rails_mailer.rb
164
168
  - lib/rspec_for_generators/rails_helpers/rails_migration.rb
165
169
  - lib/rspec_for_generators/rails_helpers/rails_model.rb
170
+ - lib/rspec_for_generators/rails_helpers/rails_observer.rb
166
171
  - lib/rspec_for_generators/rails_helpers/rails_orm.rb
167
172
  - lib/rspec_for_generators/rails_helpers/rails_view.rb
168
173
  - lib/rspec_for_generators/rails_spec_helper.rb
@@ -172,10 +177,9 @@ files:
172
177
  - spec/rspec_for_generators/helper_generator_spec.rb
173
178
  - spec/rspec_for_generators/migration_generator_spec.rb
174
179
  - spec/rspec_for_generators/model_generator_spec.rb
180
+ - spec/rspec_for_generators/observer_generator_spec.rb
175
181
  - spec/rspec_for_generators/view_generator_spec.rb
176
182
  - spec/spec_helper.rb
177
- - tmp/rails/config/routes.rb
178
- - tmp/rails/db/migrations/20100805160101_create_users.rb
179
183
  - wiki/Custom Rails 3 Generators.textile
180
184
  - wiki/content_matchers.textile
181
185
  - wiki/file_matchers.textile
@@ -218,5 +222,6 @@ test_files:
218
222
  - spec/rspec_for_generators/helper_generator_spec.rb
219
223
  - spec/rspec_for_generators/migration_generator_spec.rb
220
224
  - spec/rspec_for_generators/model_generator_spec.rb
225
+ - spec/rspec_for_generators/observer_generator_spec.rb
221
226
  - spec/rspec_for_generators/view_generator_spec.rb
222
227
  - spec/spec_helper.rb
@@ -1,31 +0,0 @@
1
- module RSpec::RubyContentMatchers
2
- class HaveMigration
3
- attr_reader :klass
4
-
5
- def initialize(klass)
6
- @klass = klass.to_s.camelize
7
- end
8
-
9
- def matches?(content)
10
- match_res = (content =~ /class\s+#{klass}\s+<\s+ActiveRecord::Migration(.*)end$/m)
11
- if block_given? && $1
12
- ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
13
- yield ruby_content
14
- else
15
- match_res
16
- end
17
- end
18
-
19
- def failure_message
20
- "Expected there to be the migration class #{klass}"
21
- end
22
-
23
- def negative_failure_message
24
- "Did no expected there to be the migration class #{klass}"
25
- end
26
- end
27
-
28
- def have_migration(klass)
29
- HaveMigration.new(klass)
30
- end
31
- end
@@ -1,2 +0,0 @@
1
- Rails.application.routes.draw do |map|
2
- end
@@ -1,14 +0,0 @@
1
- class CreateUsers < ActiveRecord::Migration
2
- def self.up
3
- create_table :users do |t|
4
- t.string :name
5
- t.integer :age
6
- t.boolean :admin
7
- end
8
- end
9
-
10
- def self.down
11
- drop_table :users
12
- end
13
-
14
- end