code-spec 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/.rspec +1 -0
- data/LICENSE +20 -0
- data/README.markdown +43 -0
- data/Rakefile +18 -0
- data/VERSION +1 -0
- data/lib/code-spec.rb +15 -0
- data/lib/code_spec/matchers/content_matcher.rb +110 -0
- data/lib/code_spec/matchers/have_block.rb +57 -0
- data/lib/code_spec/matchers/have_call.rb +62 -0
- data/lib/code_spec/matchers/have_calls.rb +60 -0
- data/lib/code_spec/matchers/have_class_self.rb +40 -0
- data/lib/code_spec/matchers/have_comment.rb +42 -0
- data/lib/code_spec/matchers/have_method.rb +66 -0
- data/lib/code_spec/matchers/have_module.rb +50 -0
- data/lib/code_spec/matchers/have_region.rb +48 -0
- data/lib/code_spec/matchers/have_subclass.rb +40 -0
- data/lib/code_spec/matchers/include_module.rb +34 -0
- data/lib/code_spec/matchers/inherit_from.rb +35 -0
- data/spec/code-spec/matchers/class_self_spec.rb +28 -0
- data/spec/code-spec/matchers/have_block_spec.rb +49 -0
- data/spec/code-spec/matchers/have_call_spec.rb +48 -0
- data/spec/code-spec/matchers/have_calls_spec.rb +41 -0
- data/spec/code-spec/matchers/have_class_spec.rb +34 -0
- data/spec/code-spec/matchers/have_method_spec.rb +56 -0
- data/spec/code-spec/matchers/have_module_spec.rb +33 -0
- data/spec/code-spec/matchers/have_region_spec.rb +52 -0
- data/spec/code-spec/matchers/have_subclass_spec.rb +32 -0
- data/spec/code-spec/matchers/include_module_spec.rb +34 -0
- data/spec/code-spec/matchers/inherit_from_spec.rb +32 -0
- data/spec/spec_helper.rb +4 -0
- metadata +154 -0
@@ -0,0 +1,40 @@
|
|
1
|
+
module RSpec::RubyContentMatchers
|
2
|
+
class HaveSubclass < RSpec::RubyContentMatcher
|
3
|
+
attr_reader :klass, :superclass, :type
|
4
|
+
|
5
|
+
def initialize(klass, superclass, type=nil)
|
6
|
+
@klass = klass.to_s.camelize
|
7
|
+
super @klass
|
8
|
+
@superclass = superclass.to_s.camelize
|
9
|
+
@type = type.to_s.camelize if type
|
10
|
+
end
|
11
|
+
|
12
|
+
def failure_message
|
13
|
+
super
|
14
|
+
"Expected there to be the subclass #{klass} of #{superclass}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def negative_failure_message
|
18
|
+
super
|
19
|
+
"Did no expected there to be the subclass #{klass} of #{superclass}"
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def main_expr
|
25
|
+
'class' + SPACES + "#{klass}#{type}" + OPT_SPACES + '<' + OPT_SPACES + "#{superclass}" + ANY_GROUP
|
26
|
+
end
|
27
|
+
|
28
|
+
def alt_end
|
29
|
+
'class'
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def have_subclass(klass, superclass, type=nil)
|
35
|
+
HaveSubclass.new(klass, superclass, type)
|
36
|
+
end
|
37
|
+
alias_method :be_subclass, :have_subclass
|
38
|
+
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module RSpec::RubyContentMatchers
|
2
|
+
class IncludeModule < RSpec::RubyContentMatcher
|
3
|
+
attr_reader :module_name, :type
|
4
|
+
|
5
|
+
def initialize module_name, type=nil
|
6
|
+
@module_name = module_name.to_s.camelize
|
7
|
+
@type = type || :include
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(content)
|
11
|
+
@content = content
|
12
|
+
(content =~ /#{type}\s+#{module_name}/)
|
13
|
+
end
|
14
|
+
|
15
|
+
def failure_message
|
16
|
+
super
|
17
|
+
"Expected there to be an inclusion of module #{module_name}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def negative_failure_message
|
21
|
+
super
|
22
|
+
"Did not expect there to be an inclusion of module #{module_name}"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def include_module(module_name)
|
28
|
+
IncludeModule.new(module_name)
|
29
|
+
end
|
30
|
+
|
31
|
+
def extend_module(module_name)
|
32
|
+
IncludeModule.new(module_name, :extend)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module RSpec::RubyContentMatchers
|
2
|
+
class InheritFrom < RSpec::RubyContentMatcher
|
3
|
+
attr_reader :klass
|
4
|
+
|
5
|
+
def initialize klass
|
6
|
+
@klass = klass.to_s.camelize
|
7
|
+
super @klass
|
8
|
+
end
|
9
|
+
|
10
|
+
def index
|
11
|
+
1
|
12
|
+
end
|
13
|
+
|
14
|
+
def failure_message
|
15
|
+
super
|
16
|
+
"Expected the class to inherit from #{klass}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def negative_failure_message
|
20
|
+
super
|
21
|
+
"Did not expect the class to inherit from #{klass}"
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def main_expr
|
27
|
+
'class' + SPACES + Q_ANY_GROUP + '<' + OPT_SPACES + "#{klass}" + ANY_GROUP
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def inherit_from(klass)
|
32
|
+
InheritFrom.new(klass)
|
33
|
+
end
|
34
|
+
alias_method :be_subclass_of, :inherit_from
|
35
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'class_self matcher' do
|
4
|
+
context "content without class << self" do
|
5
|
+
not_class_self = %q{
|
6
|
+
def hello
|
7
|
+
end}
|
8
|
+
|
9
|
+
it "should not have class << self" do
|
10
|
+
not_class_self.should_not have_class_self
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "content with class << self" do
|
15
|
+
class_self = %q{
|
16
|
+
class << self
|
17
|
+
def hello
|
18
|
+
end
|
19
|
+
end # class self}
|
20
|
+
|
21
|
+
it "should have class << self" do
|
22
|
+
class_self.should have_class_self
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'block matcher' do
|
4
|
+
context "content without block" do
|
5
|
+
not_class_self = %q{
|
6
|
+
def hello
|
7
|
+
end}
|
8
|
+
|
9
|
+
it "should not have block" do
|
10
|
+
not_class_self.should_not have_block :hello
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "content with block: hello do" do
|
15
|
+
content = %q{
|
16
|
+
hello do
|
17
|
+
blip
|
18
|
+
end # do}
|
19
|
+
|
20
|
+
it "should have block" do
|
21
|
+
content.should have_block :hello
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "content with block: hello :angel do" do
|
26
|
+
content = %q{
|
27
|
+
hello :angel do
|
28
|
+
blip
|
29
|
+
end # do}
|
30
|
+
|
31
|
+
it "should have block" do
|
32
|
+
content.should have_block :hello, :args => ':angel'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "content with block: hello :angel do |x,y|" do
|
37
|
+
content = %q{
|
38
|
+
hello :angel do |x,y|
|
39
|
+
blip
|
40
|
+
end # do}
|
41
|
+
|
42
|
+
it "should have block" do
|
43
|
+
content.should have_block :hello, :args => ':angel', :block_args => 'x,y'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'method call matcher' do
|
4
|
+
context "content without method call" do
|
5
|
+
not_call = %q{
|
6
|
+
def hello
|
7
|
+
end}
|
8
|
+
|
9
|
+
it "should not have call hello" do
|
10
|
+
not_call.should_not have_call :hello
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "content with call hello" do
|
15
|
+
call = 'hello'
|
16
|
+
it "should have call to hello" do
|
17
|
+
call.should have_call :hello
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "content with call hello" do
|
22
|
+
call = %q{
|
23
|
+
def hello
|
24
|
+
x.hello
|
25
|
+
end}
|
26
|
+
it "should have call to hello" do
|
27
|
+
call.should have_dot_call :hello
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "class User with call role_strategy" do
|
32
|
+
clazz = %q{
|
33
|
+
class User < ActiveRecord::Base
|
34
|
+
include RoleModels::Generic
|
35
|
+
role_strategy :admin_flag
|
36
|
+
|
37
|
+
roles :admin,:user
|
38
|
+
|
39
|
+
end}
|
40
|
+
|
41
|
+
it "should have call to role_strategy with args :admin_flag" do
|
42
|
+
clazz.should have_call :role_strategy, :args => ':admin_flag'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
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
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "content with calls to x and y" do
|
19
|
+
class_self = %q{
|
20
|
+
def x
|
21
|
+
x
|
22
|
+
y
|
23
|
+
end}
|
24
|
+
|
25
|
+
it "should have calls to x and y" do
|
26
|
+
class_self.should have_calls :x, :y
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "content with calls to x and y" do
|
31
|
+
class_self = %q{
|
32
|
+
def x
|
33
|
+
x(2)
|
34
|
+
y 3
|
35
|
+
end}
|
36
|
+
|
37
|
+
it "should have calls: x(2) and y 3" do
|
38
|
+
class_self.should have_calls :x => '2', :y => '3'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'module matcher' do
|
4
|
+
context "content without class Hello" do
|
5
|
+
no_class = %q{
|
6
|
+
module Hello
|
7
|
+
def hello
|
8
|
+
end
|
9
|
+
end}
|
10
|
+
|
11
|
+
it "should not have class Hello" do
|
12
|
+
no_class.should_not have_class :Hello do |content|
|
13
|
+
content.should_not be_empty
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "content with class Hello" do
|
19
|
+
with_class = %q{
|
20
|
+
class Hello
|
21
|
+
def hello
|
22
|
+
end
|
23
|
+
end}
|
24
|
+
|
25
|
+
it "should have class Hello" do
|
26
|
+
with_class.should have_class :Hello do |content|
|
27
|
+
content.should_not be_empty
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'method matcher' do
|
4
|
+
context "content without method" do
|
5
|
+
no_method = %q{
|
6
|
+
class X
|
7
|
+
hello
|
8
|
+
end}
|
9
|
+
|
10
|
+
it "should not have method hello" do
|
11
|
+
no_method.should_not have_method :hello do |content|
|
12
|
+
content.should_not be_empty
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "content with method hello" do
|
18
|
+
with_method = %q{
|
19
|
+
def hello
|
20
|
+
blip
|
21
|
+
end}
|
22
|
+
it "should have method hello" do
|
23
|
+
with_method.should have_method :hello do |content|
|
24
|
+
content.should_not be_empty
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "content: method hello with args (x, y)" do
|
30
|
+
with_method = %q{
|
31
|
+
def hello(x, y)
|
32
|
+
blip
|
33
|
+
end}
|
34
|
+
it "should have method hello" do
|
35
|
+
with_method.should have_method :hello, :args => 'x, y' do |content|
|
36
|
+
content.should_not be_empty
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "content: method hello with args x, y" do
|
42
|
+
with_method = %q{
|
43
|
+
def hello x, y
|
44
|
+
blip
|
45
|
+
end}
|
46
|
+
it "should have method hello with args x, y" do
|
47
|
+
with_method.should have_method :hello, :args => 'x, y' do |content|
|
48
|
+
content.should_not be_empty
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'module matcher' do
|
4
|
+
context "content without module Hello" do
|
5
|
+
no_module = %q{
|
6
|
+
class Hello
|
7
|
+
def hello
|
8
|
+
blip
|
9
|
+
end
|
10
|
+
end}
|
11
|
+
|
12
|
+
it "should not have module Hello" do
|
13
|
+
no_module.should_not have_module :Hello
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "content with module Hello" do
|
18
|
+
with_module = %q{
|
19
|
+
module Hello
|
20
|
+
def hello
|
21
|
+
end
|
22
|
+
end}
|
23
|
+
|
24
|
+
it "should have module Hello" do
|
25
|
+
with_module.should have_module :Hello do |content|
|
26
|
+
content.should_not be_empty
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'region matcher' do
|
4
|
+
context "content with only protected region" do
|
5
|
+
only_protected_region = %q{
|
6
|
+
def hello
|
7
|
+
end
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def goodbye
|
12
|
+
end
|
13
|
+
|
14
|
+
}
|
15
|
+
|
16
|
+
it "should not have public region" do
|
17
|
+
only_protected_region.should_not have_region :public
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have protected region" do
|
21
|
+
only_protected_region.should have_region :protected do |content|
|
22
|
+
content.should_not be_empty
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "content with only private region" do
|
28
|
+
only_private_region = %q{
|
29
|
+
def hello
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def goodbye
|
35
|
+
end
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
it "should not have public region" do
|
40
|
+
only_private_region.should_not have_region :public
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should have private region" do
|
44
|
+
only_private_region.should have_region :private do |content|
|
45
|
+
content.should_not be_empty
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'subclass matcher' do
|
4
|
+
context "content without subclass Greeting" do
|
5
|
+
no_subclass = %q{
|
6
|
+
class Hello
|
7
|
+
def hello
|
8
|
+
end
|
9
|
+
end}
|
10
|
+
|
11
|
+
it "should not have subclass Greeting" do
|
12
|
+
no_subclass.should_not have_subclass :hello, :greeting
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "content with subclass Greeting" do
|
17
|
+
with_subclass = %q{
|
18
|
+
class Hello < Greeting
|
19
|
+
def hello
|
20
|
+
end
|
21
|
+
end}
|
22
|
+
|
23
|
+
it "should not have subclass Greeting" do
|
24
|
+
with_subclass.should have_subclass :hello, :greeting do |content|
|
25
|
+
content.should_not be_empty
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'include module matcher' do
|
4
|
+
context "content without include Greeting" do
|
5
|
+
no_include = %q{
|
6
|
+
class Hello < Greeting
|
7
|
+
def hello
|
8
|
+
end
|
9
|
+
end}
|
10
|
+
|
11
|
+
it "should not have include Greeting" do
|
12
|
+
no_include.should_not include_module :greeting
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "content with include Greeting" do
|
17
|
+
with_module = %q{
|
18
|
+
class Hello
|
19
|
+
include Greeting
|
20
|
+
|
21
|
+
def hello
|
22
|
+
end
|
23
|
+
end}
|
24
|
+
|
25
|
+
it "should have include Greeting" do
|
26
|
+
with_module.should include_module :greeting do |content|
|
27
|
+
content.should_not be_empty
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'inherit matcher' do
|
4
|
+
context "content with class that inherits from Greeting" do
|
5
|
+
no_subclass = %q{
|
6
|
+
class Hello
|
7
|
+
def hello
|
8
|
+
end
|
9
|
+
end}
|
10
|
+
|
11
|
+
it "should not inherit from Greeting" do
|
12
|
+
no_subclass.should_not inherit_from :greeting
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "content with subclass Greeting" do
|
17
|
+
with_subclass = %q{
|
18
|
+
class Hello < Greeting
|
19
|
+
def hello
|
20
|
+
end
|
21
|
+
end}
|
22
|
+
|
23
|
+
it "should inherit_from Greeting" do
|
24
|
+
with_subclass.should inherit_from :greeting do |content|
|
25
|
+
content.should_not be_empty
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
data/spec/spec_helper.rb
ADDED