test: Initial unit testing

- Test quests vector loading
- Test default quest on empty fields
- Test correct quest
- Fixed config paths handling
This commit is contained in:
Alexey 2025-11-28 17:00:17 +03:00
commit 94d771107d
7 changed files with 102 additions and 15 deletions

View file

@ -1,30 +1,32 @@
//! Configuration file that handles (de-)serializing other components
use std::{fs::{self, DirEntry},path::PathBuf};
use std::{fs::{self, DirEntry},path::{Path, PathBuf}};
use serde::Deserialize;
use crate::quest::{Quest,error::QuestError};
/// Struct for containing paths to other (de-)serializable things
#[derive(Deserialize)]
#[serde(default)]
pub struct Config {
/// Path to config directory
#[serde(skip)]
path: PathBuf,
/// Path to serialized [quests][`crate::quest::Quest`] folder
#[serde(default)]
pub quests_path: PathBuf,
/// Path to serialized [accounts][`crate::account::Account`] folder
#[serde(default)]
pub accounts_path: PathBuf,
/// Path to serialized [map][`crate::map::Map`] file
#[serde(default)]
pub map: PathBuf
}
impl Default for Config {
fn default() -> Self {
Config {
path: ".".into(),
quests_path: "quests".into(),
accounts_path: "accounts".into(),
map: "map.toml".into()
@ -60,16 +62,22 @@ impl Config {
/// let config = Config::load(path);
/// ```
pub fn load(path: PathBuf) -> Self {
let dir = path.parent()
.unwrap_or(Path::new("."))
.to_owned();
match fs::read_to_string(path) {
Ok(string) => {
match toml::from_str::<Config>(&string) {
Ok(conf) => {
Ok(mut conf) => {
println!("Successfully loaded config");
conf.path = dir;
conf
},
Err(error) => {
eprintln!("Error on parsing config: {error}");
Config::default()
let mut cfg = Config::default();
cfg.path = dir;
cfg
}
}
},
@ -99,7 +107,10 @@ impl Config {
pub fn load_quests(&self) -> Vec<Quest> {
let mut out_vec = Vec::new();
match fs::read_dir(&self.quests_path) {
let mut path = self.path.clone();
path.push(self.quests_path.clone());
match fs::read_dir(path) {
Ok(iter) => {
for entry in iter {
match entry {