Skip to content

Commit

Permalink
Fix Tests for Feature Sets (#496)
Browse files Browse the repository at this point in the history
Fix tests to work with default features without `cache` and to pass all tests with the set with `gateway` and `http`.
  • Loading branch information
Lakelezz authored Feb 24, 2019
1 parent 88d914e commit e6694f2
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 128 deletions.
20 changes: 11 additions & 9 deletions src/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ impl CreateEmbed {
/// Passing a string timestamp:
///
/// ```rust,no_run
/// # #[cfg(feature = "client")] {
/// use serenity::prelude::*;
/// use serenity::model::channel::Message;
///
Expand All @@ -247,13 +248,15 @@ impl CreateEmbed {
/// let mut client = Client::new("token", Handler).unwrap();
///
/// client.start().unwrap();
/// # }
/// ```
///
/// Creating a join-log:
///
/// Note: this example isn't efficient and is for demonstrative purposes.
///
/// ```rust,no_run
/// # #[cfg(feature = "client")] {
/// use serenity::prelude::*;
/// use serenity::model::guild::Member;
/// use serenity::model::id::GuildId;
Expand All @@ -262,21 +265,19 @@ impl CreateEmbed {
///
/// impl EventHandler for Handler {
/// fn guild_member_addition(&self, _: Context, guild_id: GuildId, member: Member) {
/// use serenity::CACHE;
/// let cache = CACHE.read();
///
/// if let Some(guild) = cache.guild(guild_id) {
/// let guild = guild.read();
/// if let Ok(guild) = guild_id.to_partial_guild() {
///
/// let channel_search = guild
/// .channels
/// .values()
/// .find(|c| c.read().name == "join-log");
/// let channels = guild.channels()
/// .unwrap();
///
/// let channel_search = channels.values()
/// .find(|c| c.name == "join-log");
///
/// if let Some(channel) = channel_search {
/// let user = member.user.read();
///
/// let _ = channel.read().send_message(|m| m
/// let _ = channel.send_message(|m| m
/// .embed(|e| {
/// let mut e = e
/// .author(|a| a.icon_url(&user.face()).name(&user.name))
Expand All @@ -296,6 +297,7 @@ impl CreateEmbed {
/// let mut client = Client::new("token", Handler).unwrap();
///
/// client.start().unwrap();
/// # }
/// ```
#[inline]
pub fn timestamp<T: Into<Timestamp>>(self, timestamp: T) -> Self {
Expand Down
65 changes: 40 additions & 25 deletions src/builder/create_invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@ use utils::VecMap;
/// Create an invite with a max age of 3600 seconds and 10 max uses:
///
/// ```rust,no_run
/// # #[cfg(feature = "client")] {
/// # use serenity::prelude::*;
/// # use serenity::model::prelude::*;
/// # use serenity::model::channel::Channel;
///
/// struct Handler;
///
/// impl EventHandler for Handler {
/// fn message(&self, _: Context, msg: Message) {
/// use serenity::CACHE;
/// if msg.content == "!createinvite" {
/// let channel = match CACHE.read().guild_channel(msg.channel_id) {
/// Some(channel) => channel,
/// None => {
/// let _ = msg.channel_id.say("Error creating invite");
/// let channel = match msg.channel_id.to_channel() {
/// Ok(channel) => channel,
/// Err(why) => {
/// let _ = msg.channel_id.say(&format!("Error creating invite: {:?}", why));
///
/// return;
/// },
/// };
///
/// let reader = channel.read();
/// if let Channel::Guild(channel) = channel {
/// let channel = channel.read();
///
/// let invite = match reader.create_invite(|i| i.max_age(3600).max_uses(10)) {
/// let invite = match channel.create_invite(|i| i.max_age(3600).max_uses(10)) {
/// Ok(invite) => invite,
/// Err(why) => {
/// println!("Err creating invite: {:?}", why);
Expand All @@ -43,20 +45,22 @@ use utils::VecMap;
/// }
///
/// return;
/// },
/// };
///
/// drop(reader);
/// },
/// };
///
///
/// let content = format!("Here's your invite: {}", invite.url());
/// let _ = msg.channel_id.say(&content);
/// let content = format!("Here's your invite: {}", invite.url());
/// let _ = msg.channel_id.say(&content);
/// }
/// }
/// }
/// }
///
/// let mut client = Client::new("token", Handler).unwrap();
///
/// client.start().unwrap();
/// # }
/// ```
///
/// [`GuildChannel::create_invite`]: ../model/channel/struct.GuildChannel.html#method.create_invite
Expand All @@ -76,15 +80,18 @@ impl CreateInvite {
/// Create an invite with a max age of `3600` seconds, or 1 hour:
///
/// ```rust,no_run
/// # use serenity::CACHE;
/// # use serenity::model::id::ChannelId;
/// # use serenity::model::channel::Channel;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
/// # let channel = CACHE.read().guild_channel(81384788765712384).unwrap();
/// # let channel = channel.read();
/// # let channel = ChannelId(81384788765712384).to_channel().unwrap();
/// #
/// # if let Channel::Guild(guild_channel) = channel {
/// # let guild_channel = guild_channel.read();
/// let invite = guild_channel.create_invite(|i| i.max_age(3600))?;
/// # }
/// #
/// let invite = channel.create_invite(|i| i.max_age(3600))?;
/// # Ok(())
/// # }
/// #
Expand All @@ -109,15 +116,17 @@ impl CreateInvite {
/// Create an invite with a max use limit of `5`:
///
/// ```rust,no_run
/// # use serenity::CACHE;
/// # use serenity::model::id::ChannelId;
/// # use serenity::model::channel::Channel;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
/// # let channel = CACHE.read().guild_channel(81384788765712384).unwrap();
/// # let channel = channel.read();
/// # let channel = ChannelId(81384788765712384).to_channel().unwrap();
/// #
/// # if let Channel::Guild(channel) = channel {
/// # let channel = channel.read();
/// let invite = channel.create_invite(|i| i.max_uses(5))?;
/// # }
/// # Ok(())
/// # }
/// #
Expand All @@ -140,15 +149,19 @@ impl CreateInvite {
/// Create an invite which is temporary:
///
/// ```rust,no_run
/// # use serenity::CACHE;
/// # use serenity::model::id::ChannelId;
/// # use serenity::model::channel::Channel;
/// # use std::error::Error;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
/// # let channel = CACHE.read().guild_channel(81384788765712384).unwrap();
/// # let channel = channel.read();
/// # let channel = ChannelId(81384788765712384).to_channel().unwrap();
/// #
/// # if let Channel::Guild(channel) = channel {
/// # let channel = channel.read();
/// #
/// let invite = channel.create_invite(|i| i.temporary(true))?;
/// # }
/// #
/// # Ok(())
/// # }
/// #
Expand All @@ -171,15 +184,17 @@ impl CreateInvite {
/// Create an invite which is unique:
///
/// ```rust,no_run
/// # use serenity::CACHE;
/// # use serenity::model::id::ChannelId;
/// # use std::error::Error;
/// # use serenity::model::channel::Channel;
/// #
/// # fn try_main() -> Result<(), Box<Error>> {
/// # let channel = CACHE.read().guild_channel(81384788765712384).unwrap();
/// # let channel = channel.read();
/// # let channel = ChannelId(81384788765712384).to_channel().unwrap();
/// #
/// # if let Channel::Guild(channel) = channel {
/// # let channel = channel.read();
/// let invite = channel.create_invite(|i| i.unique(true))?;
/// # }
/// # Ok(())
/// # }
/// #
Expand Down
2 changes: 2 additions & 0 deletions src/builder/edit_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl EditProfile {
/// image from a file and return its contents in base64-encoded form:
///
/// ```rust,no_run
/// # #[cfg(feature = "client")] {
/// # use serenity::prelude::*;
/// # use serenity::model::prelude::*;
/// #
Expand All @@ -43,6 +44,7 @@ impl EditProfile {
/// # let mut client = Client::new("token", Handler).unwrap();
/// #
/// # client.start().unwrap();
/// # }
/// ```
///
/// [`utils::read_image`]: ../utils/fn.read_image.html
Expand Down
6 changes: 6 additions & 0 deletions src/builder/execute_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ impl ExecuteWebhook {
/// Sending a webhook with a content of `"foo"`:
///
/// ```rust,no_run
/// # #[cfg(feature = "client")] {
/// # use serenity::client::rest;
/// #
/// # let webhook = rest::get_webhook_with_token(0, "").unwrap();
/// #
/// if let Err(why) = webhook.execute(false, |w| w.content("foo")) {
/// println!("Err sending webhook: {:?}", why);
/// }
/// # }
/// ```
///
/// [`embeds`]: #method.embeds
Expand Down Expand Up @@ -125,13 +127,15 @@ impl ExecuteWebhook {
/// Sending a webhook with text-to-speech enabled:
///
/// ```rust,no_run
/// # #[cfg(feature = "client")] {
/// # use serenity::client::rest;
/// #
/// # let webhook = rest::get_webhook_with_token(0, "").unwrap();
/// #
/// if let Err(why) = webhook.execute(false, |w| w.content("hello").tts(true)) {
/// println!("Err sending webhook: {:?}", why);
/// }
/// # }
/// ```
pub fn tts(mut self, tts: bool) -> Self {
self.0.insert("tts", Value::Bool(tts));
Expand All @@ -146,13 +150,15 @@ impl ExecuteWebhook {
/// Overriding the username to `"hakase"`:
///
/// ```rust,no_run
/// # #[cfg(feature = "client")] {
/// # use serenity::client::rest;
/// #
/// # let webhook = rest::get_webhook_with_token(0, "").unwrap();
/// #
/// if let Err(why) = webhook.execute(false, |w| w.content("hello").username("hakase")) {
/// println!("Err sending webhook: {:?}", why);
/// }
/// # }
/// ```
pub fn username(mut self, username: &str) -> Self {
self.0.insert("username", Value::String(username.to_string()));
Expand Down
26 changes: 12 additions & 14 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ use self::bridge::voice::ClientVoiceManager;
/// receive, acting as a "ping-pong" bot is simple:
///
/// ```no_run
/// extern crate serenity;
///
/// # fn try_main() -> Result<(), Box<std::error::Error>> {
/// # fn main() {
/// use serenity::prelude::*;
/// use serenity::model::prelude::*;
/// use serenity::Client;
Expand All @@ -91,12 +89,10 @@ use self::bridge::voice::ClientVoiceManager;
/// }
/// }
///
/// let mut client = Client::new("my token here", Handler)?;
/// let mut client = Client::new("my token here", Handler).expect("Could not create new client.");
///
/// client.start();
/// # Ok(()) }
/// #
/// # fn main() { try_main().unwrap(); }
/// client.start().expect("Could not start client.");
/// # }
/// ```
///
/// [`Shard`]: ../gateway/struct.Shard.html
Expand Down Expand Up @@ -129,7 +125,6 @@ pub struct Client {
/// extern crate serenity;
/// extern crate typemap;
///
/// # fn try_main() -> Result<(), Box<std::error::Error>> {
/// // Of note, this imports `typemap`'s `Key` as `TypeMapKey`.
/// use serenity::prelude::*;
/// use serenity::model::prelude::*;
Expand All @@ -153,6 +148,7 @@ pub struct Client {
///
/// struct Handler;
///
/// # #[cfg(all(feature = "client", feature = "standard_framework", feature = "model"))]
/// impl EventHandler for Handler {
/// fn message(&self, ctx: Context, _: Message) { reg(ctx, "MessageCreate") }
/// fn message_delete(&self, ctx: Context, _: ChannelId, _: MessageId) {
Expand All @@ -163,17 +159,19 @@ pub struct Client {
/// reg(ctx, "MessageUpdate") }
/// }
///
/// let mut client = Client::new(&env::var("DISCORD_TOKEN")?, Handler)?;
/// # #[cfg(all(feature = "client", feature = "standard_framework", feature = "model"))]
/// # fn main() {
///
/// let mut client = Client::new(&env::var("DISCORD_TOKEN").expect("Could not find token."), Handler)
/// .expect("Could not create client.");
///
/// {
/// let mut data = client.data.lock();
/// data.insert::<MessageEventCounter>(HashMap::default());
/// }
///
/// client.start()?;
/// # Ok(()) }
/// #
/// # fn main() { try_main().unwrap(); }
/// client.start().expect("Could not start client.");
/// # }
/// ```
///
/// Refer to [example 05] for an example on using the `data` field.
Expand Down
5 changes: 3 additions & 2 deletions src/framework/standard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,14 @@ impl StandardFramework {
/// # struct Handler;
/// #
/// # impl EventHandler for Handler {}
/// # let mut client = Client::new("token", Handler);
/// # let mut client = Client::new("token", Handler).unwrap();
/// #
/// use serenity::framework::StandardFramework;
///
/// client.with_framework(StandardFramework::new()
/// .complex_bucket("basic", 2, 10, 3, |_, channel_id, user_id| {
/// Our bucket is somewhat strict. It can only apply in the specific channel and by the specific user.
/// // Our bucket is somewhat strict.
/// // It can only apply in the specific channel and by the specific user.
/// channel_id == 456 && user_id == 789
/// })
/// .command("ping", |c| c
Expand Down
2 changes: 2 additions & 0 deletions src/model/channel/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Attachment {
/// Download all of the attachments associated with a [`Message`]:
///
/// ```rust,no_run
/// # #[cfg(feature = "client")] {
/// use serenity::model::prelude::*;
/// use serenity::prelude::*;
/// use std::env;
Expand Down Expand Up @@ -93,6 +94,7 @@ impl Attachment {
/// let mut client = Client::new(&token, Handler).unwrap();
///
/// client.start().unwrap();
/// # }
/// ```
///
/// # Errors
Expand Down
Loading

0 comments on commit e6694f2

Please sign in to comment.