#enums #variant #testing #testing-debugging #generate #traits #tree

every_variant

Provides an EveryVariant trait that provides the every_variant method on types. Allows you to easily generate every combination of variants in structures that contains Enums, or in nested enum trees. This to allow for additional testing of codepaths

8 releases

0.4.5 Oct 27, 2023
0.4.4 Mar 24, 2022
0.4.3 Sep 14, 2021
0.2.1 Jan 12, 2021
0.2.0 Sep 30, 2020

#268 in Data structures

Download history 224/week @ 2024-07-19 641/week @ 2024-07-26 1260/week @ 2024-08-02 1366/week @ 2024-08-09 1709/week @ 2024-08-16 1224/week @ 2024-08-23 1276/week @ 2024-08-30 951/week @ 2024-09-06 806/week @ 2024-09-13 1312/week @ 2024-09-20 1021/week @ 2024-09-27 1155/week @ 2024-10-04 174/week @ 2024-10-11 893/week @ 2024-10-18 867/week @ 2024-10-25 1507/week @ 2024-11-01

3,455 downloads per month

MIT license

18KB
381 lines

every_variant

Provides an EveryVariant trait that provides the every_variant() method on types. Allows you to easily generate every combination of variants in structures that contains Enums, or in nested enum trees. This to allow for additional testing of codepaths where nested enum trees are used.

The derive macro EveryVariant will provide the every_variant() method for you, with some preset values for the std types such as floats, integers and strings,

The generated data inside at the lowest level is currently fixed to specific values. If you have types that are dependend on strings with a specific format on the strings for example, I suggest that those be made into their own types and every_variant implemented manually for those.

Example:


use every_variant::EveryVariant;

/// Type of the message
#[derive(EveryVariant, Debug, Clone)]
enum MessageType {
    Codified,
    Markdown,
    Html,
}

/// This type should generate 4 different variant
#[derive(EveryVariant, Debug, Clone)]
struct FormattedMessage {
    /// Enum dictating how to render the string, None means its hidden
    rendermethod: Option<MessageType>,
    /// The optional content of the message
    text: String,
}

fn main() {
    let all_diferent_messages = FormattedMessage::every_variant();
    println!("{:#?}", all_diferent_messages);
}


the output will be:

[
    FormattedMessage {
        rendermethod: None,
        text: "example String",
    },
    FormattedMessage {
        rendermethod: Some(
            Codified,
        ),
        text: "example String",
    },
    FormattedMessage {
        rendermethod: Some(
            Markdown,
        ),
        text: "example String",
    },
    FormattedMessage {
        rendermethod: Some(
            Html,
        ),
        text: "example String",
    },
]

Dependencies

~1.3–1.8MB
~42K SLoC