spec/api_spec.rb in method_log-0.0.5 vs spec/api_spec.rb in method_log-0.0.6
- old
+ new
@@ -5,101 +5,89 @@
require 'method_log/repository'
require 'method_log/commit'
require 'method_log/method_definition'
require 'method_log/method_commit'
-describe MethodLog::API do
- let(:repository_path) { File.expand_path('../repository.git', __FILE__) }
+module MethodLog
+ describe API do
+ let(:repository_path) { File.expand_path('../repository.git', __FILE__) }
- before do
- FileUtils.mkdir_p(repository_path)
- Rugged::Repository.init_at(repository_path, :bare)
- end
+ before do
+ FileUtils.mkdir_p(repository_path)
+ Rugged::Repository.init_at(repository_path, :bare)
+ end
- after do
- FileUtils.rm_rf(repository_path)
- end
+ after do
+ FileUtils.rm_rf(repository_path)
+ end
- it 'finds class instance method in repository with two commits with single source file' do
- foo_1 = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
-class Foo
- def bar
- # implementation 1
- end
-end
- }.strip)
+ it 'finds class instance method in repository with two commits with single source file' do
+ repository = Repository.new(repository_path)
+ commit_1 = repository.commit(source(path: 'foo.rb', source: %{
+ class Foo
+ def bar
+ # implementation 1
+ end
+ end
+ }))
+ commit_2 = repository.commit(source(path: 'foo.rb', source: %{
+ # move method definition down one line
+ class Foo
+ def bar
+ # implementation 2
+ end
+ end
+ }))
- foo_2 = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
-# move method definition down one line
-class Foo
- def bar
- # implementation 2
- end
-end
- }.strip)
+ method_commits, method_diffs = commits_and_diffs_for('Foo#bar')
- repository = MethodLog::Repository.new(path: repository_path)
+ expect(method_commits.first.sha).to eq(commit_2.sha)
+ expect(method_diffs.first.to_s.chomp).to eq(unindent(%{
+ def bar
+ - # implementation 1
+ + # implementation 2
+ end
+ }))
+ end
- commit_1 = repository.build_commit
- commit_1.add(foo_1)
- repository.add(commit_1)
+ it 'finds method which is defined, then removed, and then defined again' do
+ repository = Repository.new(repository_path)
+ commit_1 = repository.commit(source(path: 'foo.rb', source: %{
+ class Foo
+ def bar; end
+ end
+ }))
+ commit_2 = repository.commit(source(path: 'foo.rb', source: %{
+ class Foo
+ end
+ }))
+ commit_3 = repository.commit(source(path: 'foo.rb', source: %{
+ class Foo
+ def bar; end
+ end
+ }))
+ commit_4 = repository.commit(source(path: 'foo.rb', source: %{
+ class Foo
+ def bar; end
+ end
+ }))
- commit_2 = repository.build_commit
- commit_2.add(foo_2)
- repository.add(commit_2)
+ method_commits, method_diffs = commits_and_diffs_for('Foo#bar')
- repository = MethodLog::Repository.new(path: repository_path)
- api = MethodLog::API.new(repository: repository)
- method_commits = api.history('Foo#bar').to_a
+ expect(method_commits.map(&:sha)).to eq([commit_3.sha, commit_2.sha])
+ expect(method_diffs.map(&:to_s)).to eq([
+ "+ def bar; end\n",
+ "- def bar; end\n"
+ ])
+ end
- method_definition_1 = MethodLog::MethodDefinition.new(source_file: foo_1, lines: 1..3)
- method_definition_2 = MethodLog::MethodDefinition.new(source_file: foo_2, lines: 2..4)
+ private
- method_commit_1 = MethodLog::MethodCommit.new(commit: commit_1, method_definition: method_definition_1)
- method_commit_2 = MethodLog::MethodCommit.new(commit: commit_2, method_definition: method_definition_2)
-
- expect(method_commits).to eq([method_commit_2, method_commit_1])
-
- method_commit, method_diff = api.diffs('Foo#bar').first
- expect(method_diff.to_s.strip).to eq(%{
- def bar
-- # implementation 1
-+ # implementation 2
- end
- }.strip)
+ def commits_and_diffs_for(method_identifier)
+ api = API.new(Repository.new(repository_path))
+ commits_and_diffs = api.diffs(method_identifier)
+ method_commits = commits_and_diffs.map(&:first)
+ method_diffs = commits_and_diffs.map(&:last)
+ [method_commits, method_diffs]
+ end
end
-
- it 'finds method which is defined in one commit, then removed in the next commit, and defined again in the next commit' do
- foo_with_bar = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
-class Foo
- def bar; end
-end
- }.strip)
-
- foo_without_bar = MethodLog::SourceFile.new(path: 'foo.rb', source: %{
-class Foo
-end
- }.strip)
-
- repository = MethodLog::Repository.new(path: repository_path)
-
- commit_1 = repository.build_commit
- commit_1.add(foo_with_bar)
- repository.add(commit_1)
-
- commit_2 = repository.build_commit
- commit_2.add(foo_without_bar)
- repository.add(commit_2)
-
- commit_3 = repository.build_commit
- commit_3.add(foo_with_bar)
- repository.add(commit_3)
-
- repository = MethodLog::Repository.new(path: repository_path)
- api = MethodLog::API.new(repository: repository)
- diffs = api.diffs('Foo#bar').map(&:last).map(&:to_s)
- expect(diffs).to eq([
- "+ def bar; end\n",
- "- def bar; end\n"
- ])
- end
-end
+end
\ No newline at end of file