Skip to content

Commit

Permalink
fix(shokunin): test corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Sep 1, 2023
1 parent 282a457 commit a944001
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 47 deletions.
10 changes: 6 additions & 4 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ pub fn compile(
let compiled_files: Vec<FileData> = source_files
.into_iter()
.map(|file| {

let (metadata, keywords, all_meta_tags) = extract_and_prepare_metadata(&file.content);

// Generate HTML
Expand Down Expand Up @@ -210,8 +209,6 @@ pub fn compile(
let rss = generate_rss(&rss_data);
let rss_data = rss.unwrap();



// Generate a manifest data structure by extracting relevant information from the metadata.
let json = create_manifest_data(&metadata);

Expand Down Expand Up @@ -300,9 +297,14 @@ pub fn compile(
/// # Examples
///
/// ```rust
/// use ssg::data::FileData;
/// use ssg::compiler::extract_and_prepare_metadata;
///
/// let file_content = "---\n\n# Front Matter (YAML)\n\nauthor: \"Jane Doe\"\ncategory: \"Rust\"\ndescription: \"A blog about Rust programming.\"\nlayout: \"post\"\npermalink: \"https://example.com/blog/rust\"\ntags: \"rust,programming\"\ntitle: \"Rust\"\n\n---\n\n# Content\n\nThis is a blog about Rust programming.\n";
///
/// let (metadata, keywords, all_meta_tags) = extract_and_prepare_metadata(&file_content);
/// ```
fn extract_and_prepare_metadata(content: &str) -> (HashMap<String, String>, Vec<String>, MetaTagGroups) {
pub fn extract_and_prepare_metadata(content: &str) -> (HashMap<String, String>, Vec<String>, MetaTagGroups) {
// Extract metadata from front matter
let metadata = extract(content);

Expand Down
6 changes: 2 additions & 4 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ use std::collections::HashMap;
/// let title = "My Page";
/// let description = "This is a test page";
/// let html = generate_html(content, title, description, None);
/// assert_eq!(
/// html,
/// "<h1 id=\"h1-my\" id=\"\" id=\"h1-my\" class=\"my\">My Page</h1 id=\"h1-my\" class=\"my\"><p>This is a test page</p><h2 id=\"h2-hello\" class=\"hello\">Hello, world!</h2 id=\"h2-hello\" class=\"hello\">\n<p>This is a test.</p>\n"
/// );
/// let html_str = html.unwrap_or_else(|e| panic!("Error: {:?}", e));
/// assert_eq!(html_str, "<h1 id=\"h1-my\" tabindex=\"0\" id=\"\" tabindex=\"0\" class=\"my\">My Page</h1><p>This is a test page</p><h2 id=\"h2-hello\" tabindex=\"0\" class=\"hello\">Hello, world!</h2>\n<p>This is a test.</p>\n");
///
/// ```
//
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ macro_rules! macro_create_directories {
#[macro_export]
macro_rules! macro_generate_metatags {
($($key:literal, $value:expr),* $(,)?) => {
$crate::metatags::generate_metatags(&[ $(($key.to_owned(), $value.to_string())),* ])
$crate::modules::metatags::generate_metatags(&[ $(($key.to_owned(), $value.to_string())),* ])
};
}

Expand Down
26 changes: 4 additions & 22 deletions src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,13 @@ use std::collections::HashMap;
/// # Example
///
/// ```
/// let files = vec![
/// FileData {
/// content: r#"<title>Overview</title>
/// <meta content="This is a description" name="description">
/// <meta content="/overview" name="permalink">
/// </title>".to_string(),
/// },
/// FileData {
/// content: r#"<title>Other</title>
/// <meta content="This is another description" name="description">
/// <meta content="/other" name="permalink">
/// </title>".to_string(),
/// },
/// ];
/// use ssg::data::FileData;
/// use ssg::tags::generate_tags;
/// let files = vec![FileData { content: "This is a test".to_string(), ..Default::default() }];
///
/// generate_tags(files);
/// generate_tags(&files, &["test"]);
/// ```
///
/// This code will print the following to the console:
///
/// ```
/// Title: Overview
/// Description: This is a description
/// Permalink: /overview
/// ```
pub fn generate_tags(files: &[FileData], target_tags: &[&str]) -> HashMap<String, Vec<HashMap<String, String>>> {
let title_regex = Regex::new(r"<title>([^<]+)</title>").unwrap();
let meta_regex = Regex::new(r#"<meta\s+content="([^"]+)"\s+name="([^"]+)">"#).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ description: My Description
Ok(html) => {
assert_eq!(
html,
"<h1 id=\"h1-my\" id=\"\" id=\"h1-my\" class=\"my\">My Page</h1 id=\"h1-my\" class=\"my\"><p>This is a test page</p><h2 id=\"h2-hello\" class=\"hello\">Hello, world!</h2 id=\"h2-hello\" class=\"hello\">\n<p>This is a test.</p>\n"
"<h1 id=\"h1-my\" tabindex=\"0\" id=\"\" tabindex=\"0\" class=\"my\">My Page</h1><p>This is a test page</p><h2 id=\"h2-hello\" tabindex=\"0\" class=\"hello\">Hello, world!</h2>\n<p>This is a test.</p>\n"
);
},
Err(err) => {
Expand Down
18 changes: 9 additions & 9 deletions tests/test_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod tests {
let title = "Welcome";
let description = "Say hi to the world!";
let result = generate_html(content, title, description, None);
let expected = "<h1 id=\"h1-welcome\" id=\"\" id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\"><p>Say hi to the world!</p><h1 id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\">";
let expected = "<h1 id=\"h1-welcome\" tabindex=\"0\" id=\"\" tabindex=\"0\" class=\"welcome\">Welcome</h1><p>Say hi to the world!</p><h1 id=\"h1-welcome\" tabindex=\"0\" class=\"welcome\">Welcome</h1>";
match result {
Ok(res) => assert_eq!(res.trim(), expected),
Err(e) => panic!("Error: {:?}", e),
Expand All @@ -24,7 +24,7 @@ mod tests {
let title = "Welcome";
let description = "Say hi to the world!";
let result = generate_html(content, title, description, None);
let expected = "<h1 id=\"h1-welcome\" id=\"\" id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\"><p>Say hi to the world!</p><h1 id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\">";
let expected = "<h1 id=\"h1-welcome\" tabindex=\"0\" id=\"\" tabindex=\"0\" class=\"welcome\">Welcome</h1><p>Say hi to the world!</p><h1 id=\"h1-welcome\" tabindex=\"0\" class=\"welcome\">Welcome</h1>";
match result {
Ok(res) => assert_eq!(res.trim(), expected),
Err(e) => panic!("Error: {:?}", e),
Expand All @@ -38,7 +38,7 @@ mod tests {
let title = "";
let description = "Say hi to the world!";
let result = generate_html(content, title, description, None);
let expected = "<p>Say hi to the world!</p><h1 id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\">";
let expected = "<p>Say hi to the world!</p><h1 id=\"h1-welcome\" tabindex=\"0\" class=\"welcome\">Welcome</h1>";
match result {
Ok(res) => assert_eq!(res.trim(), expected),
Err(e) => panic!("Error: {:?}", e),
Expand All @@ -51,7 +51,7 @@ mod tests {
let title = "Welcome";
let description = "";
let result = generate_html(content, title, description, None);
let expected = "<h1 id=\"h1-welcome\" id=\"\" id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\"><h1 id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\">";
let expected = "<h1 id=\"h1-welcome\" tabindex=\"0\" id=\"\" tabindex=\"0\" class=\"welcome\">Welcome</h1><h1 id=\"h1-welcome\" tabindex=\"0\" class=\"welcome\">Welcome</h1>";
match result {
Ok(res) => assert_eq!(res.trim(), expected),
Err(e) => panic!("Error: {:?}", e),
Expand All @@ -64,7 +64,7 @@ mod tests {
let title = "Welcome";
let description = "Say hi to the world!";
let result = generate_html(content, title, description, None);
let expected = "<h1 id=\"h1-welcome\" id=\"\" id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\"><p>Say hi to the world!</p><h1 id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\">";
let expected = "<h1 id=\"h1-welcome\" tabindex=\"0\" id=\"\" tabindex=\"0\" class=\"welcome\">Welcome</h1><p>Say hi to the world!</p><h1 id=\"h1-welcome\" tabindex=\"0\" class=\"welcome\">Welcome</h1>";
match result {
Ok(res) => assert_eq!(res.trim(), expected),
Err(e) => panic!("Error: {:?}", e),
Expand All @@ -77,7 +77,7 @@ mod tests {
let title = "Welcome";
let description = "Say hi to the world!";
let result = generate_html(content, title, description, None);
let expected = "<h1 id=\"h1-welcome\" id=\"\" id=\"h1-welcome\" class=\"welcome\">Welcome</h1 id=\"h1-welcome\" class=\"welcome\"><p>Say hi to the world!</p>";
let expected = "<h1 id=\"h1-welcome\" tabindex=\"0\" id=\"\" tabindex=\"0\" class=\"welcome\">Welcome</h1><p>Say hi to the world!</p>";
match result {
Ok(res) => assert_eq!(res.trim(), expected),
Err(e) => panic!("Error: {:?}", e),
Expand All @@ -89,7 +89,7 @@ mod tests {
let header_str = "<h1>Hello, world!</h1>";
let id_regex = Regex::new(r"[^a-z0-9]+").unwrap();
let result = format_header_with_id_class(header_str, &id_regex);
let expected = "<h1 id=\"h1-hello\" class=\"hello\">Hello, world!</h1 id=\"h1-hello\" class=\"hello\">";
let expected = "<h1 id=\"h1-hello\" tabindex=\"0\" class=\"hello\">Hello, world!</h1>";
assert_eq!(result, expected);
}

Expand All @@ -98,7 +98,7 @@ mod tests {
let header_str = "<h1>Welcome to the world</h1>";
let id_regex = Regex::new(r"[^a-z0-9]+").unwrap();
let result = format_header_with_id_class(header_str, &id_regex);
let expected = "<h1 id=\"h1-welcome\" class=\"welcome\">Welcome to the world</h1 id=\"h1-welcome\" class=\"welcome\">";
let expected = "<h1 id=\"h1-welcome\" tabindex=\"0\" class=\"welcome\">Welcome to the world</h1>";
assert_eq!(result, expected);
}

Expand All @@ -107,7 +107,7 @@ mod tests {
let header_str = "<h1>Hello, world! #$%^&*()</h1>";
let id_regex = Regex::new(r"[^a-z0-9]+").unwrap();
let result = format_header_with_id_class(header_str, &id_regex);
let expected = "<h1 id=\"h1-hello\" class=\"hello\">Hello, world! #$%^&*()</h1 id=\"h1-hello\" class=\"hello\">";
let expected = "<h1 id=\"h1-hello\" tabindex=\"0\" class=\"hello\">Hello, world! #$%^&*()</h1>";
assert_eq!(result, expected);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/test_navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ mod tests {
tags: "".to_string(),
txt: "".to_string(),
},
FileData {
cname: "".to_string(),
content: "".to_string(),
human: "".to_string(),
json: "".to_string(),
keyword: "".to_string(),
name: "contact.md".to_string(),
rss: "".to_string(),
sitemap: "".to_string(),
tags: "".to_string(),
txt: "".to_string(),
},
];

let navigation = generate_navigation(&files);

println!("Generated navigation: {}", navigation);

// Assert that the generated navigation contains expected HTML elements
assert!(navigation.contains("<li class=\"nav-item\"><a href=\"/about/index.html\" class=\"text-uppercase p-2 \">About</a></li>"));
assert!(navigation.contains("<li class=\"nav-item\"><a href=\"/contact/index.html\" class=\"text-uppercase p-2 \">Contact</a></li>"));
Expand Down
12 changes: 6 additions & 6 deletions tests/test_utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,27 +420,27 @@ mod tests {
let test_cases = vec![
(
"<h1>This is a test</h1>",
"<h1 id=\"h1-this\" class=\"this\">This is a test</h1 id=\"h1-this\" class=\"this\">"
"<h1 id=\"h1-this\" tabindex=\"0\" class=\"this\">This is a test</h1>"
),
(
"<h2>Another Test</h2>",
"<h2 id=\"h2-another\" class=\"another\">Another Test</h2 id=\"h2-another\" class=\"another\">"
"<h2 id=\"h2-another\" tabindex=\"0\" class=\"another\">Another Test</h2>"
),
(
"<h3>Test with special characters!@#$%^&*</h3>",
"<h3 id=\"h3-test\" class=\"test\">Test with special characters!@#$%^&*</h3 id=\"h3-test\" class=\"test\">"
"<h3 id=\"h3-test\" tabindex=\"0\" class=\"test\">Test with special characters!@#$%^&*</h3>"
),
(
"<h1>Test with multiple spaces</h1>",
"<h1 id=\"h1-test\" class=\"test\">Test with multiple spaces</h1 id=\"h1-test\" class=\"test\">"
"<h1 id=\"h1-test\" tabindex=\"0\" class=\"test\">Test with multiple spaces</h1>"
),
(
"<h1>Test_with_underscores</h1>",
"<h1 id=\"h1-test\" class=\"test\">Test_with_underscores</h1 id=\"h1-test\" class=\"test\">"
"<h1 id=\"h1-test\" tabindex=\"0\" class=\"test\">Test_with_underscores</h1>"
),
(
"<h1>Test-with-dashes</h1>",
"<h1 id=\"h1-test\" class=\"test\">Test-with-dashes</h1 id=\"h1-test\" class=\"test\">"
"<h1 id=\"h1-test\" tabindex=\"0\" class=\"test\">Test-with-dashes</h1>"
),
];

Expand Down

0 comments on commit a944001

Please sign in to comment.