Skip to content

Commit

Permalink
test: use fluent assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent a9fc5f3 commit ce9882c
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 184 deletions.
20 changes: 10 additions & 10 deletions src/conventional/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ mod test {
use chrono::Utc;
use conventional_commit_parser::commit::{CommitType, ConventionalCommit};
use git2::Oid;
use speculoos::prelude::*;

#[test]
fn should_generate_changelog() -> Result<()> {
Expand Down Expand Up @@ -139,15 +140,14 @@ mod test {

// Assert
println!("{}", content);
assert!(content.contains(
"[5375e1](https://github.com/oknozor/cocogitto/commit/5375e15770ddf8821d0c1ad393d315e243014c15) - this is a commit message - coco"
));
assert!(content.contains(
assert_that(&content)
.contains("[5375e1](https://github.com/oknozor/cocogitto/commit/5375e15770ddf8821d0c1ad393d315e243014c15) - this is a commit message - coco");
assert_that!(content).contains(
"[5375e1](https://github.com/oknozor/cocogitto/commit/5375e15770ddf8821d0c1ad393d315e243014c15) - this is an other commit message - cogi"
));
assert!(content.contains("## 5375e1..35085f -"));
assert!(content.contains("### Features"));
assert!(!content.contains("### Tests"));
);
assert_that!(content).contains("## 5375e1..35085f -");
assert_that!(content).contains("### Features");
assert_that!(content).does_not_contain("### Tests");
Ok(())
}

Expand All @@ -167,8 +167,8 @@ mod test {

// Assert
println!("{}", content);
assert!(content.contains("## 5375e1..35085f"));
assert!(!content.contains("### Features"));
assert_that!(content).contains("## 5375e1..35085f");
assert_that!(content).does_not_contain("### Features");
Ok(())
}
}
20 changes: 10 additions & 10 deletions src/conventional/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,12 @@ mod test {

// Assert
let commit = commit.unwrap();
assert_eq!(commit.commit_type, CommitType::Feature);
assert_eq!(commit.scope, Some("database".to_owned()));
assert_eq!(commit.summary, "add postgresql driver".to_owned());
assert!(!commit.is_breaking_change);
assert!(commit.body.is_none());
assert!(commit.footers.is_empty());
assert_that!(commit.commit_type).is_equal_to(CommitType::Feature);
assert_that!(commit.scope).is_equal_to(Some("database".to_owned()));
assert_that!(commit.summary).is_equal_to("add postgresql driver".to_owned());
assert_that!(commit.is_breaking_change).is_false();
assert_that!(commit.body).is_none();
assert_that!(commit.footers).is_empty();
}

#[test]
Expand All @@ -271,7 +271,7 @@ mod test {
let result = verify(Some("toml".into()), message);

// Assert
assert!(result.is_ok());
assert_that!(result).is_ok();
}

#[test]
Expand All @@ -283,7 +283,7 @@ mod test {
let result = verify(Some("toml".into()), message);

// Assert
assert!(result.is_err());
assert_that!(result).is_err();
}

#[test]
Expand All @@ -308,7 +308,7 @@ mod test {
let summary = commit.format_summary();

// Assert
assert_that(&summary).is_equal_to("fix(scope): this is the message".to_string());
assert_that!(summary).is_equal_to("fix(scope): this is the message".to_string());
}

#[test]
Expand All @@ -333,6 +333,6 @@ mod test {
let summary = commit.format_summary();

// Assert
assert_that(&summary).is_equal_to("fix: this is the message".to_string());
assert_that!(summary).is_equal_to("fix: this is the message".to_string());
}
}
46 changes: 26 additions & 20 deletions src/conventional/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,42 +166,40 @@ mod test {
use chrono::Utc;
use conventional_commit_parser::commit::{CommitType, ConventionalCommit};
use semver::{Identifier, Version};
use speculoos::prelude::*;

// Auto version tests resides in test/ dir since it rely on git log
// To generate the version

#[test]
fn major_bump() -> Result<()> {
let version = VersionIncrement::Major.bump(&Version::new(1, 0, 0))?;
assert_eq!(version, Version::new(2, 0, 0));
assert_that!(version).is_equal_to(Version::new(2, 0, 0));
Ok(())
}

#[test]
fn minor_bump() -> Result<()> {
let version = VersionIncrement::Minor.bump(&Version::new(1, 0, 0))?;
assert_eq!(version, Version::new(1, 1, 0));
assert_that!(version).is_equal_to(Version::new(1, 1, 0));
Ok(())
}

#[test]
fn patch_bump() -> Result<()> {
let version = VersionIncrement::Patch.bump(&Version::new(1, 0, 0))?;
assert_eq!(version, Version::new(1, 0, 1));
assert_that!(version).is_equal_to(Version::new(1, 0, 1));
Ok(())
}

#[test]
fn parse_pre_release_valid() -> Result<()> {
let idents = parse_pre_release("alpha.0-dev.1")?;
assert_eq!(
&idents,
&[
Identifier::AlphaNumeric("alpha".into()),
Identifier::AlphaNumeric("0-dev".into()),
Identifier::Numeric(1),
]
);
assert_that!(idents).is_equal_to(&vec![
Identifier::AlphaNumeric("alpha".into()),
Identifier::AlphaNumeric("0-dev".into()),
Identifier::Numeric(1),
]);
Ok(())
}

Expand All @@ -224,7 +222,9 @@ mod test {
let version =
VersionIncrement::get_next_auto_version(&Version::parse("1.0.0").unwrap(), &[patch]);

assert_eq!(version.unwrap(), Version::new(1, 0, 1))
assert_that!(version)
.is_ok()
.is_equal_to(Version::new(1, 0, 1))
}

#[test]
Expand Down Expand Up @@ -262,7 +262,9 @@ mod test {
&[breaking_change, feature],
);

assert_eq!(version.unwrap(), Version::new(2, 0, 0))
assert_that!(version)
.is_ok()
.is_equal_to(Version::new(2, 0, 0))
}

