Skip to content

Commit

Permalink
Added tests for commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Dec 26, 2023
1 parent 9d95ef3 commit 76d4ce8
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 31 deletions.
3 changes: 3 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ pub mod blob;
pub mod commit;
pub mod tree;
pub mod types;

#[cfg(test)]
mod tests;
Empty file added src/core/tests/blob_test.rs
Empty file.
160 changes: 160 additions & 0 deletions src/core/tests/commit_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
use crate::core::commit::Commit;

#[test]
fn commit_content() {
let valid_contents: &str = "tree abcdef0123456789abcdef0123456789abcdef01
parent 1234567890123456789012345678901234567890
author Shubham Singh
date_time 2023-01-01 12:00:00 UTC
message Test commit
root_dir parent_folder";

let output_commit_object: Commit = Commit {
date_time: "2023-01-01 12:00:00 UTC".to_string(),
message: "Test commit".to_string(),
author: "Shubham Singh".to_string(),
commit_hash: "abcdef0123456789abcdef0123456789abcdef01".to_string(),
parent: Some("1234567890123456789012345678901234567890".to_string()),
parent_folder_name: "parent_folder".to_string(),
};
assert_eq!(
Commit::get_content_of_commit(output_commit_object),
valid_contents
);
}

#[test]
fn valid_content() {
let valid_contents: &str = "tree abcdef0123456789abcdef0123456789abcdef01
parent 1234567890123456789012345678901234567890
author Shubham Singh
date_time 2023-01-01 12:00:00 UTC
message Test commit
root_dir parent_folder";

let output_commit_object: Commit = Commit {
date_time: "2023-01-01 12:00:00 UTC".to_string(),
message: "Test commit".to_string(),
author: "Shubham Singh".to_string(),
commit_hash: "abcdef0123456789abcdef0123456789abcdef01".to_string(),
parent: Some("1234567890123456789012345678901234567890".to_string()),
parent_folder_name: "parent_folder".to_string(),
};
assert_eq!(
Commit::get_commit_from_content(&valid_contents.to_string()),
output_commit_object
);
}

#[test]
fn valid_content_no_parent() {
let valid_contents: &str = "tree abcdef0123456789abcdef0123456789abcdef01
parent
author Shubham Singh
date_time 2023-01-01 12:00:00 UTC
message Test commit
root_dir parent_folder";

let output_commit_object: Commit = Commit {
date_time: "2023-01-01 12:00:00 UTC".to_string(),
message: "Test commit".to_string(),
author: "Shubham Singh".to_string(),
commit_hash: "abcdef0123456789abcdef0123456789abcdef01".to_string(),
parent: None,
parent_folder_name: "parent_folder".to_string(),
};
assert_eq!(
Commit::get_commit_from_content(&valid_contents.to_string()),
output_commit_object
);
}

#[test]
#[should_panic]
fn invalid_content_new_line() {
let invalid_contents: &str = "tree abcdef0123456789abcdef0123456789abcdef01parent 1234567890123456789012345678901234567890
author Shubham Singh
date_time 2023-01-01 12:00:00 UTC
message Test commit
root_dir parent_folder";
let output_commit_object: Commit = Commit {
date_time: "2023-01-01 12:00:00 UTC".to_string(),
message: "Test commit".to_string(),
author: "Shubham Singh".to_string(),
commit_hash: "abcdef0123456789abcdef0123456789abcdef01".to_string(),
parent: Some("1234567890123456789012345678901234567890".to_string()),
parent_folder_name: "parent_folder".to_string(),
};
assert_eq!(
Commit::get_commit_from_content(&invalid_contents.to_string()),
output_commit_object
);
}

#[test]
#[should_panic]
fn invalid_content_param_order() {
let invalid_contents: &str = "tree abcdef0123456789abcdef0123456789abcdef01
author Shubham Singh
parent 1234567890123456789012345678901234567890
date_time 2023-01-01 12:00:00 UTC
message Test commit
root_dir parent_folder";
let output_commit_object: Commit = Commit {
date_time: "2023-01-01 12:00:00 UTC".to_string(),
message: "Test commit".to_string(),
author: "Shubham Singh".to_string(),
commit_hash: "abcdef0123456789abcdef0123456789abcdef01".to_string(),
parent: Some("1234567890123456789012345678901234567890".to_string()),
parent_folder_name: "parent_folder".to_string(),
};
assert_eq!(
Commit::get_commit_from_content(&invalid_contents.to_string()),
output_commit_object
);
}

#[test]
#[should_panic]
fn invalid_content_missing_param() {
let invalid_contents: &str = "tree abcdef0123456789abcdef0123456789abcdef01
parent 1234567890123456789012345678901234567890
author Shubham Singh
message Test commit
root_dir parent_folder";
let output_commit_object: Commit = Commit {
date_time: "2023-01-01 12:00:00 UTC".to_string(),
message: "Test commit".to_string(),
author: "Shubham Singh".to_string(),
commit_hash: "abcdef0123456789abcdef0123456789abcdef01".to_string(),
parent: Some("1234567890123456789012345678901234567890".to_string()),
parent_folder_name: "parent_folder".to_string(),
};
assert_eq!(
Commit::get_commit_from_content(&invalid_contents.to_string()),
output_commit_object
);
}

#[test]
#[should_panic]
fn invalid_content_missing_value() {
let invalid_contents: &str = "tree abcdef0123456789abcdef0123456789abcdef01
parent 1234567890123456789012345678901234567890
author
date_time 2023-01-01 12:00:00 UTC
message Test commit
root_dir parent_folder";
let output_commit_object: Commit = Commit {
date_time: "2023-01-01 12:00:00 UTC".to_string(),
message: "Test commit".to_string(),
author: "Shubham Singh".to_string(),
commit_hash: "abcdef0123456789abcdef0123456789abcdef01".to_string(),
parent: Some("1234567890123456789012345678901234567890".to_string()),
parent_folder_name: "parent_folder".to_string(),
};
assert_eq!(
Commit::get_commit_from_content(&invalid_contents.to_string()),
output_commit_object
);
}
1 change: 1 addition & 0 deletions src/tests/mod.rs → src/core/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod commit_test;
mod blob_test;
3 changes: 0 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use commands::revert::revert;
use commands::{cat::cat, commit::commit, create::create, switch::switch};
use std::env;

#[cfg(test)]
mod tests;

#[derive(Parser)]
#[command(author, version, about, long_about=None)]
#[command(propagate_version = true)]
Expand Down
28 changes: 0 additions & 28 deletions src/tests/commit_test.rs

This file was deleted.

0 comments on commit 76d4ce8

Please sign in to comment.