Ignore unsupported doc tests under --no-default-features

There's no pretty way of gating doctests contingent on cfg
presence/absense so this is kind of ugly.

First, a doc comment `///` is converted into its equivalent
`#[doc(...)]`, which is then converted into a conditional rust attribute
via `#[cfg_attr(...)]`. We need two of these, one to ignore the tests if
the cfg predicate isn't met and another to indicate the start of a code
block and enable testing + syntax highlighting.
This commit is contained in:
Mahmoud Al-Qudsi 2022-09-12 15:03:44 -05:00 committed by Dirkjan Ochtman
parent e0c4926841
commit 28b2200f4f
4 changed files with 18 additions and 9 deletions

View File

@ -16,7 +16,8 @@
//! C's `strftime` format. The available options can be found [here](./strftime/index.html).
//!
//! # Example
//! ```rust
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
#![cfg_attr(feature = "std", doc = "```rust")]
//! # use std::error::Error;
//! use chrono::prelude::*;
//!

View File

@ -110,7 +110,8 @@
//! or in the local time zone
//! ([`Local::now()`](./offset/struct.Local.html#method.now)).
//!
//! ```rust
#![cfg_attr(not(feature = "clock"), doc = "```ignore")]
#![cfg_attr(feature = "clock", doc = "```rust")]
//! use chrono::prelude::*;
//!
//! let utc: DateTime<Utc> = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
@ -122,7 +123,8 @@
//! This is a bit verbose due to Rust's lack of function and method overloading,
//! but in turn we get a rich combination of initialization methods.
//!
//! ```rust
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
#![cfg_attr(feature = "std", doc = "```rust")]
//! use chrono::prelude::*;
//! use chrono::offset::LocalResult;
//!
@ -314,7 +316,8 @@
//! [`DateTime.timestamp_subsec_nanos`](./struct.DateTime.html#method.timestamp_subsec_nanos)
//! to get the number of additional number of nanoseconds.
//!
//! ```rust
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
#![cfg_attr(feature = "std", doc = "```rust")]
//! // We need the trait in scope to use Utc::timestamp().
//! use chrono::{DateTime, TimeZone, Utc};
//!
@ -333,7 +336,8 @@
//! It also has time zones attached, and have to be constructed via time zones.
//! Most operations available to `DateTime` are also available to `Date` whenever appropriate.
//!
//! ```rust
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
#![cfg_attr(feature = "std", doc = "```rust")]
//! use chrono::prelude::*;
//! use chrono::offset::LocalResult;
//!

View File

@ -818,7 +818,8 @@ impl Timelike for NaiveTime {
/// ([Why?](#leap-second-handling))
/// Use the proper [formatting method](#method.format) to get a human-readable representation.
///
/// ```
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// # use chrono::{NaiveTime, Timelike};
/// let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
/// assert_eq!(leap.second(), 59);
@ -846,7 +847,8 @@ impl Timelike for NaiveTime {
/// You can reduce the range with `time.nanosecond() % 1_000_000_000`, or
/// use the proper [formatting method](#method.format) to get a human-readable representation.
///
/// ```
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// # use chrono::{NaiveTime, Timelike};
/// let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
/// assert_eq!(leap.nanosecond(), 1_000_000_000);

View File

@ -46,7 +46,8 @@ impl FixedOffset {
///
/// # Example
///
/// ```
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// use chrono::{FixedOffset, TimeZone};
/// let hour = 3600;
/// let datetime = FixedOffset::east_opt(5 * hour).unwrap().ymd_opt(2016, 11, 08).unwrap()
@ -79,7 +80,8 @@ impl FixedOffset {
///
/// # Example
///
/// ```
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// use chrono::{FixedOffset, TimeZone};
/// let hour = 3600;
/// let datetime = FixedOffset::west_opt(5 * hour).unwrap().ymd_opt(2016, 11, 08).unwrap()