code-spec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,43 @@
1
+ # Code spec
2
+
3
+ RSpec 2 matchers to facilitate making specs regarding Ruby code files, f.ex as generated or modified/mutated by a code generator.
4
+
5
+ ## Install
6
+
7
+ gem install code-spec
8
+
9
+ ## Usage
10
+
11
+ See specs for examples on how to use it.
12
+
13
+ Example: Nested DSL
14
+ <pre> ...
15
+ with_subclass = %q{
16
+ class Hello < Greeting
17
+ def hello
18
+ # howdy
19
+ end
20
+ end}
21
+
22
+ it "should not have subclass Greeting" do
23
+ with_subclass.should have_subclass :hello, :greeting do |content|
24
+ content.should have_method :hello do |hello|
25
+ hello.should have_comment 'howdy'
26
+ end
27
+ end
28
+ end
29
+ </pre>
30
+
31
+ ## Note on Patches/Pull Requests
32
+
33
+ * Fork the project.
34
+ * Make your feature addition or bug fix.
35
+ * Add tests for it. This is important so I don't break it in a
36
+ future version unintentionally.
37
+ * Commit, do not mess with rakefile, version, or history.
38
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
39
+ * Send me a pull request. Bonus points for topic branches.
40
+
41
+ ## Copyright
42
+
43
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "code-spec"
5
+ gem.summary = %Q{Code file RSpec 2 matchers}
6
+ gem.description = %Q{Code file RSpec 2 matchers that make it easy to spec Ruby code files, fx updated or generated by a Thor generator}
7
+ gem.email = "kmandrup@gmail.com"
8
+ gem.homepage = "http://github.com/kristianmandrup/code-spec"
9
+ gem.authors = ["Kristian Mandrup"]
10
+ gem.add_development_dependency "rspec", ">= 2.0.0.beta.19"
11
+ gem.add_dependency "require_all", ">= 1.1.0"
12
+ gem.add_dependency "activesupport", ">= 3.0.0.rc"
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/code-spec.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'rspec'
2
+ require 'require_all'
3
+ require 'active_support/inflector'
4
+
5
+ module RSpec
6
+ module RubyContentMatchers
7
+ end
8
+ end
9
+
10
+ require 'code_spec/matchers/content_matcher'
11
+ require_all File.dirname(__FILE__) + '/code_spec/matchers'
12
+
13
+ RSpec.configure do |config|
14
+ config.include(RSpec::RubyContentMatchers)
15
+ end
@@ -0,0 +1,110 @@
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
+ false
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
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
@@ -0,0 +1,57 @@
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
@@ -0,0 +1,62 @@
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 if !options.kind_of? Hash
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
+ debug "expr = #{expr}"
27
+ debug "content = %{#{content}}"
28
+ debug "content =~ #{expr}"
29
+ (content =~ expr)
30
+ end
31
+
32
+ def failure_message
33
+ super
34
+ "Expected there to be a call to #{method}#{args_msg}, but there wasn't"
35
+ end
36
+
37
+ def negative_failure_message
38
+ super
39
+ "Did not expect there to be a call to #{method}#{args_msg}, but there was"
40
+ end
41
+
42
+ protected
43
+
44
+ def not_def
45
+ '[^def\s]'
46
+ end
47
+
48
+ def dot_expr
49
+ return Regexp.escape(dot) if dot.kind_of?(String)
50
+ dot == true ? "#{not_def}\." : not_def
51
+ end
52
+
53
+ end
54
+
55
+ def have_call(method, options = {})
56
+ HaveCall.new(method, options)
57
+ end
58
+
59
+ def have_dot_call(method, options = {})
60
+ HaveCall.new(method, options.merge(:dot => true))
61
+ end
62
+ end
@@ -0,0 +1,60 @@
1
+ module RSpec::RubyContentMatchers
2
+ class HaveCalls < HaveCall
3
+ attr_reader :calls, :method, :args
4
+
5
+ def initialize(*calls)
6
+ case calls.first
7
+ when Array, Hash
8
+ @calls = calls.first
9
+ else
10
+ @calls = calls
11
+ end
12
+ end
13
+
14
+ def matches?(content)
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 =
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
36
+ return false if (content =~ expr) == nil
37
+ end
38
+ true
39
+ end
40
+
41
+ def do_hash
42
+ calls.each_pair do |method, args|
43
+ @method = method.to_s
44
+ @args = args
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
52
+ end
53
+ true
54
+ end
55
+ end
56
+
57
+ def have_calls(*calls)
58
+ HaveCalls.new(*calls)
59
+ end
60
+ 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
@@ -0,0 +1,42 @@
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 HaveComment < RSpec::RubyContentMatcher
10
+ attr_accessor :comment
11
+
12
+ def initialize comment
13
+ @comment = comment
14
+ end
15
+
16
+ def matches?(content)
17
+ @content = content
18
+ (content =~ /#{main_expr}/)
19
+ end
20
+
21
+ def failure_message
22
+ super
23
+ "Expected there to be the comment '# #{comment}'"
24
+ end
25
+
26
+ def negative_failure_message
27
+ super
28
+ "Did not expect there to be the comment '# #{comment}'"
29
+ end
30
+
31
+ protected
32
+
33
+ def main_expr
34
+ '\s*#\s*' + "#{comment}"
35
+ end
36
+ end
37
+
38
+ def have_comment comment
39
+ HaveComment.new comment
40
+ end
41
+
42
+ end
@@ -0,0 +1,66 @@
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 HaveMethod < RSpec::RubyContentMatcher
10
+ attr_reader :method, :type, :args
11
+
12
+ def initialize(method, type=nil, options = {})
13
+ @method = method.to_s
14
+ super method
15
+ @type = type
16
+ @args = options[:args]
17
+ end
18
+
19
+ def index
20
+ 1
21
+ end
22
+
23
+ def failure_message
24
+ super
25
+ "Expected there to be the #{class_msg} method #{method}, but there wasn't"
26
+ end
27
+
28
+ def negative_failure_message
29
+ super
30
+ "Did not expect there to be the #{class_msg} method #{method}, but there was"
31
+ end
32
+
33
+ protected
34
+
35
+ def class_msg
36
+ (type == :class) ? 'class' : ''
37
+ end
38
+
39
+ def main_expr
40
+ 'def' + SPACES + "#{self_expr}#{method}#{args_expr}" + ANY_GROUP
41
+ end
42
+
43
+ def args_expr
44
+ return super if args
45
+ any_args_expr
46
+ end
47
+
48
+ def self_expr
49
+ type == :class ? 'self.' : ''
50
+ end
51
+
52
+ def alt_end
53
+ 'def'
54
+ end
55
+
56
+ end
57
+
58
+ def have_method(method, type = nil)
59
+ HaveMethod.new(method, type)
60
+ end
61
+
62
+ def have_args_method(method, options = {})
63
+ HaveMethod.new(method, options[:type], options)
64
+ end
65
+
66
+ end
@@ -0,0 +1,50 @@
1
+ module RSpec::RubyContentMatchers
2
+ class HaveModule < RSpec::RubyContentMatcher
3
+ attr_reader :name, :type, :postfix
4
+
5
+ def initialize(name, postfix=nil)
6
+ @name = name.to_s.camelize
7
+ super @name
8
+ @postfix = postfix.to_s.camelize if postfix
9
+ @type = :module
10
+ end
11
+
12
+ def failure_message
13
+ super
14
+ "Expected there to be the #{type} #{name}"
15
+ end
16
+
17
+ def negative_failure_message
18
+ super
19
+ "Did not expected there to be the #{type} #{name}"
20
+ end
21
+
22
+ protected
23
+
24
+ def main_expr
25
+ "#{type}" + SPACES + "#{name}#{postfix}" + SPACES + ANY_GROUP
26
+ end
27
+
28
+ def alt_end
29
+ type
30
+ end
31
+ end
32
+
33
+ class HaveClass < HaveModule
34
+
35
+ def initialize(name, postfix)
36
+ super name, postfix
37
+ @type = :class
38
+ end
39
+ end
40
+
41
+ def have_module(module_name, postfix=nil)
42
+ HaveModule.new(module_name, postfix)
43
+ end
44
+ alias_method :be_module, :have_module
45
+
46
+ def have_class(klass, postfix = nil)
47
+ HaveClass.new(klass, postfix)
48
+ end
49
+ alias_method :be_class, :have_class
50
+ end
@@ -0,0 +1,48 @@
1
+ module RSpec::RubyContentMatchers
2
+ class HaveRegion < RSpec::RubyContentMatcher
3
+ attr_reader :region
4
+
5
+ def initialize(region)
6
+ @region = region.to_s
7
+ super @region
8
+ end
9
+
10
+ def failure_message
11
+ super
12
+ "Expected there to be a #{region} region"
13
+ end
14
+
15
+ def negative_failure_message
16
+ super
17
+ "Did no expected there to be a #{region} region"
18
+ end
19
+
20
+ protected
21
+
22
+ def main_expr
23
+ "#{region}" + ANY_GROUP
24
+ end
25
+
26
+ def end_expr
27
+ '(private|protected|public|$)'
28
+ end
29
+ end
30
+
31
+ def have_region name
32
+ HaveRegion.new name
33
+ end
34
+
35
+ def have_public
36
+ HaveRegion.new :public
37
+ end
38
+
39
+ def have_protected
40
+ HaveRegion.new :protected
41
+ end
42
+
43
+ def have_private
44
+ HaveRegion.new :private
45
+ end
46
+ end
47
+
48
+