generator-spec 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/.rspec +1 -0
  2. data/README.markdown +11 -2
  3. data/VERSION +1 -1
  4. data/generator-spec.gemspec +21 -7
  5. data/lib/generators/demo/demo_generator.rb +1 -1
  6. data/lib/generators/migration/migration_generator.rb +13 -0
  7. data/lib/generators/migration/templates/create_users.erb +14 -0
  8. data/lib/rspec_for_generators/generator_spec_helper.rb +6 -6
  9. data/lib/rspec_for_generators/matchers/content/have_block.rb +7 -10
  10. data/lib/rspec_for_generators/matchers/content/have_call.rb +5 -5
  11. data/lib/rspec_for_generators/matchers/content/have_calls.rb +40 -0
  12. data/lib/rspec_for_generators/matchers/content/have_method.rb +17 -10
  13. data/lib/rspec_for_generators/matchers/file/generate_file.rb +20 -1
  14. data/lib/rspec_for_generators/matchers/file/generate_migration.rb +22 -14
  15. data/lib/rspec_for_generators/matchers/helpers/migration.rb +19 -32
  16. data/lib/rspec_for_generators/matchers/migration/have_column.rb +10 -0
  17. data/lib/rspec_for_generators/matchers/migration/have_migration.rb +29 -3
  18. data/lib/rspec_for_generators/matchers/migration/have_up_down.rb +4 -4
  19. data/lib/rspec_for_generators/rails_helpers/rails_app.rb +85 -19
  20. data/lib/rspec_for_generators/rails_helpers/rails_migration.rb +36 -0
  21. data/lib/rspec_for_generators/rails_spec_helper.rb +1 -1
  22. data/spec/rspec_for_generators/controller_generator_spec.rb +0 -0
  23. data/spec/rspec_for_generators/helper_generator_spec.rb +0 -0
  24. data/spec/rspec_for_generators/migration_generator_spec.rb +40 -0
  25. data/spec/rspec_for_generators/{rspec_for_generators_spec.rb → model_generator_spec.rb} +0 -0
  26. data/spec/rspec_for_generators/view_generator_spec.rb +0 -0
  27. data/spec/spec_helper.rb +5 -0
  28. data/wiki/content_matchers.textile +72 -0
  29. data/wiki/file_matchers.textile +17 -0
  30. data/wiki/{rails_view_helpers.textile → migration_matchers.textile} +1 -72
  31. data/wiki/rails_helpers.textile +42 -0
  32. metadata +22 -8
  33. data/lib/rspec_for_generators/rails_helpers/all.rb +0 -6
  34. data/spec/spec.opts +0 -1
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format nested --color
@@ -44,7 +44,7 @@ Put a *require_generator* statement in each spec file, to ensure that particular
44
44
 
45
45
  This will load the generator : `generators/canabale_generator.rb`
46
46
 
47
- If the generator is namespaced, use a nested approach like so:
47
+ If the generator is namespaced, use a nested approach like this:
48
48
 
49
49
  <pre>require_generators :canable => ['model', 'user']</pre>
50
50
 
@@ -120,7 +120,7 @@ describe 'model_generator' do
120
120
  name = 'account'
121
121
  create_model name
122
122
  g.run_generator %w{account}
123
- g.should generate_file name, :model do |content|
123
+ g.should generate_model name do |content|
124
124
  content.should have_class name do |klass|
125
125
  klass.should include_module 'Canable::Ables'
126
126
  end
@@ -129,6 +129,15 @@ describe 'model_generator' do
129
129
  end
130
130
  end</pre>
131
131
 
132
+ ## TODO
133
+
134
+ Need to create specs to test matchers/helpers for generated:
135
+ * migration
136
+ * controller
137
+ * helper
138
+ * view
139
+ * ...
140
+
132
141
  ## Note on Patches/Pull Requests
133
142
 
