generator-spec 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/README.markdown +53 -37
  2. data/VERSION +1 -1
  3. data/lib/{rspec_for_generators.rb → generator_spec.rb} +1 -1
  4. data/lib/generator_spec/extensions/core_ext.rb +17 -0
  5. data/lib/{rspec_for_generators/rails_spec_helper.rb → generator_spec/helpers/configure_rails.rb} +0 -4
  6. data/lib/{rspec_for_generators → generator_spec/helpers}/migration_helper.rb +0 -0
  7. data/lib/generator_spec/main.rb +42 -0
  8. data/lib/generator_spec/matchers/content/content_matcher.rb +21 -0
  9. data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_block.rb +17 -8
  10. data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_call.rb +6 -6
  11. data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_calls.rb +5 -5
  12. data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_class.rb +14 -6
  13. data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_method.rb +17 -8
  14. data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_module.rb +7 -6
  15. data/lib/generator_spec/matchers/content/have_region.rb +49 -0
  16. data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_subclass.rb +19 -7
  17. data/lib/{rspec_for_generators → generator_spec}/matchers/content/include_module.rb +5 -5
  18. data/lib/{rspec_for_generators → generator_spec}/matchers/content/inherit_from.rb +7 -3
  19. data/lib/{rspec_for_generators → generator_spec}/matchers/file/generate_directory.rb +1 -1
  20. data/lib/{rspec_for_generators → generator_spec}/matchers/file/generate_file.rb +6 -0
  21. data/lib/{rspec_for_generators → generator_spec}/matchers/file/generate_migration.rb +1 -1
  22. data/lib/{rspec_for_generators → generator_spec}/matchers/helpers/content.rb +0 -0
  23. data/lib/{rspec_for_generators → generator_spec}/matchers/helpers/file.rb +0 -0
  24. data/lib/{rspec_for_generators → generator_spec}/matchers/helpers/migration.rb +1 -1
  25. data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_column.rb +0 -0
  26. data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_index.rb +0 -0
  27. data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_table.rb +0 -0
  28. data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_tbl_column.rb +0 -0
  29. data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_up_down.rb +0 -0
  30. data/lib/generator_spec/rails_helpers/rails_app.rb +159 -0
  31. data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_controller.rb +4 -2
  32. data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_helper.rb +4 -2
  33. data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_mailer.rb +4 -2
  34. data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_migration.rb +1 -1
  35. data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_model.rb +4 -2
  36. data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_observer.rb +4 -2
  37. data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_orm.rb +37 -17
  38. data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_view.rb +3 -1
  39. data/lib/generator_spec/require_generator.rb +67 -0
  40. data/lib/generator_spec/rspec_generator/configure.rb +51 -0
  41. data/lib/generator_spec/rspec_generator/generator_util.rb +57 -0
  42. data/lib/generator_spec/rspec_generator/routes.rb +2 -0
  43. data/lib/generator_spec/rspec_generator/test_case.rb +25 -0
  44. data/spec/{rspec_for_generators → generator_spec}/controller_generator_spec.rb +0 -0
  45. data/spec/{rspec_for_generators → generator_spec}/helper_generator_spec.rb +10 -10
  46. data/spec/{rspec_for_generators → generator_spec}/migration_generator_spec.rb +0 -0
  47. data/spec/{rspec_for_generators → generator_spec}/model_generator_spec.rb +0 -0
  48. data/spec/{rspec_for_generators → generator_spec}/observer_generator_spec.rb +0 -0
  49. data/spec/{rspec_for_generators → generator_spec}/view_generator_spec.rb +0 -0
  50. data/spec/spec_helper.rb +12 -9
  51. metadata +55 -51
  52. data/generator-spec.gemspec +0 -128
  53. data/lib/rspec_for_generators/fixtures/routes.rb +0 -2
  54. data/lib/rspec_for_generators/generator_spec_helper.rb +0 -151
  55. data/lib/rspec_for_generators/main.rb +0 -19
  56. data/lib/rspec_for_generators/rails_helpers/rails_app.rb +0 -119
  57. data/lib/rspec_for_generators/rspec_test_case.rb +0 -26
