Skip to content

Commit 0dcad07

Browse files
authored
Merge pull request #3 from Sajjon/moar_tests
add more test & remove some false negatives
2 parents a9d07c6 + e746ae1 commit 0dcad07

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/identified_vec.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ where
238238
/// - Returns: A new `identified_vec` initialized with the unique elements of `elements`.
239239
/// - Complexity: Expected O(*n*) on average, where *n* is the count of elements, if `ID`
240240
/// implements high-quality hashing.
241+
#[cfg(not(tarpaulin_include))] // false negative
241242
#[inline]
242243
pub fn new_from_iter_try_uniquing_ids_with<I>(
243244
elements: I,
@@ -295,6 +296,7 @@ where
295296
/// - Returns: A new `identified_vec` initialized with the unique elements of `elements`.
296297
/// - Complexity: Expected O(*n*) on average, where *n* is the count of elements, if `ID`
297298
/// implements high-quality hashing.
299+
#[cfg(not(tarpaulin_include))] // false negative
298300
#[inline]
299301
pub fn new_from_iter_uniquing_ids_with<I>(
300302
elements: I,
@@ -603,6 +605,7 @@ where
603605
/// - Complexity: The operation is expected to perform amortized O(`self.count`) copy, hash, and
604606
/// compare operations on the `ID` type, if it implements high-quality hashing. (Insertions need
605607
/// to make room in the storage identified_vec to add the inserted element.)
608+
#[cfg(not(tarpaulin_include))] // false negative
606609
#[inline]
607610
pub fn insert(&mut self, element: Element, at: usize) -> (bool, usize) {
608611
let id = self.id(&element);
@@ -675,6 +678,7 @@ where
675678
/// - Parameter id: The id of the element to be removed from the `identified_vec`.
676679
/// - Returns: The element that was removed, or `None` if the element was not present in the array.
677680
/// - Complexity: O(`count`)
681+
#[cfg(not(tarpaulin_include))] // false negative
678682
#[inline]
679683
pub fn remove_by_id(&mut self, id: &ID) -> Option<Element> {
680684
match self.index_of_id(id) {
@@ -806,6 +810,7 @@ where
806810
}
807811

808812
/// Inserting ID at an index, returning if it did, if not, the index of the existing.
813+
#[cfg(not(tarpaulin_include))] // false negative
809814
#[inline]
810815
fn _insert_id_at(&mut self, id: ID, index: usize) -> (bool, usize) {
811816
match self.index_of_id(&id) {
@@ -863,6 +868,7 @@ where
863868
Element: Debug,
864869
ID: Eq + Hash + Clone + Debug,
865870
{
871+
#[cfg(not(tarpaulin_include))]
866872
#[cfg(debug_assertions)]
867873
pub fn debug(&self) {
868874
println!("{}", self.debug_str());
@@ -877,12 +883,15 @@ where
877883
#[cfg(test)]
878884
mod tests {
879885

880-
use std::{cell::RefCell, collections::HashSet, fmt::Debug, ops::Deref};
886+
use std::{cell::RefCell, collections::HashSet, fmt::Debug};
887+
888+
use anyerror::AnyError;
881889

882890
use crate::{
883891
identifiable::Identifiable,
884892
identified_vec::{ConflictResolutionChoice, IdentifiedVec},
885893
identified_vec_of::IdentifiedVecOf,
894+
serde_error::IdentifiedVecOfSerdeFailure,
886895
};
887896

888897
#[derive(Eq, PartialEq, Clone)]
@@ -1043,6 +1052,27 @@ mod tests {
10431052
let identified_vec = SUT::from_iter([1, 2, 3]);
10441053
assert!(identified_vec.contains(&2))
10451054
}
1055+
1056+
#[test]
1057+
fn remove_by_id_not_present() {
1058+
let mut identified_vec = SUT::from_iter([1, 2, 3]);
1059+
assert!(identified_vec.remove_by_id(&5).is_none());
1060+
}
1061+
1062+
#[test]
1063+
fn get_at_index() {
1064+
let identified_vec = SUT::from_iter([1, 2, 3]);
1065+
assert_eq!(identified_vec.get_at_index(2), Some(&3));
1066+
assert_eq!(identified_vec.get_at_index(999), None);
1067+
}
1068+
1069+
#[test]
1070+
fn contains_id() {
1071+
let identified_vec = SUT::from_iter([1, 2, 3]);
1072+
assert!(identified_vec.contains_id(&1));
1073+
assert_eq!(identified_vec.contains_id(&999), false);
1074+
}
1075+
10461076
#[test]
10471077
fn index_id() {
10481078
let identified_vec = SUT::from_iter([1, 2, 3]);
@@ -1241,6 +1271,19 @@ mod tests {
12411271
serde_json::from_str::<SUT>("[1,1,1]").expect_err("should fail").to_string(),
12421272
"identified_vec::serde_error::IdentifiedVecOfSerdeFailure: Duplicate element at offset 1"
12431273
);
1274+
1275+
assert!(serde_json::from_str::<SUT>("invalid").is_err(),);
1276+
}
1277+
1278+
#[test]
1279+
fn serde_via_vec() {
1280+
let vec = vec![1, 2, 3];
1281+
let json_from_vec = serde_json::to_value(vec).unwrap();
1282+
let mut identified_vec = serde_json::from_value::<SUT>(json_from_vec).unwrap();
1283+
identified_vec.append(9);
1284+
let json_from_identified_vec = serde_json::to_value(identified_vec).unwrap();
1285+
let vec_from_json = serde_json::from_value::<Vec<i32>>(json_from_identified_vec).unwrap();
1286+
assert_eq!(vec_from_json, vec![1, 2, 3, 9]);
12441287
}
12451288

12461289
#[test]
@@ -1286,6 +1329,28 @@ mod tests {
12861329
.unwrap(),
12871330
];
12881331

1332+
assert_eq!(
1333+
IdentifiedVecOf::new_from_iter_try_uniquing_ids_with(
1334+
[Foo::new(), Foo::new()],
1335+
|e: &Foo| e.id(),
1336+
|_| Err(AnyError::new(
1337+
&IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1)
1338+
)),
1339+
),
1340+
Err(AnyError::new(
1341+
&IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1)
1342+
))
1343+
);
1344+
1345+
assert_eq!(
1346+
IdentifiedVecOf::new_from_iter_try_uniquing_with([Foo::new(), Foo::new()], |_| Err(
1347+
AnyError::new(&IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1))
1348+
),),
1349+
Err(AnyError::new(
1350+
&IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1)
1351+
))
1352+
);
1353+
12891354
vecs.iter().for_each(|l| {
12901355
vecs.iter().for_each(|r| {
12911356
assert_eq!(l, r);

src/identified_vec_of.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl<'de, Element> Deserialize<'de> for IdentifiedVecOf<Element>
132132
where
133133
Element: Deserialize<'de> + Identifiable + Debug + Clone,
134134
{
135+
#[cfg(not(tarpaulin_include))] // false negative
135136
fn deserialize<D: Deserializer<'de>>(
136137
deserializer: D,
137138
) -> Result<IdentifiedVecOf<Element>, D::Error> {

0 commit comments

Comments
 (0)