method_log 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,99 +1,97 @@
1
+ require 'spec_helper'
2
+
1
3
  require 'method_log/repository'
2
4
  require 'method_log/commit'
3
5
  require 'method_log/source_file'
4
6
 
5
- describe MethodLog::Commit do
6
- let(:sha) { 'b54d38bbd989f4b54c38fd77767d89d1' }
7
- let(:commit) { MethodLog::Commit.new(sha: sha) }
8
- let(:commit_with_same_sha) { MethodLog::Commit.new(sha: sha) }
7
+ module MethodLog
8
+ describe Commit do
9
+ let(:sha) { 'b54d38bbd989f4b54c38fd77767d89d1' }
10
+ let(:commit) { Commit.new(sha) }
11
+ let(:commit_with_same_sha) { Commit.new(sha) }
9
12
 
10
- it 'is equal to another commit with same SHA' do
11
- expect(commit).to eq(commit_with_same_sha)
12
- end
13
+ it 'is equal to another commit with same SHA' do
14
+ expect(commit).to eq(commit_with_same_sha)
15
+ end
13
16
 
14
- it 'has same hash as another commit with same SHA' do
15
- expect(commit.hash).to eq(commit_with_same_sha.hash)
16
- end
17
+ it 'has same hash as another commit with same SHA' do
18
+ expect(commit.hash).to eq(commit_with_same_sha.hash)
19
+ end
17
20
 
18
- context 'using a real git repository' do
19
- let(:repository_path) { File.expand_path('../repository.git', __FILE__) }
21
+ context 'using a real git repository' do
22
+ let(:repository_path) { File.expand_path('../repository.git', __FILE__) }
20
23
 
21
- before do
22
- FileUtils.mkdir_p(repository_path)
23
- Rugged::Repository.init_at(repository_path, :bare)
24
- end
24
+ before do
25
+ FileUtils.mkdir_p(repository_path)
26
+ Rugged::Repository.init_at(repository_path, :bare)
27
+ end
25
28
 
26
- after do
27
- FileUtils.rm_rf(repository_path)
28
- end
29
+ after do
30
+ FileUtils.rm_rf(repository_path)
31
+ end
29
32
 
30
- it 'stores source files added to a commit in the repository against a real commit' do
31
- source_one = MethodLog::SourceFile.new(path: 'path/to/source_one.rb', source: 'source-one')
32
- source_two = MethodLog::SourceFile.new(path: 'path/to/source_two.rb', source: 'source-two')
33
+ it 'stores source files added to a commit in the repository against a real commit' do
34
+ source_one = source(path: 'path/to/source_one.rb', source: 'source-one')
35
+ source_two = source(path: 'path/to/source_two.rb', source: 'source-two')
33
36
 
34
- repository = MethodLog::Repository.new(path: repository_path)
35
- commit = repository.build_commit
36
- commit.add(source_one)
37
- commit.add(source_two)
38
- commit.apply
37
+ repository = Repository.new(repository_path)
38
+ commit = repository.commit(source_one, source_two)
39
39
 
40
- repository = MethodLog::Repository.new(path: repository_path)
41
- commit = repository.commits.first
42
- expect(commit.source_files.to_a).to eq([source_one, source_two])
43
- end
40
+ repository = Repository.new(repository_path)
41
+ commit = repository.commits.first
42
+ expect(commit.source_files.to_a).to eq([source_one, source_two])
43
+ end
44
44
 
45
- it 'only includes source files with ruby file extension' do
46
- source_file = MethodLog::SourceFile.new(path: 'path/to/source_one.py', source: 'source-file')
45
+ it 'only includes source files with ruby file extension' do
46
+ source_file = source(path: 'path/to/source_one.py', source: 'source-file')
47
47
 
48
- repository = MethodLog::Repository.new(path: repository_path)
49
- commit = repository.build_commit
50
- commit.add(source_file)
51
- commit.apply
48
+ repository = Repository.new(repository_path)
49
+ commit = repository.commit(source_file)
52
50
 
