generator-spec 0.4.5 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/README.markdown +58 -46
  2. data/VERSION +1 -1
  3. data/generator-spec.gemspec +83 -14
  4. data/lib/generator_spec/helpers/migration_helper.rb +2 -2
  5. data/lib/generator_spec/main.rb +5 -0
  6. data/lib/generator_spec/matchers/content/content_matcher.rb +84 -4
  7. data/lib/generator_spec/matchers/content/have_block.rb +24 -22
  8. data/lib/generator_spec/matchers/content/have_call.rb +21 -13
  9. data/lib/generator_spec/matchers/content/have_calls.rb +32 -26
  10. data/lib/generator_spec/matchers/content/have_class_self.rb +40 -0
  11. data/lib/generator_spec/matchers/content/have_method.rb +31 -24
  12. data/lib/generator_spec/matchers/content/have_module.rb +49 -19
  13. data/lib/generator_spec/matchers/content/have_region.rb +9 -10
  14. data/lib/generator_spec/matchers/content/have_subclass.rb +9 -15
  15. data/lib/generator_spec/matchers/content/include_module.rb +9 -10
  16. data/lib/generator_spec/matchers/content/inherit_from.rb +10 -10
  17. data/lib/generator_spec/matchers/file/generate_file.rb +4 -11
  18. data/lib/generator_spec/matchers/file/generate_migration.rb +1 -5
  19. data/lib/generator_spec/matchers/file/have_file.rb +115 -0
  20. data/lib/generator_spec/rails_helpers/rails_controller.rb +30 -32
  21. data/lib/generator_spec/rails_helpers/rails_model.rb +2 -1
  22. data/lib/generator_spec/rails_helpers/rails_view.rb +19 -3
  23. data/spec/generator_spec/{controller_generator_spec.rb → generators/controller_generator_spec.rb} +0 -0
  24. data/spec/generator_spec/{helper_generator_spec.rb → generators/helper_generator_spec.rb} +0 -0
  25. data/spec/generator_spec/{migration_generator_spec.rb → generators/migration_generator_spec.rb} +0 -0
  26. data/spec/generator_spec/{model_generator_spec.rb → generators/model_generator_spec.rb} +0 -0
  27. data/spec/generator_spec/{observer_generator_spec.rb → generators/observer_generator_spec.rb} +0 -0
  28. data/spec/generator_spec/{view_generator_spec.rb → generators/view_generator_spec.rb} +0 -0
  29. data/spec/generator_spec/matchers/content/class_self_spec.rb +32 -0
  30. data/spec/generator_spec/matchers/content/have_block_spec.rb +57 -0
  31. data/spec/generator_spec/matchers/content/have_call_spec.rb +39 -0
  32. data/spec/generator_spec/matchers/content/have_calls_spec.rb +50 -0
  33. data/spec/generator_spec/matchers/content/have_class_spec.rb +34 -0
  34. data/spec/generator_spec/matchers/content/have_method_spec.rb +52 -0
  35. data/spec/generator_spec/matchers/content/have_module_spec.rb +34 -0
  36. data/spec/generator_spec/matchers/content/have_region_spec.rb +56 -0
  37. data/spec/generator_spec/matchers/content/have_subclass_spec.rb +34 -0
  38. data/spec/generator_spec/matchers/content/include_module_spec.rb +36 -0
  39. data/spec/generator_spec/matchers/content/inherit_from_spec.rb +34 -0
  40. data/spec/generator_spec/matchers/file/directory_spec.rb +0 -0
  41. data/spec/generator_spec/matchers/file/file_spec.rb +0 -0
  42. data/spec/generator_spec/matchers/file/migration_spec.rb +0 -0
  43. data/spec/generator_spec/matchers/migration/have_column_spec.rb +0 -0
  44. data/spec/generator_spec/matchers/migration/have_index_spec.rb +0 -0
  45. data/spec/generator_spec/matchers/migration/have_table_spec.rb +0 -0
  46. data/spec/generator_spec/matchers/migration/have_tbl_column_spec.rb +0 -0
  47. data/spec/generator_spec/matchers/migration/have_up_down_spec.rb +0 -0
  48. data/spec/generator_spec/matchers/rails/controller_matcher_spec.rb +26 -0
  49. data/spec/generator_spec/matchers/rails/helper_matcher_spec.rb +26 -0
  50. data/spec/generator_spec/matchers/rails/mailer_matcher_spec.rb +24 -0
  51. data/spec/generator_spec/matchers/rails/model_matcher_spec.rb +31 -0
  52. data/spec/generator_spec/matchers/rails/observer_matcher_spec.rb +19 -0
  53. data/spec/generator_spec/matchers/rails/view_matcher_spec.rb +23 -0
  54. data/spec/generator_spec/rails_helpers/rails_app_spec.rb +0 -0
  55. data/spec/generator_spec/rails_helpers/rails_controller_spec.rb +0 -0
  56. data/spec/generator_spec/rails_helpers/rails_helper_spec.rb +0 -0
  57. data/spec/generator_spec/rails_helpers/rails_mailer_spec.rb +0 -0
  58. data/spec/generator_spec/rails_helpers/rails_migration_spec.rb +0 -0
  59. data/spec/generator_spec/rails_helpers/rails_model_spec.rb +0 -0
  60. data/spec/generator_spec/rails_helpers/rails_observer_spec.rb +0 -0
  61. data/spec/generator_spec/rails_helpers/rails_orm_spec.rb +0 -0
  62. data/spec/generator_spec/rails_helpers/rails_view_spec.rb +0 -0
  63. metadata +84 -15
  64. data/lib/generator_spec/matchers/content/have_class.rb +0 -53
