generator-spec 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. data/README.markdown +14 -7
  2. data/VERSION +1 -1
  3. data/generator-spec.gemspec +2 -2
  4. data/lib/generator_spec/main.rb +22 -0
  5. data/lib/generator_spec/matchers/content/content_matcher.rb +6 -1
  6. data/lib/generator_spec/matchers/content/have_call.rb +9 -3
  7. data/lib/generator_spec/matchers/content/have_calls.rb +14 -2
  8. data/lib/generator_spec/matchers/file/generate_file.rb +1 -0
  9. data/lib/generator_spec/matchers/migration/have_column.rb +7 -1
  10. data/lib/generator_spec/rails_helpers/rails_view.rb +14 -6
  11. data/spec/generator_spec/generators/controller_generator_spec.rb +1 -1
  12. data/spec/generator_spec/generators/helper_generator_spec.rb +1 -1
  13. data/spec/generator_spec/generators/migration_generator_spec.rb +12 -6
  14. data/spec/generator_spec/generators/model_generator_spec.rb +1 -1
  15. data/spec/generator_spec/generators/observer_generator_spec.rb +1 -1
  16. data/spec/generator_spec/generators/view_generator_spec.rb +3 -3
  17. data/spec/generator_spec/matchers/content/class_self_spec.rb +2 -4
  18. data/spec/generator_spec/matchers/content/have_block_spec.rb +1 -1
  19. data/spec/generator_spec/matchers/content/have_call_spec.rb +1 -1
  20. data/spec/generator_spec/matchers/content/have_calls_spec.rb +29 -32
  21. data/spec/generator_spec/matchers/content/have_class_spec.rb +1 -1
  22. data/spec/generator_spec/matchers/content/have_method_spec.rb +1 -1
  23. data/spec/generator_spec/matchers/content/have_module_spec.rb +1 -1
  24. data/spec/generator_spec/matchers/content/have_region_spec.rb +1 -1
  25. data/spec/generator_spec/matchers/content/have_subclass_spec.rb +1 -1
  26. data/spec/generator_spec/matchers/content/include_module_spec.rb +1 -1
  27. data/spec/generator_spec/matchers/content/inherit_from_spec.rb +1 -1
  28. metadata +3 -3
@@ -89,16 +89,22 @@ require 'generators/canable'
89
89
 
90
90
  describe 'model_generator' do
91
91
  # include Rails model helpers for ActiveRecord
92
- # available: MongoMapper, Mongoid and DataMapper
92
+ # available:
93
+
94
+ # Other ORM options - :mongo_mapper, :mongoid and :data_mapper
95
+ # note: use_orm auto-includes the :model helper module
96
+ use_orm :active_record
93
97
 
94
- include RSpec::Rails::Orm::ActiveRecord
98
+ # load helper modules and make available inside spec blocks
99
+ # here the module in rails_helpers/rails_migration is included
100
+ helpers :migration
95
101
 
96
102
  before :each do
97
103
  # define generator to test
98
104
  setup_generator 'model_generator' do
99
105
  tests Canable::Generators::ModelGenerator
100
106
  end
101
- # ensure clean state before run
107
+ # ensure clean state before each run
102
108
  remove_model :account
103
109
  end
104
110
 
@@ -139,6 +145,8 @@ These mutations make it easy to setup the temporary Rails app in a specific pre-
139
145
 
140
146
  ### Examples: Rails helpers
141
147
 
148
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
149
+
142
150
  describe 'controller' do
143
151
  include RSpec::Rails::Controller
144
152
 
@@ -156,10 +164,9 @@ describe 'controller' do
156
164
  end
157
165
 
158
166
  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
167
+ Rails.application.should have_controller :account do |controller_file|
168
+ controller_file.should have_controller_class :account do |klass|
169
+ klass.should have_method :index
163
170
  end
164
171
  end
