-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(libmake): ✅ add new tests for
toml.rs
- Loading branch information
1 parent
55e181d
commit 5df2c9f
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,84 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use libmake::generators::ini::generate_from_ini; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_generate_from_ini_valid_ini() { | ||
let file_path = "./tests/data/mylibrary.ini"; | ||
generate_from_ini(file_path).unwrap(); | ||
assert_eq!(true, true); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_ini_file_not_found() { | ||
let result = generate_from_ini("non_existent_file.ini"); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_ini_cannot_read() { | ||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("test.ini"); | ||
|
||
// Create a directory with the same name to cause a read error | ||
std::fs::create_dir(&file_path).unwrap(); | ||
|
||
let result = generate_from_ini(file_path.to_str().unwrap()); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_ini_invalid_utf8() { | ||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("test.ini"); | ||
|
||
// Write invalid UTF-8 data to the file | ||
let mut file = File::create(&file_path).unwrap(); | ||
file.write_all(&[0x80, 0x81, 0x82]).unwrap(); | ||
|
||
let result = generate_from_ini(file_path.to_str().unwrap()); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_ini_parse_error() { | ||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("test.ini"); | ||
|
||
let invalid_ini_data = r#" | ||
[invalid_section] | ||
invalid_field = "invalid_value" | ||
"#; | ||
|
||
let mut file = File::create(&file_path).unwrap(); | ||
file.write_all(invalid_ini_data.as_bytes()).unwrap(); | ||
|
||
let result = generate_from_ini(file_path.to_str().unwrap()); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_ini_generate_files_error() { | ||
// This test assumes that macro_generate_files! returns an error for certain parameters. | ||
// Adjust the test if necessary based on the actual implementation of the macro. | ||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("test.ini"); | ||
|
||
let ini_data = r#" | ||
[project] | ||
output = "test_output_dir" | ||
"#; | ||
|
||
let mut file = File::create(&file_path).unwrap(); | ||
file.write_all(ini_data.as_bytes()).unwrap(); | ||
|
||
// Mocking or adjusting the macro_generate_files! to return an error would be necessary. | ||
// This part of the test may require more advanced setup depending on the macro implementation. | ||
|
||
let result = generate_from_ini(file_path.to_str().unwrap()); | ||
assert!(result.is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use libmake::generators::toml::generate_from_toml; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_generate_from_toml_success() { | ||
let file_path = "./tests/data/mylibrary.toml"; | ||
generate_from_toml(file_path).unwrap(); | ||
assert_eq!(true, true); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_toml_file_not_found() { | ||
let result = generate_from_toml("non_existent_file.toml"); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_toml_cannot_read() { | ||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("test.toml"); | ||
|
||
// Create a directory with the same name to cause a read error | ||
std::fs::create_dir(&file_path).unwrap(); | ||
|
||
let result = generate_from_toml(file_path.to_str().unwrap()); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_toml_invalid_utf8() { | ||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("test.toml"); | ||
|
||
// Write invalid UTF-8 data to the file | ||
let mut file = File::create(&file_path).unwrap(); | ||
file.write_all(&[0x80, 0x81, 0x82]).unwrap(); | ||
|
||
let result = generate_from_toml(file_path.to_str().unwrap()); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_toml_deserialize_error() { | ||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("test.toml"); | ||
|
||
let invalid_toml_data = r#" | ||
invalid_field = "invalid_value" | ||
"#; | ||
|
||
let mut file = File::create(&file_path).unwrap(); | ||
file.write_all(invalid_toml_data.as_bytes()).unwrap(); | ||
|
||
let result = generate_from_toml(file_path.to_str().unwrap()); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_generate_from_toml_generate_files_error() { | ||
// This test assumes that macro_generate_files! returns an error for certain parameters. | ||
// Adjust the test if necessary based on the actual implementation of the macro. | ||
let dir = tempdir().unwrap(); | ||
let file_path = dir.path().join("test.toml"); | ||
|
||
let toml_data = r#" | ||
output = "test_output_dir" | ||
"#; | ||
|
||
let mut file = File::create(&file_path).unwrap(); | ||
file.write_all(toml_data.as_bytes()).unwrap(); | ||
|
||
// Mocking or adjusting the macro_generate_files! to return an error would be necessary. | ||
// This part of the test may require more advanced setup depending on the macro implementation. | ||
|
||
let result = generate_from_toml(file_path.to_str().unwrap()); | ||
assert!(result.is_err()); | ||
} | ||
} |