Skip to content

Commit

Permalink
Added tests for blob
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Dec 26, 2023
1 parent 76d4ce8 commit d850287
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/core/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ blob \0UTF\023\0This is the content of the file.
blob \0NonUTF\036\0123,345,4,6,7,7,43,142,32,44
*/

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Blob {
is_utf8: FileTypes,
pub is_utf8: FileTypes,
pub content_size: i32,
pub content: String,
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ message `Commit message goes here.`
*/

#[derive(Debug)]
#[derive(PartialEq)]
#[derive(Debug, PartialEq)]
pub struct Commit {
pub date_time: String,
pub message: String,
Expand Down
62 changes: 62 additions & 0 deletions src/core/tests/blob_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::core::{blob::Blob, types::FileTypes};

#[test]
fn get_content_of_blob_utf8() {
let blob = Blob {
content_size: 10,
content: String::from("Hello!"),
is_utf8: FileTypes::Utf8,
};

let expected_content = "blob \0UTF\010\0Hello!";
assert_eq!(blob.get_content_of_blob(), expected_content);
}

#[test]
fn get_content_of_blob_non_utf8() {
let blob = Blob {
content_size: 8,
content: String::from("Binary\x00"),
is_utf8: FileTypes::NonUTF8,
};

let expected_content = "blob \0NonUTF\08\0Binary\x00";
assert_eq!(blob.get_content_of_blob(), expected_content);
}

#[test]
fn get_blob_from_content_utf8() {
let blob_content = "blob \0UTF\012\0Hello, Rust!";
let expected_blob = Blob {
content_size: 12,
content: String::from("Hello, Rust!"),
is_utf8: FileTypes::Utf8,
};

assert_eq!(
Blob::get_blob_from_content(&blob_content.to_string()),
expected_blob
);
}

#[test]
fn get_blob_from_content_non_utf8() {
let blob_content = "blob \0NonUTF\05\0Binary";
let expected_blob = Blob {
content_size: 5,
content: String::from("Binary"),
is_utf8: FileTypes::NonUTF8,
};

assert_eq!(
Blob::get_blob_from_content(&blob_content.to_string()),
expected_blob
);
}

#[test]
#[should_panic(expected = "Invalid Blob Content")]
fn get_blob_from_content_invalid_content() {
let invalid_blob_content = "invalid_content";
Blob::get_blob_from_content(&invalid_blob_content.to_string());
}
2 changes: 1 addition & 1 deletion src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub enum GitObject {
Tree,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum FileTypes {
Utf8,
NonUTF8,
Expand Down

0 comments on commit d850287

Please sign in to comment.