Skip to content

Commit

Permalink
Use trybuild for testing expected proc macro errors (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
sphynx authored and TeXitoi committed Jun 19, 2019
1 parent 2a36783 commit dd30d08
Show file tree
Hide file tree
Showing 60 changed files with 984 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ travis-ci = { repository = "TeXitoi/structopt" }
clap = { version = "2.33", default-features = false }
structopt-derive = { path = "structopt-derive", version = "0.3.0" }

[dev-dependencies]
trybuild = "1.0.5"

[workspace]
12 changes: 12 additions & 0 deletions tests/macro-errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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

#[test]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
}
23 changes: 23 additions & 0 deletions tests/ui/bool_default_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(short, default_value = true)]
b: bool,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/bool_default_value.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/bool_default_value.rs:13:10
|
13 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: default_value is meaningless for bool
23 changes: 23 additions & 0 deletions tests/ui/bool_required.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(short, required = true)]
b: bool,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/bool_required.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/bool_required.rs:13:10
|
13 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: required is meaningless for bool
32 changes: 32 additions & 0 deletions tests/ui/flatten_and_doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt)]
struct DaemonOpts {
#[structopt(short)]
user: String,
#[structopt(short)]
group: String,
}

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
/// Opts.
#[structopt(flatten)]
opts: DaemonOpts,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/flatten_and_doc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/flatten_and_doc.rs:21:10
|
21 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: methods and doc comments are not allowed for flattened entry
31 changes: 31 additions & 0 deletions tests/ui/flatten_and_methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt)]
struct DaemonOpts {
#[structopt(short)]
user: String,
#[structopt(short)]
group: String,
}

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(short, flatten)]
opts: DaemonOpts,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/flatten_and_methods.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/flatten_and_methods.rs:21:10
|
21 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: methods and doc comments are not allowed for flattened entry
31 changes: 31 additions & 0 deletions tests/ui/flatten_and_parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt)]
struct DaemonOpts {
#[structopt(short)]
user: String,
#[structopt(short)]
group: String,
}

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(flatten, parse(from_occurrences))]
opts: DaemonOpts,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/flatten_and_parse.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/flatten_and_parse.rs:21:10
|
21 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: parse attribute is not allowed for flattened entry
24 changes: 24 additions & 0 deletions tests/ui/literal_list_element.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(test(1, 2))]
debug: bool,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}

7 changes: 7 additions & 0 deletions tests/ui/literal_list_element.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/literal_list_element.rs:13:10
|
13 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: unsupported option: test ( 1 , 2 )
23 changes: 23 additions & 0 deletions tests/ui/non_existent_attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(short, non_existing_attribute = 1)]
debug: bool,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/non_existent_attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error[E0599]: no method named `non_existing_attribute` found for type `structopt::clap::Arg<'_, '_>` in the current scope
--> $DIR/non_existent_attr.rs:13:10
|
13 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^

For more information about this error, try `rustc --explain E0599`.
24 changes: 24 additions & 0 deletions tests/ui/non_literal_attr_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(short, aliases = &["debug", "dbg"])]
debug: bool,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}

7 changes: 7 additions & 0 deletions tests/ui/non_literal_attr_value.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/non_literal_attr_value.rs:13:10
|
13 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: invalid structopt syntax: expected literal: # [ structopt ( short , aliases = & [ "debug" , "dbg" ] ) ]
22 changes: 22 additions & 0 deletions tests/ui/opt_opt_nonpositional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
n: Option<Option<u32>>,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/opt_opt_nonpositional.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/opt_opt_nonpositional.rs:13:10
|
13 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: Option<Option<T>> type is meaningless for positional argument
22 changes: 22 additions & 0 deletions tests/ui/opt_vec_nonpositional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
n: Option<Vec<u32>>,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/opt_vec_nonpositional.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/opt_vec_nonpositional.rs:13:10
|
13 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: Option<Vec<T>> type is meaningless for positional argument
23 changes: 23 additions & 0 deletions tests/ui/option_default_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>
//
// 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.

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
struct Opt {
#[structopt(short, default_value = 1)]
n: Option<u32>,
}

fn main() {
let opt = Opt::from_args();
println!("{:?}", opt);
}
7 changes: 7 additions & 0 deletions tests/ui/option_default_value.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: proc-macro derive panicked
--> $DIR/option_default_value.rs:13:10
|
13 | #[derive(StructOpt, Debug)]
| ^^^^^^^^^
|
= help: message: default_value is meaningless for Option
Loading

0 comments on commit dd30d08

Please sign in to comment.