data/README.markdown CHANGED
@@ -1,17 +1,22 @@
1
- # RSpec 2 library for specifying and testing generators
1
+ # RSpec 2 add-on for specifying and testing generators
2
2
 
3
- This project contains RSpec 2 matchers, helpers and various utilities to assist in writing Rails 3 generator specs.
3
+ This project contains RSpec 2 matchers, helpers and various utilities to assist in writing Generator specs.
4
+ There is additional support geared towards spec'ing Generators for Rails 3.
4
5
 
5
- Rails 3 has a Rails::Generators::TestCase class for use with Test-Unit, to help test generators. This TestCase contains specific custom assertion methods that can be used to assert generator behavior. To create an RSpec 2 equivalent, I wrapped Rails::Generators::TestCase for use with RSpec 2 and created some RSpec 2 matchers that mimic the assertion methods of the Test-Unit TestCase. I have also a bunch of "extra goodies" to the mix. The new intuitive DSL RSpec DSL for specifying/testing generators should make it very easy and intuitive to test your Rails 3 generators with RSpec 2 :)
6
+ ## Why?
6
7
 
7
- Please advice if you find any issues or have suggestions for improvements. The code is not as pretty as it could be, so feel free to refactor and improve this gem to your hearts desire!
8
+ Rails 3 has a Rails::Generators::TestCase class for use with Test-Unit, to help test generators. This TestCase contains specific custom assertion methods that can be used to assert generator behavior. To create an RSpec 2 equivalent, I wrapped Rails::Generators::TestCase for use with RSpec 2 and created some RSpec 2 matchers that mimic the assertion methods of the Test-Unit TestCase. I have also a bunch of "extra goodies" to the mix.
9
+
10
+ This RSpec DSL should make it very easy and enjoyable to spec and test your Generators with RSpec 2 :)
11
+
12
+ ## Feedback
13
+
14
+ Please let me know if you find any issues or have suggestions for improvements.
8
15
 
9
16
  ## Install
10
17
 
11
18
  <code>gem install generator-spec</code>
12
19
 
