Skip to content

Commit

Permalink
Increase coverage
Browse files Browse the repository at this point in the history
I already had tests that should have a bigger coverage than reported.
Apparently there is an [issue with
assert_cmd](assert-rs/assert_cmd#9) that makes it not show
coverage correctly for tarpaulin.

So the workaround is to test as needed with assert_cmd and in the same
function call one that re-runs the command executing run_app and only
checks that the result is ok.
  • Loading branch information
frosklis committed Feb 7, 2021
1 parent b334a5c commit 131cbe0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 46 deletions.
9 changes: 9 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
use dinero::run_app;

pub fn test_args(args: &[&str]) {
let mut function_args: Vec<&str> = vec!["testing"];
for arg in args {
function_args.push(arg);
}
let res = run_app(function_args.iter().map(|x| x.to_string()).collect());
assert!(res.is_ok());
}
76 changes: 30 additions & 46 deletions tests/test_commands.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
use assert_cmd::Command;
use dinero::run_app;
use common::test_args;
mod common;
#[test]
fn date_filters() {
let assert_1 = Command::cargo_bin("dinero")
.unwrap()
.args(&["bal", "-f", "examples/demo.ledger"])
.assert();
let args1 = &["bal", "-f", "examples/demo.ledger"];
let assert_1 = Command::cargo_bin("dinero").unwrap().args(args1).assert();
let mut output = String::from_utf8(assert_1.get_output().to_owned().stdout).unwrap();
assert_eq!(output.lines().into_iter().count(), 18);
let assert_2 = Command::cargo_bin("dinero")
.unwrap()
.args(&[
"bal",
"-f",
"examples/demo.ledger",
"-e",
"2021-01-17",
"-b",
"2021-01-15",
"--force-color",
])
.assert();
test_args(args1);
let args2 = &[
"bal",
"-f",
"examples/demo.ledger",
"-e",
"2021-01-17",
"-b",
"2021-01-15",
"--force-color",
];
let assert_2 = Command::cargo_bin("dinero").unwrap().args(args2).assert();
output = String::from_utf8(assert_2.get_output().to_owned().stdout).unwrap();
assert_eq!(output.lines().into_iter().count(), 13);

test_args(args2);
}

/// A test for [issue 18](https://github.com/frosklis/dinero-rs/issues/18)
#[test]
fn exchange() {
let mut outputs = Vec::new();
for _ in 0..100 {
let assert = Command::cargo_bin("dinero")
.unwrap()
.args(&["bal", "-f", "examples/demo.ledger", "-X", "EUR"])
.assert();
let args = &["bal", "-f", "examples/demo.ledger", "-X", "EUR"];
let assert = Command::cargo_bin("dinero").unwrap().args(args).assert();
outputs.push(String::from_utf8(assert.get_output().to_owned().stdout).unwrap());
test_args(args);
}
for i in 1..100 {
assert_eq!(outputs[i], outputs[0], "output mismatch");
Expand All @@ -48,46 +47,31 @@ fn commodity_alias() {
let mut outputs = Vec::new();
let aliases = vec!["EUR", "eur"];
for alias in aliases {
let assert = Command::cargo_bin("dinero")
.unwrap()
.args(&["bal", "-f", "examples/demo.ledger", "-X", alias])
.assert();
let args = &["bal", "-f", "examples/demo.ledger", "-X", alias];
let assert = Command::cargo_bin("dinero").unwrap().args(args).assert();
outputs.push(String::from_utf8(assert.get_output().to_owned().stdout).unwrap());
test_args(args);
}
assert_eq!(outputs[0], outputs[1], "output mismatch");
}

#[test]
/// Check that the register report is showing virtual postings
fn virtual_postings() {
let assert_1 = Command::cargo_bin("dinero")
.unwrap()
.args(&["reg", "-f", "examples/virtual_postings.ledger"])
.assert();
let args = &["reg", "-f", "examples/virtual_postings.ledger"];
let assert_1 = Command::cargo_bin("dinero").unwrap().args(args).assert();
let output = String::from_utf8(assert_1.get_output().to_owned().stdout).unwrap();
assert_eq!(output.lines().into_iter().count(), 7);
test_args(args);
}

#[test]
/// Check that the virtual postings are being filtered out
fn real_filter() {
let assert_1 = Command::cargo_bin("dinero")
.unwrap()
.args(&["reg", "-f", "examples/virtual_postings.ledger", "--real"])
.assert();
let args = &["reg", "-f", "examples/virtual_postings.ledger", "--real"];
let assert_1 = Command::cargo_bin("dinero").unwrap().args(args).assert();
let output = String::from_utf8(assert_1.get_output().to_owned().stdout).unwrap();
assert_eq!(output.lines().into_iter().count(), 4);

let args: Vec<String> = vec![
"testing",
"reg",
"-f",
"examples/virtual_postings.ledger",
"--real",
]
.iter()
.map(|x| x.to_string())
.collect();
let res = run_app(args);
assert!(res.is_ok());
test_args(args);
}

0 comments on commit 131cbe0

Please sign in to comment.