summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-08-04 23:53:06 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-08-04 23:53:23 +0200
commita966c8038820ee29cc6c6c9ebfe47045b30aa124 (patch)
tree8cbd8d2c3a99f356e565b0694805b6681c33077a
parenta2f5913002db9f28bf82d25b4e492bc9eb56f142 (diff)
downloadmoney-a966c8038820ee29cc6c6c9ebfe47045b30aa124.tar.gz
add some tests
-rw-r--r--cli/src/search.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/cli/src/search.rs b/cli/src/search.rs
index aa0c278..7ca6a29 100644
--- a/cli/src/search.rs
+++ b/cli/src/search.rs
@@ -16,6 +16,7 @@ use crate::transaction::{Category, Transaction};
#[derive(Clone)]
#[derive(Debug)]
+#[derive(PartialEq)]
pub enum DateIsh {
Absolute(NaiveDate),
Relative(Duration),
@@ -53,6 +54,7 @@ impl DateIsh {
#[derive(Clone)]
#[derive(Debug)]
+#[derive(PartialEq)]
pub enum Comparison {
Equal,
Greater,
@@ -95,6 +97,7 @@ fn parse_comparison(i: &str) -> nom::IResult<&str, (Decimal, Comparison)> {
#[derive(Clone)]
#[derive(Debug)]
+#[derive(PartialEq)]
pub enum Constraint {
Category(Category),
Before(DateIsh),
@@ -193,6 +196,7 @@ fn string(i: &str) -> nom::IResult<&str, &str> {
#[derive(Clone)]
#[derive(Debug)]
+#[derive(PartialEq)]
pub enum Filter {
Union(Constraint),
Intersect(Constraint),
@@ -476,4 +480,60 @@ mod test {
assert_eq!(search_filters_2.get().len(), 3);
assert_eq!(ids(&search_filters_2), vec![0, 1, 2]);
}
+
+ macro_rules! assert_parse {
+ ($to_parse:expr, $expected:expr) => {
+ assert_eq!(Constraint::parse($to_parse).unwrap().1, $expected)
+ };
+ }
+
+ macro_rules! assert_parse_failed {
+ ($to_parse:expr) => {
+ assert!(Constraint::parse($to_parse).is_err())
+ };
+ }
+
+ fn category_from_str(c: &str) -> Constraint {
+ Constraint::Category(c.to_string())
+ }
+
+ #[test]
+ fn parse_category() {
+ assert_parse!("category:abc", category_from_str("abc"));
+ }
+
+ #[test]
+ fn parse_category_long() {
+ assert_parse!("category:\"abc def\"", category_from_str("abc def"));
+ }
+
+ #[test]
+ fn parse_category_fail_empty() {
+ assert_parse_failed!("category:");
+ }
+
+ #[test]
+ fn parse_category_fail_unbalanced_quotes() {
+ assert_parse_failed!("category:\"abc");
+ }
+
+ #[test]
+ fn parse_date() {
+ assert_parse!(
+ "before:2020-01-01",
+ Constraint::Before(DateIsh::Absolute(NaiveDate::from_ymd(2020, 01, 01)))
+ );
+ assert_parse!(
+ "after:2020-01-01",
+ Constraint::After(DateIsh::Absolute(NaiveDate::from_ymd(2020, 01, 01)))
+ );
+ assert_parse!(
+ "on:2020-01-01",
+ Constraint::On(DateIsh::Absolute(NaiveDate::from_ymd(2020, 01, 01)))
+ );
+ //TODO before:-1d
+ }
+
+ #[test]
+ fn parse_filter() {}
}