cowirc 0.2.0

Asychronous IRCv3 library for Rust
Documentation
/*
 * This file is part of CowIRC.
 *
 * CowIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CowIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with CowIRC. If not, see <http://www.gnu.org/licenses/>.
 */

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}"),
    };
}