generator-spec 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{generator-spec}
8
- s.version = "0.4.0"
8
+ s.version = "0.4.1"
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-07}
12
+ s.date = %q{2010-08-08}
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 = [
@@ -1,7 +1,5 @@
1
- require 'rspec_for_generators/migration_helper'
2
-
3
1
  class MigrationGenerator < Rails::Generators::NamedBase
4
- include MigrationHelper
2
+ include Rails::Generators::MigrationHelper
5
3
 
6
4
  desc "Creates a migration"
7
5
 
@@ -3,6 +3,7 @@ require 'rake'
3
3
  require 'rails'
4
4
  require 'require_all'
5
5
  require 'rails/generators'
6
+ require 'active_support/core_ext/module/aliasing'
6
7
  require 'rspec_for_generators/main'
7
8
 
8
9
  RSpec.configure do |config|
@@ -2,21 +2,26 @@ require 'test/unit'
2
2
  require 'rails/all'
3
3
  require 'rails/generators'
4
4
  require 'rails/generators/test_case'
5
-
6
5
  require 'rspec_for_generators/rails_spec_helper'
7
6
 
8
7
  # Call configure to load the settings from
9
8
  # Rails.application.config.generators to Rails::Generators
10
9
 
11
10
  # require the generators
12
- def require_generators *generator_list
13
- generator_list.each do |name, generators|
14
- if !generators || generators.empty
15
- require File.join('generators', name.to_s, "#{name.to_s}_generator")
16
- else
17
- generators.each do |generator_name|
18
- require File.join('generators', name.to_s, generator_name.to_s, "#{generator_name}_generator")
19
- end
11
+ def require_generators *generator_list
12
+ req = RSpec::Generator::Require
13
+ generator_list.each do |name, generators|
14
+ case name
15
+ when Hash
16
+ req.parse_generator_hash name
17
+ when Symbol
18
+ if !generators || generators.empty?
19
+ req.require_generator! name
20
+ else
21
+ req.parse_generator_list name, generators
22
+ end
23
+ else
24
+ raise ArgumentError, "required generator(s) must be stated either using a symbol or array of symbols"
20
25
  end
21
26
  end
22
27
  end
@@ -24,8 +29,40 @@ alias :require_generator :require_generators
24
29
 
25
30
  module RSpec
26
31
  module Generator
32
+ module Require
33
+ class << self
34
+ def parse_generator_hash hash
35
+ parse_generator_list hash.keys.first, hash.values.flatten
36
+ end
37
+
38
+ def parse_generator_list name, generators
39
+ if generators.empty?
40
+ require_generator! name
41
+ else
42
+ generators.each do |generator_name|
43
+ require_generator! name, generator_name
44
+ end
45
+ end
46
+ end
47
+
48
+ def require_generator! name, generator=nil
49
+ file = (require_file_name name, generator)
50
+ puts "Loading generator: #{file}" if RSpec::Generator.debug?
51
+ require file
52
+ end
53
+
54
+ def require_file_name name, name_space=nil
55
+ File.join('generators', name.to_s, "#{name_space}", "#{name_space || name}_generator")
56
+ end
57
+ end
58
+ end
59
+
27
60
  class << self
28
- attr_accessor :generator, :test_method_name, :remove_temp_dir
61
+ attr_accessor :generator, :test_method_name, :remove_temp_dir, :debug
62
+
63
+ def debug?
64
+ debug
65
+ end
29
66
 
30
67
  def remove_rails_dir!
31
68
  FileUtils.rm_rf ::TmpRails.root
@@ -1,3 +1,4 @@
1
+ require 'rspec_for_generators/migration_helper'
1
2
  require 'rspec_for_generators/rails_spec_helper'
2
3
  require 'rspec_for_generators/rspec_test_case'
3
4
  require 'rspec_for_generators/generator_spec_helper'
@@ -3,7 +3,7 @@ module RSpec::RubyContentMatchers
3
3
  attr_reader :name, :args, :block_args
4
4
 
5
5
  def initialize(name, options={})
6
- @name = name
6
+ @name = name.to_s
7
7
  @args = options[:args]
8
8
  @block_args = options[:block_args]
9
9
  end
@@ -9,7 +9,7 @@ module RSpec::RubyContentMatchers
9
9
 
10
10
  def matches?(content)
11
11
  calls.each_pair do |method, args|
12
- @method = method
12
+ @method = method.to_s
13
13
  @args = args
14
14
  return false if (content =~ /#{method}\s+#{args_expr}/m) == nil
15
15
  end
@@ -6,35 +6,37 @@
6
6
 
7
7
  module RSpec::FileMatchers
8
8
  class GenerateDir
9
+ attr_accessor :relative_path
9
10
 
10
11
  def initialize(relative_path, type = nil)
11
- @relative_path = relative_rails_dir relative_path, type
12
+ @relative_path = relative_rails_dir(relative_path, type)
12
13
  end
13
14
 
14
15
  def matches?(generator, &block)
15
- @expected = File.expand_path(@relative_path, generator.class.destination_root)
16
- ex = File.exists?(@expected)
17
- if block && ex
18
- read = File.read(@expected)
16
+ file = File.expand_path(relative_path, generator.class.destination_root)
17
+ file_exists = File.exists?(expected)
18
+ if block && file_exists
19
+ read = File.read(file)
19
20
  ruby_content = read.extend(RSpec::RubyContent::Helpers)
20
21
  yield ruby_content
21
22
  else
22
- ex
23
+ file_exists
23
24
  end
24
25
  end
25
26
 
26
27
  def failure_message
27
- "Expected file #{@relative_path} to have been generated, but it was not"
28
+ "Expected file #{relative_path} to have been generated, but it was not"
28
29
  end
29
30
 
30
31
  def negative_failure_message
31
- "Did not expected file #{@relative_path} to have been generated, but it was"
32
+ "Did not expected file #{relative_path} to have been generated, but it was"
32
33
  end
33
34
 
34
35
  protected
35
36
 
36
37
  def relative_rails_dir path, type = nil
37
- return File.join(::Rails.root, base_dir(type), folder(type), "#{path.to_s}") if type
38
+ path = path.to_s
39
+ return File.join(::Rails.root, base_dir(type), folder(type), "#{path}") if type
38
40
  File.join(::Rails.root, path)
39
41
  end
40
42
 
@@ -7,33 +7,36 @@
7
7
  module RSpec::FileMatchers
8
8
  class GenerateFile
9
9
 
10
+ attr_accessor :relative_path
11
+
10
12
  def initialize(relative_path, type = nil)
11
13
  @relative_path = relative_rails_file(relative_path, type)
12
14
  end
13
15
 
14
16
  def matches?(generator, &block)
15
- @expected = File.expand_path(@relative_path, generator.class.destination_root)
16
- ex = File.exists?(@expected)
17
- if block && ex
18
- read = File.read(@expected)
17
+ file = File.expand_path(relative_path, generator.class.destination_root)
18
+ file_exists = File.exists?(file)
19
+ if block && file_exists
20
+ read = File.read(file)
19
21
  ruby_content = read.extend(RSpec::RubyContent::Helpers)
20
22
  yield ruby_content
21
23
  else
22
- ex
24
+ file_exists
23
25
  end
24
26
  end
25
27
 
26
28
  def failure_message
27
- "Expected file #{@relative_path} to have been generated, but it was not"
29
+ "Expected file #{relative_path} to have been generated, but it was not"
28
30
  end
29
31
 
30
32
  def negative_failure_message
31
- "Did not expected file #{@relative_path} to have been generated, but it was"
33
+ "Did not expected file #{relative_path} to have been generated, but it was"
32
34
  end
33
35
 
34
36
  protected
35
37
 
36
38
  def relative_rails_file path, type = nil
39
+ path = path.to_s
37
40
  f_name = file_name(path, type)
38
41
  return File.join(::Rails.root, base_dir(type), folder(type), f_name) if type
39
42
  File.join(::Rails.root, path)
@@ -13,7 +13,7 @@ module RSpec
13
13
  attr_reader :name
14
14
 
15
15
  def initialize(name)
16
- @name = name
16
+ @name = name.to_s
17
17
  end
18
18
 
19
19
  def matches?(generator)
@@ -1,39 +1,43 @@
1
- module MigrationHelper
2
- include Rails::Generators::Migration
1
+ module Rails
2
+ module Generators
3
+ module MigrationHelper
4
+ include Rails::Generators::Migration
3
5
 
4
- module ClassMethods
5
- def migration_lookup_at(dirname) #:nodoc:
6
- Dir.glob("#{dirname}/[0-9]*_*.rb")
7
- end
6
+ module ClassMethods
7
+ def migration_lookup_at(dirname) #:nodoc:
8
+ Dir.glob("#{dirname}/[0-9]*_*.rb")
9
+ end
8
10
 
9
- def migration_exists?(dirname, file_name) #:nodoc:
10
- migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first
11
- end
11
+ def migration_exists?(dirname, file_name) #:nodoc:
12
+ migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first
13
+ end
12
14
 
13
- def current_migration_number(dirname) #:nodoc:
14
- migration_lookup_at(dirname).collect do |file|
15
- File.basename(file).split("_").first.to_i
16
- end.max.to_i
17
- end
15
+ def current_migration_number(dirname) #:nodoc:
16
+ migration_lookup_at(dirname).collect do |file|
17
+ File.basename(file).split("_").first.to_i
18
+ end.max.to_i
19
+ end
18
20
 
19
- def next_migration_number(dirname) #:nodoc:
20
- orm = Rails.configuration.generators.options[:rails][:orm]
21
- require "rails/generators/#{orm}"
22
- "#{orm.to_s.camelize}::Generators::Base".constantize.next_migration_number(dirname)
23
- rescue
24
- raise NotImplementedError
25
- end
26
- end
21
+ def next_migration_number(dirname) #:nodoc:
22
+ orm = Rails.configuration.generators.options[:rails][:orm]
23
+ require "rails/generators/#{orm}"
24
+ "#{orm.to_s.camelize}::Generators::Base".constantize.next_migration_number(dirname)
25
+ rescue
26
+ raise NotImplementedError
27
+ end
28
+ end
27
29
 
28
- def self.included(base) #:nodoc:
29
- base.extend ClassMethods
30
- end
30
+ def self.included(base) #:nodoc:
31
+ base.extend ClassMethods
32
+ end
31
33
 
32
- def info msg
33
- Rails.logger.info msg
34
- end
34
+ def info msg
35
+ Rails.logger.info msg
36
+ end
35
37
 
36
- def migration(options)
37
- migration_template "create_users.erb", "db/migrations/#{name}"
38
+ def migration(options)
39
+ migration_template "create_users.erb", "db/migrations/#{name}"
40
+ end
41
+ end
38
42
  end
39
43
  end
@@ -1,5 +1,8 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require_generator :helper
2
+
3
+ RSpec::Generator.debug = true
4
+
5
+ require_generator :helper => [:hello, :goodbye]
3
6
 
4
7
  module RSpec::Core
5
8
  class ExampleGroup
@@ -16,9 +16,9 @@ describe 'migration_generator' do
16
16
 
17
17
  it "should generate create_user migration" do
18
18
  with_generator do |g|
19
- name = 'create_users'
20
- remove_migration 'create_users'
21
- g.run_generator [name]
19
+ name = :create_users
20
+ remove_migration :create_users
21
+ g.run_generator [name.to_s]
22
22
  g.should generate_migration name do |content|
23
23
  content.should have_migration name do |klass|
24
24
  klass.should have_up do |up|
@@ -5,12 +5,6 @@ require 'rspec_for_generators'
5
5
  RSpec::Generator.remove_temp_dir = true
6
6
  RSpec::Generator.configure_root_dir __FILE__
7
7
 
8
- # RSpec::Core::ExampleGroup
9
-
10
- # RSpec.configure do |c|
11
- # c.extend RSpec::Generator
12
- # end
13
-
14
8
  module Rails
15
9
  def self.logger
16
10
  @logger ||= Logger.new(STDOUT)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 0
9
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
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-07 00:00:00 +02:00
17
+ date: 2010-08-08 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency