| 1 |
use displaydoc::Display; |
| 2 |
|
| 3 |
#[cfg(feature = "std")] |
| 4 |
use std::path::PathBuf; |
| 5 |
|
| 6 |
#[derive(Display)] |
| 7 |
/// Just a basic struct {thing} |
| 8 |
struct HappyStruct { |
| 9 |
thing: &'static str, |
| 10 |
} |
| 11 |
|
| 12 |
#[derive(Display)] |
| 13 |
#[ignore_extra_doc_attributes] |
| 14 |
/// Just a basic struct {thing} |
| 15 |
/// and this line should get ignored |
| 16 |
struct HappyStruct2 { |
| 17 |
thing: &'static str, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Display)] |
| 21 |
enum Happy { |
| 22 |
/// I really like Variant1 |
| 23 |
Variant1, |
| 24 |
/// Variant2 is pretty swell 2 |
| 25 |
Variant2, |
| 26 |
/// Variant3 is okay {sometimes} |
| 27 |
Variant3 { sometimes: &'static str }, |
| 28 |
/** |
| 29 |
* Variant4 wants to have a lot of lines |
| 30 |
* |
| 31 |
* Lets see how this works out for it |
| 32 |
*/ |
| 33 |
Variant4, |
| 34 |
/// Variant5 has a parameter {0} and some regular comments |
| 35 |
// A regular comment that won't get picked |
| 36 |
Variant5(u32), |
| 37 |
|
| 38 |
/// The path {0} |
| 39 |
#[cfg(feature = "std")] |
| 40 |
Variant6(PathBuf), |
| 41 |
|
| 42 |
/// These docs are ignored |
| 43 |
#[displaydoc("Variant7 has a parameter {0} and uses #[displaydoc]")] |
| 44 |
/// These docs are also ignored |
| 45 |
Variant7(u32), |
| 46 |
} |
| 47 |
|
| 48 |
// Used for testing indented doc comments |
| 49 |
mod inner_mod { |
| 50 |
use super::Display; |
| 51 |
|
| 52 |
#[derive(Display)] |
| 53 |
pub enum InnerHappy { |
| 54 |
/// I really like Variant1 |
| 55 |
Variant1, |
| 56 |
/// Variant2 is pretty swell 2 |
| 57 |
Variant2, |
| 58 |
/// Variant3 is okay {sometimes} |
| 59 |
Variant3 { sometimes: &'static str }, |
| 60 |
/** |
| 61 |
* Variant4 wants to have a lot of lines |
| 62 |
* |
| 63 |
* Lets see how this works out for it |
| 64 |
*/ |
| 65 |
Variant4, |
| 66 |
/// Variant5 has a parameter {0} and some regular comments |
| 67 |
// A regular comment that won't get picked |
| 68 |
Variant5(u32), |
| 69 |
|
| 70 |
/** what happens if we |
| 71 |
* put text on the first line? |
| 72 |
*/ |
| 73 |
Variant6, |
| 74 |
|
| 75 |
/** |
| 76 |
what happens if we don't use *? |
| 77 |
*/ |
| 78 |
Variant7, |
| 79 |
|
| 80 |
/** |
| 81 |
* |
| 82 |
* what about extra new lines? |
| 83 |
*/ |
| 84 |
Variant8, |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
fn assert_display<T: std::fmt::Display>(input: T, expected: &'static str) { |
| 89 |
let out = format!("{}", input); |
| 90 |
assert_eq!(expected, out); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn does_it_print() { |
| 95 |
assert_display(Happy::Variant1, "I really like Variant1"); |
| 96 |
assert_display(Happy::Variant2, "Variant2 is pretty swell 2"); |
| 97 |
assert_display(Happy::Variant3 { sometimes: "hi" }, "Variant3 is okay hi"); |
| 98 |
assert_display( |
| 99 |
Happy::Variant4, |
| 100 |
"Variant4 wants to have a lot of lines\n\nLets see how this works out for it", |
| 101 |
); |
| 102 |
assert_display( |
| 103 |
Happy::Variant5(2), |
| 104 |
"Variant5 has a parameter 2 and some regular comments", |
| 105 |
); |
| 106 |
assert_display( |
| 107 |
Happy::Variant7(2), |
| 108 |
"Variant7 has a parameter 2 and uses #[displaydoc]", |
| 109 |
); |
| 110 |
assert_display(HappyStruct { thing: "hi" }, "Just a basic struct hi"); |
| 111 |
|
| 112 |
assert_display(HappyStruct2 { thing: "hi2" }, "Just a basic struct hi2"); |
| 113 |
|
| 114 |
assert_display(inner_mod::InnerHappy::Variant1, "I really like Variant1"); |
| 115 |
assert_display( |
| 116 |
inner_mod::InnerHappy::Variant2, |
| 117 |
"Variant2 is pretty swell 2", |
| 118 |
); |
| 119 |
assert_display( |
| 120 |
inner_mod::InnerHappy::Variant3 { sometimes: "hi" }, |
| 121 |
"Variant3 is okay hi", |
| 122 |
); |
| 123 |
assert_display( |
| 124 |
inner_mod::InnerHappy::Variant4, |
| 125 |
"Variant4 wants to have a lot of lines\n\nLets see how this works out for it", |
| 126 |
); |
| 127 |
assert_display( |
| 128 |
inner_mod::InnerHappy::Variant5(2), |
| 129 |
"Variant5 has a parameter 2 and some regular comments", |
| 130 |
); |
| 131 |
assert_display( |
| 132 |
inner_mod::InnerHappy::Variant6, |
| 133 |
"what happens if we\nput text on the first line?", |
| 134 |
); |
| 135 |
assert_display( |
| 136 |
inner_mod::InnerHappy::Variant7, |
| 137 |
"what happens if we don\'t use *?", |
| 138 |
); |
| 139 |
assert_display( |
| 140 |
inner_mod::InnerHappy::Variant8, |
| 141 |
"what about extra new lines?", |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
#[cfg(feature = "std")] |
| 147 |
fn does_it_print_path() { |
| 148 |
assert_display( |
| 149 |
Happy::Variant6(PathBuf::from("/var/log/happy")), |
| 150 |
"The path /var/log/happy", |
| 151 |
); |
| 152 |
} |