53
- repository = MethodLog::Repository.new(path: repository_path)
54
- commit = repository.commits.first
55
- expect(commit.source_files.to_a).to be_empty
56
- end
51
+ repository = Repository.new(repository_path)
52
+ commit = repository.commits.first
53
+ expect(commit.source_files.to_a).to be_empty
54
+ end
57
55
 
58
- it 'indicates whether it contains a given source file' do
59
- source_file = MethodLog::SourceFile.new(path: 'path/to/source.rb', source: 'source-file')
56
+ it 'indicates whether it contains a given source file' do
57
+ source_file = source(path: 'path/to/source.rb', source: 'source-file')
58
+ another_source_file = source(path: 'path/to/another_source.rb', source: 'another-source-file')
60
59
 
61
- repository = MethodLog::Repository.new(path: repository_path)
62
- commit = repository.build_commit
63
- commit.add(source_file)
64
- commit.apply
65
- source_file = commit.source_files.first
60
+ repository = Repository.new(repository_path)
61
+ commit = repository.commit(source_file)
62
+ source_file = commit.source_files.first
63
+ another_commit = repository.commit(another_source_file)
64
+ another_source_file = another_commit.source_files.first
66
65
 
67
- repository = MethodLog::Repository.new(path: repository_path)
68
- commit = repository.commits.first
69
- expect(commit.contains?(source_file)).to be_true
70
- end
66
+ repository = Repository.new(repository_path)
67
+ commit = repository.commits.to_a.last
71
68
 
72
- it 'makes author available' do
73
- user = { email: 'test@example.com', name: 'test', time: Time.now.round }
74
- source_file = MethodLog::SourceFile.new(path: 'path/to/source.rb', source: 'source')
69
+ expect(commit.contains?(source_file)).to be_true
70
+ expect(commit.contains?(another_source_file)).to be_false
71
+ end
75
72
 
76
- repository = MethodLog::Repository.new(path: repository_path)
77
- commit = repository.build_commit
78
- commit.add(source_file)
79
- commit.apply(user: user)
73
+ it 'makes author available' do
74
+ user = { email: 'test@example.com', name: 'test', time: Time.now.round }
75
+ source_file = source(path: 'path/to/source.rb', source: 'source')
80
76
 
81
- repository = MethodLog::Repository.new(path: repository_path)
82
- commit = repository.commits.first
83
- expect(commit.author).to eq(user)
84
- end
77
+ repository = Repository.new(repository_path)
78
+ commit = repository.commit(source_file, user: user)
79
+
80
+ repository = Repository.new(repository_path)
81
+ commit = repository.commits.first
82
+ expect(commit.author).to eq(user)
83
+ end
85
84
 
86
- it 'makes message available' do
87
- source_file = MethodLog::SourceFile.new(path: 'path/to/source.rb', source: 'source')
85
+ it 'makes message available' do
86
+ source_file = source(path: 'path/to/source.rb', source: 'source')
88
87
 
89
- repository = MethodLog::Repository.new(path: repository_path)
90
- commit = repository.build_commit
91
- commit.add(source_file)
92
- commit.apply(message: 'commit-message')
88
+ repository = Repository.new(repository_path)
89
+ commit = repository.commit(source_file, message: 'commit-message')
93
90
 
94
- repository = MethodLog::Repository.new(path: repository_path)
95
- commit = repository.commits.first
96
- expect(commit.message).to eq('commit-message')
91
+ repository = Repository.new(repository_path)
92
+ commit = repository.commits.first
93
+ expect(commit.message).to eq('commit-message')
94
+ end
97
95
  end
98
96
  end
99
- end
97
+ end
@@ -1,33 +1,37 @@
1
+ require 'spec_helper'
2
+
1
3
  require 'method_log/method_commit'
2
4
  require 'method_log/commit'
3
5
  require 'method_log/source_file'
4
6
  require 'method_log/method_definition'
5
7
 
