1// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>, 2// Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and 3// Ana Hobden (@hoverbear) <operator@hoverbear.org> 4// 5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 8// option. This file may not be copied, modified, or distributed 9// except according to those terms. 10// 11// This work was derived from Structopt (https://github.com/TeXitoi/structopt) 12// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the 13// MIT/Apache 2.0 license. 14 15use clap::Parser; 16 17#[test] 18fn basic() { 19 #[derive(Parser, PartialEq, Debug)] 20 struct Opt { 21 #[arg(short = 'a', long = "arg")] 22 arg: i32, 23 } 24 assert_eq!( 25 Opt { arg: 24 }, 26 Opt::try_parse_from(["test", "-a24"]).unwrap() 27 ); 28} 29 30#[test] 31fn update_basic() { 32 #[derive(Parser, PartialEq, Debug)] 33 struct Opt { 34 #[arg(short, long)] 35 first: i32, 36 #[arg(short, long)] 37 second: i32, 38 } 39 40 let mut opt = Opt::try_parse_from(["test", "-f0", "-s1"]).unwrap(); 41 42 opt.try_update_from(["test", "-f42"]).unwrap(); 43 44 assert_eq!( 45 Opt { 46 first: 42, 47 second: 1 48 }, 49 opt 50 ); 51} 52 53#[test] 54fn update_explicit_required() { 55 #[derive(Parser, PartialEq, Debug)] 56 struct Opt { 57 #[arg(short, long, required = true)] 58 first: i32, 59 #[arg(short, long, required = true)] 60 second: i32, 61 } 62 63 let mut opt = Opt::try_parse_from(["test", "-f0", "-s1"]).unwrap(); 64 65 opt.try_update_from(["test", "-f42"]).unwrap(); 66 67 assert_eq!( 68 Opt { 69 first: 42, 70 second: 1 71 }, 72 opt 73 ); 74} 75 76#[test] 77fn unit_struct() { 78 #[derive(Parser, PartialEq, Debug)] 79 struct Opt; 80 81 assert_eq!(Opt {}, Opt::try_parse_from(["test"]).unwrap()); 82} 83