use std::{fmt, io};
use wireframe::{
message_assembler::{FrameHeader, FrameSequence},
test_helpers::TestAssembler,
};
use super::{
BodyLength,
HeaderLength,
MessageAssemblerWorld,
MessageKey,
MetadataLength,
SequenceNumber,
TestApp,
TestResult,
};
#[derive(Clone, Copy, Debug, PartialEq)]
struct DebugDisplay<T>(T);
impl<T: fmt::Debug> fmt::Display for DebugDisplay<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.0) }
}
impl MessageAssemblerWorld {
pub fn assert_message_key(&self, expected: MessageKey) -> TestResult {
self.assert_field("key", &expected.0, |header| {
Ok(match header {
FrameHeader::First(header) => u64::from(header.message_key),
FrameHeader::Continuation(header) => u64::from(header.message_key),
})
})
}
pub fn assert_metadata_len(&self, expected: MetadataLength) -> TestResult {
self.assert_first_field("metadata length", &expected.0, |header| header.metadata_len)
}
pub fn assert_body_len(&self, expected: BodyLength) -> TestResult {
self.assert_field("body length", &expected.0, |header| {
Ok(match header {
FrameHeader::First(header) => header.body_len,
FrameHeader::Continuation(header) => header.body_len,
})
})
}
pub fn assert_total_len(&self, expected: Option<BodyLength>) -> TestResult {
let expected = DebugDisplay(expected);
self.assert_first_field("total length", &expected, |header| {
DebugDisplay(header.total_body_len.map(BodyLength))
})
}
pub fn assert_sequence(&self, expected: Option<SequenceNumber>) -> TestResult {
let expected = expected.map(|sequence| FrameSequence::from(sequence.0));
let expected = DebugDisplay(expected);
self.assert_continuation_field("sequence", &expected, |header| {
DebugDisplay(header.sequence)
})
}
pub fn assert_is_last(&self, expected: bool) -> TestResult {
self.assert_field("is_last", &expected, |header| {
Ok(match header {
FrameHeader::First(header) => header.is_last,
FrameHeader::Continuation(header) => header.is_last,
})
})
}
pub fn assert_header_len(&self, expected: HeaderLength) -> TestResult {
let parsed = self.parsed.as_ref().ok_or("no parsed header")?;
let actual = parsed.header_len();
if actual != expected.0 {
return Err(format!("expected header length {}, got {actual}", expected.0).into());
}
Ok(())
}
pub fn assert_invalid_data_error(&self) -> TestResult {
let err = self.error.as_ref().ok_or("expected error")?;
if err.kind() != io::ErrorKind::InvalidData {
return Err(format!("expected InvalidData error, got {:?}", err.kind()).into());
}
Ok(())
}
pub fn set_app_with_message_assembler(&mut self) -> TestResult {
let app = TestApp::new()
.map_err(|err| format!("failed to build app: {err}"))?
.with_message_assembler(TestAssembler);
self.app = Some(app);
Ok(())
}
pub fn assert_message_assembler_configured(&self) -> TestResult {
let app = self.app.as_ref().ok_or("app not set")?;
if app.message_assembler().is_some() {
Ok(())
} else {
Err("expected message assembler".into())
}
}
}