/
rubydeps_spec.rb
179 lines (143 loc) · 4.75 KB
/
rubydeps_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require 'file_test_helper'
module GrandparentModule
def class_method
end
end
class Grandparent
extend GrandparentModule
def instance_method
end
end
class Parent
def self.class_method
Grandparent.class_method
end
def instance_method
end
end
class Son
def self.class_method
parent = Parent.new
parent.instance_method
parent.instance_method
class_method2
class_method2
end
def self.class_method2
end
def instance_method_that_calls_parent_class_method
Parent.class_method
end
def instance_method_calling_another_instance_method(second_receiver)
second_receiver.instance_method
end
def instance_method
Parent.class_method
Grandparent.class_method
end
end
describe "Rubydeps" do
it "should show the class level dependencies" do
dependencies, _ = ::Rubydeps.dependency_hash_for do
class IHaveAClassLevelDependency
Son.class_method
end
end
dependencies.should == {"Parent"=>["Son"]}
end
it "should be idempotent" do
::Rubydeps.dependency_hash_for do
class IHaveAClassLevelDependency
Son.class_method
end
end
dependencies, _ = ::Rubydeps.dependency_hash_for do
class IHaveAClassLevelDependency
Son.class_method
end
end
dependencies.should == {"Parent"=>["Son"]}
end
it "should show the dependency from an object singleton method" do
dependencies, _ = ::Rubydeps.dependency_hash_for do
s = Son.new
def s.attached_method
Grandparent.class_method
end
s.attached_method
end
dependencies.keys.should == ["Grandparent", "GrandparentModule"]
dependencies["Grandparent"].should == ["Son"]
dependencies["GrandparentModule"].should == ["Grandparent"]
end
it "should show the dependencies between the classes inside the block" do
dependencies, _ = ::Rubydeps.dependency_hash_for do
Son.new.instance_method
end
dependencies.keys.should =~ ["Parent", "Grandparent", "GrandparentModule"]
dependencies["Parent"].should == ["Son"]
dependencies["Grandparent"].should =~ ["Son", "Parent"]
dependencies["GrandparentModule"].should == ["Grandparent"]
end
it "should create correct dependencies for 2 instance methods called in a row" do
dependencies, _ = ::Rubydeps.dependency_hash_for do
Son.new.instance_method_calling_another_instance_method(Parent.new)
end
dependencies.should == {"Parent"=>["Son"]}
end
context "with a dumped dependencies file" do
include FileTestHelper
sample_dir_structure = {'path1/class_a.rb' => <<-CLASSA,
require '#{File.dirname(__FILE__)}/../lib/rubydeps'
require './path1/class_b'
require './path2/class_c'
class A
def depend_on_b_and_c
B.new.b
C.new.c
end
end
Rubydeps.start
A.new.depend_on_b_and_c
CLASSA
'path1/class_b.rb' => 'class B; def b; end end',
'path2/class_c.rb' => 'class C; def c; end end'}
def run(command)
system("ruby -I#{File.dirname(__FILE__)}/../lib #{command}")
end
it "should create a dot file" do
with_files(sample_dir_structure) do
run("./path1/class_a.rb")
run("#{File.dirname(__FILE__)}/../bin/rubydeps")
File.read("rubydeps.dot").should match("digraph G")
end
end
it "should be a correct test file" do
with_files(sample_dir_structure) do
status = run("./path1/class_a.rb")
status.should be_true
end
end
it "should not filter classes when no filter is specified" do
with_files(sample_dir_structure) do
run("./path1/class_a.rb")
dependencies, _ = ::Rubydeps.dependency_hash_for(:from_file => 'rubydeps.dump')
dependencies.should == {"B"=>["A"], "C"=>["A"]}
end
end
it "should filter classes when a path filter is specified" do
with_files(sample_dir_structure) do
run("./path1/class_a.rb")
dependencies, _ = ::Rubydeps.dependency_hash_for(:from_file => 'rubydeps.dump', :path_filter => /path1/)
dependencies.should == {"B"=>["A"]}
end
end
it "should filter classes when a class name filter is specified" do
with_files(sample_dir_structure) do
run("./path1/class_a.rb")
dependencies, _ = ::Rubydeps.dependency_hash_for(:from_file => 'rubydeps.dump', :class_name_filter => /C|A/)
dependencies.should == {"C"=>["A"]}
end
end
end
end