Skip to content

Commit

Permalink
grcov: increase coverage with rustdoc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
epilys committed May 9, 2023
1 parent 98b1aa6 commit 828bbfe
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 107 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/grcov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
args: --all --all-features --no-fail-fast
env:
CARGO_INCREMENTAL: '0'
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests -Cinstrument-coverage'
RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests -Cinstrument-coverage'
- uses: actions-rs/grcov@v0.1
with:
config: .github/grcov.yml
Expand Down
38 changes: 37 additions & 1 deletion core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Connection {
///
/// # Example
///
/// ```rust
/// ```rust,no_run
/// use mailpot::{Connection, Configuration};
/// use melib::smtp::{SmtpServerConf, SmtpAuth, SmtpSecurity};
/// #
Expand Down Expand Up @@ -807,3 +807,39 @@ pub mod transaction {
Panic,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_new_connection() {
use melib::smtp::{SmtpAuth, SmtpSecurity, SmtpServerConf};
use tempfile::TempDir;

use crate::SendMail;

let tmp_dir = TempDir::new().unwrap();
let db_path = tmp_dir.path().join("mpot.db");
let data_path = tmp_dir.path().to_path_buf();
let config = Configuration {
send_mail: SendMail::Smtp(SmtpServerConf {
hostname: "127.0.0.1".into(),
port: 25,
envelope_from: "foo-chat@example.com".into(),
auth: SmtpAuth::None,
security: SmtpSecurity::None,
extensions: Default::default(),
}),
db_path,
data_path,
administrators: vec![],
};
assert_eq!(
&Connection::open_db(config.clone()).unwrap_err().to_string(),
"Database doesn't exist"
);

_ = Connection::open_or_create_db(config).unwrap();
}
}
288 changes: 227 additions & 61 deletions web/src/minijinja_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,44 +189,6 @@ impl minijinja::value::StructObject for MailingList {

/// Return a vector of weeks, with each week being a vector of 7 days and
/// corresponding sum of posts per day.
///
///
/// # Example
///
/// ```rust
/// # use mailpot_web::minijinja_utils::calendarize;
/// # use minijinja::Environment;
/// # use minijinja::value::Value;
/// # use std::collections::HashMap;
///
/// let mut env = Environment::new();
/// env.add_function("calendarize", calendarize);
///
/// let month = "2001-09";
/// let mut hist = [0usize; 31];
/// hist[15] = 5;
/// hist[1] = 1;
/// hist[0] = 512;
/// hist[30] = 30;
/// assert_eq!(
/// &env.render_str(
/// "{% set c=calendarize(month, hists) %}Month: {{ c.month }} Month Name: {{ \
/// c.month_name }} Month Int: {{ c.month_int }} Year: {{ c.year }} Sum: {{ c.sum }} {% \
/// for week in c.weeks %}{% for day in week %}{% set num = c.hist[day-1] %}({{ day }}, \
/// {{ num }}){% endfor %}{% endfor %}",
/// minijinja::context! {
/// month,
/// hists => vec![(month.to_string(), hist)].into_iter().collect::<HashMap<String, [usize;
/// 31]>>(),
/// }
/// )
/// .unwrap(),
/// "Month: 2001-09 Month Name: September Month Int: 9 Year: 2001 Sum: 548 (0, 30)(0, 30)(0, \
/// 30)(0, 30)(0, 30)(1, 512)(2, 1)(3, 0)(4, 0)(5, 0)(6, 0)(7, 0)(8, 0)(9, 0)(10, 0)(11, \
/// 0)(12, 0)(13, 0)(14, 0)(15, 0)(16, 5)(17, 0)(18, 0)(19, 0)(20, 0)(21, 0)(22, 0)(23, \
/// 0)(24, 0)(25, 0)(26, 0)(27, 0)(28, 0)(29, 0)(30, 0)"
/// );
/// ```
pub fn calendarize(
_state: &minijinja::State,
args: Value,
Expand Down Expand Up @@ -294,7 +256,7 @@ pub fn calendarize(
///
/// # Examples
///
/// ```rust
/// ```rust,no_run
/// # use mailpot_web::pluralize;
/// # use minijinja::Environment;
///
Expand Down Expand Up @@ -436,26 +398,6 @@ pub fn pluralize(
/// `strip_carets` filter for [`minijinja`].
///
/// Removes `[<>]` from message ids.
///
/// # Examples
///
/// ```rust
/// # use mailpot_web::strip_carets;
/// # use minijinja::Environment;
///
/// let mut env = Environment::new();
/// env.add_filter("strip_carets", strip_carets);
/// assert_eq!(
/// &env.render_str(
/// "{{ msg_id | strip_carets }}",
/// minijinja::context! {
/// msg_id => "<hello1@example.com>",
/// }
/// )
/// .unwrap(),
/// "hello1@example.com",
/// );
/// ```
pub fn strip_carets(_state: &minijinja::State, arg: Value) -> std::result::Result<Value, Error> {
Ok(Value::from(
arg.as_str()
Expand All @@ -475,7 +417,7 @@ pub fn strip_carets(_state: &minijinja::State, arg: Value) -> std::result::Resul
///
/// # Examples
///
/// ```rust
/// ```rust,no_run
/// # use mailpot_web::urlize;
/// # use minijinja::Environment;
/// # use minijinja::value::Value;
Expand Down Expand Up @@ -505,7 +447,7 @@ pub fn urlize(state: &minijinja::State, arg: Value) -> std::result::Result<Value
/// Make an html heading: `h1, h2, h3` etc.
///
/// # Example
/// ```
/// ```rust,no_run
/// use mailpot_web::minijinja_utils::heading;
/// use minijinja::value::Value;
///
Expand Down Expand Up @@ -601,3 +543,227 @@ pub fn heading(level: Value, text: Value, id: Option<Value>) -> std::result::Res
)))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_pluralize() {
let mut env = Environment::new();
env.add_filter("pluralize", pluralize);
for (num, s) in [
(0, "You have 0 messages."),
(1, "You have 1 message."),
(10, "You have 10 messages."),
] {
assert_eq!(
&env.render_str(
"You have {{ num_messages }} message{{ num_messages|pluralize }}.",
minijinja::context! {
num_messages => num,
}
)
.unwrap(),
s
);
}

for (num, s) in [
(0, "You have 0 walruses."),
(1, "You have 1 walrus."),
(10, "You have 10 walruses."),
] {
assert_eq!(
&env.render_str(
r#"You have {{ num_walruses }} walrus{{ num_walruses|pluralize(None, "es") }}."#,
minijinja::context! {
num_walruses => num,
}
)
.unwrap(),
s
);
}

for (num, s) in [
(0, "You have 0 cherries."),
(1, "You have 1 cherry."),
(10, "You have 10 cherries."),
] {
assert_eq!(
&env.render_str(
r#"You have {{ num_cherries }} cherr{{ num_cherries|pluralize("y", "ies") }}."#,
minijinja::context! {
num_cherries => num,
}
)
.unwrap(),
s
);
}

assert_eq!(
&env.render_str(
r#"You have {{ num_cherries|length }} cherr{{ num_cherries|pluralize("y", "ies") }}."#,
minijinja::context! {
num_cherries => vec![(); 5],
}
)
.unwrap(),
"You have 5 cherries."
);

assert_eq!(
&env.render_str(
r#"You have {{ num_cherries }} cherr{{ num_cherries|pluralize("y", "ies") }}."#,
minijinja::context! {
num_cherries => "5",
}
)
.unwrap(),
"You have 5 cherries."
);
assert_eq!(
&env.render_str(
r#"You have 1 cherr{{ num_cherries|pluralize("y", "ies") }}."#,
minijinja::context! {
num_cherries => true,
}
)
.unwrap(),
"You have 1 cherry.",
);
assert_eq!(
&env.render_str(
r#"You have {{ num_cherries }} cherr{{ num_cherries|pluralize("y", "ies") }}."#,
minijinja::context! {
num_cherries => 0.5f32,
}
)
.unwrap_err()
.to_string(),
"invalid operation: Pluralize argument is not an integer, or a sequence / object with \
a length but of type number (in <string>:1)",
);
}

#[test]
fn test_urlize() {
let mut env = Environment::new();
env.add_function("urlize", urlize);
env.add_global(
"root_url_prefix",
Value::from_safe_string("/lists/prefix/".to_string()),
);
assert_eq!(
&env.render_str(
"<a href=\"{{ urlize(\"path/index.html\") }}\">link</a>",
minijinja::context! {}
)
.unwrap(),
"<a href=\"/lists/prefix/path/index.html\">link</a>",
);
}

#[test]
fn test_heading() {
assert_eq!(
"<h1 id=\"bl-bfa-b-ah-b-asdb-hadas-d\">bl bfa B AH bAsdb hadas d<a \
class=\"self-link\" href=\"#bl-bfa-b-ah-b-asdb-hadas-d\"></a></h1>",
&heading(1.into(), "bl bfa B AH bAsdb hadas d".into(), None)
.unwrap()
.to_string()
);
assert_eq!(
"<h2 id=\"short\">bl bfa B AH bAsdb hadas d<a class=\"self-link\" \
href=\"#short\"></a></h2>",
&heading(
2.into(),
"bl bfa B AH bAsdb hadas d".into(),
Some("short".into())
)
.unwrap()
.to_string()
);
assert_eq!(
r#"invalid operation: first heading() argument must be an unsigned integer less than 7 and positive"#,
&heading(
0.into(),
"bl bfa B AH bAsdb hadas d".into(),
Some("short".into())
)
.unwrap_err()
.to_string()
);
assert_eq!(
r#"invalid operation: first heading() argument must be an unsigned integer less than 7 and positive"#,
&heading(
8.into(),
"bl bfa B AH bAsdb hadas d".into(),
Some("short".into())
)
.unwrap_err()
.to_string()
);
assert_eq!(
r#"invalid operation: first heading() argument is not an integer < 7 but of type sequence"#,
&heading(
Value::from(vec![Value::from(1)]),
"bl bfa B AH bAsdb hadas d".into(),
Some("short".into())
)
.unwrap_err()
.to_string()
);
}

#[test]
fn test_strip_carets() {
let mut env = Environment::new();
env.add_filter("strip_carets", strip_carets);
assert_eq!(
&env.render_str(
"{{ msg_id | strip_carets }}",
minijinja::context! {
msg_id => "<hello1@example.com>",
}
)
.unwrap(),
"hello1@example.com",
);
}

#[test]
fn test_calendarize() {
use std::collections::HashMap;

let mut env = Environment::new();
env.add_function("calendarize", calendarize);

let month = "2001-09";
let mut hist = [0usize; 31];
hist[15] = 5;
hist[1] = 1;
hist[0] = 512;
hist[30] = 30;
assert_eq!(
&env.render_str(
"{% set c=calendarize(month, hists) %}Month: {{ c.month }} Month Name: {{ \
c.month_name }} Month Int: {{ c.month_int }} Year: {{ c.year }} Sum: {{ c.sum }} {% \
for week in c.weeks %}{% for day in week %}{% set num = c.hist[day-1] %}({{ day }}, \
{{ num }}){% endfor %}{% endfor %}",
minijinja::context! {
month,
hists => vec![(month.to_string(), hist)].into_iter().collect::<HashMap<String, [usize;
31]>>(),
}
)
.unwrap(),
"Month: 2001-09 Month Name: September Month Int: 9 Year: 2001 Sum: 548 (0, 30)(0, 30)(0, \
30)(0, 30)(0, 30)(1, 512)(2, 1)(3, 0)(4, 0)(5, 0)(6, 0)(7, 0)(8, 0)(9, 0)(10, 0)(11, \
0)(12, 0)(13, 0)(14, 0)(15, 0)(16, 5)(17, 0)(18, 0)(19, 0)(20, 0)(21, 0)(22, 0)(23, \
0)(24, 0)(25, 0)(26, 0)(27, 0)(28, 0)(29, 0)(30, 0)"
);
}
}
Loading

0 comments on commit 828bbfe

Please sign in to comment.