generator-spec 0.5.0 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. data/.document +0 -0
  2. data/.gitignore +0 -0
  3. data/.rspec +0 -0
  4. data/LICENSE +0 -0
  5. data/README.markdown +19 -13
  6. data/Rakefile +14 -35
  7. data/VERSION +1 -1
  8. data/generator-spec.gemspec +63 -148
  9. data/lib/generator-spec.rb +12 -0
  10. data/lib/generator-spec/namespaces.rb +7 -0
  11. data/lib/{generator_spec/helpers/configure_rails.rb → generator-spec/rails/configure.rb} +3 -3
  12. data/lib/generator-spec/rspec.rb +1 -0
  13. data/lib/generator-spec/rspec/configure.rb +6 -0
  14. data/lib/{generator_spec/rspec_generator/generator_util.rb → generator-spec/rspec/generator.rb} +6 -6
  15. data/lib/{generator_spec/rspec_generator → generator-spec/rspec/generator}/configure.rb +0 -0
  16. data/lib/{generator_spec/require_generator.rb → generator-spec/rspec/generator/require.rb} +0 -0
  17. data/lib/{generator_spec/rspec_generator → generator-spec/rspec/generator}/routes.rb +0 -0
  18. data/lib/{generator_spec/rspec_generator → generator-spec/rspec/generator}/test_case.rb +0 -0
  19. data/lib/generator-spec/rspec/macro.rb +18 -0
  20. data/lib/generator-spec/rspec/matchers/generate_artifact.rb +34 -0
  21. data/lib/generator-spec/rspec/matchers/generate_artifacts.rb +33 -0
  22. data/lib/generators/controller/controller_generator.rb +15 -21
  23. data/lib/generators/helper/helper_generator.rb +13 -29
  24. data/lib/generators/migration/migration_generator.rb +6 -7
  25. data/lib/generators/migration/templates/create_users.erb +0 -0
  26. data/lib/generators/model/model_generator.rb +36 -55
  27. data/lib/generators/observer/observer_generator.rb +2 -0
  28. data/lib/generators/view/view_generator.rb +2 -0
  29. data/spec/generator_spec/generators/controller_gen_spec.rb +40 -0
  30. data/spec/generator_spec/generators/{helper_generator_spec.rb → helper_gen_spec.rb} +4 -8
  31. data/spec/generator_spec/generators/{migration_generator_spec.rb → migration_gen_spec.rb} +3 -8
  32. data/spec/generator_spec/generators/model_gen_spec.rb +36 -0
  33. data/spec/generator_spec/generators/{observer_generator_spec.rb → observer_gen_spec.rb} +12 -21
  34. data/spec/generator_spec/generators/{view_generator_spec.rb → view_gen_spec.rb} +9 -20
  35. data/spec/generator_spec/matchers/generate_artifact_spec.rb +36 -0
  36. data/spec/generator_spec/matchers/generate_artifacts_spec.rb +35 -0
  37. data/spec/spec_helper.rb +5 -6
  38. data/wiki/Custom Rails 3 Generators.textile b/data/wiki/Custom Rails 3 → Generators.textile +0 -0
  39. data/wiki/content_matchers.textile +0 -0
  40. data/wiki/file_matchers.textile +0 -0
  41. data/wiki/migration_matchers.textile +0 -0
  42. data/wiki/rails_helpers.textile +0 -0
  43. metadata +117 -154
  44. data/lib/generator_spec.rb +0 -12
  45. data/lib/generator_spec/extensions/core_ext.rb +0 -47
  46. data/lib/generator_spec/helpers/generator_helper.rb +0 -11
  47. data/lib/generator_spec/helpers/migration_helper.rb +0 -69
  48. data/lib/generator_spec/main.rb +0 -77
  49. data/lib/generator_spec/matchers/content/content_matcher.rb +0 -110
  50. data/lib/generator_spec/matchers/content/have_block.rb +0 -57
  51. data/lib/generator_spec/matchers/content/have_call.rb +0 -59
  52. data/lib/generator_spec/matchers/content/have_calls.rb +0 -60
  53. data/lib/generator_spec/matchers/content/have_class_self.rb +0 -40
  54. data/lib/generator_spec/matchers/content/have_comment.rb +0 -42
  55. data/lib/generator_spec/matchers/content/have_method.rb +0 -66
  56. data/lib/generator_spec/matchers/content/have_module.rb +0 -66
  57. data/lib/generator_spec/matchers/content/have_region.rb +0 -48
  58. data/lib/generator_spec/matchers/content/have_subclass.rb +0 -50
  59. data/lib/generator_spec/matchers/content/include_module.rb +0 -51
  60. data/lib/generator_spec/matchers/content/inherit_from.rb +0 -40
  61. data/lib/generator_spec/matchers/file/generate_directory.rb +0 -73
  62. data/lib/generator_spec/matchers/file/generate_file.rb +0 -103
  63. data/lib/generator_spec/matchers/file/generate_migration.rb +0 -54
  64. data/lib/generator_spec/matchers/file/have_file.rb +0 -115
  65. data/lib/generator_spec/matchers/helpers/content.rb +0 -18
  66. data/lib/generator_spec/matchers/helpers/file.rb +0 -26
  67. data/lib/generator_spec/matchers/helpers/migration.rb +0 -89
  68. data/lib/generator_spec/matchers/migration/have_column.rb +0 -29
  69. data/lib/generator_spec/matchers/migration/have_index.rb +0 -9
  70. data/lib/generator_spec/matchers/migration/have_table.rb +0 -18
  71. data/lib/generator_spec/matchers/migration/have_tbl_column.rb +0 -25
  72. data/lib/generator_spec/matchers/migration/have_up_down.rb +0 -15
  73. data/lib/generator_spec/rails_helpers/base_helper.rb +0 -171
  74. data/lib/generator_spec/rails_helpers/rails_app.rb +0 -166
  75. data/lib/generator_spec/rails_helpers/rails_controller.rb +0 -44
  76. data/lib/generator_spec/rails_helpers/rails_helper.rb +0 -40
  77. data/lib/generator_spec/rails_helpers/rails_mailer.rb +0 -39
  78. data/lib/generator_spec/rails_helpers/rails_migration.rb +0 -51
  79. data/lib/generator_spec/rails_helpers/rails_model.rb +0 -40
  80. data/lib/generator_spec/rails_helpers/rails_observer.rb +0 -39
  81. data/lib/generator_spec/rails_helpers/rails_orm.rb +0 -114
  82. data/lib/generator_spec/rails_helpers/rails_view.rb +0 -65
  83. data/rspec_for_generators.gemspec +0 -96
  84. data/spec/generator_spec/generators/controller_generator_spec.rb +0 -47
  85. data/spec/generator_spec/generators/model_generator_spec.rb +0 -41
  86. data/spec/generator_spec/matchers/content/class_self_spec.rb +0 -30
  87. data/spec/generator_spec/matchers/content/have_block_spec.rb +0 -57
  88. data/spec/generator_spec/matchers/content/have_call_spec.rb +0 -55
  89. data/spec/generator_spec/matchers/content/have_calls_spec.rb +0 -47
  90. data/spec/generator_spec/matchers/content/have_class_spec.rb +0 -34
  91. data/spec/generator_spec/matchers/content/have_method_spec.rb +0 -52
  92. data/spec/generator_spec/matchers/content/have_module_spec.rb +0 -34
  93. data/spec/generator_spec/matchers/content/have_region_spec.rb +0 -56
  94. data/spec/generator_spec/matchers/content/have_subclass_spec.rb +0 -34
  95. data/spec/generator_spec/matchers/content/include_module_spec.rb +0 -36
  96. data/spec/generator_spec/matchers/content/inherit_from_spec.rb +0 -34
  97. data/spec/generator_spec/matchers/file/directory_spec.rb +0 -0
  98. data/spec/generator_spec/matchers/file/file_spec.rb +0 -0
  99. data/spec/generator_spec/matchers/file/migration_spec.rb +0 -0
  100. data/spec/generator_spec/matchers/migration/have_column_spec.rb +0 -0
  101. data/spec/generator_spec/matchers/migration/have_index_spec.rb +0 -0
  102. data/spec/generator_spec/matchers/migration/have_table_spec.rb +0 -0
  103. data/spec/generator_spec/matchers/migration/have_tbl_column_spec.rb +0 -0
  104. data/spec/generator_spec/matchers/migration/have_up_down_spec.rb +0 -0
  105. data/spec/generator_spec/matchers/rails/controller_matcher_spec.rb +0 -27
  106. data/spec/generator_spec/matchers/rails/helper_matcher_spec.rb +0 -26
  107. data/spec/generator_spec/matchers/rails/mailer_matcher_spec.rb +0 -24
  108. data/spec/generator_spec/matchers/rails/model_matcher_spec.rb +0 -31
  109. data/spec/generator_spec/matchers/rails/observer_matcher_spec.rb +0 -19
  110. data/spec/generator_spec/matchers/rails/view_matcher_spec.rb +0 -23
  111. data/spec/generator_spec/rails_helpers/rails_app_spec.rb +0 -0
  112. data/spec/generator_spec/rails_helpers/rails_controller_spec.rb +0 -33
  113. data/spec/generator_spec/rails_helpers/rails_helper_spec.rb +0 -33
  114. data/spec/generator_spec/rails_helpers/rails_mailer_spec.rb +0 -33
  115. data/spec/generator_spec/rails_helpers/rails_migration_spec.rb +0 -0
  116. data/spec/generator_spec/rails_helpers/rails_model_spec.rb +0 -31
  117. data/spec/generator_spec/rails_helpers/rails_observer_spec.rb +0 -33
  118. data/spec/generator_spec/rails_helpers/rails_orm_spec.rb +0 -0
  119. data/spec/generator_spec/rails_helpers/rails_view_spec.rb +0 -31
