use cowirc::Message;
#[test]
fn valid_privmsg_to_channel() {
let line: String = String::from(":nick!user@host PRIVMSG #channel :Hello world!");
let parsed_line = Message::from(line);
match parsed_line {
Ok(message) => {
assert_eq!(message.command, "PRIVMSG");
assert_eq!(message.source.as_ref().unwrap().username, "user");
assert_eq!(message.source.as_ref().unwrap().nickname, "nick");
assert_eq!(message.source.as_ref().unwrap().hostmask, "host");
assert_eq!(message.params().len(), 2);
assert_eq!(message.params()[0], "#channel");
assert_eq!(message.params()[1], "Hello world!");
}
Err(error) => {
panic!("Parsing error: {:?}", error);
}
}
}
#[test]
fn valid_ping_from_server() {
let line: String = String::from(":foo.bar PING");
let parsed_line = Message::from(line);
match parsed_line {
Ok(message) => {
assert_eq!(message.command, "PING");
assert_eq!(message.source.as_ref().unwrap().username, "");
assert_eq!(message.source.as_ref().unwrap().nickname, "foo.bar");
assert_eq!(message.source.as_ref().unwrap().hostmask, "");
assert_eq!(message.params().len(), 0);
}
Err(error) => {
panic!("Parsing error: {:?}", error);
}
}
}
#[test]
fn valid_message_one_tag() {
let line: String = String::from("@key1=value1 :source command param :");
let parsed_line = Message::from(line);
match parsed_line {
Ok(message) => {
assert_eq!(
message.tags.get("key1").unwrap().as_ref(),
"value1".to_string(),
);
assert_eq!(message.tags.len(), 1);
assert_eq!(message.source.as_ref().unwrap().username, "");
assert_eq!(message.source.as_ref().unwrap().nickname, "source");
assert_eq!(message.source.as_ref().unwrap().hostmask, "");
assert_eq!(message.command, "command");
assert_eq!(message.params()[0], "param");
}
Err(error) => panic!("{error}"),
};
}
#[test]
fn valid_message_two_tags() {
let line: String = String::from("@key1=value1;key2=value2 :source command");
let parsed_line = Message::from(line);
match parsed_line {
Ok(message) => {
assert_eq!(
message.tags.get("key1").unwrap().as_ref(),
"value1".to_string(),
);
assert_eq!(
message.tags.get("key2").unwrap().as_ref(),
"value2".to_string()
);
assert_eq!(message.tags.len(), 2);
assert_eq!(message.source.as_ref().unwrap().username, "");
assert_eq!(message.source.as_ref().unwrap().nickname, "source");
assert_eq!(message.source.as_ref().unwrap().hostmask, "");
assert_eq!(message.command, "command");
}
Err(error) => panic!("{error}"),
};
}
#[test]
fn valid_message_first_tag_no_value() {
let line: String = String::from("@key1;key2=value2 :source command");
let parsed_line = Message::from(line);
match parsed_line {
Ok(message) => {
assert_eq!(message.tags.get("key1").unwrap().as_ref(), "".to_string());
assert_eq!(
message.tags.get("key2").unwrap().as_ref(),
"value2".to_string(),
);
assert_eq!(message.tags.len(), 2);
assert_eq!(message.source.as_ref().unwrap().username, "");
assert_eq!(message.source.as_ref().unwrap().nickname, "source");
assert_eq!(message.source.as_ref().unwrap().hostmask, "");
assert_eq!(message.command, "command");
}
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse tags with missing key!")]
fn invalid_tag_without_key() {
let line: String = String::from("@=value1 :source command");
let _result = match Message::from(line) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse command-less line!")]
fn invalid_message_with_tag_without_command() {
let _result = match Message::from("@key1=value1".to_string()) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse command-less line!")]
fn invalid_message_with_tag_and_semicolon_without_command() {
let _result = match Message::from("@key1=value1;".to_string()) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse tags with trailing semicolon!")]
fn invalid_message_with_tag_trailing_semicolon() {
let _result = match Message::from("@key1=value1; command".to_string()) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse command-less line!")]
fn invalid_double_colon_and_whitespace() {
let _result = match Message::from(" : : ".to_string()) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse command-less line!")]
fn invalid_double_colon() {
let _result = match Message::from("::".to_string()) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse command-less line!")]
fn invalid_colon() {
let _result = match Message::from(":".to_string()) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse command-less line!")]
fn invalid_empty_message() {
let _result = match Message::from("".to_string()) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
#[should_panic(expected = "Cannot parse tags with trailing semicolon!")]
fn invalid_tags_with_double_semicolon() {
let _result = match Message::from("@a;;b command".to_string()) {
Ok(result) => result,
Err(error) => panic!("{error}"),
};
}
#[test]
fn valid_message_duplicate_tags() {
let line: String = String::from("@foo=bar;foo=foobar command");
let parsed_line = Message::from(line);
match parsed_line {
Ok(message) => {
assert_eq!(
message.tags.get("foo").unwrap().as_ref(),
"foobar".to_string()
);
assert_eq!(message.tags.len(), 1);
assert_eq!(message.command, "command");
}
Err(error) => panic!("{error}"),
};
}
#[test]
fn valid_message_cryillic_tags() {
let line: String = String::from("@foo=λ command");
let parsed_line = Message::from(line);
match parsed_line {
Ok(message) => {
assert_eq!(message.tags.get("foo").unwrap().as_ref(), "λ".to_string());
assert_eq!(message.tags.len(), 1);
assert_eq!(message.command, "command");
}
Err(error) => panic!("{error}"),
};
}