add some tests for the shared crate

this should test some stuff out in the test crate
This commit is contained in:
Fries 2023-07-13 09:29:28 -07:00
parent 075dd95c03
commit 5e0536d756
8 changed files with 64 additions and 9 deletions

View File

@ -13,7 +13,7 @@ pub(crate) fn add(
separator: &String,
) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
let mut names = names::load_names(names_file)?;
let mut names = names::load_names(&names_file)?;
if names.iter().any(|site| site.url.contains(url)) {
return Err(Error {

View File

@ -15,7 +15,7 @@ pub(crate) fn print(
json: bool,
) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
let mut names = names::load_names(names_file)?;
let mut names = names::load_names(&names_file)?;
if let Some(filter) = filter {
names.retain(|f| &f.url == filter);

View File

@ -5,7 +5,7 @@ use crate::commands::utils::{site_string, PrintOptions};
pub(crate) fn remove(path: &Path, url: &String, separator: &String) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
let mut names = names::load_names(names_file)?;
let mut names = names::load_names(&names_file)?;
names.retain(|site| {
if &site.url == url {

View File

@ -14,10 +14,8 @@ pub struct Error {
pub data: String,
}
pub(crate) static DIRECTORIES_ERROR_MESSAGE: &str = "could not retreive a valid home path from the operating system. maybe try to define the HOME enviroment variable if you\'re on a unix or unix like operating system.";
impl core::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl Error {
fn error_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"A {:?} error has occured.\nDetails: {}",
@ -25,3 +23,19 @@ impl core::fmt::Debug for Error {
)
}
}
impl std::error::Error for Error {}
pub(crate) static DIRECTORIES_ERROR_MESSAGE: &str = "could not retreive a valid home path from the operating system. maybe try to define the HOME enviroment variable if you\'re on a unix or unix like operating system.";
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.error_fmt(f)
}
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.error_fmt(f)
}
}

View File

@ -1,3 +1,5 @@
pub mod directories;
pub mod errors;
pub mod names;
#[cfg(test)]
mod tests;

View File

@ -8,7 +8,7 @@ pub struct Site {
pub name: Option<String>,
}
pub fn load_names(names: String) -> Result<Vec<Site>, Error> {
pub fn load_names(names: &str) -> Result<Vec<Site>, Error> {
match serde_json::from_str::<Vec<Site>>(&names) {
Ok(content) => {
log::debug!("successfully parsed names.json.");

View File

@ -0,0 +1,39 @@
use crate::{
errors::Error,
names::{load_names, read_names_file},
};
use std::{env, error};
#[test]
fn test_name_parsing() -> Result<(), Error> {
let names =
load_names(r#"[{"url": "sus.com", "name": "sussy"}, {"url": "sussy.com", "name": null}]"#)?;
assert_eq!(names.len(), 2);
assert_eq!(names[0].url, "sus.com");
assert_eq!(names[0].name.as_ref().unwrap(), "sussy");
assert_eq!(names[1].url, "sussy.com");
assert!(names[1].name.is_none());
Ok(())
}
#[test]
fn test_invalid_parsing() {
let no_url_field = load_names(r#"[{"name":""}]"#);
let no_fields = load_names(r#"[{}]"#);
let trailing_array = load_names("[");
assert!(no_url_field.is_err());
assert!(no_fields.is_err());
assert!(trailing_array.is_err());
}
#[test]
fn reading_a_non_existent_names_file() -> Result<(), Box<dyn error::Error>> {
let temp_file = env::temp_dir().join("meowy-test-names.json");
let contents = read_names_file(&temp_file)?;
assert_eq!(contents, "[]");
std::fs::remove_file(temp_file)?;
Ok(())
}

View File

@ -36,5 +36,5 @@ pub fn init_names() -> Result<(), Error> {
fn get_names() -> Result<Vec<Site>, Error> {
let names_path = directories::get_names_path()?;
let names_file = names::read_names_file(&names_path)?;
names::load_names(names_file)
names::load_names(&names_file)
}