@@ -6,8 +6,8 @@
6
6
  # say_hello_file_content.should have_method "hello"
7
7
  #
8
8
  module RSpec::RubyContentMatchers
9
- class HaveMethod
10
- attr_reader :method, :type, :content
9
+ class HaveMethod < RSpec::RubyContentMatcher
10
+ attr_reader :method, :type
11
11
 
12
12
  def initialize(method, type=nil)
13
13
  @method = method.to_s
@@ -15,12 +15,12 @@ module RSpec::RubyContentMatchers
15
15
  end
16
16
 
17
17
  def matches?(content)
18
- @content = content
18
+ super
19
19
  match_res = case type
20
20
  when :class
21
- content =~ /def\s+self.#{method}\s*(\(.+\))?(.*?)(def|end)/m
21
+ content =~ /def\s+self.#{method}#{args_expr}(.*?)#{end_expr}/m
22
22
  else
23
- content =~ /def\s+#{method}\s*(\(.+\))?(.*?)(def|end)/m
23
+ content =~ /def\s+#{method}#{args_expr}(.*?)#{end_expr}/m
24
24
  end
25
25
  if block_given? && $2
26
26
  ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
@@ -31,17 +31,26 @@ module RSpec::RubyContentMatchers
31
31
  end
32
32
 
33
33
  def failure_message
34
- puts "Content: #{content}" if RSpec::Generator.debug?
34
+ super
35
35
  return "Expected there to be the class method #{method}, but there wasn't" if type == :class
36
36
  "Expected there to be the method #{method}, but there wasn't"
37
37
  end
38
38
 
39
39
  def negative_failure_message
40
- puts "Content: #{content}" if RSpec::Generator.debug?
40
+ super
41
41
  return "Did not expect there to be the class method #{method}, but there was" if type == :class
42
42
  "Did not expect there to be the method #{method}, but there was"
43
43
  end
44
-
44
+
45
+ protected
46
+
47
+ def args_expr
48
+ '\s*(\(.+\))?'
49
+ end
50
+
51
+ def end_expr
52
+ "(def|end|# #{method})"
53
+ end
45
54
  end
46
55
 
47
56
  def have_method(method, type = nil)
@@ -1,13 +1,13 @@
1
1
  module RSpec::RubyContentMatchers
2
- class HaveModule
3
- attr_reader :module_name, :content
2
+ class HaveModule < RSpec::RubyContentMatcher
3
+ attr_reader :module_name
4
4
 
5
5
  def initialize(module_name)
6
6
  @module_name = module_name.to_s.camelize
7
7
  end
8
8
 
9
9
  def matches?(content)
10
- @content = content
10
+ super
11
11
  match_res =~ /module\s+#{module_name}\s+(.*)end/m
12
12
  if block_given? && match_res
13
13
  ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
@@ -18,12 +18,12 @@ module RSpec::RubyContentMatchers
18
18
  end
19
19
 
20
20
  def failure_message
21
- puts "Content: #{content}" if RSpec::Generator.debug?
21
+ super
22
22
  "Expected there to be the module #{module_name}"
23
23
  end
24
24
 
25
25
  def negative_failure_message
26
- puts "Content: #{content}" if RSpec::Generator.debug?
26
+ super
27
27
  "Did no expected there to be the module #{module_name}"
28
28
  end
29
29
 
@@ -31,5 +31,6 @@ module RSpec::RubyContentMatchers
31
31
 
32
32
  def have_module(module_name)
33
33
  HaveModule.new(module_name)
34
- end
34
+ end
35
+ alias_method :be_module, :have_module
35
36
  end
