generator-spec 0.5.0 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +0 -0
- data/.gitignore +0 -0
- data/.rspec +0 -0
- data/LICENSE +0 -0
- data/README.markdown +19 -13
- data/Rakefile +14 -35
- data/VERSION +1 -1
- data/generator-spec.gemspec +63 -148
- data/lib/generator-spec.rb +12 -0
- data/lib/generator-spec/namespaces.rb +7 -0
- data/lib/{generator_spec/helpers/configure_rails.rb → generator-spec/rails/configure.rb} +3 -3
- data/lib/generator-spec/rspec.rb +1 -0
- data/lib/generator-spec/rspec/configure.rb +6 -0
- data/lib/{generator_spec/rspec_generator/generator_util.rb → generator-spec/rspec/generator.rb} +6 -6
- data/lib/{generator_spec/rspec_generator → generator-spec/rspec/generator}/configure.rb +0 -0
- data/lib/{generator_spec/require_generator.rb → generator-spec/rspec/generator/require.rb} +0 -0
- data/lib/{generator_spec/rspec_generator → generator-spec/rspec/generator}/routes.rb +0 -0
- data/lib/{generator_spec/rspec_generator → generator-spec/rspec/generator}/test_case.rb +0 -0
- data/lib/generator-spec/rspec/macro.rb +18 -0
- data/lib/generator-spec/rspec/matchers/generate_artifact.rb +34 -0
- data/lib/generator-spec/rspec/matchers/generate_artifacts.rb +33 -0
- data/lib/generators/controller/controller_generator.rb +15 -21
- data/lib/generators/helper/helper_generator.rb +13 -29
- data/lib/generators/migration/migration_generator.rb +6 -7
- data/lib/generators/migration/templates/create_users.erb +0 -0
- data/lib/generators/model/model_generator.rb +36 -55
- data/lib/generators/observer/observer_generator.rb +2 -0
- data/lib/generators/view/view_generator.rb +2 -0
- data/spec/generator_spec/generators/controller_gen_spec.rb +40 -0
- data/spec/generator_spec/generators/{helper_generator_spec.rb → helper_gen_spec.rb} +4 -8
- data/spec/generator_spec/generators/{migration_generator_spec.rb → migration_gen_spec.rb} +3 -8
- data/spec/generator_spec/generators/model_gen_spec.rb +36 -0
- data/spec/generator_spec/generators/{observer_generator_spec.rb → observer_gen_spec.rb} +12 -21
- data/spec/generator_spec/generators/{view_generator_spec.rb → view_gen_spec.rb} +9 -20
- data/spec/generator_spec/matchers/generate_artifact_spec.rb +36 -0
- data/spec/generator_spec/matchers/generate_artifacts_spec.rb +35 -0
- data/spec/spec_helper.rb +5 -6
- data/wiki/Custom Rails 3 Generators.textile b/data/wiki/Custom Rails 3 → Generators.textile +0 -0
- data/wiki/content_matchers.textile +0 -0
- data/wiki/file_matchers.textile +0 -0
- data/wiki/migration_matchers.textile +0 -0
- data/wiki/rails_helpers.textile +0 -0
- metadata +117 -154
- data/lib/generator_spec.rb +0 -12
- data/lib/generator_spec/extensions/core_ext.rb +0 -47
- data/lib/generator_spec/helpers/generator_helper.rb +0 -11
- data/lib/generator_spec/helpers/migration_helper.rb +0 -69
- data/lib/generator_spec/main.rb +0 -77
- data/lib/generator_spec/matchers/content/content_matcher.rb +0 -110
- data/lib/generator_spec/matchers/content/have_block.rb +0 -57
- data/lib/generator_spec/matchers/content/have_call.rb +0 -59
- data/lib/generator_spec/matchers/content/have_calls.rb +0 -60
- data/lib/generator_spec/matchers/content/have_class_self.rb +0 -40
- data/lib/generator_spec/matchers/content/have_comment.rb +0 -42
- data/lib/generator_spec/matchers/content/have_method.rb +0 -66
- data/lib/generator_spec/matchers/content/have_module.rb +0 -66
- data/lib/generator_spec/matchers/content/have_region.rb +0 -48
- data/lib/generator_spec/matchers/content/have_subclass.rb +0 -50
- data/lib/generator_spec/matchers/content/include_module.rb +0 -51
- data/lib/generator_spec/matchers/content/inherit_from.rb +0 -40
- data/lib/generator_spec/matchers/file/generate_directory.rb +0 -73
- data/lib/generator_spec/matchers/file/generate_file.rb +0 -103
- data/lib/generator_spec/matchers/file/generate_migration.rb +0 -54
- data/lib/generator_spec/matchers/file/have_file.rb +0 -115
- data/lib/generator_spec/matchers/helpers/content.rb +0 -18
- data/lib/generator_spec/matchers/helpers/file.rb +0 -26
- data/lib/generator_spec/matchers/helpers/migration.rb +0 -89
- data/lib/generator_spec/matchers/migration/have_column.rb +0 -29
- data/lib/generator_spec/matchers/migration/have_index.rb +0 -9
- data/lib/generator_spec/matchers/migration/have_table.rb +0 -18
- data/lib/generator_spec/matchers/migration/have_tbl_column.rb +0 -25
- data/lib/generator_spec/matchers/migration/have_up_down.rb +0 -15
- data/lib/generator_spec/rails_helpers/base_helper.rb +0 -171
- data/lib/generator_spec/rails_helpers/rails_app.rb +0 -166
- data/lib/generator_spec/rails_helpers/rails_controller.rb +0 -44
- data/lib/generator_spec/rails_helpers/rails_helper.rb +0 -40
- data/lib/generator_spec/rails_helpers/rails_mailer.rb +0 -39
- data/lib/generator_spec/rails_helpers/rails_migration.rb +0 -51
- data/lib/generator_spec/rails_helpers/rails_model.rb +0 -40
- data/lib/generator_spec/rails_helpers/rails_observer.rb +0 -39
- data/lib/generator_spec/rails_helpers/rails_orm.rb +0 -114
- data/lib/generator_spec/rails_helpers/rails_view.rb +0 -65
- data/rspec_for_generators.gemspec +0 -96
- data/spec/generator_spec/generators/controller_generator_spec.rb +0 -47
- data/spec/generator_spec/generators/model_generator_spec.rb +0 -41
- data/spec/generator_spec/matchers/content/class_self_spec.rb +0 -30
- data/spec/generator_spec/matchers/content/have_block_spec.rb +0 -57
- data/spec/generator_spec/matchers/content/have_call_spec.rb +0 -55
- data/spec/generator_spec/matchers/content/have_calls_spec.rb +0 -47
- data/spec/generator_spec/matchers/content/have_class_spec.rb +0 -34
- data/spec/generator_spec/matchers/content/have_method_spec.rb +0 -52
- data/spec/generator_spec/matchers/content/have_module_spec.rb +0 -34
- data/spec/generator_spec/matchers/content/have_region_spec.rb +0 -56
- data/spec/generator_spec/matchers/content/have_subclass_spec.rb +0 -34
- data/spec/generator_spec/matchers/content/include_module_spec.rb +0 -36
- data/spec/generator_spec/matchers/content/inherit_from_spec.rb +0 -34
- data/spec/generator_spec/matchers/file/directory_spec.rb +0 -0
- data/spec/generator_spec/matchers/file/file_spec.rb +0 -0
- data/spec/generator_spec/matchers/file/migration_spec.rb +0 -0
- data/spec/generator_spec/matchers/migration/have_column_spec.rb +0 -0
- data/spec/generator_spec/matchers/migration/have_index_spec.rb +0 -0
- data/spec/generator_spec/matchers/migration/have_table_spec.rb +0 -0
- data/spec/generator_spec/matchers/migration/have_tbl_column_spec.rb +0 -0
- data/spec/generator_spec/matchers/migration/have_up_down_spec.rb +0 -0
- data/spec/generator_spec/matchers/rails/controller_matcher_spec.rb +0 -27
- data/spec/generator_spec/matchers/rails/helper_matcher_spec.rb +0 -26
- data/spec/generator_spec/matchers/rails/mailer_matcher_spec.rb +0 -24
- data/spec/generator_spec/matchers/rails/model_matcher_spec.rb +0 -31
- data/spec/generator_spec/matchers/rails/observer_matcher_spec.rb +0 -19
- data/spec/generator_spec/matchers/rails/view_matcher_spec.rb +0 -23
- data/spec/generator_spec/rails_helpers/rails_app_spec.rb +0 -0
- data/spec/generator_spec/rails_helpers/rails_controller_spec.rb +0 -33
- data/spec/generator_spec/rails_helpers/rails_helper_spec.rb +0 -33
- data/spec/generator_spec/rails_helpers/rails_mailer_spec.rb +0 -33
- data/spec/generator_spec/rails_helpers/rails_migration_spec.rb +0 -0
- data/spec/generator_spec/rails_helpers/rails_model_spec.rb +0 -31
- data/spec/generator_spec/rails_helpers/rails_observer_spec.rb +0 -33
- data/spec/generator_spec/rails_helpers/rails_orm_spec.rb +0 -0
- data/spec/generator_spec/rails_helpers/rails_view_spec.rb +0 -31
data/lib/generator_spec.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
require 'rake'
|
3
|
-
require 'rails'
|
4
|
-
require 'require_all'
|
5
|
-
require 'rails/generators'
|
6
|
-
require 'active_support/core_ext/module/aliasing'
|
7
|
-
require 'generator_spec/main'
|
8
|
-
|
9
|
-
RSpec.configure do |config|
|
10
|
-
config.include(RSpec::FileMatchers)
|
11
|
-
config.include(RSpec::RubyContentMatchers)
|
12
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
class Array
|
2
|
-
def args
|
3
|
-
flatten.map{|a| a.args}.flatten
|
4
|
-
end
|
5
|
-
end
|
6
|
-
|
7
|
-
class Symbol
|
8
|
-
def args
|
9
|
-
[to_s]
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class String
|
14
|
-
def args
|
15
|
-
(self =~ /\w+\s+\w+/) ? self.split : self
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class Module
|
20
|
-
def multi_alias name, options={}
|
21
|
-
config_options = options[:options]
|
22
|
-
options.each_pair do |original, aliases|
|
23
|
-
next if original == :options
|
24
|
-
alias_methods name, original, [aliases].flatten, config_options
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
protected
|
29
|
-
|
30
|
-
def alias_methods name, original, aliases, config_options
|
31
|
-
aliases.each do |alias_name|
|
32
|
-
new_alias = make_name(name, alias_name.to_s, config_options)
|
33
|
-
original_name = make_name(name, original.to_s, config_options)
|
34
|
-
alias_method new_alias, original_name if respond_to? original_name
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def make_name name, alias_name, config_options
|
39
|
-
return alias_name.gsub(/X/, name.to_s) if alias_name =~ /X/
|
40
|
-
case config_options
|
41
|
-
when :after
|
42
|
-
"#{alias_name}_#{name}"
|
43
|
-
when :before
|
44
|
-
"#{name}_#{alias_name}"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
module Rails::Generators
|
2
|
-
module MigrationHelper
|
3
|
-
include Rails::Generators::Migration
|
4
|
-
include Rails::Generators::BasicHelper
|
5
|
-
|
6
|
-
module ClassMethods
|
7
|
-
def migration_lookup_at(dirname) #:nodoc:
|
8
|
-
Dir.glob("#{dirname}/[0-9]*_*.rb")
|
9
|
-
end
|
10
|
-
|
11
|
-
def migration_exists?(dirname, file_name) #:nodoc:
|
12
|
-
migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first
|
13
|
-
end
|
14
|
-
alias_method :first_migration_file, :migration_exists?
|
15
|
-
|
16
|
-
def latest_migration_file(dirname, file_name) #:nodoc:
|
17
|
-
migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).sort.last
|
18
|
-
end
|
19
|
-
|
20
|
-
def current_migration_number(dirname) #:nodoc:
|
21
|
-
migration_lookup_at(dirname).collect do |file|
|
22
|
-
File.basename(file).split("_").first.to_i
|
23
|
-
end.max.to_i
|
24
|
-
end
|
25
|
-
|
26
|
-
def next_migration_number(dirname) #:nodoc:
|
27
|
-
orm = Rails.configuration.generators.options[:rails][:orm]
|
28
|
-
require "rails/generators/#{orm}"
|
29
|
-
"#{orm.to_s.camelize}::Generators::Base".constantize.next_migration_number(dirname)
|
30
|
-
rescue
|
31
|
-
raise NotImplementedError
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.included(base) #:nodoc:
|
36
|
-
base.extend ClassMethods
|
37
|
-
end
|
38
|
-
|
39
|
-
def reverse_migration_name name
|
40
|
-
name.gsub(/^add_/, 'remove_').gsub(/^create_/, 'drop_')
|
41
|
-
end
|
42
|
-
|
43
|
-
def reverse_migration! migration_path
|
44
|
-
reverse_class_names! migration_path
|
45
|
-
reverse_up_down_methods! migration_path
|
46
|
-
end
|
47
|
-
|
48
|
-
def reverse_class_names! migration_path
|
49
|
-
# Change class name
|
50
|
-
gsub_file migration_path, /class Add/, 'class Remove'
|
51
|
-
gsub_file migration_path, /class Create/, 'class Drop'
|
52
|
-
end
|
53
|
-
|
54
|
-
def reverse_up_down_methods! migration_path
|
55
|
-
# swap up and down methods
|
56
|
-
gsub_file migration_path, /up/, 'dwn'
|
57
|
-
gsub_file migration_path, /down/, 'up'
|
58
|
-
gsub_file migration_path, /dwn/, 'down'
|
59
|
-
end
|
60
|
-
|
61
|
-
def latest_migration_file dir, name
|
62
|
-
self.class.latest_migration_file dir, name
|
63
|
-
end
|
64
|
-
|
65
|
-
def migration(options)
|
66
|
-
migration_template "create_users.erb", "db/migrations/#{name}"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
data/lib/generator_spec/main.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
module Generator
|
3
|
-
end
|
4
|
-
end
|
5
|
-
|
6
|
-
module RSpec
|
7
|
-
module Rails
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
module Rails
|
12
|
-
module Generators
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
|
17
|
-
module RSpec
|
18
|
-
module RubyContentMatchers
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
module RSpec
|
23
|
-
module FileMatchers
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
require_all File.dirname(__FILE__) + '/extensions'
|
28
|
-
require_all File.dirname(__FILE__) + '/helpers'
|
29
|
-
require_all File.dirname(__FILE__) + '/rails_helpers'
|
30
|
-
require_all File.dirname(__FILE__) + '/rspec_generator'
|
31
|
-
|
32
|
-
|
33
|
-
module RSpec::Core
|
34
|
-
class ExampleGroup
|
35
|
-
def with_generator &block
|
36
|
-
RSpec::Generator.with_generator &block
|
37
|
-
end
|
38
|
-
|
39
|
-
def setup_generator test_method_name=nil, &block
|
40
|
-
RSpec::Generator.setup_generator test_method_name, &block
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
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
|
-
alias_method :load_helper, :helpers
|
57
|
-
alias_method :load_helpers, :helpers
|
58
|
-
|
59
|
-
protected
|
60
|
-
|
61
|
-
def include_helper type
|
62
|
-
raise ArgumentError, "Can not specify for #{type}" if ![:migration, :model, :helper, :controller, :view, :observer, :mailer].include?(type)
|
63
|
-
class_eval do
|
64
|
-
include "RSpec::Rails::#{type.to_s.camelize}".constantize
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
require 'generator_spec/require_generator'
|
70
|
-
|
71
|
-
require 'generator_spec/matchers/content/content_matcher'
|
72
|
-
require_all File.dirname(__FILE__) + '/matchers/content'
|
73
|
-
|
74
|
-
require_all File.dirname(__FILE__) + '/matchers/file'
|
75
|
-
require_all File.dirname(__FILE__) + '/matchers/migration'
|
76
|
-
|
77
|
-
require_all File.dirname(__FILE__) + '/matchers/helpers'
|
@@ -1,110 +0,0 @@
|
|
1
|
-
module RSpec
|
2
|
-
class RubyContentMatcher
|
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
|
-
def debug?
|
18
|
-
RSpec::Generator.debug?
|
19
|
-
end
|
20
|
-
|
21
|
-
def matches? content, &block
|
22
|
-
@content = content
|
23
|
-
match = is_match? content
|
24
|
-
content_to_yield = if indexes
|
25
|
-
content_matches[indexes.first] || content_matches[indexes.last]
|
26
|
-
else
|
27
|
-
content_matches[index]
|
28
|
-
end
|
29
|
-
handle_result(content_to_yield, match, &block)
|
30
|
-
end
|
31
|
-
|
32
|
-
def is_match? content
|
33
|
-
expr = get_expr(content)
|
34
|
-
debug "match expression: #{expr}"
|
35
|
-
match = (content =~ expr)
|
36
|
-
@content_matches = [$1, $2, $3]
|
37
|
-
match
|
38
|
-
end
|
39
|
-
|
40
|
-
def handle_result content, match, &block
|
41
|
-
if block && match && content
|
42
|
-
ruby_content = content.strip.extend(RSpec::RubyContent::Helpers)
|
43
|
-
yield ruby_content
|
44
|
-
end
|
45
|
-
match
|
46
|
-
end
|
47
|
-
|
48
|
-
def get_expr content
|
49
|
-
expr = /#{main_expr}#{comment_end}/m
|
50
|
-
if (content =~ expr).nil?
|
51
|
-
/#{main_expr}#{end_expr}/m
|
52
|
-
else
|
53
|
-
expr
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
Q_ANY_GROUP = '(.*?)'
|
58
|
-
ANY_GROUP = '(.*)'
|
59
|
-
|
60
|
-
SPACES = '\s+'
|
61
|
-
OPT_SPACES = '\s*'
|
62
|
-
LPAR = '(\()'
|
63
|
-
RPAR = '(\))'
|
64
|
-
OPT_ARGS = '(\(.+\))?'
|
65
|
-
|
66
|
-
def opt expr
|
67
|
-
"#{expr}?"
|
68
|
-
end
|
69
|
-
|
70
|
-
def args_msg
|
71
|
-
args ? " with arguments #{args}" : ''
|
72
|
-
end
|
73
|
-
|
74
|
-
def any_args_expr
|
75
|
-
OPT_SPACES + OPT_ARGS
|
76
|
-
end
|
77
|
-
|
78
|
-
def args_expr
|
79
|
-
args ? OPT_SPACES + opt(LPAR) + OPT_SPACES + "#{args}" + OPT_SPACES + opt(RPAR) : ''
|
80
|
-
end
|
81
|
-
|
82
|
-
def main_expr
|
83
|
-
raise "Must override main_expr"
|
84
|
-
end
|
85
|
-
|
86
|
-
def end_expr
|
87
|
-
"$"
|
88
|
-
end
|
89
|
-
|
90
|
-
def comment_end
|
91
|
-
alt_end != nil ? '#' + SPACES + "(#{end_option}|#{alt_end})" : '#' + SPACES + "#{end_option}"
|
92
|
-
end
|
93
|
-
|
94
|
-
def failure_message
|
95
|
-
debug_content
|
96
|
-
end
|
97
|
-
|
98
|
-
def negative_failure_message
|
99
|
-
debug_content
|
100
|
-
end
|
101
|
-
|
102
|
-
def debug_content
|
103
|
-
debug "Content: #{content}"
|
104
|
-
end
|
105
|
-
|
106
|
-
def debug msg
|
107
|
-
puts msg if debug?
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
module RSpec::RubyContentMatchers
|
2
|
-
class HaveBlock < RSpec::RubyContentMatcher
|
3
|
-
attr_reader :name, :args, :block_args, :do_option, :end_option
|
4
|
-
|
5
|
-
def initialize(name, options={})
|
6
|
-
super options[:end] || name
|
7
|
-
@name = name.to_s
|
8
|
-
@args = options[:args]
|
9
|
-
@do_option = options[:do].nil? ? true : options[:do]
|
10
|
-
@block_args = options[:block_args]
|
11
|
-
end
|
12
|
-
|
13
|
-
def failure_message
|
14
|
-
super
|
15
|
-
return "Expected there to be a block #{name}#{args_msg}#{block_args_msg}, but there wasn't"
|
16
|
-
end
|
17
|
-
|
18
|
-
def negative_failure_message
|
19
|
-
super
|
20
|
-
return "Did not expect there to be a block #{name}#{args_msg}, but there was"
|
21
|
-
end
|
22
|
-
|
23
|
-
protected
|
24
|
-
|
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}"
|
39
|
-
end
|
40
|
-
|
41
|
-
def args_expr
|
42
|
-
args ? '(\()?\s*' + args + '\s*(\))?\s+' : ''
|
43
|
-
end
|
44
|
-
|
45
|
-
def block_args_expr
|
46
|
-
block_args ? '\|\s*' + block_args + '\s*\|' : ''
|
47
|
-
end
|
48
|
-
|
49
|
-
def alt_end
|
50
|
-
'do'
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def have_block(name, options={})
|
55
|
-
HaveBlock.new(name, options)
|
56
|
-
end
|
57
|
-
end
|
@@ -1,59 +0,0 @@
|
|
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 HaveCall < RSpec::RubyContentMatcher
|
10
|
-
attr_reader :method, :args, :dot
|
11
|
-
|
12
|
-
def initialize(method, options = {})
|
13
|
-
@method = method.to_s
|
14
|
-
@args = options[:args] || options
|
15
|
-
@dot = options[:dot]
|
16
|
-
end
|
17
|
-
|
18
|
-
def matches?(content)
|
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)
|
27
|
-
end
|
28
|
-
|
29
|
-
def failure_message
|
30
|
-
super
|
31
|
-
"Expected there to be a call to #{method}#{args_msg}, but there wasn't"
|
32
|
-
end
|
33
|
-
|
34
|
-
def negative_failure_message
|
35
|
-
super
|
36
|
-
"Did not expect there to be a call to #{method}#{args_msg}, but there was"
|
37
|
-
end
|
38
|
-
|
39
|
-
protected
|
40
|
-
|
41
|
-
def not_def
|
42
|
-
'[^def\s]'
|
43
|
-
end
|
44
|
-
|
45
|
-
def dot_expr
|
46
|
-
return Regexp.escape(dot) if dot.kind_of?(String)
|
47
|
-
dot == true ? "#{not_def}\." : not_def
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
def have_call(method, options = {})
|
53
|
-
HaveCall.new(method, options)
|
54
|
-
end
|
55
|
-
|
56
|
-
def have_dot_call(method, options = {})
|
57
|
-
HaveCall.new(method, options[:args], :dot => options[:dot] || true)
|
58
|
-
end
|
59
|
-
end
|