165
172
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.7
1
+ 0.4.8
@@ -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.7"
8
+ s.version = "0.4.8"
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-10}
12
+ s.date = %q{2010-08-11}
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 = [
@@ -42,6 +42,28 @@ module RSpec::Core
42
42
  end
43
43
  end
44
44
 
45
+ class Class
46
+ def use_orm orm
47
+ class_eval do
48
+ raise ArgumentError, "Unknown orm #{orm}" if ![:active_record, :mongoid, :mongo_mapper, :data_mapper].include?(orm)
49
+ include "RSpec::Rails::Orm::#{orm.to_s.camelize}".constantize
50
+ end
51
+ end
52
+
53
+ def helpers *types
54
+ types.each{|type| include_helper type}
55
+ end
56
+
57
+ protected
58
+
59
+ def include_helper type
60
+ raise ArgumentError, "Can not specify for #{type}" if ![:migration, :model, :helper, :controller, :view, :observer, :mailer].include?(type)
61
+ class_eval do
62
+ include "RSpec::Rails::#{type.to_s.camelize}".constantize
63
+ end
64
+ end
65
+ end
66
+
45
67
  require 'generator_spec/require_generator'
46
68
 
47
69
  require 'generator_spec/matchers/content/content_matcher'
@@ -14,6 +14,9 @@ module RSpec
14
14
  nil
15
15
  end
16
16
 
17
+ def debug?
18
+ false
19
+ end
17
20
 
18
21
  def matches? content, &block
19
22
  @content = content
@@ -28,8 +31,10 @@ module RSpec
28
31
 
29
32
  def is_match? content
30
33
  expr = get_expr(content)
34
+ puts "match expression: #{expr}" if debug?
31
35
  match = (content =~ expr)
32
36
  @content_matches = [$1, $2, $3]
37
+ match
33
38
  end
34
39
 
35
40
  def handle_result content, match, &block
@@ -71,7 +76,7 @@ module RSpec
71
76
  end
72
77
 
73
78
  def args_expr
74
- args ? SPACES + opt(LPAR) + OPT_SPACES + "#{args}" + OPT_SPACES + opt(RPAR) : ''
79
+ args ? OPT_SPACES + opt(LPAR) + OPT_SPACES + "#{args}" + OPT_SPACES + opt(RPAR) : ''
75
80
  end
76
81
 
77
82
  def main_expr
@@ -16,8 +16,14 @@ module RSpec::RubyContentMatchers
16
16
  end
17
17
 
18
18
  def matches?(content)
19
- @content = content
20
- (content =~ /#{dot_expr}#{method}#{args_expr}/m)
19
+ @content = content
20
+ has_def = (content =~ /def(.*)#{method}/)
21
+ expr = if has_def
22
+ /#{dot_expr}#{method}#{args_expr}/m
23
+ else
24
+ /#{dot_expr}?#{method}#{args_expr}/m
25
+ end
26
+ (content =~ expr)
21
27
  end
22
28
 
23
29
  def failure_message
@@ -33,7 +39,7 @@ module RSpec::RubyContentMatchers
33
39
  protected
34
40
 
35
41
  def not_def
36
- '[^def\s]?\s*'
42
+ '[^def\s]'
37
43
  end
38
44
 
39
45
  def dot_expr
@@ -26,7 +26,13 @@ module RSpec::RubyContentMatchers
26
26
  def do_list
27
27
  calls.each do |method|
28
28
  @method = method.to_s
29
- expr = /#{not_def}#{method}/m
29
+ expr =
30
+ has_def = (content =~ /def(.*)#{method}/)
31
+ expr = if has_def
32
+ /#{not_def}\s*#{method}/m
33
+ else
34
+ /#{not_def}?\s*#{method}/m
35
+ end
30
36
  return false if (content =~ expr) == nil
31
37
  end
32
38
  true
@@ -36,7 +42,13 @@ module RSpec::RubyContentMatchers
36
42
  calls.each_pair do |method, args|
37
43
  @method = method.to_s
38
44
  @args = args
39
- return false if (content =~ /#{not_def}#{method}\s+#{args_expr}/m) == nil
45
+ has_def = (content =~ /def(.*)#{method}/)
46
+ expr = if has_def
47
+ /#{not_def}\s*#{method}\s*#{args_expr}/m
48
+ else
49
+ /#{not_def}?\s*#{method}\s*#{args_expr}/m
50
+ end
51
+ return false if (content =~ expr) == nil
40
52
  end
41
53
  true
42
54
  end
@@ -18,6 +18,7 @@ module RSpec::FileMatchers
18
18
  file_exists = File.exists?(file)
19
19
  if block && file_exists
20
20
  read = File.read(file)
21
+ puts "file content: #{read}, #{file}"
21
22
  ruby_content = read.extend(RSpec::RubyContent::Helpers)
22
23
  yield ruby_content
23
24
  else
@@ -10,7 +10,13 @@ module RSpec::RubyContentMatchers
10
10
 
11
11
  def have_columns(columns = {})
12
12
  raise ArgumentError, "Columns must be passed as a :name => :type Hash" if !columns.kind_of? Hash
13
- HaveCalls.new columns
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
14
20
  end
15
21
 
16
22
  def have_change_column(name, type='string')
@@ -2,16 +2,24 @@ module RSpec::Rails
2
2
  module View
3
3
  include RSpec::Rails::App
4
4
 
5
- def create_view name, action='show', *args
5
+ def create_view name, action='show', *args, &block
6
6
  type, content = parse_args(*args)
7
7
  file = view_file_name(name, action, type)
8
+
9
+ # make dir
8
10
  unless File.exist?(file)
9
11
  FileUtils.mkdir_p File.dirname(file)
10
- content ||= yield if block_given?
11
- return if !content
12
- File.open(file, 'w') do |f|
13
- f.puts content
14
- end
12
+ end
13
+
14
+ # set content to block
15
+ content = yield if block
16
+
17
+ # abort if no content given
18
+ return if !content
19
+
20
+ # write file content of view
21
+ File.open(file, 'w') do |f|
22
+ f.puts content
15
23
  end
16
24
  end
17
25
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
  require_generator :controller
3
3
 
4
4
  module RSpec::Core
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  RSpec::Generator.debug = true
4
4
 
@@ -1,17 +1,24 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  RSpec::Generator.debug = true
4
4
 
5
5
  require_generator :migration
6
6
 
7
+ class Class
8
+ def spec
9
+ RSpec::DSL
10
+ end
11
+ end
12
+
7
13
  describe 'migration_generator' do
8
- include RSpec::Rails::Orm::ActiveRecord
9
- include RSpec::Rails::Migration
14
+ use_orm :active_record
15
+ helpers :migration, :helper
10
16
 
11
17
  before :each do
12
18
  setup_generator 'migration_generator' do
13
19
  tests MigrationGenerator
14
- end
20
+ end
21
+ remove_migration :create_users
15
22
  end
16
23
 
17
24
  after :each do
@@ -19,13 +26,12 @@ describe 'migration_generator' do
19
26
 
20
27
  it "should generate create_user migration" do
21
28
  with_generator do |g|
22
- remove_migration :create_users
23
29
  g.run_generator [:create_users].args
24
30
  g.should generate_migration :create_users do |content|
25
31
  content.should have_migration :create_users do |klass|
26
32
  klass.should have_up do |up|
27
33
  up.should have_create_table :users do |user_tbl|
28
- user_tbl.should have_columns :name => :string, :age => :string
34
+ user_tbl.should have_columns :name => :string, :age => :integer, :admin => :boolean
29
35
  user_tbl.should_not have_timestamps
30
36
  end
31
37
  end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
  require_generator :model
3
3
 
4
4
  describe 'model_generator' do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
  require_generator :observer
3
3
 
4
4
  module RSpec::Core
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
  require_generator :view
3
3
 
4
4
  module RSpec::Core
@@ -39,8 +39,8 @@ describe 'view_generator' do
39
39
  '# view content'
40
40
  end
41
41
  g.run_generator [name, action, type].map(&:to_s)
42
- g.should generate_view name, action, type do |content|
43
- content.should match /Hello You/
42
+ g.should generate_view name, action, type do |file|
43
+ file.should match /Hello You/
44
44
  end
45
45
  end
46
46
  end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'class_self matcher' do
4
4
  context "content without class << self" do
@@ -7,9 +7,7 @@ describe 'class_self matcher' do
7
7
  end}
8
8
 
9
9
  it "should not have class << self" do
10
- not_class_self.should_not have_class_self do |content|
11
- puts content
12
- end
10
+ not_class_self.should_not have_class_self
13
11
  end
14
12
  end
15
13
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'block matcher' do
4
4
  context "content without block" do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'method call matcher' do
4
4
  context "content without method call" do
@@ -1,21 +1,21 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'method calls matcher' do
4
- # context "content without calls" do
5
- # not_class_self = %q{
6
- # def x
7
- # end
8
- #
9
- # def y
10
- # end
11
- # }
12
- #
13
- # it "should not have calls to x or y" do
14
- # not_class_self.should_not have_calls :x, :y do |content|
15
- # puts content
16
- # end
17
- # end
18
- # end
4
+ context "content without calls" do
5
+ not_class_self = %q{
6
+ def x
7
+ end
8
+
9
+ def y
10
+ end
11
+ }
12
+
13
+ it "should not have calls to x or y" do
14
+ not_class_self.should_not have_calls :x, :y do |content|
15
+ puts content
16
+ end
17
+ end
18
+ end
19
19
 
20
20
  context "content with calls to x and y" do
21
21
  class_self = %q{
@@ -31,20 +31,17 @@ describe 'method calls matcher' do
31
31
  end
32
32
  end
33
33
 
34
- # context "content with calls to x and y" do
35
- # class_self = %q{
36
- # def x
37
- # x(2)
38
- # y 3
39
- # end}
40
- #
41
- # it "should have calls: x(2) and y 3" do
42
- # class_self.should have_calls :x => '2', :y => '3' do |content|
43
- # puts "content: #{content}"
44
- # end
45
- # end
46
- # end
34
+ context "content with calls to x and y" do
35
+ class_self = %q{
36
+ def x
37
+ x(2)
38
+ y 3
39
+ end}
40
+
41
+ it "should have calls: x(2) and y 3" do
42
+ class_self.should have_calls :x => '2', :y => '3' do |content|
43
+ puts "content: #{content}"
44
+ end
45
+ end
46
+ end
47
47
  end
48
-
49
-
50
-
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'module matcher' do
4
4
  context "content without class Hello" do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'method matcher' do
4
4
  context "content without method" do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'module matcher' do
4
4
  context "content without module Hello" do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'region matcher' do
4
4
  context "content with only protected region" do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'subclass matcher' do
4
4
  context "content without subclass Greeting" do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'include module matcher' do
4
4
  context "content without include Greeting" do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
2
 
3
3
  describe 'inherit matcher' do
4
4
  context "content with class that inherits from Greeting" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 7
9
- version: 0.4.7
8
+ - 8
9
+ version: 0.4.8
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-10 00:00:00 +02:00
17
+ date: 2010-08-11 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency