Skip to content

Commit

Permalink
Refactoring towards Diff, step 5
Browse files Browse the repository at this point in the history
  • Loading branch information
beatmadsen committed Nov 2, 2024
1 parent 57b532e commit 0c25d8b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
5 changes: 2 additions & 3 deletions lib/files_in_my_diff/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ module Git
class DiffError < Error; end

class Diff
def initialize(folder:, revision:, adapter: Adapter.new(folder:))
def initialize(object:, revision:)
@object = object
@revision = revision
@adapter = adapter
@object = adapter.object(@revision)
end

def validate!
Expand Down
34 changes: 14 additions & 20 deletions test/git/diff_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ module FilesInMyDiff
module Git
class DiffTest < Minitest::Test
def test_it_validates_that_revision_exists
adapter = adapter_stub(revision_exists: false)
assert_raises(ValidationError) do
subject(adapter:).validate!
subject(object: nil).validate!
end
end

Expand All @@ -17,9 +16,8 @@ def test_it_returns_the_sha_of_the_revision
end

def test_that_error_on_diff_calculation_is_propagated
adapter = adapter_stub(diff_success: false)
assert_raises(Git::DiffError) do
subject(adapter:).changes
subject(object: git_object_stub(diff_success: false)).changes
end
end

Expand All @@ -29,23 +27,12 @@ def test_it_returns_the_changes_since_the_parent_revision

private

def subject(adapter: adapter_stub)
Diff.new(folder: 'x', revision: 'y', adapter:)
def subject(object: git_object_stub, revision: 'y')
Diff.new(object:, revision:)
end

def adapter_stub(revision_exists: true, diff_success: true)
AdapterStub.new(revision_exists, diff_success)
end

class AdapterStub
def initialize(revision_exists, diff_success)
@revision_exists = revision_exists
@diff_success = diff_success
end

def object(_revision)
@revision_exists ? GitObjectStub.new(@diff_success) : nil
end
def git_object_stub(diff_success: true)
GitObjectStub.new(diff_success)
end

class GitObjectStub
Expand All @@ -60,7 +47,14 @@ def diff_parent
end
end

Change = Data.define(:path, :type)
class Change
attr_reader :path, :type

def initialize(path, type)
@path = path
@type = type
end
end
end
end
end

0 comments on commit 0c25d8b

Please sign in to comment.