test(config): add more tests

This commit is contained in:
Paul Campbell 2024-05-17 20:19:47 +01:00
parent 6889235b07
commit 896e1cba42

View file

@ -1,9 +1,11 @@
type TestResult = Result<(), Box<dyn std::error::Error>>; type TestResult = Result<(), Box<dyn std::error::Error>>;
mod server_repo_config { mod server_repo_config {
use std::path::PathBuf;
use assert2::let_assert; use assert2::let_assert;
use crate::{RepoBranches, RepoConfig, RepoConfigSource}; use crate::{BranchName, GitDir, RepoBranches, RepoConfig, RepoConfigSource, RepoPath};
use super::super::server_repo_config::*; use super::super::server_repo_config::*;
@ -34,6 +36,48 @@ mod server_repo_config {
) )
); );
} }
#[test]
fn should_return_repo() {
let src = ServerRepoConfig::new(
"repo".to_string(),
"branch".to_string(),
None,
Some("main".to_string()),
Some("next".to_string()),
Some("dev".to_string()),
);
assert_eq!(src.repo(), RepoPath::new("repo".to_string()));
}
#[test]
fn should_return_branch() {
let src = ServerRepoConfig::new(
"repo".to_string(),
"branch".to_string(),
None,
Some("main".to_string()),
Some("next".to_string()),
Some("dev".to_string()),
);
assert_eq!(src.branch(), BranchName::new("branch".to_string()));
}
#[test]
fn should_return_gitdir() {
let src = ServerRepoConfig::new(
"repo".to_string(),
"branch".to_string(),
Some("gitdir".into()),
Some("main".to_string()),
Some("next".to_string()),
Some("dev".to_string()),
);
assert_eq!(
src.gitdir(),
Some(GitDir::new(&PathBuf::default().join("gitdir")))
);
}
} }
mod repo_config { mod repo_config {
use crate::{RepoBranches, RepoConfigSource}; use crate::{RepoBranches, RepoConfigSource};
@ -62,15 +106,32 @@ mod repo_config {
Ok(()) Ok(())
} }
#[test]
fn should_return_branches() {
let branches = RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string());
let repo_config = RepoConfig::new(branches.clone(), RepoConfigSource::Repo);
assert_eq!(repo_config.branches(), &branches);
}
#[test]
fn should_return_source() {
let repo_config = RepoConfig::new(
RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string()),
RepoConfigSource::Repo,
);
assert_eq!(repo_config.source(), RepoConfigSource::Repo);
}
} }
mod forge_config { mod forge_config {
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::{ForgeConfig, ForgeType, RepoAlias, ServerRepoConfig}; use secrecy::ExposeSecret;
use crate::{ForgeConfig, ForgeType, Hostname, RepoAlias, ServerRepoConfig, User};
use super::*;
#[test] #[test]
fn should_return_repos() -> TestResult { fn should_return_repos() {
let forge_type = ForgeType::MockForge; let forge_type = ForgeType::MockForge;
let hostname = "localhost".to_string(); let hostname = "localhost".to_string();
let user = "bob".to_string(); let user = "bob".to_string();
@ -106,6 +167,290 @@ mod forge_config {
(RepoAlias::new("red"), &red), (RepoAlias::new("red"), &red),
] ]
); );
Ok(()) }
#[test]
fn should_return_forge_type() {
let forge_type = ForgeType::MockForge;
let hostname = "localhost".to_string();
let user = "bob".to_string();
let token = "alpha".to_string();
let repos = BTreeMap::new();
let fc = ForgeConfig::new(forge_type, hostname, user, token, repos);
assert_eq!(fc.forge_type(), ForgeType::MockForge);
}
#[test]
fn should_return_hostname() {
let forge_type = ForgeType::MockForge;
let hostname = "localhost".to_string();
let user = "bob".to_string();
let token = "alpha".to_string();
let repos = BTreeMap::new();
let fc = ForgeConfig::new(forge_type, hostname, user, token, repos);
assert_eq!(fc.hostname(), Hostname::new("localhost"));
}
#[test]
fn should_return_user() {
let forge_type = ForgeType::MockForge;
let hostname = "localhost".to_string();
let user = "bob".to_string();
let token = "alpha".to_string();
let repos = BTreeMap::new();
let fc = ForgeConfig::new(forge_type, hostname, user, token, repos);
assert_eq!(fc.user(), User::new("bob".to_string()));
}
#[test]
fn should_return_token() {
let forge_type = ForgeType::MockForge;
let hostname = "localhost".to_string();
let user = "bob".to_string();
let token = "alpha".to_string();
let repos = BTreeMap::new();
let fc = ForgeConfig::new(forge_type, hostname, user, token, repos);
assert_eq!(fc.token().expose_secret(), "alpha");
}
#[test]
fn should_return_repo() {
let forge_type = ForgeType::MockForge;
let hostname = "localhost".to_string();
let user = "bob".to_string();
let token = "alpha".to_string();
let red = ServerRepoConfig::new(
"red".to_string(),
"main".to_string(),
None,
None,
None,
None,
);
let blue = ServerRepoConfig::new(
"blue".to_string(),
"main".to_string(),
None,
None,
None,
None,
);
let mut repos = BTreeMap::new();
repos.insert("red".to_string(), red.clone());
repos.insert("blue".to_string(), blue);
let fc = ForgeConfig::new(forge_type, hostname, user, token, repos);
let returned_repo = fc.get_repo("red");
assert_eq!(returned_repo, Some(&red),);
}
}
mod forge_details {
use std::collections::BTreeMap;
use secrecy::ExposeSecret;
use crate::{ApiToken, ForgeConfig, ForgeDetails, ForgeName, ForgeType, Hostname, User};
#[test]
fn should_return_forge_name() {
let forge_type = ForgeType::MockForge;
let hostname = Hostname::new("localhost".to_string());
let user = User::new("bob".to_string());
let token = ApiToken::new("alpha".to_string().into());
let forge_name = ForgeName::new("gamma".to_string());
let forge_details =
ForgeDetails::new(forge_name.clone(), forge_type, hostname, user, token);
let result = forge_details.forge_name();
assert_eq!(result, &forge_name);
}
#[test]
fn should_return_forge_type() {
let forge_type = ForgeType::MockForge;
let hostname = Hostname::new("localhost".to_string());
let user = User::new("bob".to_string());
let token = ApiToken::new("alpha".to_string().into());
let forge_name = ForgeName::new("gamma".to_string());
let forge_details = ForgeDetails::new(forge_name, forge_type, hostname, user, token);
let result = forge_details.forge_type();
assert_eq!(result, forge_type);
}
#[test]
fn should_return_hostname() {
let forge_type = ForgeType::MockForge;
let hostname = Hostname::new("localhost".to_string());
let user = User::new("bob".to_string());
let token = ApiToken::new("alpha".to_string().into());
let forge_name = ForgeName::new("gamma".to_string());
let forge_details =
ForgeDetails::new(forge_name, forge_type, hostname.clone(), user, token);
let result = forge_details.hostname();
assert_eq!(result, &hostname);
}
#[test]
fn should_return_user() {
let forge_type = ForgeType::MockForge;
let hostname = Hostname::new("localhost".to_string());
let user = User::new("bob".to_string());
let token = ApiToken::new("alpha".to_string().into());
let forge_name = ForgeName::new("gamma".to_string());
let forge_details =
ForgeDetails::new(forge_name, forge_type, hostname, user.clone(), token);
let result = forge_details.user();
assert_eq!(result, &user);
}
#[test]
fn should_return_token() {
let forge_type = ForgeType::MockForge;
let hostname = Hostname::new("localhost".to_string());
let user = User::new("bob".to_string());
let token = ApiToken::new("alpha".to_string().into());
let forge_name = ForgeName::new("gamma".to_string());
let forge_details =
ForgeDetails::new(forge_name, forge_type, hostname, user, token.clone());
let result = forge_details.token();
assert_eq!(result.expose_secret(), token.expose_secret());
}
#[test]
fn with_hostname_should_return_new_instance() {
let forge_type = ForgeType::MockForge;
let hostname = Hostname::new("localhost".to_string());
let user = User::new("bob".to_string());
let token = ApiToken::new("alpha".to_string().into());
let forge_name = ForgeName::new("gamma".to_string());
let forge_details = ForgeDetails::new(forge_name, forge_type, hostname, user, token);
let result = forge_details.with_hostname(Hostname::new("remotehost".to_string()));
assert_eq!(result.hostname(), &Hostname::new("remotehost".to_string()));
}
#[test]
fn should_convert_from_name_and_config() {
let forge_type = ForgeType::MockForge;
let hostname = Hostname::new("localhost".to_string());
let user = User::new("bob".to_string());
let token = ApiToken::new("alpha".to_string().into());
let forge_name = ForgeName::new("gamma".to_string());
let forge_config = ForgeConfig::new(
forge_type,
"localhost".to_string(),
"bob".to_string(),
"alpha".to_string(),
BTreeMap::new(),
);
let forge_details = ForgeDetails::from((&forge_name, &forge_config));
assert_eq!(forge_details.forge_name(), &forge_name);
assert_eq!(forge_details.hostname(), &hostname);
assert_eq!(forge_details.user(), &user);
assert_eq!(forge_details.token().expose_secret(), token.expose_secret());
}
}
mod forge_name {
use std::path::PathBuf;
use crate::ForgeName;
#[test]
fn should_convert_to_pathbuf() {
let forge_name = ForgeName::new("alpha".to_string());
let pathbuf: PathBuf = (&forge_name).into();
assert_eq!(pathbuf, PathBuf::new().join("alpha"));
}
}
mod forge_type {
use crate::ForgeType;
#[test]
fn should_display_as_lowercase() {
assert_eq!(ForgeType::MockForge.to_string(), "mockforge".to_string());
}
}
mod gitdir {
use std::path::PathBuf;
use derive_more::Deref;
use crate::GitDir;
#[test]
fn should_return_pathbuf() {
let pathbuf = PathBuf::default().join("foo");
let gitdir = GitDir::new(&pathbuf);
let result = gitdir.pathbuf();
assert_eq!(result, &pathbuf);
}
#[test]
fn should_display() {
let pathbuf = PathBuf::default().join("foo");
let gitdir = GitDir::new(&pathbuf);
let result = gitdir.to_string();
assert_eq!(result, "foo");
}
#[test]
fn should_convert_from_str() {
let pathbuf = PathBuf::default().join("foo");
let gitdir: GitDir = "foo".into();
assert_eq!(gitdir, GitDir::new(&pathbuf));
}
#[test]
fn should_convert_to_pathbuf_from_ref() {
let pathbuf = PathBuf::default().join("foo");
let gitdir: GitDir = "foo".into();
let result: PathBuf = (&gitdir).into();
assert_eq!(result, pathbuf);
}
#[test]
fn should_convert_to_pathbuf_from_inst() {
let pathbuf = PathBuf::default().join("foo");
let gitdir: GitDir = "foo".into();
let result: PathBuf = gitdir.into();
assert_eq!(result, pathbuf);
}
}
mod repo_branches {
use crate::{BranchName, RepoBranches};
#[test]
fn should_return_main() {
let repo_branches =
RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string());
assert_eq!(repo_branches.main(), BranchName::new("main"));
}
#[test]
fn should_return_next() {
let repo_branches =
RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string());
assert_eq!(repo_branches.next(), BranchName::new("next"));
}
#[test]
fn should_return_dev() {
let repo_branches =
RepoBranches::new("main".to_string(), "next".to_string(), "dev".to_string());
assert_eq!(repo_branches.dev(), BranchName::new("dev"));
} }
} }