Skip to content

Commit ab17621

Browse files
committed
test: add tests to verify Git::Object.new creates the right type of object
1 parent abb0efb commit ab17621

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/units/test_object_new.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require 'git'
5+
require 'fileutils'
6+
require 'tmpdir'
7+
8+
# A test case to verify the functionality of the Git::Object.new factory method.
9+
class ObjectNewTest < Test::Unit::TestCase
10+
# Set up a temporary Git repository with objects of different types.
11+
def setup
12+
@repo_path = Dir.mktmpdir('git_test')
13+
@repo = Git.init(@repo_path)
14+
15+
Dir.chdir(@repo_path) do
16+
File.write('file.txt', 'This is a test file.')
17+
@repo.add('file.txt')
18+
@repo.commit('Initial commit')
19+
@repo.add_tag('v1.0', message: 'Version 1.0', annotate: true)
20+
end
21+
22+
@commit = @repo.gcommit('HEAD')
23+
@tree = @commit.gtree
24+
@blob = @tree.blobs['file.txt']
25+
@tag = @repo.tag('v1.0')
26+
end
27+
28+
# Clean up the temporary repository after each test.
29+
def teardown
30+
FileUtils.rm_rf(@repo_path)
31+
end
32+
33+
# Test that the factory method creates a Git::Object::Commit for a commit SHA.
34+
def test_new_creates_commit_object
35+
object = Git::Object.new(@repo, @commit.sha)
36+
assert_instance_of(Git::Object::Commit, object, 'Should create a Commit object.')
37+
assert(object.commit?, 'Object#commit? should be true.')
38+
end
39+
40+
# Test that the factory method creates a Git::Object::Tree for a tree SHA.
41+
def test_new_creates_tree_object
42+
object = Git::Object.new(@repo, @tree.sha)
43+
assert_instance_of(Git::Object::Tree, object, 'Should create a Tree object.')
44+
assert(object.tree?, 'Object#tree? should be true.')
45+
end
46+
47+
# Test that the factory method creates a Git::Object::Blob for a blob SHA.
48+
def test_new_creates_blob_object
49+
object = Git::Object.new(@repo, @blob.sha)
50+
assert_instance_of(Git::Object::Blob, object, 'Should create a Blob object.')
51+
assert(object.blob?, 'Object#blob? should be true.')
52+
end
53+
54+
# Test that using the deprecated `is_tag` argument creates a Tag object
55+
# and issues a deprecation warning.
56+
def test_new_with_is_tag_deprecation
57+
# Set up the mock expectation for the deprecation warning.
58+
Git::Deprecation.expects(:warn).with(
59+
'Git::Object.new with is_tag argument is deprecated. Use Git::Object::Tag.new instead.'
60+
)
61+
62+
# Action: Call the factory method with the deprecated argument.
63+
# The `objectish` here is the tag name, as was the old pattern.
64+
tag_object = Git::Object.new(@repo, 'v1.0', nil, true)
65+
66+
# Verification
67+
assert_instance_of(Git::Object::Tag, tag_object, 'Should create a Tag object.')
68+
assert(tag_object.tag?, 'Object#tag? should be true.')
69+
assert_equal('v1.0', tag_object.name, 'Tag object should have the correct name.')
70+
# Mocha automatically verifies the expectation at the end of the test.
71+
end
72+
end

0 commit comments

Comments
 (0)