Skip to content

Commit

Permalink
Add usage of multi-values fact to the map assertions (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelebra authored Jul 18, 2023
1 parent 660331a commit 8918aa0
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions src/assertions/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::borrow::Borrow;
use std::collections::hash_map::Keys;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fmt::{Debug, Formatter};
use std::hash::Hash;

use crate::assertions::basic::EqualityAssertion;
Expand Down Expand Up @@ -395,9 +395,16 @@ fn feed_missing_entries_facts<K: Eq + Hash + Debug, V: Eq + Debug>(
),
)
.add_splitter();
for (key, value) in &diff.missing {
result = result.add_fact("entry was not found", format!("{:?} -> {:?}", key, value));
}
result = result.add_formatted_values_fact(
format!(
"{} not found",
pluralize(diff.missing.len(), "entry was", "entries were")
),
(&diff.missing)
.into_iter()
.map(|(k, v)| MapEntry::new(k, v))
.collect(),
);
}
(result, has_diffs)
}
Expand All @@ -422,16 +429,37 @@ fn feed_extra_entries_facts<K: Eq + Hash + Debug, V: Eq + Debug>(
),
)
.add_splitter();
for (key, value) in &diff.extra {
result = result.add_fact(
"unexpected entry was found",
format!("{:?} -> {:?}", key, value),
);
}
result = result.add_formatted_values_fact(
format!(
"unexpected {} found",
pluralize(diff.extra.len(), "entry was", "entries were")
),
(&diff.extra)
.into_iter()
.map(|(k, v)| MapEntry::new(k, v))
.collect(),
);
}
(result, has_diffs)
}

struct MapEntry<'a, K: Debug, V: Debug> {
key: &'a K,
value: &'a V,
}

impl<'a, K: Debug, V: Debug> MapEntry<'a, K, V> {
fn new(key: &'a K, value: &'a V) -> MapEntry<'a, K, V> {
Self { key, value }
}
}

impl<'a, K: Debug, V: Debug> Debug for MapEntry<'a, K, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(format!("{:?} -> {:?}", self.key, self.value).as_str())
}
}

#[cfg(test)]
mod tests {
use crate::testing::*;
Expand Down Expand Up @@ -633,7 +661,7 @@ mod tests {
"but 1 entry not found",
),
Fact::new_splitter(),
Fact::new("entry was not found", r#""not exist" -> "1""#),
Fact::new_multi_value_fact("entry was not found", vec![r#""not exist" -> "1""#]),
]);

// case 2: mismatched entries
Expand All @@ -659,7 +687,7 @@ mod tests {
"but 1 entry not found",
),
Fact::new_splitter(),
Fact::new("entry was not found", r#""not exist" -> "1""#),
Fact::new_multi_value_fact("entry was not found", vec![r#""not exist" -> "1""#]),
Fact::new_splitter(),
Fact::new(
"expected to contain the same entries",
Expand Down Expand Up @@ -689,7 +717,7 @@ mod tests {
"but 1 entry not found",
),
Fact::new_splitter(),
Fact::new("entry was not found", r#""not exist" -> "1""#),
Fact::new_multi_value_fact("entry was not found", vec![r#""not exist" -> "1""#]),
]);

// case 2: extra key
Expand All @@ -706,7 +734,7 @@ mod tests {
"but 1 additional entry was found",
),
Fact::new_splitter(),
Fact::new("unexpected entry was found", r#""ex" -> "1""#),
Fact::new_multi_value_fact("unexpected entry was found", vec![r#""ex" -> "1""#]),
]);

// case 3: mismatched entries
Expand All @@ -733,14 +761,14 @@ mod tests {
"but 1 entry not found",
),
Fact::new_splitter(),
Fact::new("entry was not found", r#""c" -> "2""#),
Fact::new_multi_value_fact("entry was not found", vec![r#""c" -> "2""#]),
Fact::new_splitter(),
Fact::new(
"expected to not contain additional entries",
"but 1 additional entry was found",
),
Fact::new_splitter(),
Fact::new("unexpected entry was found", r#""b" -> "2""#),
Fact::new_multi_value_fact("unexpected entry was found", vec![r#""b" -> "2""#]),
Fact::new_splitter(),
Fact::new(
"expected to contain the same entries",
Expand Down

0 comments on commit 8918aa0

Please sign in to comment.