6
- describe MethodLog::MethodCommit do
7
- let(:commit) { MethodLog::Commit.new(sha: nil) }
8
- let(:source_file) { MethodLog::SourceFile.new(path: '/path/to/source.rb', source: "line 0\nline 1\nline 2\n") }
9
- let(:method_definition) { MethodLog::MethodDefinition.new(source_file: source_file, lines: 0..1) }
10
- let(:method_commit) {
11
- MethodLog::MethodCommit.new(commit: commit, method_definition: method_definition)
12
- }
13
- let(:method_commit_with_same_commit_and_method_definition) {
14
- MethodLog::MethodCommit.new(commit: commit, method_definition: method_definition)
15
- }
8
+ module MethodLog
9
+ describe MethodCommit do
10
+ let(:commit) { Commit.new(nil) }
11
+ let(:source_file) { source(path: '/path/to/source.rb', source: "line 0\nline 1\nline 2\n") }
12
+ let(:method_definition) { MethodDefinition.new(source_file, 0..1) }
13
+ let(:method_commit) {
14
+ MethodCommit.new(commit, method_definition)
15
+ }
16
+ let(:method_commit_with_same_commit_and_method_definition) {
17
+ MethodCommit.new(commit, method_definition)
18
+ }
16
19
 
17
- it 'is equal to another method commit with same commit and method definition' do
18
- expect(method_commit).to eq(method_commit_with_same_commit_and_method_definition)
19
- end
20
+ it 'is equal to another method commit with same commit and method definition' do
21
+ expect(method_commit).to eq(method_commit_with_same_commit_and_method_definition)
22
+ end
20
23
 
21
- it 'has same hash as another method commit with same commit and method definition' do
22
- expect(method_commit.hash).to eq(method_commit_with_same_commit_and_method_definition.hash)
23
- end
24
+ it 'has same hash as another method commit with same commit and method definition' do
25
+ expect(method_commit.hash).to eq(method_commit_with_same_commit_and_method_definition.hash)
26
+ end
24
27
 
25
- it 'returns method source with trailing newline' do
26
- expect(method_commit.method_source).to eq("line 0\nline 1\n")
27
- end
28
+ it 'returns method source with trailing newline' do
29
+ expect(method_commit.method_source).to eq("line 0\nline 1\n")
30
+ end
28
31
 
29
- it 'returns nil if no method definition' do
30
- method_commit = MethodLog::MethodCommit.new(commit: commit, method_definition: nil)
31
- expect(method_commit.method_source).to be_nil
32
+ it 'returns nil if no method definition' do
33
+ method_commit = MethodCommit.new(commit, method_definition = nil)
34
+ expect(method_commit.method_source).to be_nil
35
+ end
32
36
  end
33
- end
37
+ end
@@ -1,34 +1,38 @@
1
+ require 'spec_helper'
2
+
1
3
  require 'method_log/method_definition'
2
4
  require 'method_log/source_file'
3
5
 
4
- describe MethodLog::MethodDefinition do
5
- let(:source_file) do
6
- MethodLog::SourceFile.new(path: 'path/to/source.rb', source: %{
7
- class Foo
8
- def bar
9
- # implementation
10
- end
11
- end
12
- }.strip)
13
- end
6
+ module MethodLog
7
+ describe MethodDefinition do
8
+ let(:source_file) do
9
+ source(path: 'path/to/source.rb', source: %{
10
+ class Foo
11
+ def bar
12
+ # implementation
13
+ end
14
+ end
15
+ })
16
+ end
14
17
 
15
- it 'is equal to another method definition with same source file and line numbers' do
16
- definition_one = MethodLog::MethodDefinition.new(source_file: source_file, lines: 1..3)
17
- definition_two = MethodLog::MethodDefinition.new(source_file: source_file, lines: 1..3)
18
+ it 'is equal to another method definition with same source file and line numbers' do
19
+ definition_one = MethodDefinition.new(source_file, 1..3)
20
+ definition_two = MethodDefinition.new(source_file, 1..3)
18
21
 
