Skip to content

Commit ca99882

Browse files
CopilotByron
andcommitted
Implement WriteTo trait for gix::Blob with comprehensive tests
Co-authored-by: Byron <[email protected]>
1 parent f891c37 commit ca99882

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

gix/src/object/impls.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ impl std::fmt::Debug for Object<'_> {
150150
}
151151
}
152152

153+
impl<'repo> gix_object::WriteTo for Blob<'repo> {
154+
fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
155+
out.write_all(&self.data)
156+
}
157+
158+
fn kind(&self) -> gix_object::Kind {
159+
gix_object::Kind::Blob
160+
}
161+
162+
fn size(&self) -> u64 {
163+
self.data.len() as u64
164+
}
165+
}
166+
153167
/// In conjunction with the handles free list, leaving an empty Vec in place of the original causes it to not be
154168
/// returned to the free list.
155169
fn steal_from_freelist(data: &mut Vec<u8>) -> Vec<u8> {

gix/tests/gix/repository/object.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,80 @@ mod write_object {
292292
);
293293
Ok(())
294294
}
295+
296+
#[test]
297+
fn blob_write_to_implementation() -> crate::Result {
298+
let repo = empty_bare_in_memory_repo()?;
299+
let test_data = b"hello world";
300+
301+
// Create a blob directly to test our WriteTo implementation
302+
let blob_id = repo.write_blob(test_data)?;
303+
let blob = repo.find_object(blob_id)?.into_blob();
304+
305+
// Test that we can use the blob with write_object (which requires WriteTo)
306+
let written_id = repo.write_object(blob)?;
307+
308+
// The written blob should have the same ID as the original
309+
assert_eq!(blob_id, written_id, "WriteTo implementation should produce identical blob");
310+
311+
// Verify the content is correct
312+
let retrieved_blob = repo.find_object(written_id)?.into_blob();
313+
assert_eq!(retrieved_blob.data, test_data, "Blob data should be preserved");
314+
315+
Ok(())
316+
}
317+
318+
#[test]
319+
fn blob_write_to_properties() -> crate::Result {
320+
let repo = empty_bare_in_memory_repo()?;
321+
let test_data = b"test data for WriteTo properties";
322+
323+
// Create a blob to test WriteTo trait methods
324+
let blob_id = repo.write_blob(test_data)?;
325+
let blob = repo.find_object(blob_id)?.into_blob();
326+
327+
// Test WriteTo trait methods directly
328+
use gix_object::WriteTo;
329+
330+
// Test kind() method
331+
assert_eq!(blob.kind(), gix_object::Kind::Blob, "kind() should return Blob");
332+
333+
// Test size() method
334+
assert_eq!(blob.size(), test_data.len() as u64, "size() should return data length");
335+
336+
// Test write_to() method
337+
let mut buffer = Vec::new();
338+
blob.write_to(&mut buffer)?;
339+
assert_eq!(buffer, test_data, "write_to() should write blob data verbatim");
340+
341+
Ok(())
342+
}
343+
344+
#[test]
345+
fn blob_write_to_empty_blob() -> crate::Result {
346+
let repo = empty_bare_in_memory_repo()?;
347+
let empty_data = b"";
348+
349+
// Create an empty blob to test edge case
350+
let blob_id = repo.write_blob(empty_data)?;
351+
let blob = repo.find_object(blob_id)?.into_blob();
352+
353+
// Test WriteTo trait methods with empty blob
354+
use gix_object::WriteTo;
355+
356+
assert_eq!(blob.kind(), gix_object::Kind::Blob, "kind() should return Blob for empty blob");
357+
assert_eq!(blob.size(), 0, "size() should return 0 for empty blob");
358+
359+
let mut buffer = Vec::new();
360+
blob.write_to(&mut buffer)?;
361+
assert_eq!(buffer, empty_data, "write_to() should write empty data for empty blob");
362+
363+
// Test that we can write the empty blob using write_object
364+
let written_id = repo.write_object(blob)?;
365+
assert_eq!(blob_id, written_id, "WriteTo implementation should work for empty blobs");
366+
367+
Ok(())
368+
}
295369
}
296370

297371
mod write_blob {

0 commit comments

Comments
 (0)