Skip to content

Commit

Permalink
test: improve test code (#60)
Browse files Browse the repository at this point in the history
* done

* fix
  • Loading branch information
shixinhuang99 committed Mar 4, 2024
1 parent 51189fa commit e49bf6f
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 227 deletions.
2 changes: 1 addition & 1 deletion src/colorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod tests {
use super::Colorize;

#[test]
fn test_no_color() {
fn test_no_color_when_test() {
assert_eq!("foo".blue(), "foo");
assert_eq!("foo".to_string().blue(), "foo");
}
Expand Down
39 changes: 24 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,30 @@ mod test_utils {

pub struct ConfigMock {
pub config: Config,
pub tmpdir: TempDir,
pub tmp_dir: TempDir,
}

impl ConfigMock {
pub fn new() -> Self {
let tmpdir = tempdir().unwrap();
let config = Config::new(tmpdir.path()).unwrap();
let tmp_dir = tempdir().unwrap();
let config = Config::new(tmp_dir.path()).unwrap();

Self {
tmpdir,
tmp_dir,
config,
}
}

pub fn with_content(self) -> Self {
let tmpdir_path = self.tmpdir.path();
let tmp_dir_path = self.tmp_dir.path();

fs::write(
tmpdir_path.join(Config::FILE_NAME),
tmp_dir_path.join(Config::FILE_NAME),
"{\n \"token\": \"token\"\n}",
)
.unwrap();

let config = Config::new(tmpdir_path).unwrap();
let config = Config::new(tmp_dir_path).unwrap();

Self {
config,
Expand All @@ -96,26 +96,35 @@ mod tests {

#[test]
fn test_config_new_not_exists() {
let config_mock = ConfigMock::new();
let ConfigMock {
tmp_dir: _tmp_dir,
config,
} = ConfigMock::new();

assert_eq!(config_mock.config.token(), None);
assert_eq!(config.token(), None);
}

#[test]
fn test_config_new_exists() {
let config_mock = ConfigMock::new().with_content();
let ConfigMock {
tmp_dir: _tmp_dir,
config,
} = ConfigMock::new().with_content();

assert_eq!(config_mock.config.token(), Some("token"));
assert_eq!(config.token(), Some("token"));
}

#[test]
fn test_config_save() -> Result<()> {
let mut config_mock = ConfigMock::new();
let ConfigMock {
tmp_dir: _tmp_dir,
mut config,
} = ConfigMock::new();

config_mock.config.set_token("token2");
config_mock.config.save()?;
config.set_token("token2");
config.save()?;

let actual = fs::read_to_string(&config_mock.config.path)?;
let actual = fs::read_to_string(&config.path)?;
assert_eq!(actual, "{\n \"token\": \"token2\"\n}");

Ok(())
Expand Down
59 changes: 38 additions & 21 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ where
return Ok(value);
}
}

let default = Self::default();
fs::write(file_path, serde_json::to_string_pretty(&default)?)?;

Expand Down Expand Up @@ -48,24 +49,24 @@ mod test_utils {

pub struct JsonContentMock {
pub foo: Foo,
pub tmpdir: TempDir,
pub tmp_dir: TempDir,
pub path: PathBuf,
}

impl JsonContentMock {
pub fn new() -> Self {
let tmpdir = tempdir().unwrap();
let file_path = tmpdir.path().join("foo.json");
let foo = Foo::load(&file_path).unwrap();
let tmp_dir = tempdir().unwrap();
let path = tmp_dir.path().join("foo.json");
let foo = Foo::load(&path).unwrap();

Self {
foo,
tmpdir,
path: file_path,
tmp_dir,
path,
}
}

pub fn with_empty_content(self) -> Self {
pub fn no_content(self) -> Self {
fs::write(&self.path, "").unwrap();

let foo = Foo::load(&self.path).unwrap();
Expand Down Expand Up @@ -99,34 +100,50 @@ mod tests {

#[test]
fn test_json_load_file() {
let json_content_mock = JsonContentMock::new().with_content();
let JsonContentMock {
tmp_dir: _tmp_dir,
foo,
..
} = JsonContentMock::new().with_content();

assert_eq!(json_content_mock.foo.bar, "bar");
assert_eq!(foo.bar, "bar");
}

#[test]
fn test_json_load_empty_content() {
let json_content_mock = JsonContentMock::new().with_empty_content();

assert_eq!(json_content_mock.foo.bar, "");
fn test_json_load_no_content() {
let JsonContentMock {
tmp_dir: _tmp_dir,
foo,
..
} = JsonContentMock::new().no_content();

assert_eq!(foo.bar, "");
}

#[test]
fn test_json_load_file_not_exists() {
let json_content_mock = JsonContentMock::new();

assert_eq!(json_content_mock.foo.bar, "");
assert!(json_content_mock.path.exists());
let JsonContentMock {
tmp_dir: _tmp_dir,
foo,
path,
} = JsonContentMock::new();

assert_eq!(foo.bar, "");
assert!(path.exists());
}

#[test]
fn test_json_save() -> Result<()> {
let mut json_content_mock = JsonContentMock::new().with_content();
let JsonContentMock {
tmp_dir: _tmp_dir,
mut foo,
path,
} = JsonContentMock::new().with_content();

json_content_mock.foo.bar = "bar2".to_string();
json_content_mock.foo.save(&json_content_mock.path)?;
foo.bar = "bar2".to_string();
foo.save(&path)?;

let actual = fs::read_to_string(&json_content_mock.path)?;
let actual = fs::read_to_string(&path)?;
assert_eq!(actual, "{\n \"bar\": \"bar2\"\n}");

Ok(())
Expand Down
45 changes: 25 additions & 20 deletions src/path_ext.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
use std::path::{Path, PathBuf};

#[cfg(test)]
use path_slash::PathBufExt;

#[cfg(test)]
pub trait JoinSlash {
fn join_slash<T>(&self, s: T) -> PathBuf
where
T: AsRef<str>;
}

#[cfg(test)]
impl JoinSlash for Path {
fn join_slash<T>(&self, s: T) -> PathBuf
where
T: AsRef<str>,
{
self.join(PathBuf::from_slash(s))
}
}

pub trait JoinIter<I> {
fn join_iter<T>(&self, iter: T) -> PathBuf
where
Expand All @@ -34,3 +14,28 @@ impl<I: AsRef<Path>> JoinIter<I> for Path {
self.join(PathBuf::from_iter(iter))
}
}

#[cfg(test)]
pub use join_slash_ext::JoinSlash;

#[cfg(test)]
mod join_slash_ext {
use std::path::{Path, PathBuf};

use path_slash::PathBufExt;

pub trait JoinSlash {
fn join_slash<T>(&self, s: T) -> PathBuf
where
T: AsRef<str>;
}

impl JoinSlash for Path {
fn join_slash<T>(&self, s: T) -> PathBuf
where
T: AsRef<str>,
{
self.join(PathBuf::from_slash(s))
}
}
}
1 change: 1 addition & 0 deletions src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod tests {
#[test_case("foo/bar/baz"; "paths exceeded")]
fn test_repo_parse_err(input: &str) {
let repo = Repository::parse(input);

assert!(repo.is_err());
}
}
42 changes: 23 additions & 19 deletions src/repository_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,30 @@ mod test_utils {

pub struct RepositoryConfigMock {
pub repo_cfg: RepositoryConfig,
pub tmpdir: TempDir,
pub tmp_dir: TempDir,
pub path: PathBuf,
}

impl RepositoryConfigMock {
pub fn new() -> Self {
let tmpdir = tempdir().unwrap();
let tmpdir_path = tmpdir.path().to_path_buf();
let repo_cfg = RepositoryConfig::load(&tmpdir_path);
let tmp_dir = tempdir().unwrap();
let path = tmp_dir.path().to_path_buf();
let repo_cfg = RepositoryConfig::load(&path);

Self {
repo_cfg,
tmpdir,
path: tmpdir_path,
tmp_dir,
path,
}
}

pub fn with_fixture(self) -> Self {
let fixture_path = PathBuf::from("fixtures");
let repo_cfg = RepositoryConfig::load(&fixture_path);
let path = PathBuf::from("fixtures");
let repo_cfg = RepositoryConfig::load(&path);

Self {
repo_cfg,
path: fixture_path,
path,
..self
}
}
Expand All @@ -73,26 +73,30 @@ mod test_utils {
mod tests {
use std::collections::HashMap;

use anyhow::Result;

use super::test_utils::RepositoryConfigMock;

#[test]
fn test_config_file_load() {
let repo_cfg_mock = RepositoryConfigMock::new().with_fixture();
let RepositoryConfigMock {
tmp_dir: _tmp_dir,
repo_cfg,
..
} = RepositoryConfigMock::new().with_fixture();

assert_eq!(
repo_cfg_mock.repo_cfg.copy_on_add,
repo_cfg.copy_on_add,
HashMap::from_iter([("foo".to_string(), vec!["baz".to_string()])])
);
}

#[test]
fn test_config_load_file_not_exists() -> Result<()> {
let repo_cfg_mock = RepositoryConfigMock::new();

assert!(repo_cfg_mock.repo_cfg.copy_on_add.is_empty());

Ok(())
fn test_config_load_file_not_exists() {
let RepositoryConfigMock {
tmp_dir: _tmp_dir,
repo_cfg,
..
} = RepositoryConfigMock::new();

assert!(repo_cfg.copy_on_add.is_empty());
}
}

0 comments on commit e49bf6f

Please sign in to comment.