Skip to content

Commit

Permalink
test(template): add tests (#674)
Browse files Browse the repository at this point in the history
* chore(test) : increase tests coverage

* fix
  • Loading branch information
R11baka authored Jun 5, 2024
1 parent e270235 commit a51422d
Showing 1 changed file with 73 additions and 41 deletions.
114 changes: 73 additions & 41 deletions git-cliff-core/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,40 @@ mod test {
};
use regex::Regex;

fn get_fake_release_data() -> Release<'static> {
Release {
version: Some(String::from("1.0")),
commits: vec![
Commit::new(
String::from("123123"),
String::from("feat(xyz): add xyz"),
),
Commit::new(
String::from("124124"),
String::from("fix(abc): fix abc"),
),
]
.into_iter()
.filter_map(|c| c.into_conventional().ok())
.collect(),
commit_id: None,
timestamp: 0,
previous: None,
#[cfg(feature = "github")]
github: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
#[cfg(feature = "gitlab")]
gitlab: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
#[cfg(feature = "bitbucket")]
bitbucket: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
}
}

#[test]
fn render_template() -> Result<()> {
let template = r#"
Expand All @@ -195,55 +229,19 @@ mod test {
- {{ commit.message | upper_first }}
{% endfor %}"#;
let mut template = Template::new(template.to_string(), false)?;
let release = get_fake_release_data();
assert_eq!(
r#"
## 1.0 - 2023
### feat
- Add xyz
### fix
- Fix abc
"#,
"\n\t\t## 1.0 - 2023\n\t\t\n\t\t### feat\n\t\t- Add xyz\n\t\t\n\t\t### \
fix\n\t\t- Fix abc\n\t\t",
template.render(
&Release {
version: Some(String::from("1.0")),
commits: vec![
Commit::new(
String::from("123123"),
String::from("feat(xyz): add xyz"),
),
Commit::new(
String::from("124124"),
String::from("fix(abc): fix abc"),
)
]
.into_iter()
.filter_map(|c| c.into_conventional().ok())
.collect(),
commit_id: None,
timestamp: 0,
previous: None,
#[cfg(feature = "github")]
github: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
#[cfg(feature = "gitlab")]
gitlab: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
#[cfg(feature = "bitbucket")]
bitbucket: crate::remote::RemoteReleaseMetadata {
contributors: vec![],
},
},
&release,
Option::<HashMap<&str, String>>::None.as_ref(),
&[TextProcessor {
pattern: Regex::new("<DATE>")
.expect("failed to compile regex"),
replace: Some(String::from("2023")),
replace_command: None,
}]
}],
)?
);
template.variables.sort();
Expand All @@ -263,4 +261,38 @@ mod test {
}
Ok(())
}

#[test]
fn render_trimmed_template() -> Result<()> {
let template = r#"
## {{ version }}
"#;
let template = Template::new(template.to_string(), true)?;
let release = get_fake_release_data();
assert_eq!(
"\n## 1.0\n",
template.render(
&release,
Option::<HashMap<&str, String>>::None.as_ref(),
&[],
)?
);
assert_eq!(vec![String::from("version"),], template.variables);
Ok(())
}

#[test]
fn test_upper_first_filter() -> Result<()> {
let template =
"{% set hello_variable = 'hello' %}{{ hello_variable | upper_first }}";
let release = get_fake_release_data();
let template = Template::new(template.to_string(), true)?;
let r = template.render(
&release,
Option::<HashMap<&str, String>>::None.as_ref(),
&[],
)?;
assert_eq!("Hello", r);
Ok(())
}
}

0 comments on commit a51422d

Please sign in to comment.