134
143
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
@@ -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.3"
8
+ s.version = "0.3.4"
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-04}
12
+ s.date = %q{2010-08-05}
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 = [
@@ -19,18 +19,22 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ ".rspec",
22
23
  "LICENSE",
23
24
  "README.markdown",
24
25
  "Rakefile",
25
26
  "VERSION",
26
27
  "generator-spec.gemspec",
27
28
  "lib/generators/demo/demo_generator.rb",
29
+ "lib/generators/migration/migration_generator.rb",
30
+ "lib/generators/migration/templates/create_users.erb",
28
31
  "lib/rspec_for_generators.rb",
29
32
  "lib/rspec_for_generators/fixtures/routes.rb",
30
33
  "lib/rspec_for_generators/generator_spec_helper.rb",
31
34
  "lib/rspec_for_generators/main.rb",
32
35
  "lib/rspec_for_generators/matchers/content/have_block.rb",
33
36
  "lib/rspec_for_generators/matchers/content/have_call.rb",
37
+ "lib/rspec_for_generators/matchers/content/have_calls.rb",
34
38
  "lib/rspec_for_generators/matchers/content/have_class.rb",
35
39
  "lib/rspec_for_generators/matchers/content/have_method.rb",
36
40
  "lib/rspec_for_generators/matchers/content/have_module.rb",
@@ -48,22 +52,28 @@ Gem::Specification.new do |s|
48
52
  "lib/rspec_for_generators/matchers/migration/have_table.rb",
49
53
  "lib/rspec_for_generators/matchers/migration/have_tbl_column.rb",
50
54
  "lib/rspec_for_generators/matchers/migration/have_up_down.rb",
51
- "lib/rspec_for_generators/rails_helpers/all.rb",
52
55
  "lib/rspec_for_generators/rails_helpers/rails_app.rb",
53
56
  "lib/rspec_for_generators/rails_helpers/rails_controller.rb",
54
57
  "lib/rspec_for_generators/rails_helpers/rails_helper.rb",
55
58
  "lib/rspec_for_generators/rails_helpers/rails_mailer.rb",
59
+ "lib/rspec_for_generators/rails_helpers/rails_migration.rb",
56
60
  "lib/rspec_for_generators/rails_helpers/rails_model.rb",
57
61
  "lib/rspec_for_generators/rails_helpers/rails_orm.rb",
58
62
  "lib/rspec_for_generators/rails_helpers/rails_view.rb",
59
63
  "lib/rspec_for_generators/rails_spec_helper.rb",
60
64
  "lib/rspec_for_generators/rspec_test_case.rb",
61
65
  "rspec_for_generators.gemspec",
62
- "spec/rspec_for_generators/rspec_for_generators_spec.rb",
63
- "spec/spec.opts",
66
+ "spec/rspec_for_generators/controller_generator_spec.rb",
67
+ "spec/rspec_for_generators/helper_generator_spec.rb",
68
+ "spec/rspec_for_generators/migration_generator_spec.rb",
69
+ "spec/rspec_for_generators/model_generator_spec.rb",
70
+ "spec/rspec_for_generators/view_generator_spec.rb",
64
71
  "spec/spec_helper.rb",
65
72
  "wiki/Custom Rails 3 Generators.textile",
66
- "wiki/rails_view_helpers.textile"
73
+ "wiki/content_matchers.textile",
74
+ "wiki/file_matchers.textile",
75
+ "wiki/migration_matchers.textile",
76
+ "wiki/rails_helpers.textile"
67
77
  ]
68
78
  s.homepage = %q{http://github.com/kristianmandrup/rspec_for_generators}
69
79
  s.rdoc_options = ["--charset=UTF-8"]
@@ -71,7 +81,11 @@ Gem::Specification.new do |s|
71
81
  s.rubygems_version = %q{1.3.7}
72
82
  s.summary = %q{RSpec 2 library for writing generator specs}
73
83
  s.test_files = [
74
- "spec/rspec_for_generators/rspec_for_generators_spec.rb",
84
+ "spec/rspec_for_generators/controller_generator_spec.rb",
85
+ "spec/rspec_for_generators/helper_generator_spec.rb",
86
+ "spec/rspec_for_generators/migration_generator_spec.rb",
87
+ "spec/rspec_for_generators/model_generator_spec.rb",
88
+ "spec/rspec_for_generators/view_generator_spec.rb",
75
89
  "spec/spec_helper.rb"
76
90
  ]
77
91
 
@@ -7,7 +7,7 @@ class DemoGenerator < Rails::Generators::Base
7
7
  @source_root ||= File.expand_path("../../templates", __FILE__)
8
8
  end
9
9
 
10
- def make_canable_able
10
+ def make_canable_able
11
11
  if File.exist?(model_file_name)
12
12
  @model_file_txt = File.open(model_file_name).read
13
13
  after_txt = find_after_txt(model_file_txt)
@@ -0,0 +1,13 @@
1
+ class MigrationGenerator < Rails::Generators::Base
2
+ desc "Creates a migration"
3
+
4
+ argument :name, :type => :string
5
+
6
+ def self.source_root
7
+ @source_root ||= File.expand_path("../templates", __FILE__)
8
+ end
9
+
10
+ def create_migration
11
+ template "#{name}.erb", 'db/migrations/20100203_create_users.rb'
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ class <%= name.camelize %> < 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
@@ -25,7 +25,7 @@ alias :require_generator :require_generators
25
25
  module RSpec
26
26
  module Generator
27
27
  class << self
28
- attr_accessor :generator, :test_method_name
28
+ attr_accessor :generator, :test_method_name, :remove_temp_dir
29
29
 
30
30
  def remove_rails_dir!
31
31
  FileUtils.rm_rf ::TmpRails.root
@@ -36,17 +36,17 @@ module RSpec
36
36
  config.root_dir = options == :custom ? TmpRails.root_dir(File.dirname(path) + '/../tmp', :custom) : TmpRails.root_dir(path)
37
37
  end
38
38
 
39
- ::RSpec::Generators::TestCase.destination ::Rails.root
40
- ::Rails::Generators.configure!
41
-
42
39
  ::RSpec.configure do |config|
43
40
  config.after(:suite) do
44
- ::RSpec::Generator.remove_rails_dir!
41
+ ::RSpec::Generator.remove_rails_dir! if remove_temp_dir
45
42
  end
46
43
  end
44
+
45
+ ::RSpec::Generators::TestCase.destination ::Rails.root
46
+ ::Rails::Generators.configure!
47
47
  end
48
48
 
49
- def run_generator *args, &block
49
+ def run_generator *args, &block
50
50
  generator.run_generator *args
51
51
  if block
52
52
  block.arity < 1 ? generator.instance_eval(&block) : block.call(generator, self)
@@ -5,13 +5,14 @@ module RSpec::RubyContentMatchers
5
5
  def initialize(name, options={})
6
6
  @name = name
7
7
  @args = options[:args]
8
- @block_args = options[:block_args]
8
+ @block_args = options[:block_args]
9
9
  end
10
10
 
11
11
  def matches?(content)
12
- match_res = match(content)
13
- if block_given? && $1
14
- ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
12
+ match_res = (content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)end/m)
13
+ block_content = $1 || $3
14
+ if block_given? && block_content
15
+ ruby_content = block_content.strip.extend(RSpec::RubyContent::Helpers)
15
16
  yield ruby_content
