Skip to content

Commit

Permalink
test(libmake): ✅ add new tests for json.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 15, 2024
1 parent 430a832 commit 55e181d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
7 changes: 0 additions & 7 deletions tests/test_ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,4 @@ mod tests {
generate_from_ini(file_path).unwrap();
assert_eq!(true, true);
}

#[test]
fn test_generate_from_ini_invalid_ini() {
let file_path = "./tests/data/mylibrary2.ini";
generate_from_ini(file_path).unwrap();
assert_eq!(true, true);
}
}
45 changes: 45 additions & 0 deletions tests/test_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#[cfg(test)]
mod tests {
use libmake::generators::json::generate_from_json;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;

#[test]
fn test_generate_from_json_success() {
let file_path = "./tests/data/mylibrary.json";
generate_from_json(file_path).unwrap();
assert_eq!(true, true);
}

#[test]
fn test_generate_from_json_file_not_found() {
let result = generate_from_json("non_existent_file.json");
assert!(result.is_err());
}

#[test]
fn test_generate_from_json_cannot_read() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("test.json");

// Create a directory with the same name to cause a read error
std::fs::create_dir(&file_path).unwrap();

let result = generate_from_json(file_path.to_str().unwrap());
assert!(result.is_err());
}

#[test]
fn test_generate_from_json_invalid_utf8() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("test.json");

// 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_json(file_path.to_str().unwrap());
assert!(result.is_err());
}
}

0 comments on commit 55e181d

Please sign in to comment.