19
- expect(definition_one).to eq(definition_two)
20
- end
22
+ expect(definition_one).to eq(definition_two)
23
+ end
21
24
 
22
- it 'has same hash as another method definition with same path and line numbers' do
23
- definition_one = MethodLog::MethodDefinition.new(source_file: source_file, lines: 1..3)
24
- definition_two = MethodLog::MethodDefinition.new(source_file: source_file, lines: 1..3)
25
+ it 'has same hash as another method definition with same path and line numbers' do
26
+ definition_one = MethodDefinition.new(source_file, 1..3)
27
+ definition_two = MethodDefinition.new(source_file, 1..3)
25
28
 
26
- expect(definition_one.hash).to eq(definition_two.hash)
27
- end
29
+ expect(definition_one.hash).to eq(definition_two.hash)
30
+ end
28
31
 
29
- it 'provides access to the method source' do
30
- definition = MethodLog::MethodDefinition.new(source_file: source_file, lines: 1..3)
32
+ it 'provides access to the method source' do
33
+ definition = MethodDefinition.new(source_file, 1..3)
31
34
 
32
- expect(definition.source).to eq(%{ def bar\n # implementation\n end})
35
+ expect(definition.source).to eq(source_file.snippet(1..3))
36
+ end
33
37
  end
34
- end
38
+ end
@@ -1,13 +1,17 @@
1
+ require 'spec_helper'
2
+
1
3
  require 'method_log/method_diff'
2
4
 
3
- describe MethodLog::MethodDiff do
4
- let(:first_commit) { double(:first_commit) }
5
- let(:second_commit) { double(:second_commit) }
6
- let(:diff) { MethodLog::MethodDiff.new(first_commit: first_commit, second_commit: second_commit) }
5
+ module MethodLog
6
+ describe MethodDiff do
7
+ let(:first_commit) { double(:first_commit) }
8
+ let(:second_commit) { double(:second_commit) }
9
+ let(:diff) { MethodDiff.new(first_commit, second_commit) }
7
10
 
8
- it 'generates text diff of the method source for two commits' do
9
- first_commit.stub(:method_source).and_return(%{line 1\nline 2\n})
10
- second_commit.stub(:method_source).and_return(%{line 2\nline 3\n})
11
- expect(diff.to_s).to eq(%{-line 1\n line 2\n+line 3\n})
11
+ it 'generates text diff of the method source for two commits' do
12
+ first_commit.stub(:method_source).and_return(%{line 1\nline 2\n})
13
+ second_commit.stub(:method_source).and_return(%{line 2\nline 3\n})
14
+ expect(diff.to_s).to eq(%{-line 1\n line 2\n+line 3\n})
15
+ end
12
16
  end
13
- end
17
+ end
@@ -3,249 +3,245 @@ require 'spec_helper'
3
3
  require 'method_log/source_file'
4
4
  require 'method_log/method_finder'
5
5
 
6
- describe MethodLog::MethodFinder do
7
- it 'finds definition of instance method on class' do
8
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
9
- class Foo
10
- def bar
11
- # implementation
12
- end
13
- end
14
- }.strip)
15
-
16
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
17
- method_definition = method_finder.find('Foo#bar')
18
-
19
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 1..3))
20
- end
21
-
22
- it 'finds definition of instance method on module' do
23
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
24
- module Foo
25
- def bar
26
- # implementation
27
- end
28
- end
29
- }.strip)
30
-
31
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
32
- method_definition = method_finder.find('Foo#bar')
33
-
34
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 1..3))
35
- end
36
-
37
- it 'finds definition of instance method on class within module' do
38
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
39
- module Foo
40
- class Bar
41
- def baz
42
- # implementation
6
+ module MethodLog
7
+ describe MethodFinder do
8
+ it 'finds definition of instance method on class' do
9
+ foo = source(path: 'foo.rb', source: %{
10
+ class Foo
11
+ def bar
12
+ # implementation
13
+ end
14
+ end
15
+ })
16
+
17
+ method_finder = MethodFinder.new(foo)
18
+ method_definition = method_finder.find('Foo#bar')
19
+
20
+ expect(method_definition).to eq(MethodDefinition.new(foo, 1..3))
43
21
  end
