Skip to content

Commit

Permalink
!fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Sep 13, 2023
1 parent fd39adf commit af206ef
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion eyeball-im-util/src/vector/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ where
}
VectorDiff::PopBack => {
if length > limit {
// Let's ignore the diff.
// Pop back outside the limit, let's ignore the diff.
} else {
self.push_ready_value(VectorDiff::PopBack);
}
Expand Down
62 changes: 60 additions & 2 deletions eyeball-im-util/tests/it/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,10 @@ fn pop_back() {
// Add 4 values.
ob.append(vector![10, 11, 12, 13]);

// Set limit to 2.
// Set limit to 3.
Observable::set(&mut limit, 3);

// Observe 2 values.
// Observe 3 values.
assert_next_eq!(sub, VectorDiff::Append { values: vector![10, 11, 12] });

// Remove 1 value.
Expand Down Expand Up @@ -467,3 +467,61 @@ fn pop_back() {
drop(ob);
assert_closed!(sub);
}

#[test]
fn insert() {
let mut ob = ObservableVector::<usize>::new();
let mut limit = Observable::new(0);
let mut sub =
DynamicLimit::new(ob.clone(), ob.subscribe().into_stream(), Observable::subscribe(&limit));

// Add 4 values.
ob.append(vector![10, 11]);

// Set limit to 3.
Observable::set(&mut limit, 3);

// Observe 2 values.
assert_next_eq!(sub, VectorDiff::Append { values: vector![10, 11] });

// Insert 1 value.
ob.insert(1, 12);

// Observe 1 value.
assert_next_eq!(sub, VectorDiff::Insert { index: 1, value: 12 });

// Insert 1 value.
ob.insert(1, 13);

// Observe 1 value being removed, and 1 value being added.
assert_next_eq!(sub, VectorDiff::PopBack);
assert_next_eq!(sub, VectorDiff::Insert { index: 1, value: 13 });

// Insert 1 value at the limit.
ob.insert(2, 14);

// Observe 1 value being removed, and 1 value being added.
assert_next_eq!(sub, VectorDiff::PopBack);
assert_next_eq!(sub, VectorDiff::Insert { index: 2, value: 14 });

// Insert 1 value after the limit.
ob.insert(4, 15);

// Observe nothing.
assert_pending!(sub);

// Check the content of the vector.
{
let expected = vector![10, 13, 14, 12, 15, 11];
assert_eq!(*ob, expected);

Observable::set(&mut limit, 0);
assert_next_eq!(sub, VectorDiff::Truncate { length: 0 });

Observable::set(&mut limit, 42);
assert_next_eq!(sub, VectorDiff::Append { values: expected });
}

drop(ob);
assert_closed!(sub);
}

0 comments on commit af206ef

Please sign in to comment.