@@ -0,0 +1,49 @@
1
+ module RSpec::RubyContentMatchers
2
+ class HaveRegion < RSpec::RubyContentMatcher
3
+ attr_reader :region
4
+
5
+ def initialize(region)
6
+ @region = region.to_s
7
+ end
8
+
9
+ def matches?(content)
10
+ super
11
+ match_res = (content =~ /#{region}(.*)#{end_epxr}/m)
12
+ if block_given? && $1
13
+ ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
14
+ yield ruby_content
15
+ end
16
+ match_res
17
+ end
18
+
19
+ def failure_message
20
+ super
21
+ "Expected there to be a #{region} region"
22
+ end
23
+
24
+ def negative_failure_message
25
+ super
26
+ "Did no expected there to be a #{region} region"
27
+ end
28
+
29
+ protected
30
+
31
+ def end_expr
32
+ '(private|protected|public|$)'
33
+ end
34
+ end
35
+
36
+ def have_public
37
+ HaveRegion.new :public
38
+ end
39
+
40
+ def have_protected
41
+ HaveRegion.new :protected
42
+ end
43
+
44
+ def have_private
45
+ HaveRegion.new :private
46
+ end
47
+ end
48
+
49
+
@@ -1,6 +1,6 @@
1
1
  module RSpec::RubyContentMatchers
2
- class HaveSubclass
3
- attr_reader :klass, :superclass, :type, :content
2
+ class HaveSubclass < RSpec::RubyContentMatcher
3
+ attr_reader :klass, :superclass, :type
4
4
 
5
5
  def initialize(klass, superclass, type=nil)
6
6
  @klass = klass.to_s.camelize
@@ -9,8 +9,8 @@ module RSpec::RubyContentMatchers
9
9
  end
10
10
 
11
11
  def matches?(content)
12
- @content = content
13
- match_res = (content =~ /class\s+#{klass}#{type}\s+<\s+#{superclass}(.*)end$/m)
12
+ super
13
+ match_res = (content =~ /class\s+#{klass}#{type}\s+<\s+#{superclass}(.*)#{end_expr}/m)
14
14
  if block_given? && $1
15
15
  ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
16
16
  yield ruby_content
@@ -20,25 +20,37 @@ module RSpec::RubyContentMatchers
20
20
  end
21
21
 
22
22
  def failure_message
23
- puts "Content: #{content}" if RSpec::Generator.debug?
23
+ super
24
24
  "Expected there to be the subclass #{klass} of #{superclass}"
25
25
  end
26
26
 
27
27
  def negative_failure_message
28
- puts "Content: #{content}" if RSpec::Generator.debug?
28
+ super
29
29
  "Did no expected there to be the subclass #{klass} of #{superclass}"
30
+ end
31
+
32
+ protected
33
+
34
+ def end_expr
35
+ "(end$|# #{klass})"
30
36
  end
37
+
31
38
  end
32
39
 
33
- def have_migration(klass, superclass, type=nil)
40
+ def have_subclass(klass, superclass, type=nil)
34
41
  HaveSubclass.new(klass, superclass, type)
35
42
  end
43
+ alias_method :be_subclass, :have_subclass
36
44
 
37
45
  def have_observer_class(klass)
38
46
  HaveSubclass.new(klass, 'ActiveRecord::Observer', :observer)
39
47
  end
48
+ alias_method :be_observer, :have_observer_class
49
+ alias_method :be_observer_class, :have_observer_class
50
+
40
51
 
41
52
  def have_migration(klass)
42
53
  HaveSubclass.new(klass, 'ActiveRecord::Migration')
43
54
  end
55
+ alias_method :be_migration, :have_migration
44
56
  end
@@ -1,13 +1,13 @@
1
1
  module RSpec::RubyContentMatchers
2
- class IncludeModule
3
- attr_reader :module_name, :content
2
+ class IncludeModule < RSpec::RubyContentMatcher
3
+ attr_reader :module_name
4
4
 
5
5
  def initialize module_name
6
6
  @module_name = module_name.to_s.camelize
7
7
  end
8
8
 
9
9
  def matches?(content)
10
- @content = content
10
+ super
11
11
  match_res = (content =~ /include\s+#{module_name}/)
12
12
  if block_given? && match_res
13
13
  ruby_content = content.extend(RSpec::RubyContent::Helpers)
@@ -18,12 +18,12 @@ module RSpec::RubyContentMatchers
18
18
  end
19
19
 
20
20
  def failure_message
21
- puts "Content: #{content}" if RSpec::Generator.debug?
21
+ super
22
22
  "Expected there to be an inclusion of module #{module_name}"
23
23
  end
24
24
 
25
25
  def negative_failure_message
26
- puts "Content: #{content}" if RSpec::Generator.debug?
26
+ super
27
27
  "Did not expect there to be an inclusion of module #{module_name}"
28
28
  end
29
29
 
@@ -1,12 +1,13 @@
1
1
  module RSpec::RubyContentMatchers
2
- class InheritFrom
2
+ class InheritFrom < RSpec::RubyContentMatcher
3
3
  attr_reader :klass
4
4
 
5
5
  def initialize klass
6
6
  @klass = klass.to_s.camelize
7
7
  end
8
8
 
9
- def matches?(content)
9
+ def matches?(content)
10
+ super
10
11
  match_res =~ /class\s+(.*?)<\s+#{klass}(.*)end/
11
12
  if block_given? && match_res
12
13
  ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
@@ -17,17 +18,20 @@ module RSpec::RubyContentMatchers
17
18
  end
18
19
 
19
20
  def failure_message
21
+ super
20
22
  "Expected the class to inherit from #{klass}"
21
23
  end
22
24
 
23
25
  def negative_failure_message
26
+ super
24
27
  "Did not expect the class to inherit from #{klass}"
25
28
  end
26
29
  end
27
30
 
28
31
  def inherit_from(klass)
29
32
  InheritFrom.new(klass)
30
- end
33
+ end
34
+ alias_method :be_subclass_of, :inherit_from
31
35
 
32
36
  def be_active_record
33
37
  InheritFrom.new('ActiveRecord::Base')
@@ -68,6 +68,6 @@ module RSpec::FileMatchers
68
68
  def generate_dir(relative, type = nil)
69
69
  GenerateDir.new(relative, type)
70
70
  end
71
-
71
+ alias_method :have_dir, :generate_dir
72
72
  end
73
73
 
@@ -80,26 +80,32 @@ module RSpec::FileMatchers
80
80
  def generate_file(relative, type = nil)
81
81
  GenerateFile.new(relative, type)
82
82
  end
83
+ alias_method :have_file, :generate_file
83
84
 
84
85
  def generate_model(relative)
85
86
  GenerateFile.new(relative, :model)
86
87
  end
88
+ alias_method :have_model, :generate_model
87
89
 
88
90
  def generate_controller(relative)
89
91
  puts "Generate controller: #{relative}"
90
92
  GenerateFile.new(relative, :controller)
91
93
  end
94
+ alias_method :have_controller, :generate_controller
92
95
 
93
96
  def generate_helper(relative)
94
97
  GenerateFile.new(relative, :helper)
95
98
  end
99
+ alias_method :have_helper, :generate_helper
96
100
 
97
101
  def generate_view(relative, action, ext)
98
102
  GenerateFile.new("#{relative}/#{action}.#{ext}", :view)
99
103
  end
104
+ alias_method :have_view, :generate_view
100
105
 
101
106
  def generate_observer(relative)
102
107
  GenerateFile.new(relative, :observer)
103
108
  end
109
+ alias_method :have_observer, :generate_observer
104
110
  end
105
111
 
@@ -53,6 +53,6 @@ module RSpec
53
53
  def generate_migration(name)
54
54
  GenerateMigration.new(name)
55
55
  end
56
-
56
+ alias_method :have_migration, :generate_migration
57
57
  end
58
58
  end
@@ -1,7 +1,7 @@
1
1
  module RSpec::Generator
2
2
  module Helpers
3
3
  def check_migration(file_name, &block)
4
- self.should generate_file(file_name, :migration) do |content|
4
+ self.should generate_migration file_name do |content|
5
5
  yield content if block
6
6
  end
7
7
  end
@@ -0,0 +1,159 @@
1
+ module RSpec::Rails
2
+ module App
3
+ def root_dir
4
+ ::Rails.root
5
+ end
6
+
7
+ def app_dir
8
+ File.join(root_dir, 'app')
9
+ end
10
+
11
+ def config_dir
12
+ File.join(root_dir, 'config')
13
+ end
14
+
15
+ def db_dir
16
+ File.join(root_dir, 'db')
17
+ end
18
+
19
+ def locale_dir
20
+ File.join(root_dir, 'public')
21
+ end
22
+
23
+ def model_dir
24
+ File.join(app_dir, 'models')
25
+ end
26
+
27
+ def observer_dir
28
+ model_dir
29
+ end
30
+
31
+ def controller_dir
32
+ File.join(app_dir, 'controllers')
33
+ end
34
+
35
+ def mailer_dir
36
+ File.join(app_dir, 'mailers')
37
+ end
38
+
39
+ def migration_dir
40
+ File.join(db_dir, 'migrations')
41
+ end
42
+
43
+ def helper_dir
44
+ File.join(app_dir, 'helpers')
45
+ end
46
+
47
+ def view_dir
48
+ File.join(app_dir, 'views')
49
+ end
50
+
51
+ def initializer_dir
52
+ File.join(config_dir, 'initializers')
53
+ end
54
+
55
+ def migration_dir
56
+ File.join(db_dir, 'migrations')
57
+ end
58
+
59
+ def locale_dir
60
+ File.join(config_dir, 'locales')
61
+ end
62
+
63
+ def stylesheet_dir
64
+ File.join(public_dir, 'stylesheets')
65
+ end
66
+
67
+ def javascript_dir
68
+ File.join(public_dir, 'javascripts')
69
+ end
70
+
71
+ def all_files
72
+ rails_files root_dir
73
+ end
74
+
75
+ def app_files
76
+ rails_app_files
77
+ end
78
+
79
+ def model_files
80
+ rails_app_files :models
81
+ end
82
+
83
+ def controller_files
84
+ rails_app_files :controllers
85
+ end
86
+
87
+ def mailer_files
88
+ rails_app_files :mailers
89
+ end
90
+
91
+ def helper_files
92
+ rails_app_files :helpers
93
+ end
94
+
95
+ def observer_files
96
+ rails_app_files :models, '**/*_observer.rb'
97
+ end
98
+
99
+ def view_files
100
+ rails_app_files :views
101
+ end
102
+
103
+ def view_files_html
104
+ rails_app_files :views, '**/*.html*'
105
+ end
106
+
107
+ def view_files_haml
108
+ rails_app_files :views, '**/*.haml*'
109
+ end
110
+
111
+ def initializer_files
112
+ rails_files initializer_dir
113
+ end
114
+
115
+ def db_files
116
+ rails_files db_dir
117
+ end
118
+
119
+ def migration_files
120
+ rails_files migration_dir
121
+ end
122
+
123
+ def locale_files
124
+ rails_files locale_dir, '**/*.yml'
125
+ end
126
+
127
+ def javascript_files
128
+ rails_files javascript_dir, '**/*.js'
129
+ end
130
+
131
+ def css_files
132
+ rails_files stylesheet_dir, '**/*.css'
133
+ end
134
+
135
+ def sass_files
136
+ rails_files stylesheet_dir, '**/*.sass'
137
+ end
138
+
139
+ protected
140
+
141
+ RB_FILES = '**/*.rb'
142
+
143
+ def rails_files dir, type=nil, pattern=nil
144
+ path = case type
145
+ when nil
146
+ "#{dir}/#{RB_FILES}"
147
+ when String
148
+ "#{dir}/#{type}"
149
+ when Symbol
150
+ pattern ? "#{dir}/#{type}/#{pattern}" : "#{dir}/#{type}"
151
+ end
152
+ FileList[path]
153
+ end
154
+
155
+ def rails_app_files type=nil, pattern='**/*.rb'
156
+ rails_files :app, type, pattern
157
+ end
158
+ end
159
+ end