44
- end
45
- end
46
- }.strip)
47
-
48
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
49
- method_definition = method_finder.find('Foo::Bar#baz')
50
-
51
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 2..4))
52
- end
53
-
54
- it 'finds definition of instance method on namespaced class within previously defined module' do
55
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
56
- module Foo
57
- end
58
-
59
- class Foo::Bar
60
- def baz
61
- # implementation
62
- end
63
- end
64
- }.strip)
65
-
66
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
67
- method_definition = method_finder.find('Foo::Bar#baz')
68
22
 
69
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 4..6))
70
- end
23
+ it 'finds definition of instance method on module' do
24
+ foo = source(path: 'foo.rb', source: %{
25
+ module Foo
26
+ def bar
27
+ # implementation
28
+ end
29
+ end
30
+ })
71
31
 
72
- it 'finds definition of instance method on namespaced module within previously defined module' do
73
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
74
- module Foo
75
- end
32
+ method_finder = MethodFinder.new(foo)
33
+ method_definition = method_finder.find('Foo#bar')
76
34
 
77
- module Foo::Bar
78
- def baz
79
- # implementation
80
- end
81
- end
82
- }.strip)
83
-
84
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
85
- method_definition = method_finder.find('Foo::Bar#baz')
86
-
87
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 4..6))
88
- end
89
-
90
- it 'finds definition of class method on class' do
91
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
92
- module Foo
93
- def self.bar
94
- # implementation
95
- end
96
- end
97
- }.strip)
98
-
99
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
100
- method_definition = method_finder.find('Foo.bar')
101
-
102
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 1..3))
103
- end
104
-
105
- it 'finds definition of class method on class with explicit reference to class' do
106
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
107
- module Foo
108
- def Foo.bar
109
- # implementation
110
- end
111
- end
112
- }.strip)
113
-
114
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
115
- method_definition = method_finder.find('Foo.bar')
116
-
117
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 1..3))
118
- end
119
-
120
- it 'finds definition of class method on class with explicit reference to namespaced class' do
121
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
122
- module Foo
123
- class Bar
124
- def (Foo::Bar).baz
125
- # implementation
35
+ expect(method_definition).to eq(MethodDefinition.new(foo, 1..3))
126
36
  end
127
- end
128
- end
129
- }.strip)
130
-
131
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
132
- method_definition = method_finder.find('Foo::Bar.baz')
133
-
134
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 2..4))
135
- end
136
-
137
- it 'finds definition of class method on class with explicit reference to namespaced class outside current scope' do
138
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
139
- class Foo
140
- end
141
-
142
- class Bar
143
- def Foo.baz
144
- # implementation
145
- end
146
- end
147
- }.strip)
148
37
 
149
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
150
- method_definition = method_finder.find('Foo.baz')
151
-
152
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 4..6))
153
- end
154
-
155
- it 'finds definition of class method on class when singleton class is re-opened' do
156
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
157
- class Foo
158
- class << self
159
- def bar
160
- # implementation
38
+ it 'finds definition of instance method on class within module' do
39
+ foo = source(path: 'foo.rb', source: %{
40
+ module Foo
41
+ class Bar
42
+ def baz
43
+ # implementation
44
+ end
45
+ end
46
+ end
47
+ })
48
+
49
+ method_finder = MethodFinder.new(foo)
50
+ method_definition = method_finder.find('Foo::Bar#baz')
51
+
52
+ expect(method_definition).to eq(MethodDefinition.new(foo, 2..4))
161
53
  end
162
- end
163
- end
164
- }.strip)
165
54
 