@@ -1,29 +0,0 @@
1
- module RSpec::RubyContentMatchers
2
- def have_remove_column(name)
3
- HaveCall.new('t.remove', ":#{name}")
4
- end
5
-
6
- def have_add_column(name, type='string')
7
- HaveCall.new("t.#{type}", ":#{name}")
8
- end
9
- alias_method :have_column, :have_add_column
10
-
11
- def have_columns(columns = {})
12
- raise ArgumentError, "Columns must be passed as a :name => :type Hash" if !columns.kind_of? Hash
13
- col_hash = {}
14
- columns.each_pair do |name, type|
15
- method_call = "t.#{type}"
16
- arg = ":#{name}"
17
- col_hash[method_call] = arg
18
- end
19
- HaveCalls.new col_hash
20
- end
21
-
22
- def have_change_column(name, type='string')
23
- HaveCall.new("t.change", ":#{name}\s*,\s*:#{type}")
24
- end
25
-
26
- def have_timestamps
27
- HaveCall.new("t.timestamps")
28
- end
29
- end
@@ -1,9 +0,0 @@
1
- module RSpec::RubyContentMatchers
2
- def have_add_tbl_index(tbl_name, index_name)
3
- HaveCall.new('add_index', ":#{tbl_name}\s*,\s*:#{index_name}")
4
- end
5
-
6
- def have_remove_index(name)
7
- HaveCall.new('remove_index', ":#{tbl_name}\s*,\s*:#{index_name}")
8
- end
9
- end
@@ -1,18 +0,0 @@
1
- module RSpec::RubyContentMatchers
2
- def have_create_table(name)
3
- HaveBlock.new('create_table', :args => ":#{name}")
4
- end
5
-
6
- def have_change_table(name)
7
- HaveBlock.new('change_table', :args => ":#{name}")
8
- end
9
-
10
- def have_drop_table(name)
11
- HaveCall.new('drop_table', ":#{name}")
12
- end
13
-
14
- def have_rename_table(name)
15
- HaveCall.new('rename_table', ":#{name}")
16
- end
17
- end
18
-
@@ -1,25 +0,0 @@
1
- module RSpec::RubyContentMatchers
2
- def have_remove_tbl_column(tbl_name, col_name)
3
- HaveCall.new('remove_column', ":#{tbl_name}\s*,\s*:#{col_name}")
4
- end
5
-
6
- def have_rename_tbl_column(tbl_name, new_name)
7
- HaveCall.new('rename_column', ":#{tbl_name}\s*,\s*:#{new_name}")
8
- end
9
-
10
- def have_add_tbl_column(tbl_name, col_name, type = 'string')
11
- HaveCall.new('add_column', ":#{tbl_name}\s*,\s*:#{col_name}\s*,\s*:#{type}")
12
- end
13
-
14
- def have_change_tbl_column tbl_name, col_name, type='string'
15
- HaveCall.new('change_column', ":#{tbl_name}\s*,\s*:#{col_name}\s*,\s*:#{type}")
16
- end
17
-
18
- def have_add_column(name, type='string')
19
- HaveCall.new("t.#{type}", ":#{name}")
20
- end
21
-
22
- def have_change_column(name, type='string')
23
- HaveCall.new("t.change", ":#{name}\s*,\s*:#{type}")
24
- end
25
- end
@@ -1,15 +0,0 @@
1
- module RSpec::RubyContentMatchers
2
- def have_up &block
3
- HaveMethod.new 'up', :class, &block
4
- end
5
- alias_method :have_up_method, :have_up
6
-
7
- def have_down &block
8
- HaveMethod.new 'down', :class, &block
9
- end
10
- alias_method :have_down_method, :have_down
11
- end
12
-
13
-
14
-
15
-
@@ -1,171 +0,0 @@
1
- module RSpec::Rails
2
- module BaseHelper
3
- def self.included(base)
4
- base.extend ClassMethods
5
- base.class_eval do
6
- include RSpec::Rails::App
7
- include Thor::Actions
8
- end
9
- end
10
-
11
- module ClassMethods
12
- def aliases_for name
13
- multi_alias name,
14
- :create => :new, :insert_into => [:inject_into, :update], :read => :X_content, :remove => :delete,
15
- :options => :after
16
- end
17
- end
18
-
19
- def type_postfix type
20
- "_#{type}" if ![:model].include?(type)
21
- end
22
-
23
- def artifact_path name, type, dir=nil
24
- dir ||= send :"#{type}_dir"
25
- File.join(dir, "#{name}#{type_postfix type}.rb")
26
- end
27
-
28
- # CREATE
29
- def create_artifact name, options={}, &block
30
- type = get_type options
31
-
32
- file = file_name name, type
33
- return if File.exist?(file)
34
-
35
- debug "create #{type}: #{name}"
36
- debug "file: #{file}"
37
-
38
- # make dir
39
- dir = File.dirname(file)
40
- FileUtils.mkdir_p dir if !File.directory?(dir)
41
-
42
- content = options[:content]
43
- content ||= yield if block
44
-
45
- content_method = :"new_#{type}_content"
46
- debug "inner content: #{content}, to inject using ##{content_method}"
47
-
48
- raise "Content method #{content_method} not found #{orm_notify}" if !respond_to?(content_method)
49
- insert_content = send content_method, name, content
50
-
51
- debug "content to insert: #{insert_content}"
52
-
53
- return if insert_content.blank?
54
-
55
- File.open(file, 'w') do |f|
56
- f.puts insert_content
57
- end
58
- debug "file created and content inserted"
59
- end
60
-
61
- # READ
62
- def read_artifact(name, options, &block)
63
- type = get_type options
64
- file_name = existing_file_name(name, type)
65
- debug "reading from: #{file_name}"
66
- content = File.new(file_name).try :read
67
- debug "read content: #{content}"
68
- yield content if block
69
- content
70
- end
71
-
72
- # UPDATE
73
- def insert_content(name, options={}, &block)
74
- type = get_type options
75
- file = existing_file_name(name, type)
76
- file_insertion file, marker(name, type, options), options, &block
77
- end
78
-
79
- # DELETE
80
- def remove_artifact name, type
81
- debug "remove artifact: #{name}"
82
- file = file_name name, type
83
- debug "removed" if File.exist?(file) && FileUtils.rm_f(file)
84
- end
85
-
86
- def remove_artifacts type,*names
87
- names.flatten.each{|name| send :"remove_#{type}", name }
88
- end
89
-
90
- def new_artifact_content name, type, content=nil, &block
91
- content ||= yield if block
92
- %Q{class #{marker(name, type)}
93
- #{content}
94
- end}
95
- end
96
-
97
- protected
98
-
99
- def get_type options = {}
100
- raise ArgumentError, "No artifact type specified" if !options[:type]
101
- options[:type]
102
- end
103
-
104
- def debug?
105
- RSpec::Generator.debug?
106
- end
107
-
108
- def debug msg
109
- puts msg if debug?
110
- end
111
-
112
- def set options, type
113
- options.merge!(:type => type)
114
- options
115
- end
116
-
117
- def orm_notify
118
- ''
119
- end
120
-
121
- def marker name, type, options={}
122
- send :"#{type}_marker", name, options
123
- end
124
-
125
- def file_name name, type, options={}
126
- send :"#{type}_file_name", name, options
127
- end
128
-
129
- def existing_file_name name, type
130
- # first try finder method
131
- finder_method = :"find_#{type}"
132
- found = send finder_method, name if respond_to? finder_method
133
- # default
134
- file_name(name, type) if !found
135
- end
136
-
137
- def file_insertion(file_name, marker, options={}, &block)
138
- debug "file insertion: marker = #{marker}"
139
- return if !marker
140
-
141
- file = File.new(file_name)
142
- return if !File.exist?(file)
143
-
144
- insert_content = options[:content] || (yield if block)
145
-
146
- # already inserted?
147
- return if insert_content.blank? || (file.read =~ /#{insert_content}/)
148
-
149
- place = options[:before] ? :before : :after
150
-
151
- debug "inserted #{place}: '#{marker}'"
152
- debug "content: #{insert_content}"
153
-
154
- mutate_file file.path, marker, place do
155
- insert_content
156
- end
157
- end
158
-
159
- def mutate_file file, marker, place, &block
160
- raise ArgumentError, "You must define a replacement marker for a :before or :after key" if !marker
161
- replace_in_file file, /(#{Regexp.escape(marker)})/mi do |match|
162
- place == :after ? "#{match}#{yield}" : "#{yield}#{match}"
163
- end
164
- end
165
-
166
- def replace_in_file(path, regexp, *args, &block)
167
- content = File.read(path).gsub(regexp, *args, &block)
168
- File.open(path, 'wb') { |file| file.write(content) }
169
- end
170
- end
171
- end
@@ -1,166 +0,0 @@
1
- module RSpec::Rails
2
- module App
3
- def create_empty_tmp *args
4
- args.flatten.each do |arg|
5
- dir = send :"#{arg}_dir"
6
- FileUtils.mkdir_p dir
7
- end
8
- end
9
-
10
- def root_dir
11
- ::Rails.root
12
- end
13
-
14
- def app_dir
15
- File.join(root_dir, 'app')
16
- end
17
-
18
- def config_dir
19
- File.join(root_dir, 'config')
20
- end
21
-
22
- def db_dir
23
- File.join(root_dir, 'db')
24
- end
25
-
26
- def locale_dir
27
- File.join(root_dir, 'public')
28
- end
29
-
30
- def model_dir
31
- File.join(app_dir, 'models')
32
- end
33
-
34
- def observer_dir
35
- model_dir
36
- end
37
-
38
- def controller_dir
39
- File.join(app_dir, 'controllers')
40
- end
41
-
42
- def mailer_dir
43
- File.join(app_dir, 'mailers')
44
- end
45
-
46
- def migration_dir
47
- File.join(db_dir, 'migrations')
48
- end
49
-
50
- def helper_dir
51
- File.join(app_dir, 'helpers')
52
- end
53
-
54
- def view_dir
55
- File.join(app_dir, 'views')
56
- end
57
-
58
- def initializer_dir
59
- File.join(config_dir, 'initializers')
60
- end
61
-
62
- def migration_dir
63
- File.join(db_dir, 'migrations')
64
- end
65
-
66
- def locale_dir
67
- File.join(config_dir, 'locales')
68
- end
69
-
70
- def stylesheet_dir
71
- File.join(public_dir, 'stylesheets')
72
- end
73
-
74
- def javascript_dir
75
- File.join(public_dir, 'javascripts')
76
- end
77
-
78
- def all_files
79
- rails_files root_dir
80
- end
81
-
82
- def app_files
83
- rails_app_files
84
- end
85
-
86
- def model_files
87
- rails_app_files :models
88
- end
89
-
90
- def controller_files
91
- rails_app_files :controllers
92
- end
93
-
94
- def mailer_files
95
- rails_app_files :mailers, '**/*_mailer.rb'
96
- end
97
-
98
- def helper_files
99
- rails_app_files :helpers
100
- end
101
-
102
- def observer_files
103
- rails_app_files :models, '**/*_observer.rb'
104
- end
105
-
106
- def view_files
107
- rails_app_files :views
108
- end
109
-
110
- def view_files_html
111
- rails_app_files :views, '**/*.html*'
112
- end
113
-
114
- def view_files_haml
115
- rails_app_files :views, '**/*.haml*'
116
- end
117
-
118
- def initializer_files
119
- rails_files initializer_dir
120
- end
121
-
122
- def db_files
123
- rails_files db_dir
124
- end
125
-
126
- def migration_files
127
- rails_files migration_dir
128
- end
129
-
130
- def locale_files
131
- rails_files locale_dir, '**/*.yml'
132
- end
133
-
134
- def javascript_files
135
- rails_files javascript_dir, '**/*.js'
136
- end
137
-
138
- def css_files
139
- rails_files stylesheet_dir, '**/*.css'
140
- end
141
-
142
- def sass_files
143
- rails_files stylesheet_dir, '**/*.sass'
144
- end
145
-
146
- protected
147
-
148
- RB_FILES = '**/*.rb'
149
-
150
- def rails_files dir, type=nil, pattern=nil
151
- path = case type
152
- when nil
153
- "#{dir}/#{RB_FILES}"
154
- when String
155
- "#{dir}/#{type}"
156
- when Symbol
157
- pattern ? "#{dir}/#{type}/#{pattern}" : "#{dir}/#{type}"
158
- end
159
- FileList[path]
160
- end
161
-
162
- def rails_app_files type=nil, pattern='**/*.rb'
163
- rails_files :app, type, pattern
164
- end
165
- end
166
- end
@@ -1,44 +0,0 @@
1
- module RSpec::Rails
2
- module Controller
3
- include RSpec::Rails::BaseHelper
4
-
5
- def controller_file_name name, options=nil
6
- artifact_path name, :controller
7
- end
8
-
9
- # CREATE
10
- def create_controller name, options={}, &block
11
- create_artifact(name, set(options, :controller), &block)
12
- end
13
-
14
- # UPDATE
15
- def insert_into_controller(name, options={}, &block)
16
- insert_content(name, set(options, :controller), &block)
17
- end
18
-
19
- # READ
20
- def read_controller(name, options={}, &block)
21
- read_artifact(name, set(options, :controller), &block)
22
- end
23
-
24
- # DELETE
25
- def remove_controller name
26
- remove_artifact name, :controller
27
- end
28
-
29
- def remove_controllers *names
30
- remove_artifacts :controller, *names
31
- end
32
-
33
-
34
- def controller_marker name, options=nil
35
- "#{name.to_s.camelize}Controller < ActionController::Base"
36
- end
37
-
38
- def new_controller_content name, content=nil, &block
39
- new_artifact_content name, :controller, content, &block
40
- end
41
-
42
- aliases_for :controller
43
- end
44
- end