Skip to content

Commit

Permalink
test(libmake): ✅ add new tests for ascii_art.rs and interface.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 14, 2024
1 parent 4ed479c commit d0d2b39
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,22 @@ pub fn generate_files(params: FileGenerationParams) -> io::Result<()> {
// Add check GitHub Actions workflows template
("github/workflows/check.tpl", ".github/workflows/check.yml"),
// Add coverage GitHub Actions workflows template
("github/workflows/coverage.tpl", ".github/workflows/coverage.yml"),
(
"github/workflows/coverage.tpl",
".github/workflows/coverage.yml",
),
// Add document GitHub Actions workflows template
("github/workflows/document.tpl", ".github/workflows/document.yml"),
(
"github/workflows/document.tpl",
".github/workflows/document.yml",
),
// Add lint GitHub Actions workflows template
("github/workflows/lint.tpl", ".github/workflows/lint.yml"),
// Add release GitHub Actions workflows template
("github/workflows/release.tpl", ".github/workflows/release.yml"),
(
"github/workflows/release.tpl",
".github/workflows/release.yml",
),
// Add test GitHub Actions workflows template
("github/workflows/test.tpl", ".github/workflows/test.yml"),
// --- #End GitHub Actions workflows ---
Expand Down
7 changes: 5 additions & 2 deletions tests/test_error_ascii_art.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use std::error::Error;
use libmake::models::error_ascii_art::AsciiArtError;
use std::error::Error;

#[test]
fn test_ascii_art_error_font_load_error() {
Expand All @@ -12,7 +12,10 @@ mod tests {
#[test]
fn test_ascii_art_error_conversion_error() {
let error = AsciiArtError::ConversionError;
assert_eq!(error.to_string(), "Failed to convert text to ASCII art");
assert_eq!(
error.to_string(),
"Failed to convert text to ASCII art"
);
}

#[test]
Expand Down
225 changes: 225 additions & 0 deletions tests/test_interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#[cfg(test)]
mod tests {
use libmake::{
interface::replace_placeholders,
models::model_params::FileGenerationParams,
};
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
};

fn create_test_file(path: &PathBuf, content: &str) {
let mut file =
File::create(path).expect("Unable to create test file");
file.write_all(content.as_bytes())
.expect("Unable to write to test file");
}

#[test]
fn test_successful_replacement() {
let template_path = PathBuf::from("./tests/test_template.txt");
let output_path = PathBuf::from("./tests/test_output.txt");

// Create a template file with placeholders
create_test_file(
&template_path,
"Hello, {{name}}!\nWelcome to {{website}}.",
);

// Setup FileGenerationParams with values to replace placeholders
let params = FileGenerationParams {
author: None,
build: None,
categories: None,
description: None,
documentation: None,
edition: None,
email: None,
homepage: None,
keywords: None,
license: None,
name: Some(String::from("Alice")),
output: None,
readme: None,
repository: None,
rustversion: None,
version: None,
website: Some(String::from("example.com")),
};

// Call the function and check the result
if let Err(e) =
replace_placeholders(&template_path, &output_path, &params)
{
eprintln!("Function failed with error: {:?}", e);
panic!("Function failed");
}

// Check if the output file was created
if !output_path.exists() {
panic!("Output file was not created");
}

let output_content = fs::read_to_string(&output_path)
.expect("Unable to read output file");
assert_eq!(
output_content,
"Hello, {Alice}!\nWelcome to {example.com}.\n"
);

// Clean up
fs::remove_file(template_path)
.expect("Unable to delete test template file");
fs::remove_file(output_path)
.expect("Unable to delete test output file");
}

#[test]
fn test_file_read_error() {
let template_path = PathBuf::from("non_existent_template.txt");
let output_path = PathBuf::from("test_output.txt");
let params = FileGenerationParams::default();

let result =
replace_placeholders(&template_path, &output_path, &params);
assert!(result.is_err());
}

#[test]
fn test_file_write_error() {
let template_path = PathBuf::from("test_template.txt");
let output_path = PathBuf::from("/invalid/test_output.txt");

create_test_file(&template_path, "Hello, {{name}}!");

let params = FileGenerationParams {
name: Some(String::from("Alice")),
..Default::default()
};

let result =
replace_placeholders(&template_path, &output_path, &params);
assert!(result.is_err());

// Clean up
fs::remove_file(template_path)
.expect("Unable to delete test template file");
}

#[test]
fn test_empty_template_file() {
let template_path = PathBuf::from("empty_template.txt");
let output_path = PathBuf::from("test_output.txt");

// Create an empty template file
create_test_file(&template_path, "");

let params = FileGenerationParams::default();

replace_placeholders(&template_path, &output_path, &params)
.expect("Function failed");

let output_content = fs::read_to_string(&output_path)
.expect("Unable to read output file");
assert_eq!(output_content, "");

// Clean up
fs::remove_file(template_path)
.expect("Unable to delete test template file");
fs::remove_file(output_path)
.expect("Unable to delete test output file");
}

#[test]
fn test_no_placeholders() {
let template_path = PathBuf::from("no_placeholders.txt");
let output_path = PathBuf::from("no_placeholders_output.txt");

// Create a template file with no placeholders
create_test_file(&template_path, "Hello, World!");

let params = FileGenerationParams::default();

replace_placeholders(&template_path, &output_path, &params)
.expect("Function failed");

let output_content = fs::read_to_string(&output_path)
.expect("Unable to read output file");
assert_eq!(output_content, "Hello, World!\n");

// Clean up
fs::remove_file(template_path)
.expect("Unable to delete test template file");
fs::remove_file(output_path)
.expect("Unable to delete test output file");
}

#[test]
fn test_missing_placeholders() {
let template_path = PathBuf::from("missing_placeholders.txt");
let output_path =
PathBuf::from("missing_placeholders_output.txt");

// Create a template file with some placeholders
create_test_file(
&template_path,
"Hello, {{name}}! Your email is {{email}}.",
);

// Setup FileGenerationParams with values to replace some placeholders
let params = FileGenerationParams {
name: Some(String::from("Alice")),
email: None,
// Initialize other fields as necessary
..Default::default()
};

replace_placeholders(&template_path, &output_path, &params)
.expect("Function failed");

let output_content = fs::read_to_string(&output_path)
.expect("Unable to read output file");
assert_eq!(
output_content,
"Hello, {Alice}! Your email is {}.\n"
);

// Clean up
fs::remove_file(template_path)
.expect("Unable to delete test template file");
fs::remove_file(output_path)
.expect("Unable to delete test output file");
}

#[test]
fn test_large_file() {
let template_path = PathBuf::from("large_template.txt");
let output_path = PathBuf::from("large_output.txt");

// Create a large template file
let large_content = "Hello, {name}! ".repeat(1000);
create_test_file(&template_path, &large_content);

let params = FileGenerationParams {
name: Some(String::from("Alice")),
// Initialize other fields as necessary
..Default::default()
};

replace_placeholders(&template_path, &output_path, &params)
.expect("Function failed");

let output_content = fs::read_to_string(&output_path)
.expect("Unable to read output file");
let looped_name = "Hello, Alice! ".repeat(1000);
assert_eq!(output_content, format!("{}\n", looped_name));

// Clean up
fs::remove_file(template_path)
.expect("Unable to delete test template file");
fs::remove_file(output_path)
.expect("Unable to delete test output file");
}
}

0 comments on commit d0d2b39

Please sign in to comment.