166
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
167
- method_definition = method_finder.find('Foo.bar')
168
-
169
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 2..4))
170
- end
171
-
172
- it 'finds definition of class method on class when singleton class is re-opened with explicit reference to class' do
173
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
174
- class Foo
175
- class << Foo
176
- def bar
177
- # implementation
55
+ it 'finds definition of instance method on namespaced class within previously defined module' do
56
+ foo = source(path: 'foo.rb', source: %{
57
+ module Foo
58
+ end
59
+ class Foo::Bar
60
+ def baz
61
+ # implementation
62
+ end
63
+ end
64
+ })
65
+
66
+ method_finder = MethodFinder.new(foo)
67
+ method_definition = method_finder.find('Foo::Bar#baz')
68
+
69
+ expect(method_definition).to eq(MethodDefinition.new(foo, 3..5))
178
70
  end
179
- end
180
- end
181
- }.strip)
182
71
 
183
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
184
- method_definition = method_finder.find('Foo.bar')
72
+ it 'finds definition of instance method on namespaced module within previously defined module' do
73
+ foo = source(path: 'foo.rb', source: %{
74
+ module Foo
75
+ end
76
+ module Foo::Bar
77
+ def baz
78
+ # implementation
79
+ end
80
+ end
81
+ })
82
+
83
+ method_finder = MethodFinder.new(foo)
84
+ method_definition = method_finder.find('Foo::Bar#baz')
85
+
86
+ expect(method_definition).to eq(MethodDefinition.new(foo, 3..5))
87
+ end
185
88
 
186
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 2..4))
187
- end
89
+ it 'finds definition of class method on class' do
90
+ foo = source(path: 'foo.rb', source: %{
91
+ module Foo
92
+ def self.bar
93
+ # implementation
94
+ end
95
+ end
96
+ })
188
97
 
189
- it 'finds definition of class method on class when singleton class is re-opened with explicit reference to class outside current scope' do
190
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
191
- class Foo
192
- end
98
+ method_finder = MethodFinder.new(foo)
99
+ method_definition = method_finder.find('Foo.bar')
193
100
 
194
- class Bar
195
- class << Foo
196
- def bar
197
- # implementation
101
+ expect(method_definition).to eq(MethodDefinition.new(foo, 1..3))
198
102
  end
199
- end
200
- end
201
- }.strip)
202
103
 
203
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
204
- method_definition = method_finder.find('Foo.bar')
104
+ it 'finds definition of class method on class with explicit reference to class' do
105
+ foo = source(path: 'foo.rb', source: %{
106
+ module Foo
107
+ def Foo.bar
108
+ # implementation
109
+ end
110
+ end
111
+ })
205
112
 
206
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 5..7))
207
- end
113
+ method_finder = MethodFinder.new(foo)
114
+ method_definition = method_finder.find('Foo.bar')
208
115
 
209
- it 'finds definition of class method on unknown object when singleton class is re-opened' do
210
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
211
- class Foo
212
- def initialize
213
- @foo = new
214
- class << @foo
215
- def bar
216
- # implementation
217
- end
116
+ expect(method_definition).to eq(MethodDefinition.new(foo, 1..3))
218
117
  end
219
- end
220
- end
221
- }.strip)
222
118
 
223
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
224
- method_definition = method_finder.find('Foo::(ivar :@foo).bar')
119
+ it 'finds definition of class method on class with explicit reference to namespaced class' do
120
+ foo = source(path: 'foo.rb', source: %{
121
+ module Foo
122
+ class Bar
123
+ def (Foo::Bar).baz
124
+ # implementation
125
+ end
126
+ end
127
+ end
128
+ })
129
+
130
+ method_finder = MethodFinder.new(foo)
131
+ method_definition = method_finder.find('Foo::Bar.baz')
132
+
133
+ expect(method_definition).to eq(MethodDefinition.new(foo, 2..4))
134
+ end
225
135
 
