2018-07-02 17:41:01 +00:00
|
|
|
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
|
|
|
|
// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
|
|
|
|
// Andrew Hobden (@hoverbear) <andrew@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
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
mod utils;
|
2017-06-15 18:14:16 +00:00
|
|
|
|
2018-07-02 19:06:46 +00:00
|
|
|
use clap::Clap;
|
2020-01-07 10:17:23 +00:00
|
|
|
use utils::*;
|
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
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, 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
|
2020-01-07 10:17:23 +00:00
|
|
|
#[clap(short, long)]
|
2017-06-15 18:14:16 +00:00
|
|
|
foo: bool,
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
let help = get_long_help::<LoremIpsum>();
|
|
|
|
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
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
#[clap(name = "lorem-ipsum", about = "Dolor sit amet")]
|
2017-06-15 18:14:16 +00:00
|
|
|
struct LoremIpsum {
|
|
|
|
/// Fooify a bar
|
2020-01-07 10:17:23 +00:00
|
|
|
#[clap(short, long, help = "DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES")]
|
2017-06-15 18:14:16 +00:00
|
|
|
foo: bool,
|
|
|
|
}
|
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
let help = get_long_help::<LoremIpsum>();
|
|
|
|
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
|
2018-07-02 19:06:46 +00:00
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
2020-01-07 10:17:23 +00:00
|
|
|
#[clap(name = "lorem-ipsum", no_version)]
|
2018-02-16 22:11:33 +00:00
|
|
|
struct LoremIpsum {}
|
|
|
|
|
2020-01-07 10:17:23 +00:00
|
|
|
let help = get_long_help::<LoremIpsum>();
|
|
|
|
assert!(help.starts_with("lorem-ipsum \nFoo.\n\nBar\n\nUSAGE:"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn field_long_doc_comment_both_help_long_help() {
|
|
|
|
/// Lorem ipsumclap
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
#[clap(name = "lorem-ipsum", about = "Dolor sit amet")]
|
|
|
|
struct LoremIpsum {
|
|
|
|
/// DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES.
|
|
|
|
///
|
|
|
|
/// Or something else
|
|
|
|
#[clap(long)]
|
|
|
|
foo: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
let short_help = get_help::<LoremIpsum>();
|
|
|
|
let long_help = get_long_help::<LoremIpsum>();
|
|
|
|
|
|
|
|
assert!(short_help.contains("CIRCUMSTANCES"));
|
|
|
|
assert!(!short_help.contains("CIRCUMSTANCES."));
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn top_long_doc_comment_both_help_long_help() {
|
|
|
|
/// Lorem ipsumclap
|
|
|
|
#[derive(Clap, Debug)]
|
|
|
|
#[clap(name = "lorem-ipsum", about = "Dolor sit amet")]
|
|
|
|
struct LoremIpsum {
|
|
|
|
#[clap(subcommand)]
|
|
|
|
foo: SubCommand,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clap, Debug)]
|
|
|
|
pub enum SubCommand {
|
|
|
|
/// DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES
|
|
|
|
///
|
|
|
|
/// Or something else
|
|
|
|
Foo {
|
|
|
|
#[clap(help = "foo")]
|
|
|
|
bars: Vec<String>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
let short_help = get_help::<LoremIpsum>();
|
|
|
|
let long_help = 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
|
|
|
}
|