2018-07-02 17:41:01 +00:00
|
|
|
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
|
|
|
|
// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
|
2022-01-04 20:10:35 +00:00
|
|
|
// Ana Hobden (@hoverbear) <operator@hoverbear.org>
|
2017-06-15 18:14:16 +00:00
|
|
|
//
|
2018-02-25 10:22:24 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2018-07-02 17:41:01 +00:00
|
|
|
//
|
|
|
|
// This work was derived from Structopt (https://github.com/TeXitoi/structopt)
|
|
|
|
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
|
|
|
|
// MIT/Apache 2.0 license.
|
2017-06-15 18:14:16 +00:00
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
use crate::utils;
|
2017-06-15 18:14:16 +00:00
|
|
|
|
2022-02-14 22:09:38 +00:00
|
|
|
use clap::{ArgEnum, CommandFactory, Parser};
|
2017-06-15 18:14:16 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-01-07 10:17:23 +00:00
|
|
|
fn doc_comments() {
|
2017-06-15 18:14:16 +00:00
|
|
|
/// Lorem ipsum
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2017-06-15 18:14:16 +00:00
|
|
|
struct LoremIpsum {
|
|
|
|
/// Fooify a bar
|
2017-11-01 21:00:15 +00:00
|
|
|
/// and a baz
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(short, long, action)]
|
2017-06-15 18:14:16 +00:00
|
|
|
foo: bool,
|
|
|
|
}
|
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<LoremIpsum>();
|
2020-01-07 10:17:23 +00:00
|
|
|
assert!(help.contains("Lorem ipsum"));
|
|
|
|
assert!(help.contains("Fooify a bar and a baz"));
|
2017-06-15 18:14:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_is_better_than_comments() {
|
|
|
|
/// Lorem ipsum
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2018-07-02 19:06:46 +00:00
|
|
|
#[clap(name = "lorem-ipsum", about = "Dolor sit amet")]
|
2017-06-15 18:14:16 +00:00
|
|
|
struct LoremIpsum {
|
|
|
|
/// Fooify a bar
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(
|
|
|
|
short,
|
|
|
|
long,
|
|
|
|
help = "DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES",
|
|
|
|
action
|
|
|
|
)]
|
2017-06-15 18:14:16 +00:00
|
|
|
foo: bool,
|
|
|
|
}
|
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<LoremIpsum>();
|
2020-01-07 10:17:23 +00:00
|
|
|
assert!(help.contains("Dolor sit amet"));
|
|
|
|
assert!(!help.contains("Lorem ipsum"));
|
|
|
|
assert!(help.contains("DO NOT PASS A BAR"));
|
2017-06-15 18:14:16 +00:00
|
|
|
}
|
2018-02-16 22:11:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_line_in_doc_comment_is_double_linefeed() {
|
|
|
|
/// Foo.
|
|
|
|
///
|
|
|
|
/// Bar
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-02-05 11:12:39 +00:00
|
|
|
#[clap(name = "lorem-ipsum")]
|
2018-02-16 22:11:33 +00:00
|
|
|
struct LoremIpsum {}
|
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<LoremIpsum>();
|
2021-12-14 22:05:05 +00:00
|
|
|
assert!(help.starts_with("lorem-ipsum \nFoo.\n\nBar\n\nUSAGE:"));
|
2020-01-07 10:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn field_long_doc_comment_both_help_long_help() {
|
|
|
|
/// Lorem ipsumclap
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, PartialEq, Debug)]
|
2020-01-07 10:17:23 +00:00
|
|
|
#[clap(name = "lorem-ipsum", about = "Dolor sit amet")]
|
|
|
|
struct LoremIpsum {
|
2019-12-21 10:39:56 +00:00
|
|
|
/// Dot is removed from multiline comments.
|
2020-01-07 10:17:23 +00:00
|
|
|
///
|
2019-12-21 10:39:56 +00:00
|
|
|
/// Long help
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(long, action)]
|
2020-01-07 10:17:23 +00:00
|
|
|
foo: bool,
|
2019-12-21 10:39:56 +00:00
|
|
|
|
|
|
|
/// Dot is removed from one short comment.
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(long, action)]
|
2019-12-21 10:39:56 +00:00
|
|
|
bar: bool,
|
2020-01-07 10:17:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let short_help = utils::get_help::<LoremIpsum>();
|
|
|
|
let long_help = utils::get_long_help::<LoremIpsum>();
|
2020-01-07 10:17:23 +00:00
|
|
|
|
2019-12-21 10:39:56 +00:00
|
|
|
assert!(short_help.contains("Dot is removed from one short comment"));
|
|
|
|
assert!(!short_help.contains("Dot is removed from one short comment."));
|
|
|
|
assert!(short_help.contains("Dot is removed from multiline comments"));
|
|
|
|
assert!(!short_help.contains("Dot is removed from multiline comments."));
|
|
|
|
assert!(long_help.contains("Long help"));
|
|
|
|
assert!(!short_help.contains("Long help"));
|
2020-01-07 10:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn top_long_doc_comment_both_help_long_help() {
|
|
|
|
/// Lorem ipsumclap
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug)]
|
2020-01-07 10:17:23 +00:00
|
|
|
#[clap(name = "lorem-ipsum", about = "Dolor sit amet")]
|
|
|
|
struct LoremIpsum {
|
|
|
|
#[clap(subcommand)]
|
|
|
|
foo: SubCommand,
|
|
|
|
}
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug)]
|
2020-01-07 10:17:23 +00:00
|
|
|
pub enum SubCommand {
|
|
|
|
/// DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES
|
|
|
|
///
|
|
|
|
/// Or something else
|
|
|
|
Foo {
|
2022-05-23 20:42:34 +00:00
|
|
|
#[clap(value_parser, help = "foo")]
|
2021-11-04 15:15:04 +00:00
|
|
|
bars: String,
|
2020-01-07 10:17:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let short_help = utils::get_help::<LoremIpsum>();
|
|
|
|
let long_help = utils::get_subcommand_long_help::<LoremIpsum>("foo");
|
2018-02-16 22:11:33 +00:00
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
assert!(!short_help.contains("Or something else"));
|
|
|
|
assert!(long_help.contains("DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES"));
|
|
|
|
assert!(long_help.contains("Or something else"));
|
2018-02-16 22:11:33 +00:00
|
|
|
}
|
2019-12-21 10:39:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verbatim_doc_comment() {
|
|
|
|
/// DANCE!
|
|
|
|
///
|
|
|
|
/// ()
|
|
|
|
/// |
|
|
|
|
/// ( () )
|
|
|
|
/// ) ________ // )
|
|
|
|
/// () |\ \ //
|
|
|
|
/// ( \\__ \ ______\//
|
|
|
|
/// \__) | |
|
|
|
|
/// | | |
|
|
|
|
/// \ | |
|
|
|
|
/// \|_______|
|
|
|
|
/// // \\
|
|
|
|
/// (( ||
|
|
|
|
/// \\ ||
|
|
|
|
/// ( () ||
|
|
|
|
/// ( () ) )
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug)]
|
2019-12-21 10:39:56 +00:00
|
|
|
#[clap(verbatim_doc_comment)]
|
|
|
|
struct SeeFigure1 {
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(long, action)]
|
2019-12-21 10:39:56 +00:00
|
|
|
foo: bool,
|
|
|
|
}
|
|
|
|
|
2021-11-30 15:36:53 +00:00
|
|
|
let help = utils::get_long_help::<SeeFigure1>();
|
2019-12-21 10:39:56 +00:00
|
|
|
let sample = r#"
|
|
|
|
()
|
|
|
|
|
|
|
|
|
( () )
|
|
|
|
) ________ // )
|
|
|
|
() |\ \ //
|
|
|
|
( \\__ \ ______\//
|
|
|
|
\__) | |
|
|
|
|
| | |
|
|
|
|
\ | |
|
|
|
|
\|_______|
|
|
|
|
// \\
|
|
|
|
(( ||
|
|
|
|
\\ ||
|
|
|
|
( () ||
|
|
|
|
( () ) )"#;
|
|
|
|
|
|
|
|
assert!(help.contains(sample))
|
|
|
|
}
|
2020-01-24 13:14:51 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verbatim_doc_comment_field() {
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug)]
|
2022-02-12 03:48:29 +00:00
|
|
|
struct Command {
|
2020-01-24 13:14:51 +00:00
|
|
|
/// This help ends in a period.
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(long, verbatim_doc_comment, action)]
|
2020-01-24 13:14:51 +00:00
|
|
|
foo: bool,
|
|
|
|
/// This help does not end in a period.
|
2022-06-01 15:31:23 +00:00
|
|
|
#[clap(long, action)]
|
2020-01-24 13:14:51 +00:00
|
|
|
bar: bool,
|
|
|
|
}
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let help = utils::get_long_help::<Command>();
|
2020-01-24 13:14:51 +00:00
|
|
|
|
2020-02-05 06:51:31 +00:00
|
|
|
assert!(help.contains("This help ends in a period."));
|
|
|
|
assert!(help.contains("This help does not end in a period"));
|
2020-01-24 13:14:51 +00:00
|
|
|
}
|
2021-10-17 04:05:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiline_separates_default() {
|
|
|
|
#[derive(Parser, Debug)]
|
2022-02-12 03:48:29 +00:00
|
|
|
struct Command {
|
2021-10-17 04:05:35 +00:00
|
|
|
/// Multiline
|
|
|
|
///
|
|
|
|
/// Doc comment
|
2022-05-23 20:42:34 +00:00
|
|
|
#[clap(long, default_value = "x", value_parser)]
|
2021-10-17 04:05:35 +00:00
|
|
|
x: String,
|
|
|
|
}
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
let help = utils::get_long_help::<Command>();
|
2021-10-17 04:05:35 +00:00
|
|
|
assert!(!help.contains("Doc comment [default"));
|
|
|
|
assert!(help.lines().any(|s| s.trim().starts_with("[default")));
|
|
|
|
|
|
|
|
// The short help should still have the default on the same line
|
2022-02-12 03:48:29 +00:00
|
|
|
let help = utils::get_help::<Command>();
|
2021-10-17 04:05:35 +00:00
|
|
|
assert!(help.contains("Multiline [default"));
|
|
|
|
}
|
2021-10-19 15:34:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn argenum_multiline_doc_comment() {
|
|
|
|
#[derive(ArgEnum, Clone)]
|
|
|
|
enum LoremIpsum {
|
|
|
|
/// Multiline
|
|
|
|
///
|
|
|
|
/// Doc comment
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 15:58:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn doc_comment_about_handles_both_abouts() {
|
|
|
|
/// Opts doc comment summary
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
pub struct Opts {
|
|
|
|
#[clap(subcommand)]
|
|
|
|
pub cmd: Sub,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sub doc comment summary
|
|
|
|
///
|
|
|
|
/// Sub doc comment body
|
|
|
|
#[derive(Parser, PartialEq, Eq, Debug)]
|
|
|
|
pub enum Sub {
|
2022-05-23 20:42:34 +00:00
|
|
|
Compress {
|
|
|
|
#[clap(value_parser)]
|
|
|
|
output: String,
|
|
|
|
},
|
2021-12-14 15:58:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:04:07 +00:00
|
|
|
let cmd = Opts::command();
|
2022-02-14 21:47:20 +00:00
|
|
|
assert_eq!(cmd.get_about(), Some("Opts doc comment summary"));
|
2021-12-14 16:03:28 +00:00
|
|
|
// clap will fallback to `about` on `None`. The main care about is not providing a `Sub` doc
|
|
|
|
// comment.
|
2022-02-14 21:47:20 +00:00
|
|
|
assert_eq!(cmd.get_long_about(), None);
|
2021-12-14 15:58:11 +00:00
|
|
|
}
|