Skip to content

Commit

Permalink
test(ssg): ✅ Add new unit tests for ssg-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Sep 21, 2024
1 parent 4094dcb commit bbb1ebf
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ssg-metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ thiserror = "1.0"
tokio = { version = "1.0", features = ["full"] }
time = "0.3"
regex = "1.10"
serde_yml = "0.0.12"
quick-xml = "0.36"

[build-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion ssg-metadata/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde_yml::Error as SerdeYmlError;
use thiserror::Error;

/// Custom error types for the ssg-metadata library
Expand Down Expand Up @@ -25,7 +26,7 @@ pub enum MetadataError {

/// YAML parsing error
#[error("YAML parsing error: {0}")]
YamlError(#[from] yaml_rust2::ScanError),
YamlError(#[from] SerdeYmlError),

/// JSON parsing error
#[error("JSON parsing error: {0}")]
Expand Down
64 changes: 64 additions & 0 deletions ssg-metadata/tests/test_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

#[cfg(test)]
mod tests {
use serde_json::Error as JsonError;
use serde_yml::Error as YamlError;
use ssg_metadata::error::MetadataError;
use std::io;
use toml::de::Error as TomlError;

/// Test `ExtractionError` construction.
///
Expand Down Expand Up @@ -72,4 +75,65 @@ mod tests {
let error = MetadataError::from(io_error);
assert_eq!(error.to_string(), "I/O error: File not found");
}

/// Test `YamlError` conversion.
///
/// This test ensures that a `serde_yml::Error` is correctly converted into the `YamlError` variant.
#[test]
fn test_yaml_error() {
// Malformed YAML content
let invalid_yaml = "invalid: yaml: data";

// Try to parse the invalid YAML, which will trigger a `serde_yml::Error`
let yaml_error: Result<serde_yml::Value, YamlError> =
serde_yml::from_str(invalid_yaml);

if let Err(yaml_error) = yaml_error {
// Convert the `serde_yml::Error` into `MetadataError`
let error = MetadataError::from(yaml_error);

// Check that the error message is correctly formatted
assert!(error.to_string().contains("YAML parsing error"));
} else {
panic!("Expected YAML parsing error, but got Ok");
}
}

/// Test `JsonError` conversion.
///
/// This test ensures that a `serde_json::Error` is correctly converted into the `JsonError` variant.
#[test]
fn test_json_error() {
let invalid_json = "{ invalid json }"; // Malformed JSON
let json_error: Result<serde_json::Value, JsonError> =
serde_json::from_str(invalid_json);

if let Err(json_error) = json_error {
let error = MetadataError::from(json_error);
// Check if the error message contains the correct phrase
assert!(
error.to_string().contains("JSON parsing error"),
"Error message should contain 'JSON parsing error'"
);
} else {
panic!("Expected JSON parsing error, but got Ok");
}
}

/// Test `TomlError` conversion.
///
/// This test ensures that a `toml::de::Error` is correctly converted into the `TomlError` variant.
#[test]
fn test_toml_error() {
let invalid_toml = "invalid = toml"; // Malformed TOML
let toml_error: Result<toml::Value, TomlError> =
toml::from_str(invalid_toml);

if let Err(toml_error) = toml_error {
let error = MetadataError::from(toml_error);
assert!(error.to_string().contains("TOML parsing error"));
} else {
panic!("Expected TOML parsing error, but got Ok");
}
}
}
97 changes: 97 additions & 0 deletions ssg-metadata/tests/test_metatags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#[cfg(test)]
mod tests {
use regex::Regex;
use ssg_metadata::generate_metatags;

Check failure on line 8 in ssg-metadata/tests/test_metatags.rs

View workflow job for this annotation

GitHub Actions / Lint

unused import: `ssg_metadata::generate_metatags`

Check warning on line 8 in ssg-metadata/tests/test_metatags.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `ssg_metadata::generate_metatags`
use ssg_metadata::MetaTagGroups;
use std::collections::HashMap;

/// Parses meta tags from an HTML string and returns a HashMap.
Expand Down Expand Up @@ -48,4 +50,99 @@ mod tests {
"Empty meta tags should not be parsed"
);
}

/// Test adding a custom primary meta tag.
#[test]
fn test_add_custom_primary_tag() {
let mut meta_tags = MetaTagGroups::default();
meta_tags.add_custom_tag("custom-tag", "custom value");

assert!(meta_tags.primary.contains("custom-tag"));
assert!(meta_tags.primary.contains("custom value"));
}

/// Test adding a custom OpenGraph (og) meta tag.
#[test]
fn test_add_custom_og_tag() {
let mut meta_tags = MetaTagGroups::default();
meta_tags.add_custom_tag("og:custom", "custom og value");

assert!(meta_tags.og.contains("og:custom"));
assert!(meta_tags.og.contains("custom og value"));
}

/// Test adding a custom Twitter meta tag.
#[test]
fn test_add_custom_twitter_tag() {
let mut meta_tags = MetaTagGroups::default();
meta_tags
.add_custom_tag("twitter:custom", "custom twitter value");

assert!(meta_tags.twitter.contains("twitter:custom"));
assert!(meta_tags.twitter.contains("custom twitter value"));
}

/// Test adding a custom Apple meta tag.
#[test]
fn test_add_custom_apple_tag() {
let mut meta_tags = MetaTagGroups::default();
meta_tags.add_custom_tag(
"apple-mobile-web-app-custom",
"custom apple value",
);

assert!(meta_tags
.apple
.contains("apple-mobile-web-app-custom"));
assert!(meta_tags.apple.contains("custom apple value"));
}

/// Test adding a custom Microsoft meta tag.
#[test]
fn test_add_custom_ms_tag() {
let mut meta_tags = MetaTagGroups::default();
meta_tags
.add_custom_tag("msapplication-custom", "custom ms value");

assert!(meta_tags.ms.contains("msapplication-custom"));
assert!(meta_tags.ms.contains("custom ms value"));
}

/// Test adding multiple custom tags of different types.
#[test]
fn test_add_multiple_custom_tags() {
let mut meta_tags = MetaTagGroups::default();
meta_tags.add_custom_tag("custom-primary", "primary value");
meta_tags.add_custom_tag("og:custom", "og value");
meta_tags.add_custom_tag("twitter:custom", "twitter value");
meta_tags.add_custom_tag(
"apple-mobile-web-app-custom",
"apple value",
);
meta_tags.add_custom_tag("msapplication-custom", "ms value");

// Check primary meta tags
assert!(meta_tags.primary.contains("<meta name=\"custom-primary\" content=\"primary value\">"),
"Primary meta tag should contain 'custom-primary'");

// Check Open Graph (og) meta tags
assert!(
meta_tags.og.contains(
"<meta name=\"og:custom\" content=\"og value\">"
),
"OG meta tag should contain 'og:custom'"
);

// Check Twitter meta tags
assert!(meta_tags.twitter.contains("<meta name=\"twitter:custom\" content=\"twitter value\">"),
"Twitter meta tag should contain 'twitter:custom'");

// Check Apple meta tags
assert!(meta_tags.apple.contains("<meta name=\"apple-mobile-web-app-custom\" content=\"apple value\">"),
"Apple meta tag should contain 'apple-mobile-web-app-custom'");

// Check Microsoft meta tags
assert!(meta_tags.ms.contains("<meta name=\"msapplication-custom\" content=\"ms value\">"),
"MS meta tag should contain 'msapplication-custom'");
}
}

0 comments on commit bbb1ebf

Please sign in to comment.