Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 1 | // Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu> |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | mod utils; |
| 10 | |
| 11 | use structopt::StructOpt; |
| 12 | use utils::*; |
| 13 | |
| 14 | #[test] |
| 15 | fn doc_comments() { |
| 16 | /// Lorem ipsum |
| 17 | #[derive(StructOpt, PartialEq, Debug)] |
| 18 | struct LoremIpsum { |
| 19 | /// Fooify a bar |
| 20 | /// and a baz |
| 21 | #[structopt(short, long)] |
| 22 | foo: bool, |
| 23 | } |
| 24 | |
| 25 | let help = get_long_help::<LoremIpsum>(); |
| 26 | assert!(help.contains("Lorem ipsum")); |
| 27 | assert!(help.contains("Fooify a bar and a baz")); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn help_is_better_than_comments() { |
| 32 | /// Lorem ipsum |
| 33 | #[derive(StructOpt, PartialEq, Debug)] |
| 34 | #[structopt(name = "lorem-ipsum", about = "Dolor sit amet")] |
| 35 | struct LoremIpsum { |
| 36 | /// Fooify a bar |
| 37 | #[structopt(short, long, help = "DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES")] |
| 38 | foo: bool, |
| 39 | } |
| 40 | |
| 41 | let help = get_long_help::<LoremIpsum>(); |
| 42 | assert!(help.contains("Dolor sit amet")); |
| 43 | assert!(!help.contains("Lorem ipsum")); |
| 44 | assert!(help.contains("DO NOT PASS A BAR")); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn empty_line_in_doc_comment_is_double_linefeed() { |
| 49 | /// Foo. |
| 50 | /// |
| 51 | /// Bar |
| 52 | #[derive(StructOpt, PartialEq, Debug)] |
| 53 | #[structopt(name = "lorem-ipsum", no_version)] |
| 54 | struct LoremIpsum {} |
| 55 | |
| 56 | let help = get_long_help::<LoremIpsum>(); |
| 57 | assert!(help.starts_with("lorem-ipsum \nFoo.\n\nBar\n\nUSAGE:")); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn field_long_doc_comment_both_help_long_help() { |
| 62 | /// Lorem ipsumclap |
| 63 | #[derive(StructOpt, PartialEq, Debug)] |
| 64 | #[structopt(name = "lorem-ipsum", about = "Dolor sit amet")] |
| 65 | struct LoremIpsum { |
| 66 | /// Dot is removed from multiline comments. |
| 67 | /// |
| 68 | /// Long help |
| 69 | #[structopt(long)] |
| 70 | foo: bool, |
| 71 | |
| 72 | /// Dot is removed from one short comment. |
| 73 | #[structopt(long)] |
| 74 | bar: bool, |
| 75 | } |
| 76 | |
| 77 | let short_help = get_help::<LoremIpsum>(); |
| 78 | let long_help = get_long_help::<LoremIpsum>(); |
| 79 | |
| 80 | assert!(short_help.contains("Dot is removed from one short comment")); |
| 81 | assert!(!short_help.contains("Dot is removed from one short comment.")); |
| 82 | assert!(short_help.contains("Dot is removed from multiline comments")); |
| 83 | assert!(!short_help.contains("Dot is removed from multiline comments.")); |
| 84 | assert!(long_help.contains("Long help")); |
| 85 | assert!(!short_help.contains("Long help")); |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn top_long_doc_comment_both_help_long_help() { |
| 90 | /// Lorem ipsumclap |
| 91 | #[derive(StructOpt, Debug)] |
| 92 | #[structopt(name = "lorem-ipsum", about = "Dolor sit amet")] |
| 93 | struct LoremIpsum { |
| 94 | #[structopt(subcommand)] |
| 95 | foo: SubCommand, |
| 96 | } |
| 97 | |
| 98 | #[derive(StructOpt, Debug)] |
| 99 | pub enum SubCommand { |
| 100 | /// DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES |
| 101 | /// |
| 102 | /// Or something else |
| 103 | Foo { |
| 104 | #[structopt(help = "foo")] |
| 105 | bars: Vec<String>, |
| 106 | }, |
| 107 | } |
| 108 | |
| 109 | let short_help = get_help::<LoremIpsum>(); |
| 110 | let long_help = get_subcommand_long_help::<LoremIpsum>("foo"); |
| 111 | |
| 112 | assert!(!short_help.contains("Or something else")); |
| 113 | assert!(long_help.contains("DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES")); |
| 114 | assert!(long_help.contains("Or something else")); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn verbatim_doc_comment() { |
| 119 | /// DANCE! |
| 120 | /// |
| 121 | /// () |
| 122 | /// | |
| 123 | /// ( () ) |
| 124 | /// ) ________ // ) |
| 125 | /// () |\ \ // |
| 126 | /// ( \\__ \ ______\// |
| 127 | /// \__) | | |
| 128 | /// | | | |
| 129 | /// \ | | |
| 130 | /// \|_______| |
| 131 | /// // \\ |
| 132 | /// (( || |
| 133 | /// \\ || |
| 134 | /// ( () || |
| 135 | /// ( () ) ) |
| 136 | #[derive(StructOpt, Debug)] |
| 137 | #[structopt(verbatim_doc_comment)] |
| 138 | struct SeeFigure1 { |
| 139 | #[structopt(long)] |
| 140 | foo: bool, |
| 141 | } |
| 142 | |
| 143 | let help = get_long_help::<SeeFigure1>(); |
| 144 | let sample = r#" |
| 145 | () |
| 146 | | |
| 147 | ( () ) |
| 148 | ) ________ // ) |
| 149 | () |\ \ // |
| 150 | ( \\__ \ ______\// |
| 151 | \__) | | |
| 152 | | | | |
| 153 | \ | | |
| 154 | \|_______| |
| 155 | // \\ |
| 156 | (( || |
| 157 | \\ || |
| 158 | ( () || |
| 159 | ( () ) )"#; |
| 160 | |
| 161 | assert!(help.contains(sample)) |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn verbatim_doc_comment_field() { |
| 166 | #[derive(StructOpt, Debug)] |
| 167 | struct App { |
| 168 | /// This help ends in a period. |
| 169 | #[structopt(long, verbatim_doc_comment)] |
| 170 | foo: bool, |
| 171 | /// This help does not end in a period. |
| 172 | #[structopt(long)] |
| 173 | bar: bool, |
| 174 | } |
| 175 | |
| 176 | let help = get_long_help::<App>(); |
| 177 | let sample = r#" |
| 178 | --bar |
| 179 | This help does not end in a period |
| 180 | |
| 181 | --foo |
| 182 | This help ends in a period."#; |
| 183 | |
| 184 | assert!(help.contains(sample)) |
| 185 | } |