16
17
  else
17
18
  match_res
@@ -28,16 +29,12 @@ module RSpec::RubyContentMatchers
28
29
 
29
30
  protected
30
31
 
31
- def match content
32
- content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)end/m
33
- end
34
-
35
32
  def args_expr
36
- args ? "\(?\s*#{args}\s*\)?\s+" : ''
33
+ args ? '(\()?\s*' + args + '\s*(\))?\s+' : ''
37
34
  end
38
35
 
39
36
  def block_args_expr
40
- args ? "\|\s*#{block_args}\s*\|" : ''
37
+ block_args ? '\|\s*' + block_args + '\s*\|' : ''
41
38
  end
42
39
  end
43
40
 
@@ -19,19 +19,19 @@ module RSpec::RubyContentMatchers
19
19
  end
20
20
 
21
21
  def failure_message
22
- return "Expected there to be the call to #{method} with args #{args}, but there wasn't" if args
23
- "Expected there to be the call to #{method}, but there wasn't"
22
+ return "Expected there to be a call to #{method} with args #{args}, but there wasn't" if args
23
+ "Expected there to be a call to #{method}, but there wasn't"
24
24
  end
25
25
 
26
26
  def negative_failure_message
27
- return "Did not expect there to be the call to #{method} with args #{args}, but there was" if args
28
- "Did not expect there to be the call to #{method}, but there was"
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
29
  end
30
30
 
31
31
  protected
32
32
 
33
33
  def args_expr
34
- args ? "\(?\s*#{args}\s*\)?\s+" : ''
34
+ args ? "(\()?\s*#{args}\s*(\))?" : ''
35
35
  end
36
36
 
37
37
  end
