TestComponent: Unqueue from the front. This isn't a stack
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
ab9f824dec
commit
f86ca3fa66
1 changed files with 13 additions and 12 deletions
|
@ -6,6 +6,7 @@
|
|||
|
||||
use crate::error::Error;
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt;
|
||||
use std::marker::Send;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
@ -93,35 +94,35 @@ impl fmt::Debug for Expect {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct TestComponent {
|
||||
in_buffer: Vec<Element>,
|
||||
out_buffer: Vec<Element>,
|
||||
expect_buffer: Vec<Expect>,
|
||||
in_buffer: VecDeque<Element>,
|
||||
out_buffer: VecDeque<Element>,
|
||||
expect_buffer: VecDeque<Expect>,
|
||||
}
|
||||
|
||||
impl TestComponent {
|
||||
pub fn new(in_buffer: Vec<Element>) -> Self {
|
||||
TestComponent {
|
||||
in_buffer,
|
||||
out_buffer: Vec::new(),
|
||||
expect_buffer: Vec::new(),
|
||||
in_buffer: VecDeque::from(in_buffer),
|
||||
out_buffer: VecDeque::new(),
|
||||
expect_buffer: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds elements to be expected, in the order they're being added
|
||||
pub fn expect<E: Into<Element>>(&mut self, el: E) {
|
||||
self.expect_buffer.push(Expect::Element(el.into()))
|
||||
self.expect_buffer.push_back(Expect::Element(el.into()))
|
||||
}
|
||||
|
||||
pub fn expect_with<F: FnOnce(Element) + Send + 'static>(&mut self, callback: F) {
|
||||
self.expect_buffer
|
||||
.push(Expect::Callback(Box::new(callback)))
|
||||
.push_back(Expect::Callback(Box::new(callback)))
|
||||
}
|
||||
|
||||
/// Asserts expected output and actual output are the same
|
||||
pub fn assert(&mut self) {
|
||||
loop {
|
||||
let out = self.out_buffer.pop();
|
||||
let expected = self.expect_buffer.pop();
|
||||
let out = self.out_buffer.pop_front();
|
||||
let expected = self.expect_buffer.pop_front();
|
||||
|
||||
match (out, expected) {
|
||||
(None, None) => break,
|
||||
|
@ -139,7 +140,7 @@ impl TestComponent {
|
|||
}
|
||||
|
||||
fn _send_stanza<E: Into<Element> + Send>(&mut self, el: E) -> Result<(), Error> {
|
||||
Ok(self.out_buffer.push(el.into()))
|
||||
Ok(self.out_buffer.push_back(el.into()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,7 +149,7 @@ impl Stream for TestComponent {
|
|||
|
||||
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||
while self.in_buffer.len() > 0 {
|
||||
return Poll::Ready(self.in_buffer.pop());
|
||||
return Poll::Ready(self.in_buffer.pop_front());
|
||||
}
|
||||
|
||||
Poll::Ready(None)
|
||||
|
|
Loading…
Reference in a new issue