pub type BinaryMessage = Vec<u8>;
#[derive(Debug, Clone, PartialEq)]
pub enum ServerMockerInstruction {
SendMessage(BinaryMessage),
SendMessageDependingOnLastReceivedMessage(fn(Option<BinaryMessage>) -> Option<BinaryMessage>),
ReceiveMessage,
ReceiveMessageWithMaxSize(usize),
StopExchange,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ServerMockerInstructionsList {
pub(crate) instructions: Vec<ServerMockerInstruction>,
}
impl ServerMockerInstructionsList {
pub fn new() -> ServerMockerInstructionsList {
ServerMockerInstructionsList {
instructions: Vec::new(),
}
}
pub fn new_with_instructions(
instructions: &[ServerMockerInstruction],
) -> ServerMockerInstructionsList {
ServerMockerInstructionsList {
instructions: instructions.to_vec(),
}
}
pub fn add_send_message(&mut self, message: BinaryMessage) {
self.instructions
.push(ServerMockerInstruction::SendMessage(message));
}
pub fn with_added_send_message(mut self, message: BinaryMessage) -> Self {
self.add_send_message(message);
self
}
pub fn add_send_message_depending_on_last_received_message(
&mut self,
message: fn(Option<BinaryMessage>) -> Option<BinaryMessage>,
) {
self.instructions
.push(ServerMockerInstruction::SendMessageDependingOnLastReceivedMessage(message));
}
pub fn with_added_send_message_depending_on_last_received_message(
mut self,
message: fn(Option<BinaryMessage>) -> Option<BinaryMessage>,
) -> Self {
self.add_send_message_depending_on_last_received_message(message);
self
}
pub fn add_receive_message(&mut self) {
self.instructions
.push(ServerMockerInstruction::ReceiveMessage);
}
pub fn with_added_receive_message(mut self) -> Self {
self.add_receive_message();
self
}
pub fn add_receive_message_with_max_size(&mut self, max_size: usize) {
self.instructions
.push(ServerMockerInstruction::ReceiveMessageWithMaxSize(max_size));
}
pub fn with_added_receive_message_with_max_size(mut self, max_size: usize) -> Self {
self.add_receive_message_with_max_size(max_size);
self
}
pub fn add_stop_exchange(&mut self) {
self.instructions
.push(ServerMockerInstruction::StopExchange);
}
pub fn with_added_stop_exchange(mut self) -> Self {
self.add_stop_exchange();
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_server_mocker_instructions_list() {
let mut instructions_list = ServerMockerInstructionsList::new_with_instructions(
[
ServerMockerInstruction::ReceiveMessage,
ServerMockerInstruction::SendMessage("hello from server".as_bytes().to_vec()),
]
.as_slice(),
)
.with_added_receive_message();
instructions_list.add_stop_exchange();
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![
ServerMockerInstruction::ReceiveMessage,
ServerMockerInstruction::SendMessage("hello from server".as_bytes().to_vec()),
ServerMockerInstruction::ReceiveMessage,
ServerMockerInstruction::StopExchange,
]
}
);
}
#[test]
fn test_server_mocker_instructions_list_new() {
let instructions_list = ServerMockerInstructionsList::new();
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![]
}
);
}
#[test]
fn test_server_mocker_instructions_list_new_with_instructions() {
let instructions_list = ServerMockerInstructionsList::new_with_instructions(
[
ServerMockerInstruction::ReceiveMessage,
ServerMockerInstruction::SendMessage("hello from server".as_bytes().to_vec()),
]
.as_slice(),
);
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![
ServerMockerInstruction::ReceiveMessage,
ServerMockerInstruction::SendMessage("hello from server".as_bytes().to_vec()),
]
}
);
}
#[test]
fn test_server_mocker_instructions_list_add_send_message() {
let mut instructions_list = ServerMockerInstructionsList::new();
instructions_list.add_send_message("hello from server".as_bytes().to_vec());
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![ServerMockerInstruction::SendMessage(
"hello from server".as_bytes().to_vec()
),]
}
);
}
#[test]
fn test_server_mocker_instructions_list_with_added_send_message() {
let instructions_list = ServerMockerInstructionsList::new()
.with_added_send_message("hello from server".as_bytes().to_vec());
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![ServerMockerInstruction::SendMessage(
"hello from server".as_bytes().to_vec()
),]
}
);
}
#[test]
fn test_server_mocker_instructions_list_add_send_message_depending_on_last_received_message() {
let mut instructions_list = ServerMockerInstructionsList::new();
let message_generator = |message| {
if let Some(message) = message {
if message == "hello from client".as_bytes().to_vec() {
Some("hello from server".as_bytes().to_vec())
} else {
None
}
} else {
None
}
};
instructions_list.add_send_message_depending_on_last_received_message(message_generator);
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![
ServerMockerInstruction::SendMessageDependingOnLastReceivedMessage(
message_generator
),
]
}
);
}
#[test]
fn test_server_mocker_instructions_list_with_added_send_message_depending_on_last_received_message(
) {
let message_generator = |message| {
if let Some(message) = message {
if message == "hello from client".as_bytes().to_vec() {
Some("hello from server".as_bytes().to_vec())
} else {
None
}
} else {
None
}
};
let instructions_list = ServerMockerInstructionsList::new()
.with_added_send_message_depending_on_last_received_message(message_generator);
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![
ServerMockerInstruction::SendMessageDependingOnLastReceivedMessage(
message_generator
),
]
}
);
}
#[test]
fn test_server_mocker_instructions_list_add_receive_message() {
let mut instructions_list = ServerMockerInstructionsList::new();
instructions_list.add_receive_message();
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![ServerMockerInstruction::ReceiveMessage,]
}
);
}
#[test]
fn test_server_mocker_instructions_list_with_added_receive_message() {
let instructions_list = ServerMockerInstructionsList::new().with_added_receive_message();
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![ServerMockerInstruction::ReceiveMessage,]
}
);
}
#[test]
fn test_server_mocker_instructions_list_add_receive_message_with_max_size() {
let mut instructions_list = ServerMockerInstructionsList::new();
instructions_list.add_receive_message_with_max_size(100);
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![ServerMockerInstruction::ReceiveMessageWithMaxSize(100),]
}
);
}
#[test]
fn test_server_mocker_instructions_list_with_added_receive_message_with_max_size() {
let instructions_list =
ServerMockerInstructionsList::new().with_added_receive_message_with_max_size(100);
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![ServerMockerInstruction::ReceiveMessageWithMaxSize(100),]
}
);
}
#[test]
fn test_server_mocker_instructions_list_add_stop_exchange() {
let mut instructions_list = ServerMockerInstructionsList::new();
instructions_list.add_stop_exchange();
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![ServerMockerInstruction::StopExchange,]
}
);
}
#[test]
fn test_server_mocker_instructions_list_with_added_stop_exchange() {
let instructions_list = ServerMockerInstructionsList::new().with_added_stop_exchange();
assert_eq!(
instructions_list,
ServerMockerInstructionsList {
instructions: vec![ServerMockerInstruction::StopExchange,]
}
);
}
}