#[test]
Expand Down Expand Up @@ -300,7 +302,9 @@ mod test {
&[breaking_change, feature],
);

assert_eq!(version.unwrap(), Version::new(0, 2, 0))
assert_that!(version)
.is_ok()
.is_equal_to(Version::new(0, 2, 0))
}

#[test]
Expand Down Expand Up @@ -338,23 +342,25 @@ mod test {
&[patch, feature],
);

assert_eq!(version.unwrap(), Version::new(1, 1, 0))
assert_that!(version)
.is_ok()
.is_equal_to(Version::new(1, 1, 0))
}

#[test]
fn parse_pre_release_non_ascii() {
assert!(parse_pre_release("РАСТ").is_err());
assert_that!(parse_pre_release("РАСТ")).is_err();
}

#[test]
fn parse_pre_release_illegal_ascii() {
assert!(parse_pre_release("alpha$5").is_err());
assert_that!(parse_pre_release("alpha$5")).is_err();
}

#[test]
fn parse_pre_release_empty_ident() {
assert!(parse_pre_release(".alpha.5").is_err());
assert!(parse_pre_release("alpha..5").is_err());
assert!(parse_pre_release("alpha.5.").is_err());
assert_that!(parse_pre_release(".alpha.5")).is_err();
assert_that!(parse_pre_release("alpha..5")).is_err();
assert_that!(parse_pre_release("alpha.5.")).is_err();
}
}
20 changes: 9 additions & 11 deletions src/git/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ mod tests {
use crate::git::hook::HookKind;
use crate::CocoGitto;
use anyhow::Result;
use speculoos::prelude::*;
use std::env;
use std::fs::File;
use std::ops::Not;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;
Expand All @@ -82,8 +82,8 @@ mod tests {

cog.install_hook(HookKind::PrepareCommit)?;

assert!(PathBuf::from(".git/hooks/prepare-commit-msg").exists());
assert!(PathBuf::from(".git/hooks/pre-push").exists().not());
assert_that!(PathBuf::from(".git/hooks/prepare-commit-msg")).exists();
assert_that!(PathBuf::from(".git/hooks/pre-push")).does_not_exist();
Ok(())
}

Expand All @@ -99,10 +99,8 @@ mod tests {

cog.install_hook(HookKind::PrePush)?;

assert!(PathBuf::from(".git/hooks/pre-push").exists());
assert!(PathBuf::from(".git/hooks/prepare-commit-msg")
.exists()
.not());
assert_that!(PathBuf::from(".git/hooks/pre-push")).exists();
assert_that!(PathBuf::from(".git/hooks/prepare-commit-msg")).does_not_exist();
Ok(())
}

Expand All @@ -118,8 +116,8 @@ mod tests {

cog.install_hook(HookKind::All)?;

assert!(PathBuf::from(".git/hooks/pre-push").exists());
assert!(PathBuf::from(".git/hooks/prepare-commit-msg").exists());
assert_that!(PathBuf::from(".git/hooks/pre-push")).exists();
assert_that!(PathBuf::from(".git/hooks/prepare-commit-msg")).exists();
Ok(())
}

Expand All @@ -140,8 +138,8 @@ mod tests {

let prepush = File::open(".git/hooks/pre-push")?;
let metadata = prepush.metadata()?;
assert!(PathBuf::from(".git/hooks/pre-push").exists());
assert_eq!(metadata.permissions().mode() & 0o777, 0o755);
assert_that!(PathBuf::from(".git/hooks/pre-push")).exists();
assert_that!(metadata.permissions().mode() & 0o777).is_equal_to(0o755);
Ok(())
}
}
Loading

0 comments on commit ce9882c

Please sign in to comment.