226
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 4..6))
227
- end
136
+ it 'finds definition of class method on class with explicit reference to namespaced class outside current scope' do
137
+ foo = source(path: 'foo.rb', source: %{
138
+ class Foo
139
+ end
140
+ class Bar
141
+ def Foo.baz
142
+ # implementation
143
+ end
144
+ end
145
+ })
146
+
147
+ method_finder = MethodFinder.new(foo)
148
+ method_definition = method_finder.find('Foo.baz')
149
+
150
+ expect(method_definition).to eq(MethodDefinition.new(foo, 3..5))
151
+ end
228
152
 
229
- it 'finds definition of class method on ambiguous module referenced via top-level module' do
230
- foo = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
231
- module Foo
232
- class Bar; end
233
- end
153
+ it 'finds definition of class method on class when singleton class is re-opened' do
154
+ foo = source(path: 'foo.rb', source: %{
155
+ class Foo
156
+ class << self
157
+ def bar
158
+ # implementation
159
+ end
160
+ end
161
+ end
162
+ })
163
+
164
+ method_finder = MethodFinder.new(foo)
165
+ method_definition = method_finder.find('Foo.bar')
166
+
167
+ expect(method_definition).to eq(MethodDefinition.new(foo, 2..4))
168
+ end
234
169
 
235
- module Baz
236
- module Foo
237
- class Bar; end
238
- end
170
+ it 'finds definition of class method on class when singleton class is re-opened with explicit reference to class' do
171
+ foo = source(path: 'foo.rb', source: %{
172
+ class Foo
173
+ class << Foo
174
+ def bar
175
+ # implementation
176
+ end
177
+ end
178
+ end
179
+ })
180
+
181
+ method_finder = MethodFinder.new(foo)
182
+ method_definition = method_finder.find('Foo.bar')
183
+
184
+ expect(method_definition).to eq(MethodDefinition.new(foo, 2..4))
185
+ end
239
186
 
240
- def (::Foo::Bar).foo
241
- # implementation
242
- end
243
- end
244
- }.strip)
187
+ it 'finds definition of class method on class when singleton class is re-opened with explicit reference to class outside current scope' do
188
+ foo = source(path: 'foo.rb', source: %{
189
+ class Foo
190
+ end
191
+ class Bar
192
+ class << Foo
193
+ def bar
194
+ # implementation
195
+ end
196
+ end
197
+ end
198
+ })
199
+
200
+ method_finder = MethodFinder.new(foo)
201
+ method_definition = method_finder.find('Foo.bar')
202
+
203
+ expect(method_definition).to eq(MethodDefinition.new(foo, 4..6))
204
+ end
245
205
 
246
- method_finder = MethodLog::MethodFinder.new(source_file: foo)
247
- method_definition = method_finder.find('Foo::Bar.foo')
206
+ it 'finds definition of class method on unknown object when singleton class is re-opened' do
207
+ foo = source(path: 'foo.rb', source: %{
208
+ class Foo
209
+ def initialize
210
+ @foo = new
211
+ class << @foo
212
+ def bar
213
+ # implementation
214
+ end
215
+ end
216
+ end
217
+ end
218
+ })
219
+
220
+ method_finder = MethodFinder.new(foo)
221
+ method_definition = method_finder.find('Foo::(ivar :@foo).bar')
222
+
223
+ expect(method_definition).to eq(MethodDefinition.new(foo, 4..6))
224
+ end
248
225
 
249
- expect(method_definition).to eq(MethodLog::MethodDefinition.new(source_file: foo, lines: 9..11))
226
+ it 'finds definition of class method on ambiguous module referenced via top-level module' do
227
+ foo = source(path: 'foo.rb', source: %{
228
+ module Foo
229
+ class Bar; end
230
+ end
231
+ module Baz
232
+ module Foo
233
+ class Bar; end
234
+ end
235
+ def (::Foo::Bar).foo
236
+ # implementation
237
+ end
238
+ end
239
+ })
240
+
241
+ method_finder = MethodFinder.new(foo)
242
+ method_definition = method_finder.find('Foo::Bar.foo')
243
+
244
+ expect(method_definition).to eq(MethodDefinition.new(foo, 7..9))
245
+ end
250
246
  end
251
247
  end