13
- ### Install from code
14
-
15
20
  The gem is a jewel based on [jeweler](http://github.com/technicalpickles/jeweler).
16
21
  To install the gem from the code, simply use the jeweler rake task:
17
22
 
@@ -19,7 +24,7 @@ To install the gem from the code, simply use the jeweler rake task:
19
24
 
20
25
  ## Usage
21
26
 
22
- The following demonstrates usage of this gem. There are many more options and DSL convenience methods.
27
+ The following demonstrates usage of this library. There are many more options and DSL convenience methods (see wiki, code or specs).
23
28
 
24
29
  ### Configuration
25
30
 
@@ -35,12 +40,12 @@ RSpec::Generator.configure do |config|
35
40
  config.debug = false
36
41
  config.remove_temp_dir = true
37
42
  config.default_rails_root(__FILE__)
43
+ config.logger = :stdout # :file to output to a log file, logger only active when debug is true
38
44
  end
39
45
 
40
46
  # or customize the location of the temporary Rails 3 app dir used
41
47
  RSpec::Generator.configure do |config|
42
- config.debug = false
43
- config.remove_temp_dir = true
48
+ # ...
44
49
  config.rails_root = '~/my/rails/folder'
45
50
  end
46
51
 
@@ -48,20 +53,21 @@ end
48
53
 
49
54
  ### Specs for generators
50
55
 
51
- I recommend having a separate spec for each generator called a 'generator spec'.
52
- You can use the special *require_generator* statement to ensure that one or more generators are loaded and made available for the spec.
56
+ I recommend having a separate spec file for each generator (generator specs).
57
+ You can use the special *require_generator* statement to ensure that one or more generators are loaded and made available for a given spec.
53
58
 
54
59
  <pre>require_generator :canable</pre>
55
60
 
56
61
  This will load the generator : `generators/canabale_generator.rb`
57
62
 
58
- If the generator is in a namespace, use a nested approach like this:
63
+ If the generator is in a namespace (subfolder of generators), use a nested approach like this:
59
64
 
60
65
  <pre>require_generators :canable => ['model', 'user']</pre>
61
66
 
62
67
  This will load the generators: `generators/canable/model_generator.rb` and `generators/canable/user_generator.rb`
63
68
 
64
- You can also load generators from multiple namespaces and mix and match like this
69
+ You can also load generators from multiple namespaces and mix and match like this.
70
+ I recommend against this however as it is difficult to read.
65
71
 
66
72
  <pre>require_generators [:canable => ['model', 'user'], :other => :side, :simple]</pre>
67
73
 
@@ -72,39 +78,6 @@ You can also require all generators or all within a specific namespace like this
72
78
  require_generators :all
73
79
  require_generators :canable => :all
74
80
 
75
- ## Using a shared generator loader file
76
-
77
- If you have multiple generators in a shared namespace, the following alternative approach can be used.
78
- Create a file that loads all the generators you are specifying in this folder, fx all generators in a specific namespace.
79
- Then include this 'generator load file' in each spec.
80
-
81
- This uses the 'require_all' gem functionality
82
-
83
- ## Example usage: require generators
84
-
85
- <pre># spec/generators/canable.rb
86
- require_generators :canable => ['model', 'user']</pre>
87
-
88
- <pre># spec/generators/canable/model_generator_spec.rb
89
- ...
90
- # load generators to spec
91
- require 'generators/canable'
92
-
93
- describe 'model_generator' do
94
- ...
95
- end
96
- </pre>
97
-
98
- <pre># spec/generators/canable/user_generator_spec.rb
99
- ...
100
- # load generators to spec
101
- require 'generators/canable'
102
-
103
- describe 'user_generator' do
104
- ...
105
- end
106
- </pre>
107
-
108
81
  ### Example: full generator spec
109
82
 
110
83
  <pre># spec/generators/model_generator_spec.rb
@@ -154,6 +127,45 @@ describe 'model_generator' do
154
127
  end
155
128
  end</pre>
156
129
 
130
+ ## Code specs
131
+
132
+ There are a bunch of specialized ruby code matchers in the matchers/content folder which can be used to spec code files in general.
133
+ Check out the specs in spec/generator_spec/matchers/content for examples on how to use these.
134
+
135
+ ## Rails specs
136
+
137
+ The rails_helpers folder contains a bunch of files which makes it easy to spec rails files and to perform various "Rails mutations".
138
+ These mutations make it easy to setup the temporary Rails app in a specific pre-condition, which is required for a given spec.
139
+
140
+ ### Examples: Rails helpers
141
+
142
+ describe 'controller' do
143
+ include RSpec::Rails::Controller
144
+
145
+ before :each do
146
+ create_controller :account do
147
+ %q{
148
+ def index
149
+ end
150
+ }
151
+ end
152
+ end
153
+
154
+ after :each do
155
+ remove_controller :account
156
+ end
157
+
158
+ it "should have an account_controller file that contains an AccountController class with an index method inside" do
159
+ Rails.application.should have_controller :account_controller do |controller_file|
160
+ controller_file.should have_controller_class :account_controller do |klass|
161
+ klass.should have_method :index
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+
168
+
157
169
  ## Note on Patches/Pull Requests
158
170
 
159
171
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.5
1
+ 0.4.7
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{generator-spec}
8
- s.version = "0.4.5"
8
+ s.version = "0.4.7"
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"]
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/generator_spec/matchers/content/have_block.rb",
36
36
  "lib/generator_spec/matchers/content/have_call.rb",
37
37
  "lib/generator_spec/matchers/content/have_calls.rb",
38
- "lib/generator_spec/matchers/content/have_class.rb",
38
+ "lib/generator_spec/matchers/content/have_class_self.rb",
39
39
  "lib/generator_spec/matchers/content/have_method.rb",
40
40
  "lib/generator_spec/matchers/content/have_module.rb",
41
41
  "lib/generator_spec/matchers/content/have_region.rb",
@@ -45,6 +45,7 @@ Gem::Specification.new do |s|
45
45
  "lib/generator_spec/matchers/file/generate_directory.rb",
46
46
  "lib/generator_spec/matchers/file/generate_file.rb",
47
47
  "lib/generator_spec/matchers/file/generate_migration.rb",
48
+ "lib/generator_spec/matchers/file/have_file.rb",
48
49
  "lib/generator_spec/matchers/helpers/content.rb",
49
50
  "lib/generator_spec/matchers/helpers/file.rb",
50
51
  "lib/generator_spec/matchers/helpers/migration.rb",
@@ -75,12 +76,46 @@ Gem::Specification.new do |s|
75
76
  "lib/generators/observer/observer_generator.rb",
76
77
  "lib/generators/view/view_generator.rb",
77
78
  "rspec_for_generators.gemspec",
78
- "spec/generator_spec/controller_generator_spec.rb",
79
- "spec/generator_spec/helper_generator_spec.rb",
80
- "spec/generator_spec/migration_generator_spec.rb",
81
- "spec/generator_spec/model_generator_spec.rb",
82
- "spec/generator_spec/observer_generator_spec.rb",
83
- "spec/generator_spec/view_generator_spec.rb",
79
+ "spec/generator_spec/generators/controller_generator_spec.rb",
80
+ "spec/generator_spec/generators/helper_generator_spec.rb",
81
+ "spec/generator_spec/generators/migration_generator_spec.rb",
82
+ "spec/generator_spec/generators/model_generator_spec.rb",
83
+ "spec/generator_spec/generators/observer_generator_spec.rb",
84
+ "spec/generator_spec/generators/view_generator_spec.rb",
85
+ "spec/generator_spec/matchers/content/class_self_spec.rb",
86
+ "spec/generator_spec/matchers/content/have_block_spec.rb",
87
+ "spec/generator_spec/matchers/content/have_call_spec.rb",
88
+ "spec/generator_spec/matchers/content/have_calls_spec.rb",
89
+ "spec/generator_spec/matchers/content/have_class_spec.rb",
90
+ "spec/generator_spec/matchers/content/have_method_spec.rb",
91
+ "spec/generator_spec/matchers/content/have_module_spec.rb",
92
+ "spec/generator_spec/matchers/content/have_region_spec.rb",
93
+ "spec/generator_spec/matchers/content/have_subclass_spec.rb",
94
+ "spec/generator_spec/matchers/content/include_module_spec.rb",
95
+ "spec/generator_spec/matchers/content/inherit_from_spec.rb",
96
+ "spec/generator_spec/matchers/file/directory_spec.rb",
97
+ "spec/generator_spec/matchers/file/file_spec.rb",
98
+ "spec/generator_spec/matchers/file/migration_spec.rb",
99
+ "spec/generator_spec/matchers/migration/have_column_spec.rb",
100
+ "spec/generator_spec/matchers/migration/have_index_spec.rb",
101
+ "spec/generator_spec/matchers/migration/have_table_spec.rb",
102
+ "spec/generator_spec/matchers/migration/have_tbl_column_spec.rb",
103
+ "spec/generator_spec/matchers/migration/have_up_down_spec.rb",
104
+ "spec/generator_spec/matchers/rails/controller_matcher_spec.rb",
105
+ "spec/generator_spec/matchers/rails/helper_matcher_spec.rb",
106
+ "spec/generator_spec/matchers/rails/mailer_matcher_spec.rb",
107
+ "spec/generator_spec/matchers/rails/model_matcher_spec.rb",
108
+ "spec/generator_spec/matchers/rails/observer_matcher_spec.rb",
109
+ "spec/generator_spec/matchers/rails/view_matcher_spec.rb",
110
+ "spec/generator_spec/rails_helpers/rails_app_spec.rb",
111
+ "spec/generator_spec/rails_helpers/rails_controller_spec.rb",
112
+ "spec/generator_spec/rails_helpers/rails_helper_spec.rb",
113
+ "spec/generator_spec/rails_helpers/rails_mailer_spec.rb",
114
+ "spec/generator_spec/rails_helpers/rails_migration_spec.rb",
115
+ "spec/generator_spec/rails_helpers/rails_model_spec.rb",
116
+ "spec/generator_spec/rails_helpers/rails_observer_spec.rb",
117
+ "spec/generator_spec/rails_helpers/rails_orm_spec.rb",
118
+ "spec/generator_spec/rails_helpers/rails_view_spec.rb",
84
119
  "spec/spec_helper.rb",
85
120
  "wiki/Custom Rails 3 Generators.textile",
86
121
  "wiki/content_matchers.textile",
@@ -94,12 +129,46 @@ Gem::Specification.new do |s|
94
129
  s.rubygems_version = %q{1.3.7}
95
130
  s.summary = %q{RSpec 2 library for writing generator specs}
96
131
  s.test_files = [
97
- "spec/generator_spec/controller_generator_spec.rb",
98
- "spec/generator_spec/helper_generator_spec.rb",
99
- "spec/generator_spec/migration_generator_spec.rb",
100
- "spec/generator_spec/model_generator_spec.rb",
101
- "spec/generator_spec/observer_generator_spec.rb",
102
- "spec/generator_spec/view_generator_spec.rb",
132
+ "spec/generator_spec/generators/controller_generator_spec.rb",
133
+ "spec/generator_spec/generators/helper_generator_spec.rb",
134
+ "spec/generator_spec/generators/migration_generator_spec.rb",
135
+ "spec/generator_spec/generators/model_generator_spec.rb",
136
+ "spec/generator_spec/generators/observer_generator_spec.rb",
137
+ "spec/generator_spec/generators/view_generator_spec.rb",
138
+ "spec/generator_spec/matchers/content/class_self_spec.rb",
139
+ "spec/generator_spec/matchers/content/have_block_spec.rb",
140
+ "spec/generator_spec/matchers/content/have_call_spec.rb",
141
+ "spec/generator_spec/matchers/content/have_calls_spec.rb",
142
+ "spec/generator_spec/matchers/content/have_class_spec.rb",
143
+ "spec/generator_spec/matchers/content/have_method_spec.rb",
144
+ "spec/generator_spec/matchers/content/have_module_spec.rb",
145
+ "spec/generator_spec/matchers/content/have_region_spec.rb",
146
+ "spec/generator_spec/matchers/content/have_subclass_spec.rb",
147
+ "spec/generator_spec/matchers/content/include_module_spec.rb",
148
+ "spec/generator_spec/matchers/content/inherit_from_spec.rb",
149
+ "spec/generator_spec/matchers/file/directory_spec.rb",
150
+ "spec/generator_spec/matchers/file/file_spec.rb",
151
+ "spec/generator_spec/matchers/file/migration_spec.rb",
152
+ "spec/generator_spec/matchers/migration/have_column_spec.rb",
153
+ "spec/generator_spec/matchers/migration/have_index_spec.rb",
154
+ "spec/generator_spec/matchers/migration/have_table_spec.rb",
155
+ "spec/generator_spec/matchers/migration/have_tbl_column_spec.rb",
156
+ "spec/generator_spec/matchers/migration/have_up_down_spec.rb",
157
+ "spec/generator_spec/matchers/rails/controller_matcher_spec.rb",
158
+ "spec/generator_spec/matchers/rails/helper_matcher_spec.rb",
159
+ "spec/generator_spec/matchers/rails/mailer_matcher_spec.rb",
160
+ "spec/generator_spec/matchers/rails/model_matcher_spec.rb",
161
+ "spec/generator_spec/matchers/rails/observer_matcher_spec.rb",
162
+ "spec/generator_spec/matchers/rails/view_matcher_spec.rb",
163
+ "spec/generator_spec/rails_helpers/rails_app_spec.rb",
164
+ "spec/generator_spec/rails_helpers/rails_controller_spec.rb",
165
+ "spec/generator_spec/rails_helpers/rails_helper_spec.rb",
166
+ "spec/generator_spec/rails_helpers/rails_mailer_spec.rb",
167
+ "spec/generator_spec/rails_helpers/rails_migration_spec.rb",
168
+ "spec/generator_spec/rails_helpers/rails_model_spec.rb",
169
+ "spec/generator_spec/rails_helpers/rails_observer_spec.rb",
170
+ "spec/generator_spec/rails_helpers/rails_orm_spec.rb",
171
+ "spec/generator_spec/rails_helpers/rails_view_spec.rb",
103
172
  "spec/spec_helper.rb"
104
173
  ]
105
174
 
@@ -53,8 +53,8 @@ module Rails::Generators
53
53
  gsub_file reverse_migration_path, /dwn/, 'down'
54
54
  end
55
55
 
56
- def latest_migration_file
57
- self.class.latest_migration_file
56
+ def latest_migration_file dir, name
57
+ self.class.latest_migration_file dir, name
58
58
  end
59
59
 
60
60
  def migration(options)
@@ -3,6 +3,11 @@ module RSpec
3
3
  end
4
4
  end
5
5
 
6
+ module RSpec
7
+ module Rails
8
+ end
9
+ end
10
+
6
11
  module Rails
7
12
  module Generators
8
13
  end
@@ -1,10 +1,90 @@
1
1
  module RSpec
2
2
  class RubyContentMatcher
3
- attr_reader :content
4
-
5
- def matches?(content)
3
+ attr_reader :content, :end_option, :alt_end, :content_matches
4
+
5
+ def initialize name
6
+ @end_option = name
7
+ end
8
+
9
+ def index
10
+ 0
11
+ end
12
+
13
+ def indexes
14
+ nil
15
+ end
16
+
17
+
18
+ def matches? content, &block
6
19
  @content = content
7
- end
20
+ match = is_match? content
21
+ content_to_yield = if indexes
22
+ content_matches[indexes.first] || content_matches[indexes.last]
23
+ else
24
+ content_matches[index]
25
+ end
26
+ handle_result(content_to_yield, match, &block)
27
+ end
28
+
29
+ def is_match? content
30
+ expr = get_expr(content)
31
+ match = (content =~ expr)
32
+ @content_matches = [$1, $2, $3]
33
+ end
34
+
35
+ def handle_result content, match, &block
36
+ if block && match && content
37
+ ruby_content = content.strip.extend(RSpec::RubyContent::Helpers)
38
+ yield ruby_content
39
+ end
40
+ match
41
+ end
42
+
43
+ def get_expr content
44
+ expr = /#{main_expr}#{comment_end}/m
45
+ if (content =~ expr).nil?
46
+ /#{main_expr}#{end_expr}/m
47
+ else
48
+ expr
49
+ end
50
+ end
51
+
52
+ Q_ANY_GROUP = '(.*?)'
53
+ ANY_GROUP = '(.*)'
54
+
55
+ SPACES = '\s+'
56
+ OPT_SPACES = '\s*'
57
+ LPAR = '(\()'
58
+ RPAR = '(\))'
59
+ OPT_ARGS = '(\(.+\))?'
60
+
61
+ def opt expr
62
+ "#{expr}?"
63
+ end
64
+
65
+ def args_msg
66
+ args ? " with arguments #{args}" : ''
67
+ end
68
+
69
+ def any_args_expr
70
+ OPT_SPACES + OPT_ARGS
71
+ end
72
+
73
+ def args_expr
74
+ args ? SPACES + opt(LPAR) + OPT_SPACES + "#{args}" + OPT_SPACES + opt(RPAR) : ''
75
+ end
76
+
77
+ def main_expr
78
+ raise "Must override main_expr"
79
+ end
80
+
81
+ def end_expr
82
+ "$"
83
+ end
84
+
85
+ def comment_end
86
+ alt_end != nil ? '#' + SPACES + "(#{end_option}|#{alt_end})" : '#' + SPACES + "#{end_option}"
87
+ end
8
88
 
9
89
  def failure_message
10
90
  debug_content
@@ -1,38 +1,41 @@
1
1
  module RSpec::RubyContentMatchers
2
2
  class HaveBlock < RSpec::RubyContentMatcher
3
- attr_reader :name, :args, :block_args
3
+ attr_reader :name, :args, :block_args, :do_option, :end_option
4
4
 
5
5
  def initialize(name, options={})
6
+ super options[:end] || name
6
7
  @name = name.to_s
7
- @args = options[:args]
8
+ @args = options[:args]
9
+ @do_option = options[:do].nil? ? true : options[:do]
8
10
  @block_args = options[:block_args]
9
11
  end
10
-
11
- def matches?(content)
12
- super
13
- match_res = (content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)#{end_expr}/m) != nil
14
- block_content = $1 || $3
15
- if block_given? && block_content
16
- ruby_content = block_content.strip.extend(RSpec::RubyContent::Helpers)
17
- yield ruby_content
18
- end
19
- match_res
20
- end
21
12
 
22
13
  def failure_message
23
14
  super
24
- return "Expected there to be a block #{name} with arguments #{args}, but there wasn't"
15
+ return "Expected there to be a block #{name}#{args_msg}#{block_args_msg}, but there wasn't"
25
16
  end
26
17
 
27
18
  def negative_failure_message
28
19
  super
29
- return "Did not expect there to be a block #{name} with arguments #{args}, but there was"
20
+ return "Did not expect there to be a block #{name}#{args_msg}, but there was"
30
21
  end
31
22
 
32
23
  protected
33
24
 
34
- def end_expr
35
- "(end|# #{name})"
25
+ def block_args_msg
26
+ " and block arguments #{block_args}" if block_args
27
+ end
28
+
29
+ def indexes
30
+ [0, 2]
31
+ end
32
+
33
+ def main_expr
34
+ "#{name}" + SPACES + "#{args_expr}#{do_expr}" + ANY_GROUP
35
+ end
36
+
37
+ def do_expr
38
+ do_option == false ? '' : 'do' + SPACES + "#{block_args_expr}"
36
39
  end
37
40
 
38
41
  def args_expr
@@ -42,14 +45,13 @@ module RSpec::RubyContentMatchers
42
45
  def block_args_expr
43
46
  block_args ? '\|\s*' + block_args + '\s*\|' : ''
44
47
  end
48
+
49
+ def alt_end
50
+ 'do'
51
+ end
45
52
  end
46
53
 
47
54
  def have_block(name, options={})
48
55
  HaveBlock.new(name, options)
49
56
  end
50
-
51
- def have_class_self(name, options={})
52
- HaveBlock.new('class', :args => '<< self')
53
- end
54
-
55
57
  end
@@ -7,39 +7,47 @@
7
7
  #
8
8
  module RSpec::RubyContentMatchers
9
9
  class HaveCall < RSpec::RubyContentMatcher
10
- attr_reader :method, :args
10
+ attr_reader :method, :args, :dot
11
11
 
12
- def initialize(method, args = nil)
12
+ def initialize(method, args = nil, options = {})
13
13
  @method = method.to_s
14
14
  @args = args
15
+ @dot = options[:dot]
15
16
  end
16
17
 
17
18
  def matches?(content)
18
- super
19
- (content =~ /#{method}#{args_expr}/m) != nil
19
+ @content = content
20
+ (content =~ /#{dot_expr}#{method}#{args_expr}/m)
20
21
  end
21
22
 
22
23
  def failure_message
23
24
  super
24
- return "Expected there to be a call to #{method} with args #{args}, but there wasn't" if args
25
- "Expected there to be a call to #{method}, but there wasn't"
25
+ "Expected there to be a call to #{method}#{args_msg}, but there wasn't"
26
26
  end
27
27
 
28
28
  def negative_failure_message
29
29
  super
30
- return "Did not expect there to be a call to #{method} with args #{args}, but there was" if args
31
- "Did not expect there to be a call to #{method}, but there was"
30
+ "Did not expect there to be a call to #{method}#{args_msg}, but there was"
32
31
  end
33
32
 
34
33
  protected
35
-
36
- def args_expr
37
- args ? "\s+(\()?\s*#{args}\s*(\))?" : ''
38
- end
39
-
34
+
35
+ def not_def
36
+ '[^def\s]?\s*'
37
+ end
38
+
39
+ def dot_expr
40
+ return Regexp.escape(dot) if dot.kind_of?(String)
41
+ dot == true ? "#{not_def}\." : not_def
42
+ end
43
+
40
44
  end
41
45
 
42
46
  def have_call(method, args = nil)
43
47
  HaveCall.new(method, args)
44
48
  end
49
+
50
+ def have_dot_call(method, options = {})
51
+ HaveCall.new(method, options[:args], :dot => options[:dot] || true)
52
+ end
45
53
  end
@@ -1,42 +1,48 @@
1
1
  module RSpec::RubyContentMatchers
2
- class HaveCalls < RSpec::RubyContentMatcher
2
+ class HaveCalls < HaveCall
3
3
  attr_reader :calls, :method, :args
4
4
 
5
- def initialize(calls)
6
- @calls = calls
5
+ def initialize(*calls)
6
+ case calls.first
7
+ when Array, Hash
8
+ @calls = calls.first
9
+ else
10
+ @calls = calls
11
+ end
7
12
  end
8
13
 
9
14
  def matches?(content)
10
15
  super
16
+ case calls
17
+ when Array
18
+ do_list
19
+ when Hash
20
+ do_hash
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def do_list
27
+ calls.each do |method|
28
+ @method = method.to_s
29
+ expr = /#{not_def}#{method}/m
30
+ return false if (content =~ expr) == nil
31
+ end
32
+ true
33
+ end
34
+
35
+ def do_hash
11
36
  calls.each_pair do |method, args|
12
37
  @method = method.to_s
13
38
  @args = args
14
- return false if (content =~ /#{method}\s+#{args_expr}/m) == nil
39
+ return false if (content =~ /#{not_def}#{method}\s+#{args_expr}/m) == nil
15
40
  end
16
41
  true
17
- end
18
-
19
- def failure_message
20
- super
21
- return "Expected there to be a call to #{method} with args #{args}, but there wasn't" if args
22
- "Expected there to be a call to #{method}, but there wasn't"
23
- end
24
-
25
- def negative_failure_message
26
- super
27
- return "Did not expect there to be a call to #{method} with args #{args}, but there was" if args
28
- "Did not expect there to be a call to #{method}, but there was"
29
- end
30
-
31
- protected
32
-
33
- def args_expr
34
- args ? "(\()?\s*#{args}\s*(\))?" : ''
35
- end
36
-
42
+ end
37
43
  end
38
44
 
39
- def have_calls(calls)
40
- HaveCalls.new(calls)
45
+ def have_calls(*calls)
46
+ HaveCalls.new(*calls)
41
47
  end
42
48
  end
@@ -0,0 +1,40 @@
1
+ # This method tries to see if a specific method is contained in the generated file.
2
+ # It can operate (should) on either a file name or the raw content
3
+ #
4
+ # generated_file_name.should have_method "hello" # 'my/path/say_hello.rb'.should have_method "hello"
5
+ #
6
+ # say_hello_file_content.should have_method "hello"
7
+ #
8
+ module RSpec::RubyContentMatchers
9
+ class HaveClassSelf < RSpec::RubyContentMatcher
10
+
11
+ def initialize
12
+ @end_option = 'class'
13
+ end
14
+
15
+ def failure_message
16
+ super
17
+ return "Expected there to be a: class << self"
18
+ end
19
+
20
+ def negative_failure_message
21
+ super
22
+ "Did not expect there to be a: class << self"
23
+ end
24
+
25
+ protected
26
+
27
+ def main_expr
28
+ 'class\s*<<\s*self' + ANY_GROUP
29
+ end
30
+
31
+ def alt_end
32
+ 'class self'
33
+ end
34
+ end
35
+
36
+ def have_class_self
37
+ HaveClassSelf.new
38
+ end
39
+
40
+ end