Skip to content

Commit

Permalink
refactor(serde_yml): ✅ Add new tests for tag.rs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 30, 2024
1 parent 28dec03 commit fade0d8
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 2 deletions.
92 changes: 90 additions & 2 deletions examples/libyml/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
//! This file demonstrates the creation, usage, and comparison of `Tag` instances,
//! as well as the usage of its various methods.

use serde_yml::libyml::tag::Tag;
use serde_yml::libyml::tag::TagFormatError;
use serde_yml::libyml::tag::{Tag, TagFormatError};

pub(crate) fn main() {
// Print a message to indicate the file being executed.
Expand Down Expand Up @@ -73,4 +72,93 @@ pub(crate) fn main() {
"\n✅ Created a new Tag instance for FLOAT: {:?}",
tag_float
);

// Example: Handling TagFormatError when the prefix is longer than the tag
match custom_tag.starts_with("tag:example.org,2024:custom:extra") {
Ok(_) => println!("\n✅ The tag starts with the given prefix."),
Err(TagFormatError) => {
println!("\n✅ Error: The prefix is longer than the tag.")
}
}

// Example: Validating a list of YAML tags
let tags = vec![
Tag::new("tag:example.org,2024:custom1"),
Tag::new("tag:example.org,2024:custom2"),
Tag::new("tag:example.com,2024:other"),
];

for tag in &tags {
if tag.starts_with("tag:example.org").unwrap_or(false) {
println!("\n✅ The tag {:?} starts with the prefix 'tag:example.org'", tag);
} else {
println!("\n✅ The tag {:?} does not start with the prefix 'tag:example.org'", tag);
}
}

// Example: Comparing tags with different formats
let another_custom_tag = Tag::new("tag:example.org,2024:custom");
if custom_tag == another_custom_tag {
println!("\n✅ The custom_tag is equal to another_custom_tag.");
} else {
println!(
"\n✅ The custom_tag is not equal to another_custom_tag."
);
}

// Example: Filtering tags based on a prefix
let filtered_tags: Vec<&Tag> = tags
.iter()
.filter(|tag| {
tag.starts_with("tag:example.org").unwrap_or(false)
})
.collect();

println!(
"\n✅ Filtered tags that start with 'tag:example.org': {:?}",
filtered_tags
);

// Example: Creating a custom function to process tags
fn print_tag_info(tag: &Tag) {
println!("\n📌 Tag info: {:?}", tag);
if tag.starts_with("tag:example.org").unwrap_or(false) {
println!("✅ This tag starts with 'tag:example.org'");
} else {
println!(
"❌ This tag does not start with 'tag:example.org'"
);
}
}

let custom_tag = Tag::new("tag:example.org,2024:custom");
print_tag_info(&custom_tag);

// Example: Error handling with invalid tags
let invalid_tag = "invalid:tag";
match Tag::new(invalid_tag).starts_with("tag:example.org") {
Ok(_) => println!(
"\n✅ The invalid_tag starts with the given prefix."
),
Err(TagFormatError) => {
println!("\n❌ Error: The prefix is longer than the invalid_tag.")
}
}

// Example: Real-world scenario - parsing and validating tags from a YAML document
let yaml_tags = vec![
"tag:example.org,2024:custom1",
"tag:example.org,2024:custom2",
"tag:example.com,2024:other",
"invalid:tag",
];

for yaml_tag in yaml_tags {
let tag = Tag::new(yaml_tag);
match tag.starts_with("tag:example.org") {
Ok(true) => println!("\n✅ The tag {:?} is valid and starts with 'tag:example.org'", tag),
Ok(false) => println!("\n✅ The tag {:?} is valid but does not start with 'tag:example.org'", tag),
Err(TagFormatError) => println!("\n❌ The tag {:?} is invalid or the prefix is too long", tag),
}
}
}
3 changes: 3 additions & 0 deletions tests/libyml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ pub mod test_emitter;
/// This module contains the tests for the `error` module.
pub mod test_error;

/// This module contains the tests for the `test_tag` module.
pub mod test_tag;

/// This module contains the tests for the `util` module.
pub mod test_util;
84 changes: 84 additions & 0 deletions tests/libyml/test_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#[cfg(test)]
mod tests {
use serde_yml::libyml::tag::{Tag, TagFormatError};

/// Tests the creation of a new Tag instance using the NULL constant.
/// Verifies that the created Tag instance is not null.
#[test]
fn test_new_tag_null() {
let tag_null = Tag::new(Tag::NULL);
assert!(!tag_null.is_empty());
}

/// Tests the creation of a new Tag instance with a custom tag.
/// Verifies that the created Tag instance matches the provided custom tag.
#[test]
fn test_new_custom_tag() {
let custom_tag = Tag::new("tag:example.org,2024:custom");
assert_eq!(custom_tag, "tag:example.org,2024:custom");
}

/// Tests if a Tag starts with a given prefix.
/// Verifies that the method returns true for a matching prefix and false otherwise.
#[test]
fn test_tag_starts_with() {
let custom_tag = Tag::new("tag:example.org,2024:custom");
assert!(custom_tag.starts_with("tag:example.org").unwrap());
assert!(!custom_tag.starts_with("tag:example.com").unwrap());
}

/// Tests the handling of TagFormatError when the prefix is longer than the tag.
/// Verifies that the method returns an error for a longer prefix.
#[test]
fn test_tag_starts_with_error() {
let custom_tag = Tag::new("tag:example.org,2024:custom");
let result =
custom_tag.starts_with("tag:example.org,2024:custom:extra");
assert!(result.is_err());
assert_eq!(result.unwrap_err(), TagFormatError);
}

/// Tests the comparison of a Tag with a &str.
/// Verifies that the comparison returns true for matching values and false otherwise.
#[test]
fn test_tag_comparison() {
let custom_tag = Tag::new("tag:example.org,2024:custom");
let comparison_str = "tag:example.org,2024:custom";
assert_eq!(custom_tag, comparison_str);

let non_matching_str = "tag:example.org,2024:other";
assert_ne!(custom_tag, non_matching_str);
}

/// Tests the Deref implementation to access the underlying byte slice.
/// Verifies that the dereferenced value matches the original tag string.
#[test]
fn test_tag_deref() {
let custom_tag = Tag::new("tag:example.org,2024:custom");
let tag_bytes: &[u8] = &custom_tag;
assert_eq!(tag_bytes, b"tag:example.org,2024:custom");
}

/// Tests the Debug implementation for a Tag instance.
/// Verifies that the debug representation of the Tag instance is correct.
#[test]
fn test_tag_debug() {
let custom_tag = Tag::new("tag:example.org,2024:custom");
let debug_str = format!("{:?}", custom_tag);
assert_eq!(debug_str, "\"tag:example.org,2024:custom\"");
}

/// Tests the creation of Tag instances using Tag constants for BOOL, INT, and FLOAT.
/// Verifies that the created Tag instances match the respective constants.
#[test]
fn test_tag_constants() {
let tag_bool = Tag::new(Tag::BOOL);
assert_eq!(tag_bool, Tag::BOOL);

let tag_int = Tag::new(Tag::INT);
assert_eq!(tag_int, Tag::INT);

let tag_float = Tag::new(Tag::FLOAT);
assert_eq!(tag_float, Tag::FLOAT);
}
}

0 comments on commit fade0d8

Please sign in to comment.