diff options
| author | Zeyla Hellyer <[email protected]> | 2018-08-01 08:08:23 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-08-01 08:10:05 -0700 |
| commit | 3fed313193356c6784a33b79d1c2f583ea3944f9 (patch) | |
| tree | 875b4bab989fc573850d30317a1797bca5027e9f /src/utils | |
| parent | Reword the inner doc comment in `complex_bucket` (diff) | |
| download | serenity-3fed313193356c6784a33b79d1c2f583ea3944f9.tar.xz serenity-3fed313193356c6784a33b79d1c2f583ea3944f9.zip | |
Move unit tests into source
Move the unit tests into the relevant source files. There's no need for them to
be seprate, especially when the `tests` directory is meant to be for integration
tests.
The deserialization tests that include JSON files are still in the `tests` dir,
along with the public prelude re-export tests.
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/colour.rs | 52 | ||||
| -rw-r--r-- | src/utils/message_builder.rs | 78 | ||||
| -rw-r--r-- | src/utils/mod.rs | 51 |
3 files changed, 181 insertions, 0 deletions
diff --git a/src/utils/colour.rs b/src/utils/colour.rs index c901e9b..9e84e4e 100644 --- a/src/utils/colour.rs +++ b/src/utils/colour.rs @@ -309,3 +309,55 @@ impl Default for Colour { /// Creates a default value for a `Colour`, setting the inner value to `0`. fn default() -> Colour { Colour(0) } } + +#[cfg(test)] +mod test { + use super::Colour; + use std::u32; + + #[test] + fn new() { + assert_eq!(Colour::new(1).0, 1); + assert_eq!(Colour::new(u32::MIN).0, u32::MIN); + assert_eq!(Colour::new(u32::MAX).0, u32::MAX); + } + + #[test] + fn from_rgb() { + assert_eq!(Colour::from_rgb(255, 0, 0).0, 0xFF0000); + assert_eq!(Colour::from_rgb(0, 255, 0).0, 0x00FF00); + assert_eq!(Colour::from_rgb(0, 0, 255).0, 0x0000FF); + } + + #[test] + fn r() { + assert_eq!(Colour::new(0x336123).r(), 0x33); + } + + #[test] + fn g() { + assert_eq!(Colour::new(0x336123).g(), 0x61); + } + + #[test] + fn b() { + assert_eq!(Colour::new(0x336123).b(), 0x23); + } + + #[test] + fn tuple() { + assert_eq!(Colour::new(0x336123).tuple(), (0x33, 0x61, 0x23)); + } + + #[test] + fn default() { + assert_eq!(Colour::default().0, 0); + } + + #[test] + fn from() { + assert_eq!(Colour::from(7i32).0, 7); + assert_eq!(Colour::from(7u32).0, 7); + assert_eq!(Colour::from(7u64).0, 7); + } +} diff --git a/src/utils/message_builder.rs b/src/utils/message_builder.rs index 7bbfddf..c609c28 100644 --- a/src/utils/message_builder.rs +++ b/src/utils/message_builder.rs @@ -988,3 +988,81 @@ fn normalize(text: &str) -> String { .replace("@everyone", "@\u{200B}everyone") .replace("@here", "@\u{200B}here") } + +#[cfg(test)] +mod test { + use model::prelude::*; + use super::{ + ContentModifier::*, + MessageBuilder, + }; + + #[test] + fn code_blocks() { + let content = MessageBuilder::new() + .push_codeblock("test", Some("rb")) + .build(); + assert_eq!(content, "```rb\ntest\n```"); + } + + #[test] + fn safe_content() { + let content = MessageBuilder::new() + .push_safe("@everyone discord.gg/discord-api") + .build(); + assert_ne!(content, "@everyone discord.gg/discord-api"); + } + + #[test] + fn no_free_formatting() { + let content = MessageBuilder::new().push_bold_safe("test**test").build(); + assert_ne!(content, "**test**test**"); + } + + #[test] + fn mentions() { + let content_emoji = MessageBuilder::new() + .emoji(&Emoji { + animated: false, + id: EmojiId(32), + name: "Rohrkatze".to_string(), + managed: false, + require_colons: true, + roles: vec![], + }) + .build(); + let content_mentions = MessageBuilder::new() + .channel(1) + .mention(&UserId(2)) + .role(3) + .user(4) + .build(); + assert_eq!(content_mentions, "<#1><@2><@&3><@4>"); + assert_eq!(content_emoji, "<:Rohrkatze:32>"); + } + + #[test] + fn content() { + let content = Bold + Italic + Code + "Fun!"; + + assert_eq!(content.to_string(), "***`Fun!`***"); + } + + #[test] + fn message_content() { + let message_content = MessageBuilder::new() + .push(Bold + Italic + Code + "Fun!") + .build(); + + assert_eq!(message_content, "***`Fun!`***"); + } + + #[test] + fn message_content_safe() { + let message_content = MessageBuilder::new() + .push_safe(Bold + Italic + "test**test") + .build(); + + assert_eq!(message_content, "***test\\*\\*test***"); + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e20dd22..6c84384 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -500,3 +500,54 @@ pub fn with_cache_mut<T, F>(mut f: F) -> T let mut cache = CACHE.write(); f(&mut cache) } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_invite_parser() { + assert_eq!(parse_invite("https://discord.gg/abc"), "abc"); + assert_eq!(parse_invite("http://discord.gg/abc"), "abc"); + assert_eq!(parse_invite("discord.gg/abc"), "abc"); + } + + #[test] + fn test_username_parser() { + assert_eq!(parse_username("<@12345>").unwrap(), 12_345); + assert_eq!(parse_username("<@!12345>").unwrap(), 12_345); + } + + #[test] + fn role_parser() { + assert_eq!(parse_role("<@&12345>").unwrap(), 12_345); + } + + #[test] + fn test_channel_parser() { + assert_eq!(parse_channel("<#12345>").unwrap(), 12_345); + } + + #[test] + fn test_emoji_parser() { + let emoji = parse_emoji("<:name:12345>").unwrap(); + assert_eq!(emoji.name, "name"); + assert_eq!(emoji.id, 12_345); + } + + #[test] + fn test_quote_parser() { + let parsed = parse_quotes("a \"b c\" d\"e f\" g"); + assert_eq!(parsed, ["a", "b c", "d", "e f", "g"]); + } + + #[test] + fn test_is_nsfw() { + assert!(!is_nsfw("general")); + assert!(is_nsfw("nsfw")); + assert!(is_nsfw("nsfw-test")); + assert!(!is_nsfw("nsfw-")); + assert!(!is_nsfw("général")); + assert!(is_nsfw("nsfw-général")); + } +} |