diff --git a/src/tag_engine.rs b/src/tag_engine.rs index c00b135..327fa09 100644 --- a/src/tag_engine.rs +++ b/src/tag_engine.rs @@ -67,6 +67,18 @@ pub fn parse_tags(filename: &str) -> Result<(String, Vec, String), Parse Ok((base_name, tags, extension)) } +pub fn add_tags(current: Vec, new: Vec) -> Vec { + let mut result = current; + + for tag in new { + if !result.contains(&tag) { + result.push(tag); + } + } + + result +} + pub fn serialize_tags(base: &str, tags: &[String], extension: &str) -> String { let mut sorted_tags = tags.to_vec(); sorted_tags.sort(); @@ -117,6 +129,46 @@ mod tests { assert_eq!(result, "README -- note"); } + #[test] + fn test_add_tags_no_duplicates() { + let current = vec!["tag1".to_string(), "tag2".to_string()]; + let new = vec!["tag3".to_string(), "tag4".to_string()]; + let result = add_tags(current, new); + assert_eq!(result, vec!["tag1", "tag2", "tag3", "tag4"]); + } + + #[test] + fn test_add_tags_with_duplicates() { + let current = vec!["tag1".to_string(), "tag2".to_string()]; + let new = vec!["tag2".to_string(), "tag3".to_string()]; + let result = add_tags(current, new); + assert_eq!(result, vec!["tag1", "tag2", "tag3"]); + } + + #[test] + fn test_add_tags_case_sensitive() { + let current = vec!["Tag1".to_string(), "tag2".to_string()]; + let new = vec!["tag1".to_string(), "Tag2".to_string()]; + let result = add_tags(current, new); + assert_eq!(result, vec!["Tag1", "tag2", "tag1", "Tag2"]); + } + + #[test] + fn test_add_tags_empty_current() { + let current = Vec::new(); + let new = vec!["tag1".to_string(), "tag2".to_string()]; + let result = add_tags(current, new); + assert_eq!(result, vec!["tag1", "tag2"]); + } + + #[test] + fn test_add_tags_empty_new() { + let current = vec!["tag1".to_string(), "tag2".to_string()]; + let new = Vec::new(); + let result = add_tags(current, new); + assert_eq!(result, vec!["tag1", "tag2"]); + } + #[test] fn test_validate_tag_valid() { assert!(validate_tag("valid-tag").is_ok());