From 3157305105dae855529d092173fab8ee54d15234 Mon Sep 17 00:00:00 2001 From: Waffle Date: Wed, 12 Aug 2020 23:21:13 +0300 Subject: [PATCH] add automatic test of code blocks from readme --- Cargo.toml | 2 ++ README.md | 20 ++++++++++---------- src/lib.rs | 5 +++++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d4475692..37109156 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,8 @@ bincode-serializer = ["bincode"] frunk- = ["frunk"] +nightly = [] # currently used only for `README.md` tests + [dependencies] serde_json = "1.0.55" serde = { version = "1.0.114", features = ["derive"] } diff --git a/README.md b/README.md index ca7ad4c6..4cdc75c3 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] } This bot throws a dice on each incoming message: ([Full](https://github.com/teloxide/teloxide/blob/master/examples/dices_bot/src/main.rs)) -```rust +```rust,no_run use teloxide::prelude::*; #[tokio::main] @@ -130,8 +130,8 @@ Commands are strongly typed and defined declaratively, similar to how we define [serde-json]: https://github.com/serde-rs/json ([Full](https://github.com/teloxide/teloxide/blob/master/examples/simple_commands_bot/src/main.rs)) -```rust -// Imports are omitted... +```rust,no_run +use teloxide::{utils::command::BotCommand, prelude::*}; #[derive(BotCommand)] #[command(rename = "lowercase", description = "These commands are supported:")] @@ -166,7 +166,7 @@ async fn main() { let bot = Bot::from_env(); let bot_name: String = panic!("Your bot's name here"); - teloxide::commands_repl(bot, bot_name, action).await; + teloxide::commands_repl(bot, bot_name, answer).await; } ``` @@ -184,7 +184,7 @@ A dialogue is described by an enumeration, where each variant is one of possible Below is a bot, which asks you three questions and then sends the answers back to you. First, let's start with an enumeration (a collection of our dialogue's states): ([dialogue_bot/src/dialogue/mod.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/mod.rs)) -```rust +```rust,ignore // Imports are omitted... #[derive(Transition, From)] @@ -208,7 +208,7 @@ When a user sends a message to our bot, and such a dialogue does not yet exist, Dialogue::Start ([dialogue_bot/src/dialogue/states/start.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/start.rs)) -```rust +```rust,ignore // Imports are omitted... pub struct StartState; @@ -226,7 +226,7 @@ async fn start(_state: StartState, cx: TransitionIn, _ans: String) -> Transition Dialogue::ReceiveFullName ([dialogue_bot/src/dialogue/states/receive_full_name.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/receive_full_name.rs)) -```rust +```rust,ignore // Imports are omitted... #[derive(Generic)] @@ -249,7 +249,7 @@ async fn receive_full_name( Dialogue::ReceiveAge ([dialogue_bot/src/dialogue/states/receive_age.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/receive_age.rs)) -```rust +```rust,ignore // Imports are omitted... #[derive(Generic)] @@ -282,7 +282,7 @@ async fn receive_age_state( Dialogue::ReceiveLocation ([dialogue_bot/src/dialogue/states/receive_location.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/dialogue/states/receive_location.rs)) -```rust +```rust,ignore // Imports are omitted... #[derive(Generic)] @@ -310,7 +310,7 @@ All these subtransitions accept a corresponding state (one of the many variants Finally, the `main` function looks like this: ([dialogue_bot/src/main.rs](https://github.com/teloxide/teloxide/blob/master/examples/dialogue_bot/src/main.rs)) -```rust +```rust,ignore // Imports are omitted... #[tokio::main] diff --git a/src/lib.rs b/src/lib.rs index c3116be2..7ffb1775 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,7 @@ )] #![allow(clippy::match_bool)] #![forbid(unsafe_code)] +#![cfg_attr(all(feature = "nightly", doctest), feature(external_doc))] pub use bot::{Bot, BotBuilder}; pub use dispatching::repls::{ @@ -61,3 +62,7 @@ pub mod types; pub mod utils; extern crate teloxide_macros; + +#[cfg(all(feature = "nightly", doctest))] +#[doc(include = "../README.md")] +enum ReadmeDocTests {}