@@ -0,0 +1,40 @@
1
+ module RSpec::RubyContentMatchers
2
+ class HaveCalls
3
+ attr_reader :calls
4
+ attr_accessor :method, :args
5
+
6
+ def initialize(calls)
7
+ @calls = calls
8
+ end
9
+
10
+ def matches?(content)
11
+ calls.each_pair do |method, args|
12
+ @method = method
13
+ @args = args
14
+ return false if (content =~ /#{method}\s+#{args_expr}/m) == nil
15
+ end
16
+ true
17
+ end
18
+
19
+ def failure_message
20
+ return "Expected there to be a call to #{method} with args #{args}, but there wasn't" if args
21
+ "Expected there to be a call to #{method}, but there wasn't"
22
+ end
23
+
24
+ def negative_failure_message
25
+ return "Did not expect there to be a call to #{method} with args #{args}, but there was" if args
26
+ "Did not expect there to be a call to #{method}, but there was"
27
+ end
28
+
29
+ protected
30
+
31
+ def args_expr
32
+ args ? "(\()?\s*#{args}\s*(\))?" : ''
33
+ end
34
+
35
+ end
36
+
37
+ def have_calls(calls)
38
+ HaveCalls.new(calls)
39
+ end
40
+ end
@@ -7,29 +7,36 @@
7
7
  #
8
8
  module RSpec::RubyContentMatchers
9
9
  class HaveMethod
10
- attr_reader :method
10
+ attr_reader :method, :type
11
11
 
12
- def initialize(method, type)
13
- @type = type
12
+ def initialize(method, type=nil)
14
13
  @method = method.to_s
14
+ @type = type
15
15
  end
16
16
 
17
- def matches?(content)
18
- case @type
17
+ def matches?(content)
18
+ match_res = case type
19
19
  when :class
20
- content =~ /def\s+self.#{method}\s*(\(.+\))?(.*?)/m
20
+ content =~ /def\s+self.#{method}\s*(\(.+\))?(.*?)(def|end)/m
21
21
  else
22
- content =~ /def\s+#{method}\s*(\(.+\))?(.*?)/m
23
- end
22
+ content =~ /def\s+#{method}\s*(\(.+\))?(.*?)(def|end)/m
23
+ end
24
+ if block_given? && $2
25
+ ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
26
+ ruby_content << "\nend" if !(ruby_content =~ /\nend$/)
27
+ yield ruby_content
28
+ else
29
+ match_res
30
+ end
24
31
  end
25
32
 
26
33
  def failure_message
27
- return "Expected there to be the class method #{method}, but there wasn't" if @type == :class
34
+ return "Expected there to be the class method #{method}, but there wasn't" if type == :class
28
35
  "Expected there to be the method #{method}, but there wasn't"
29
36
  end
30
37
 
31
38
  def negative_failure_message
32
- return "Did not expect there to be the method #{method}, but there was" if @type == :class
39
+ return "Did not expect there to be the method #{method}, but there was" if type == :class
33
40
  "Did not expect there to be the method #{method}, but there was"
34
41
  end
35
42
 
@@ -66,6 +66,25 @@ module RSpec::FileMatchers
66
66
  def generate_file(relative, type = nil)
67
67
  GenerateFile.new(relative, type)
68
68
  end
69
- alias_method :generate_directory, :generate_file
69
+
70
+ def generate_model(relative)
71
+ GenerateFile.new(relative, :model)
72
+ end
73
+
74
+ def generate_controller(relative)
75
+ GenerateFile.new(relative, :controller)
76
+ end
77
+
78
+ def generate_helper(relative)
79
+ GenerateFile.new(relative, :helper)
80
+ end
81
+
82
+ def generate_view(relative)
83
+ GenerateFile.new(relative, :view)
84
+ end
85
+
86
+ def generate_observer(relative)
87
+ GenerateFile.new(relative, :observer)
88
+ end
70
89
  end
71
90
 
@@ -8,42 +8,50 @@
8
8
  module RSpec
9
9
  module FileMatchers
10
10
  class GenerateMigration
11
+ include RSpec::Rails::Migration
11
12
 
12
- def initialize(relative)
13
- @relative = relative
13
+ attr_reader :name
14
+
15
+ def initialize(name)
16
+ @name = name
14
17
  end
15
18
 
16
- def matches?(generator)
17
- ex = migration_file_name(relative, generator)
18
- if block && ex
19
- read = File.read(@expected)
19
+ def matches?(generator)
20
+ expected = migration_file_name(name, generator)
21
+ if block_given? && expected
22
+ read = File.read(expected)
20
23
  ruby_content = read.extend(RSpec::RubyContent::Helpers)
21
24
  yield ruby_content
22
25
  else
23
- ex
26
+ expected
24
27
  end
25
28
  end
26
29
 
27
30
  def failure_message
28
- "Expected migration #{relative} to have been generated, but it was not"
31
+ "Expected migration #{name} to have been generated, but it was not"
29
32
  end
30
33
 
31
34
  def negative_failure_message
32
- "Did not expect migration #{relative} to have been generated, but it was"
35
+ "Did not expect migration #{name} to have been generated, but it was"
33
36
  end
34
37
 
35
38
  protected
36
39
 
37
40
  def migration_file_name(relative, generator) #:nodoc:
38
- absolute = File.expand_path(relative, generator.class.destination_root)
39
- dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '')
40
- Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
41
+ dirname = migration_dir
42
+ file_name = relative.sub(/\.rb$/, '')
43
+ migrations = Dir.glob("#{dirname}/[0-9]*_*.rb")
44
+ if !migrations.empty?
45
+ migrations.grep(/\d+_#{file_name}\.rb$/).first
46
+ else
47
+ false
48
+ end
41
49
  end
42
50
 
43
51
  end
44
52
 
45
- def generate_migration(relative)
46
- GenerateMigration.new(relative)
53
+ def generate_migration(name)
54
+ GenerateMigration.new(name)
47
55
  end
48
56
 
49
57
  end
@@ -11,92 +11,79 @@ end
11
11
 
12
12
  module RSpec::RubyContent
13
13
  module Helpers
14
- def check_up(&block)
15
- self.should have_method('up', :class) do |content|
16
- yield content if block
17
- end
14
+ def check_up &block
15
+ self.should have_up, &block
18
16
  end
19
17
  alias_method :up, :check_up
20
18
 
21
- def check_down(&block)
22
- self.should have_method('down', :class) do |content|
23
- yield content if block
24
- end
19
+ def check_down &block
20
+ self.should have_down, &block
25
21
  end
26
22
  alias_method :down, :check_down
27
23
 
28
- def check_create_table(name, &block)
29
- self.should match /create_table\s+:#{name}\s+(.*)end/m
30
- if block_given?
31
- ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
32
- yield ruby_content
33
- end
24
+ def check_create_table name, &block
25
+ self.should create_table name, &block
34
26
  end
35
27
  alias_method :creates_table, :check_create_table
36
28
 
37
- def check_change_table(name, &block)
38
- self.should match /change_table\s+:#{name}\s+(.*)end/m
39
- if block_given?
40
- ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
41
- yield ruby_content
42
- end
29
+ def check_change_table name, &block
30
+ self.should change_table name, &block
43
31
  end
44
32
  alias_method :changes_table, :check_change_table
45
33
 
46
34
  def check_drop_table name
47
- self.should match /drop_table\s+:#{name}/
35
+ self.should drop_table name, &block
48
36
  end
49
37
  alias_method :drops_table, :check_drop_table
50
38
 
51
39
  def check_rename_table name
52
- self.should match /rename_table\s+:#{name}/
40
+ self.should rename_table name, &block
53
41
  end
54
42
  alias_method :renames_table, :check_rename_table
55
43
 
56
44
  def check_remove_column name
57
- self.should match /t.remove\s+:#{name}/
45
+ self.should remove_colum name, &block
58
46
  end
59
47
  alias_method :removes_column, :check_remove_column
60
48
 
61
49
  def check_add_column name, type='string'
62
- self.should match /t.#{type}\s+:#{name}/
50
+ self.should add_column name, type, &block
63
51
  end
64
52
  alias_method :adds_column, :check_add_column
65
53
 
66
54
  def check_change_column name, type='string'
67
- self.should match /t.change\s+:#{name}\s*,\s*:#{type}/
55
+ self.should change_column name, type, &block
68
56
  end
69
57
  alias_method :changes_column, :check_change_column
70
58
 
71
59
  def check_add_tbl_column tbl_name, name, type='string'
72
- self.should match /add_column\s+:#{tbl_name}\s*,\s*:#{name},#{type}/
60
+ self.should add_tbl_column tbl_name, name, type
73
61
  end
74
62
  alias_method :adds_tbl_column, :check_add_tbl_column
75
63
 
76
64
  def check_change_tbl_column tbl_name, name, type='string'
77
- self.should match /change_column\s+:#{tbl_name}\s*,\s*:#{name}\s*,\s*#{type}/
65
+ self.should change_tbl_column tbl_name, name, type
78
66
  end
79
67
  alias_method :changes_tbl_column, :check_change_tbl_column
80
68
 
81
69
  def check_rename_tbl_column tbl_name, new_name
82
- self.should match /rename_column\s+:#{tbl_name}\s*,\s*:#{new_name}/
70
+ self.should rename_tbl_column tbl_name, new_name
83
71
  end
84
72
  alias_method :renames_tbl_column, :check_rename_tbl_column
85
73
 
86
74
  def check_remove_tbl_column tbl_name, name
87
- self.should match /remove_column\s+:#{tbl_name}\s*,\s*:#{name}/
75
+ self.should remove_tbl_column tbl_name, name
88
76
  end
89
77
  alias_method :removes_tbl_column, :check_remove_tbl_column
90
78
 
91
79
  def check_add_tbl_index tbl_name, name
92
- self.should match /add_index\s+:#{tbl_name}\s*,\s*:#{name}/
80
+ self.should add_tbl_index tbl_name, name
93
81
  end
94
82
  alias_method :adds_tbl_index, :check_add_tbl_index
95
83
 
96
84
  def check_remove_tbl_index tbl_name, name
97
- self.should match /remove_index\s+:#{tbl_name}\s*,\s*:#{name}/
85
+ self.should remove_tbl_index tbl_name, name
98
86
  end
99
87
  alias_method :removes_tbl_index, :check_remove_tbl_index
100
-
101
88
  end
102
89
  end
@@ -6,8 +6,18 @@ module RSpec::RubyContentMatchers
6
6
  def have_add_column(name, type='string')
7
7
  HaveCall.new("t.#{type}", ":#{name}")
8
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
+ HaveCalls.new columns
14
+ end
9
15
 
10
16
  def have_change_column(name, type='string')
11
17
  HaveCall.new("t.change", ":#{name}\s*,\s*:#{type}")
12
18
  end
19
+
20
+ def have_timestamps
21
+ HaveCall.new("t.timestamps")
22
+ end
13
23
  end
@@ -1,5 +1,31 @@
1
- module RSpec::FileMatchers
2
- def have_migration(name)
3
- GenerateFile.new(name, :migration)
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
4
26
  end
27
+
28
+ def have_migration(klass)
29
+ HaveMigration.new(klass)
30
+ end
5
31
  end
@@ -1,10 +1,10 @@
1
1
  module RSpec::RubyContentMatchers
2
- def have_up(method, type = nil)
3
- HaveMethod.new up, :class
2
+ def have_up &block
3
+ HaveMethod.new 'up', :class, &block
4
4
  end
5
5
 
6
- def have_down(method, type = nil)
7
- HaveMethod.new up, :class
6
+ def have_down &block
7
+ HaveMethod.new 'down', :class, &block
8
8
  end
9
9
  end
10
10
 
@@ -1,52 +1,118 @@
1
1
  module RSpec::Rails
2
2
  module App
3
- def self.debug!
4
- self.extend(Debug)
5
- end
3
+ module Dirs
4
+ def app_dir
5
+ File.join(::Rails.root, 'app')
6
+ end
7
+
8
+ def config_dir
9
+ File.join(::Rails.root, 'config')
10
+ end
11
+
12
+ def db_dir
13
+ File.join(::Rails.root, 'db')
14
+ end
15
+
16
+ def locale_dir
17
+ File.join(::Rails.root, 'public')
18
+ end
19
+
20
+ def model_dir
21
+ File.join(app_dir, 'models')
22
+ end
23
+
24
+ def controller_dir
25
+ File.join(app_dir, 'controllers')
26
+ end
27
+
28
+ def migration_dir
29
+ File.join(db_dir, 'migrations')
30
+ end
31
+
32
+ def helper_dir
33
+ File.join(app_dir, 'helpers')
34
+ end
35
+
36
+ def view_dir
37
+ File.join(app_dir, 'views')
38
+ end
39
+
40
+ def initializer_dir
41
+ File.join(config_dir, 'initializers')
42
+ end
43
+
44
+ def migration_dir
45
+ File.join(db_dir, 'migrations')
46
+ end
47
+
48
+ def locale_dir
49
+ File.join(config_dir, 'locales')
50
+ end
51
+
52
+ def stylesheet_dir
53
+ File.join(public_dir, 'stylesheets')
54
+ end
55
+
56
+ def javascript_dir
57
+ File.join(public_dir, 'javascripts')
58
+ end
59
+ end
6
60
 
7
- module Debug
8
- def rails_files
61
+ module Files
62
+ def all
9
63
  FileList["#{::Rails.root}/**/*.rb"]
10
64
  end
11
65
 
12
- def rails_app_files
66
+ def app_files
13
67
  FileList["#{::Rails.root}/app/**/*.rb"]
14
68
  end
15
69
 
16
- def rails_model_files
70
+ def model_files
17
71
  FileList["#{::Rails.root}/app/models/**/*.rb"]
18
72
  end
19
73
 
20
- def rails_controller_files
74
+ def controller_files
21
75
  FileList["#{::Rails.root}/app/controllers/**/*.rb"]
22
76
  end
23
77
 
24
- def rails_helper_files
78
+ def helper_files
25
79
  FileList["#{::Rails.root}/app/helpers/**/*.rb"]
26
80
  end
27
81
 
28
- def rails_view_files
29
- FileList["#{::Rails.root}/app/views/**/*.yml"]
82
+ def view_files
83
+ FileList["#{::Rails.root}/app/views/**/*"]
84
+ end
85
+
86
+ def view_files_html
87
+ FileList["#{::Rails.root}/app/views/**/*.html*"]
30
88
  end
31
89
 
32
- def rails_initializer_files
90
+ def view_files_haml
91
+ FileList["#{::Rails.root}/app/views/**/*.haml*"]
92
+ end
93
+
94
+ def initializer_files
33
95
  FileList["#{::Rails.root}/config/initializers/**/*.rb"]
34
96
  end
35
97
 
36
- def rails_migration_files
37
- FileList["#{::Rails.root}/db/migration/**/*.rb"]
98
+ def migration_files
99
+ FileList["#{migration_dir}/**/*.rb"]
38
100
  end
39
101
 
40
- def rails_locale_files
102
+ def locale_files
41
103
  FileList["#{::Rails.root}/config/locales/**/*.yml"]
42
104
  end
43
105
 
44
- def rails_javascript_files
45
- FileList["#{::Rails.root}/public/javascripts/**/*.yml"]
106
+ def javascript_files
107
+ FileList["#{javascript_dir}/**/*.js"]
108
+ end
109
+
110
+ def css_files
111
+ FileList["#{stylesheet_dir}/**/*.css"]
46
112
  end
47
113
 
48
- def rails_stylesheet_files
49
- FileList["#{::Rails.root}/public/stylesheets/**/*.yml"]
114
+ def sass_files
115
+ FileList["#{stylesheet_dir}/**/*.css"]
50
116
  end
51
117
  end
52
118
  end
@@ -0,0 +1,36 @@
1
+ module RSpec::Rails
2
+ module Migration
3
+ include RSpec::Rails::App::Dirs
4
+
5
+ def create_migration number, name, &block
6
+ file = migration_file_name(number, name)
7
+ unless File.exist?(file)
8
+ FileUtils.mkdir_p File.dirname(file)
9
+ File.open(file, 'w') do |f|
10
+ f.puts file_content(name)
11
+ yield f if block_given?
12
+ end
13
+ end
14
+ end
15
+
16
+ def remove_migration name
17
+ migrations = Dir.glob("#{migration_dir}/[0-9]*_*.rb")
18
+ migration_file = if !migrations.empty?
19
+ migrations.grep(/\d+_#{name}\.rb$/).first
20
+ else
21
+ false
22
+ end
23
+ FileUtils.rm_f(migration_file) if migration_file
24
+ end
25
+
26
+ def remove_migrations *names
27
+ names.each do |name|
28
+ remove_migration name
29
+ end
30
+ end
31
+
32
+ def migration_file_name number, name
33
+ File.join(migration_dir, "#{number}_#{name}.rb")
34
+ end
35
+ end
36
+ end
@@ -1,4 +1,4 @@
1
- require 'rspec_for_generators/rails_helpers/all'
1
+ require_all File.dirname(__FILE__) + '/rails_helpers'
2
2
 
3
3
  module TmpRails
4
4
  class << self
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require_generator :migration
3
+
4
+ describe 'migration_generator' do
5
+ include RSpec::Rails::Orm::ActiveRecord
6
+ include RSpec::Rails::Migration
7
+
8
+ before :each do
9
+ RSpec::Generator.setup_generator 'migration_generator' do
10
+ tests MigrationGenerator
11
+ end
12
+ end
13
+
14
+ after :each do
15
+ end
16
+
17
+ it "should generate create_user migration" do
18
+ RSpec::Generator.with_generator do |g|
19
+ name = 'create_users'
20
+ remove_migration 'create_users'
21
+ g.run_generator [name]
22
+ g.should generate_migration name do |content|
23
+ content.should have_migration name do |klass|
24
+ klass.should have_up do |up|
25
+ up.should have_create_table :users do |user_tbl|
26
+ user_tbl.should have_columns :name => :string, :age => :string
27
+ end
28
+ end
29
+
30
+ klass.should have_down do |up|
31
+ up.should have_drop_table :users
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+
@@ -2,5 +2,10 @@ require 'rspec'
2
2
  require 'rspec/autorun'
3
3
  require 'rspec_for_generators'
4
4
 
5
+ RSpec::Generator.remove_temp_dir = true
5
6
  RSpec::Generator.configure_root_dir __FILE__
6
7
 
8
+ class RSpec::Core::ExampleGroup
9
+
10
+ end
11
+
@@ -0,0 +1,72 @@
1
+ The RSpec 2 generator matchers are used to match on file content that are the result of running the generator under test.
2
+ These matchers can be used to test if the resulting file contains content/ruby code as expected (or specified).
3
+
4
+ * have_class name, &block
5
+ * have_module name, &block
6
+ * have_method name, &block, type:Symbol (:class)
7
+ * include_module name
8
+ * inherit_from name, &block
9
+ * have_call name, [args]
10
+ * have_block name, [options]
11
+
12
+ h3. Example use of matchers
13
+
14
+ <pre>
15
+ g.should generate_model name do |content|
16
+ content.should have_class name do |klass_content|
17
+ klass_content.should include_module 'Canable::Ables'
18
+ end
19
+ end
20
+ </pre>
21
+
22
+ h4. have_class
23
+
24
+ <pre>have_class MyClass do |class_content|
25
+ class_content.should ...
26
+ end
27
+ </pre>
28
+
29
+ h4. have_module
30
+
31
+ <pre>have_module MyModule do |module_content|
32
+ module_content.should ...
33
+ end
34
+ </pre>
35
+
36
+ h4. have_method
37
+
38
+ <pre>have_method :song do |song|
39
+ song.should ...
40
+ end
41
+ </pre>
42
+
43
+ h4. have_method
44
+
45
+ <pre>have_method :song do |song|
46
+ song.should ...
47
+ end
48
+ </pre>
49
+
50
+ h4. include_module
51
+
52
+ <pre>include_module 'UserRoles'</pre>
53
+
54
+ h4. inherit_from
55
+
56
+ <pre>inherit_from 'ActiveRecord::Base' do |klass|
57
+ klass.should ...
58
+ end
59
+ </pre>
60
+
61
+ h4. have_call
62
+
63
+ <pre>have_call 'move', :args => ':north'</pre>
64
+
65
+ Note: Currently not tested. Any issues?
66
+
67
+ h4. have_block
68
+
69
+ <pre>have_block 'move', :args => ':north', :block_args => 'place'</pre>
70
+
71
+ Note: Currently not tested. Any issues?
72
+
@@ -0,0 +1,17 @@
1
+ * generate_file name, type:Symbol, &block
2
+ * generate_dir name, type:Symbol, &block
3
+
4
+ *generate_file* can take any of the symbols below as a type argument and perform matchings on
5
+ that file in the appropriate location for that type of file in the Rails 3 app.
6
+
7
+ * :model, :controller, :view, :helper, :observer, :migration
8
+ * :javascript, :stylesheet, :initializer, :locale
9
+
10
+ The following are some convenience matchers that acts as wrappers on generate_file
11
+
12
+ * generate_model name, &block
13
+ * generate_controller name, &block
14
+ * generate_helper name, &block
15
+ * generate_view name, &block
16
+ * generate_observer name, &block
17
+ * generate_migration name, &block
@@ -1,74 +1,3 @@
1
- The Rails 3 spec helpers can be used to setup the temporary Rails 3 app used for the specs.
2
- Use the `create` methods to setup an application layout of expected files expected/required by the generator to test it.
3
-
4
- The `remove` methods can be used to cleanup before or after a test run, f.ex using the RSpec `before(:each)` and `after(:each)` blocks.
5
-
6
- h3. View helpers
7
-
8
- * create_view name, content
9
- * create_view name, &block
10
- * remove_view name
11
- * remove_views *names
12
-
13
- h3. Model helpers
14
-
15
- * create_model name, content
16
- * create_model name, &block
17
- * remove_model name
18
- * remove_models *names
19
-
20
- h3. Controller helpers
21
-
22
- * create_controller name, content
23
- * create_controller name, &block
24
- * remove_controller name
25
- * remove_controller *names
26
-
27
- h3. Mailer helpers
28
-
29
- * create_mailer name, content
30
- * create_mailer name, &block
31
- * remove_mailer name
32
- * remove_mailers *names
33
-
34
- h3. Helper helpers
35
-
36
- * create_helper name, content
37
- * create_helper name, &block
38
- * remove_helper name
39
- * remove_helpers *names
40
-
41
- --
42
-
43
- The RSpec 2 generator matchers are used to match on file content that are the result of running the generator under test.
44
- These matchers can be used to test if the resulting file contains content/ruby code as expected (or specified).
45
-
46
- * generate_file name, type:Symbol, &block
47
- * generate_dir name, type:Symbol, &block
48
- * generate_migration name, &block
49
-
50
- * have_class name, &block
51
- * have_module name, &block
52
- * have_method name, &block, type:Symbol (:class)
53
- * include_module name, &block
54
- * inherit_from name, &block
55
-
56
- *generate_file* can take any of the symbols below as a type argument and perform matchings on
57
- that file in the appropriate location in the Rails 3 app.
58
-
59
- * :model, :controller, :view, :helper, :observer, :migration
60
- * :javascript, :stylesheet, :initializer, :locale
61
-
62
- h3. Example use of matchers
63
-
64
- <pre>
65
- g.should generate_file name, :model do |content|
66
- content.should have_class name do |klass|
67
- klass.should include_module 'Canable::Ables'
68
- end
69
- end
70
- </pre>
71
-
72
1
  h2. Migration file
73
2
 
74
3
  There are a bunch of ways to match the layout of a generated migration file as the following demonstrates.
@@ -89,7 +18,7 @@ g.should have_migration(:create_users) do |content|
89
18
  end
90
19
  </pre>
91
20
 
92
- Migration file matcher
21
+ Migration class matcher
93
22
 
94
23
  * have_migration name, &block
95
24
 
@@ -0,0 +1,42 @@
1
+ The Rails 3 spec helpers can be used to setup the temporary Rails 3 app used for the specs.
2
+ Use the `create` methods to setup an application layout of expected files expected/required by the generator to test it.
3
+
4
+ The `remove` methods can be used to cleanup before or after a test run, f.ex using the RSpec `before(:each)` and `after(:each)` blocks.
5
+
6
+ h3. View helpers
7
+
8
+ * create_view name, content
9
+ * create_view name, &block
10
+ * remove_view name
11
+ * remove_views *names
12
+
13
+ h3. Model helpers
14
+
15
+ * create_model name, content
16
+ * create_model name, &block
17
+ * remove_model name
18
+ * remove_models *names
19
+
20
+ h3. Controller helpers
21
+
22
+ * create_controller name, content
23
+ * create_controller name, &block
24
+ * remove_controller name
25
+ * remove_controller *names
26
+
27
+ h3. Mailer helpers
28
+
29
+ * create_mailer name, content
30
+ * create_mailer name, &block
31
+ * remove_mailer name
32
+ * remove_mailers *names
33
+
34
+ h3. Helper helpers
35
+
36
+ * create_helper name, content
37
+ * create_helper name, &block
38
+ * remove_helper name
39
+ * remove_helpers *names
40
+
41
+
42
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 3
9
- version: 0.3.3
8
+ - 4
9
+ version: 0.3.4
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-04 00:00:00 +02:00
17
+ date: 2010-08-05 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -123,18 +123,22 @@ extra_rdoc_files:
123
123
  files:
124
124
  - .document
125
125
  - .gitignore
126
+ - .rspec
126
127
  - LICENSE
127
128
  - README.markdown
128
129
  - Rakefile
129
130
  - VERSION
130
131
  - generator-spec.gemspec
131
132
  - lib/generators/demo/demo_generator.rb
133
+ - lib/generators/migration/migration_generator.rb
134
+ - lib/generators/migration/templates/create_users.erb
132
135
  - lib/rspec_for_generators.rb
133
136
  - lib/rspec_for_generators/fixtures/routes.rb
134
137
  - lib/rspec_for_generators/generator_spec_helper.rb
135
138
  - lib/rspec_for_generators/main.rb
136
139
  - lib/rspec_for_generators/matchers/content/have_block.rb
137
140
  - lib/rspec_for_generators/matchers/content/have_call.rb
141
+ - lib/rspec_for_generators/matchers/content/have_calls.rb
138
142
  - lib/rspec_for_generators/matchers/content/have_class.rb
139
143
  - lib/rspec_for_generators/matchers/content/have_method.rb
140
144
  - lib/rspec_for_generators/matchers/content/have_module.rb
@@ -152,22 +156,28 @@ files:
152
156
  - lib/rspec_for_generators/matchers/migration/have_table.rb
153
157
  - lib/rspec_for_generators/matchers/migration/have_tbl_column.rb
154
158
  - lib/rspec_for_generators/matchers/migration/have_up_down.rb
155
- - lib/rspec_for_generators/rails_helpers/all.rb
156
159
  - lib/rspec_for_generators/rails_helpers/rails_app.rb
157
160
  - lib/rspec_for_generators/rails_helpers/rails_controller.rb
158
161
  - lib/rspec_for_generators/rails_helpers/rails_helper.rb
159
162
  - lib/rspec_for_generators/rails_helpers/rails_mailer.rb
163
+ - lib/rspec_for_generators/rails_helpers/rails_migration.rb
160
164
  - lib/rspec_for_generators/rails_helpers/rails_model.rb
161
165
  - lib/rspec_for_generators/rails_helpers/rails_orm.rb
162
166
  - lib/rspec_for_generators/rails_helpers/rails_view.rb
163
167
  - lib/rspec_for_generators/rails_spec_helper.rb
164
168
  - lib/rspec_for_generators/rspec_test_case.rb
165
169
  - rspec_for_generators.gemspec
166
- - spec/rspec_for_generators/rspec_for_generators_spec.rb
167
- - spec/spec.opts
170
+ - spec/rspec_for_generators/controller_generator_spec.rb
171
+ - spec/rspec_for_generators/helper_generator_spec.rb
172
+ - spec/rspec_for_generators/migration_generator_spec.rb
173
+ - spec/rspec_for_generators/model_generator_spec.rb
174
+ - spec/rspec_for_generators/view_generator_spec.rb
168
175
  - spec/spec_helper.rb
169
176
  - wiki/Custom Rails 3 Generators.textile
170
- - wiki/rails_view_helpers.textile
177
+ - wiki/content_matchers.textile
178
+ - wiki/file_matchers.textile
179
+ - wiki/migration_matchers.textile
180
+ - wiki/rails_helpers.textile
171
181
  has_rdoc: true
172
182
  homepage: http://github.com/kristianmandrup/rspec_for_generators
173
183
  licenses: []
@@ -201,5 +211,9 @@ signing_key:
201
211
  specification_version: 3
202
212
  summary: RSpec 2 library for writing generator specs
203
213
  test_files:
204
- - spec/rspec_for_generators/rspec_for_generators_spec.rb
214
+ - spec/rspec_for_generators/controller_generator_spec.rb
215
+ - spec/rspec_for_generators/helper_generator_spec.rb
216
+ - spec/rspec_for_generators/migration_generator_spec.rb
217
+ - spec/rspec_for_generators/model_generator_spec.rb
218
+ - spec/rspec_for_generators/view_generator_spec.rb
205
219
  - spec/spec_helper.rb
@@ -1,6 +0,0 @@
1
- require 'rspec_for_generators/rails_helpers/rails_app'
2
- require 'rspec_for_generators/rails_helpers/rails_orm'
3
- require 'rspec_for_generators/rails_helpers/rails_controller'
4
- require 'rspec_for_generators/rails_helpers/rails_helper'
5
- require 'rspec_for_generators/rails_helpers/rails_view'
6
- require 'rspec_for_generators/rails_helpers/rails_mailer'
@@ -1 +0,0 @@
1
- --color