summaryrefslogtreecommitdiffstats
path: root/third_party/rust/displaydoc/tests/happy.rs
blob: f8fde9c61200a53b25e38e0f3649a8b7c9ed6fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use displaydoc::Display;

#[cfg(feature = "std")]
use std::path::PathBuf;

#[derive(Display)]
/// Just a basic struct {thing}
struct HappyStruct {
    thing: &'static str,
}

#[derive(Display)]
#[ignore_extra_doc_attributes]
/// Just a basic struct {thing}
/// and this line should get ignored
struct HappyStruct2 {
    thing: &'static str,
}

#[derive(Display)]
enum Happy {
    /// I really like Variant1
    Variant1,
    /// Variant2 is pretty swell 2
    Variant2,
    /// Variant3 is okay {sometimes}
    Variant3 { sometimes: &'static str },
    /**
     * Variant4 wants to have a lot of lines
     *
     * Lets see how this works out for it
     */
    Variant4,
    /// Variant5 has a parameter {0} and some regular comments
    // A regular comment that won't get picked
    Variant5(u32),

    /// The path {0}
    #[cfg(feature = "std")]
    Variant6(PathBuf),

    /// These docs are ignored
    #[displaydoc("Variant7 has a parameter {0} and uses #[displaydoc]")]
    /// These docs are also ignored
    Variant7(u32),
}

// Used for testing indented doc comments
mod inner_mod {
    use super::Display;

    #[derive(Display)]
    pub enum InnerHappy {
        /// I really like Variant1
        Variant1,
        /// Variant2 is pretty swell 2
        Variant2,
        /// Variant3 is okay {sometimes}
        Variant3 { sometimes: &'static str },
        /**
         * Variant4 wants to have a lot of lines
         *
         * Lets see how this works out for it
         */
        Variant4,
        /// Variant5 has a parameter {0} and some regular comments
        // A regular comment that won't get picked
        Variant5(u32),

        /** what happens if we
         * put text on the first line?
         */
        Variant6,

        /**
        what happens if we don't use *?
        */
        Variant7,

        /**
         *
         * what about extra new lines?
         */
        Variant8,
    }
}

fn assert_display<T: std::fmt::Display>(input: T, expected: &'static str) {
    let out = format!("{}", input);
    assert_eq!(expected, out);
}

#[test]
fn does_it_print() {
    assert_display(Happy::Variant1, "I really like Variant1");
    assert_display(Happy::Variant2, "Variant2 is pretty swell 2");
    assert_display(Happy::Variant3 { sometimes: "hi" }, "Variant3 is okay hi");
    assert_display(
        Happy::Variant4,
        "Variant4 wants to have a lot of lines\n\nLets see how this works out for it",
    );
    assert_display(
        Happy::Variant5(2),
        "Variant5 has a parameter 2 and some regular comments",
    );
    assert_display(
        Happy::Variant7(2),
        "Variant7 has a parameter 2 and uses #[displaydoc]",
    );
    assert_display(HappyStruct { thing: "hi" }, "Just a basic struct hi");

    assert_display(HappyStruct2 { thing: "hi2" }, "Just a basic struct hi2");

    assert_display(inner_mod::InnerHappy::Variant1, "I really like Variant1");
    assert_display(
        inner_mod::InnerHappy::Variant2,
        "Variant2 is pretty swell 2",
    );
    assert_display(
        inner_mod::InnerHappy::Variant3 { sometimes: "hi" },
        "Variant3 is okay hi",
    );
    assert_display(
        inner_mod::InnerHappy::Variant4,
        "Variant4 wants to have a lot of lines\n\nLets see how this works out for it",
    );
    assert_display(
        inner_mod::InnerHappy::Variant5(2),
        "Variant5 has a parameter 2 and some regular comments",
    );
    assert_display(
        inner_mod::InnerHappy::Variant6,
        "what happens if we\nput text on the first line?",
    );
    assert_display(
        inner_mod::InnerHappy::Variant7,
        "what happens if we don\'t use *?",
    );
    assert_display(
        inner_mod::InnerHappy::Variant8,
        "what about extra new lines?",
    );
}

#[test]
#[cfg(feature = "std")]
fn does_it_print_path() {
    assert_display(
        Happy::Variant6(PathBuf::from("/var/log/happy")),
        "The path /var/log/happy",
    );
}