Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed May 25, 2022
1 parent 03cc60a commit 9c3d283
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/test/ui/macros/assert-trailing-junk.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: with-generic-asset without-generic-asset
// [with-generic-asset] compile-flags: --cfg feature="generic_assert"

// Ensure assert macro does not ignore trailing garbage.
//
// See https://github.com/rust-lang/rust/issues/60024 for details.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
error: expected one of `,`, `.`, `?`, or an operator, found `some`
--> $DIR/assert-trailing-junk.rs:6:18
--> $DIR/assert-trailing-junk.rs:9:18
|
LL | assert!(true some extra junk, "whatever");
| ^^^^ expected one of `,`, `.`, `?`, or an operator

error: expected one of `,`, `.`, `?`, or an operator, found `some`
--> $DIR/assert-trailing-junk.rs:9:18
--> $DIR/assert-trailing-junk.rs:12:18
|
LL | assert!(true some extra junk);
| ^^^^ expected one of `,`, `.`, `?`, or an operator

error: no rules expected the token `blah`
--> $DIR/assert-trailing-junk.rs:12:30
--> $DIR/assert-trailing-junk.rs:15:30
|
LL | assert!(true, "whatever" blah);
| -^^^^ no rules expected this token in macro call
| |
| help: missing comma here

error: unexpected string literal
--> $DIR/assert-trailing-junk.rs:15:18
--> $DIR/assert-trailing-junk.rs:18:18
|
LL | assert!(true "whatever" blah);
| -^^^^^^^^^^
| |
| help: try adding a comma

error: no rules expected the token `blah`
--> $DIR/assert-trailing-junk.rs:15:29
--> $DIR/assert-trailing-junk.rs:18:29
|
LL | assert!(true "whatever" blah);
| -^^^^ no rules expected this token in macro call
| |
| help: missing comma here

error: macro requires an expression as an argument
--> $DIR/assert-trailing-junk.rs:19:5
--> $DIR/assert-trailing-junk.rs:22:5
|
LL | assert!(true;);
| ^^^^^^^^^^^^-^
| |
| help: try removing semicolon

error: unexpected string literal
--> $DIR/assert-trailing-junk.rs:22:27
--> $DIR/assert-trailing-junk.rs:25:27
|
LL | assert!(false || true "error message");
| -^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
error: expected one of `,`, `.`, `?`, or an operator, found `some`
--> $DIR/assert-trailing-junk.rs:9:18
|
LL | assert!(true some extra junk, "whatever");
| ^^^^ expected one of `,`, `.`, `?`, or an operator

error: expected one of `,`, `.`, `?`, or an operator, found `some`
--> $DIR/assert-trailing-junk.rs:12:18
|
LL | assert!(true some extra junk);
| ^^^^ expected one of `,`, `.`, `?`, or an operator

error: no rules expected the token `blah`
--> $DIR/assert-trailing-junk.rs:15:30
|
LL | assert!(true, "whatever" blah);
| -^^^^ no rules expected this token in macro call
| |
| help: missing comma here

error: unexpected string literal
--> $DIR/assert-trailing-junk.rs:18:18
|
LL | assert!(true "whatever" blah);
| -^^^^^^^^^^
| |
| help: try adding a comma

error: no rules expected the token `blah`
--> $DIR/assert-trailing-junk.rs:18:29
|
LL | assert!(true "whatever" blah);
| -^^^^ no rules expected this token in macro call
| |
| help: missing comma here

error: macro requires an expression as an argument
--> $DIR/assert-trailing-junk.rs:22:5
|
LL | assert!(true;);
| ^^^^^^^^^^^^-^
| |
| help: try removing semicolon

error: unexpected string literal
--> $DIR/assert-trailing-junk.rs:25:27
|
LL | assert!(false || true "error message");
| -^^^^^^^^^^^^^^^
| |
| help: try adding a comma

error: aborting due to 7 previous errors

3 changes: 3 additions & 0 deletions src/test/ui/macros/assert.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// revisions: with-generic-asset without-generic-asset
// [with-generic-asset] compile-flags: --cfg feature="generic_assert"

fn main() {
assert!(); //~ ERROR requires a boolean expression
assert!(struct); //~ ERROR expected expression
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
error: macro requires a boolean expression as an argument
--> $DIR/assert.rs:2:5
--> $DIR/assert.rs:5:5
|
LL | assert!();
| ^^^^^^^^^ boolean expression required

error: expected expression, found keyword `struct`
--> $DIR/assert.rs:3:13
--> $DIR/assert.rs:6:13
|
LL | assert!(struct);
| ^^^^^^ expected expression

error: macro requires a boolean expression as an argument
--> $DIR/assert.rs:4:5
--> $DIR/assert.rs:7:5
|
LL | debug_assert!();
| ^^^^^^^^^^^^^^^ boolean expression required
|
= note: this error originates in the macro `debug_assert` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected expression, found keyword `struct`
--> $DIR/assert.rs:5:19
--> $DIR/assert.rs:8:19
|
LL | debug_assert!(struct);
| ^^^^^^ expected expression
Expand Down
28 changes: 28 additions & 0 deletions src/test/ui/macros/assert.without-generic-asset.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error: macro requires a boolean expression as an argument
--> $DIR/assert.rs:5:5
|
LL | assert!();
| ^^^^^^^^^ boolean expression required

error: expected expression, found keyword `struct`
--> $DIR/assert.rs:6:13
|
LL | assert!(struct);
| ^^^^^^ expected expression

error: macro requires a boolean expression as an argument
--> $DIR/assert.rs:7:5
|
LL | debug_assert!();
| ^^^^^^^^^^^^^^^ boolean expression required
|
= note: this error originates in the macro `debug_assert` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected expression, found keyword `struct`
--> $DIR/assert.rs:8:19
|
LL | debug_assert!(struct);
| ^^^^^^ expected expression

error: aborting due to 4 previous errors

188 changes: 188 additions & 0 deletions src/test/ui/rfc-2011-nicer-assert-messages/all-expr-kinds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// aux-build:common.rs
// edition:2021
// ignore-tidy-linelength
// run-pass

#![allow(path_statements, unused_allocation)]
#![feature(box_syntax, core_intrinsics, generic_assert, generic_assert_internals)]

extern crate common;

// Use common::test once https://github.com/rust-lang/rust/issues/96997 is fixed
macro_rules! test {
(
let mut $elem_ident:ident = $elem_expr:expr;
[ $($assert:tt)* ] => $msg:literal
) => {
{
#[allow(unused_assignments, unused_mut, unused_variables)]
let rslt = std::panic::catch_unwind(|| {
let mut $elem_ident = $elem_expr;
assert!($($assert)*);
});
let err = rslt.unwrap_err();
if let Some(elem) = err.downcast_ref::<String>() {
assert_eq!(elem, &$msg);
}
else if let Some(elem) = err.downcast_ref::<&str>() {
assert_eq!(elem, &$msg);
}
else {
panic!("assert!( ... ) should return a string");
}
}
}
}

macro_rules! tests {
(
let mut $elem_ident:ident = $elem_expr:expr;

$(
[ $($elem_assert:tt)* ] => $elem_msg:literal
)+
) => {
$(
test!(
let mut $elem_ident = $elem_expr;
[ $($elem_assert)* ] => $elem_msg
);
)+
}
}

const FOO: Foo = Foo { bar: 1 };

#[derive(Clone, Copy, Debug, PartialEq)]
struct Foo {
bar: i32
}

impl Foo {
fn add(&self, a: i32, b: i32) -> i32 { a + b }
}

fn add(a: i32, b: i32) -> i32 { a + b }

fn main() {
// ***** Allowed *****

tests!(
let mut elem = 1i32;

// addr of
[ &elem == &3 ] => "Assertion failed: &elem == &3\nWith captures:\n elem = 1\n"

// array
[ [elem][0] == 3 ] => "Assertion failed: [elem][0] == 3\nWith captures:\n elem = 1\n"

// binary
[ elem + 1 == 3 ] => "Assertion failed: elem + 1 == 3\nWith captures:\n elem = 1\n"

// call
[ add(elem, elem) == 3 ] => "Assertion failed: add(elem, elem) == 3\nWith captures:\n elem = 1\n"

// cast
[ elem as i32 == 3 ] => "Assertion failed: elem as i32 == 3\nWith captures:\n elem = 1\n"

// index
[ [1i32, 1][elem as usize] == 3 ] => "Assertion failed: [1i32, 1][elem as usize] == 3\nWith captures:\n elem = 1\n"

// method call
[ FOO.add(elem, elem) == 3 ] => "Assertion failed: FOO.add(elem, elem) == 3\nWith captures:\n elem = 1\n"

// paren
[ (elem) == 3 ] => "Assertion failed: (elem) == 3\nWith captures:\n elem = 1\n"

// range
[ (0..elem) == (0..3) ] => "Assertion failed: (0..elem) == (0..3)\nWith captures:\n elem = 1\n"

// repeat
[ [elem; 1] == [3; 1] ] => "Assertion failed: [elem; 1] == [3; 1]\nWith captures:\n elem = 1\n"

// struct
[ Foo { bar: elem } == Foo { bar: 3 } ] => "Assertion failed: Foo { bar: elem } == Foo { bar: 3 }\nWith captures:\n elem = 1\n"

// tuple
[ (elem, 1) == (3, 3) ] => "Assertion failed: (elem, 1) == (3, 3)\nWith captures:\n elem = 1\n"

// unary
[ -elem == -3 ] => "Assertion failed: -elem == -3\nWith captures:\n elem = 1\n"
);

// ***** Disallowed *****

tests!(
let mut elem = 1i32;

// assign
[ { let local = elem; local } == 3 ] => "Assertion failed: { let local = elem; local } == 3"

// assign op
[ { elem += 1; elem } == 3 ] => "Assertion failed: { elem += 1; elem } == 3"

// async
[ { let _ = async { elem }; elem } == 3 ] => "Assertion failed: { let _ = async { elem }; elem } == 3"

// await

// block
[ { elem } == 3 ] => "Assertion failed: { elem } == 3"

// box
[ box elem == box 3 ] => "Assertion failed: box elem == box 3"

// break
[ loop { break elem; } == 3 ] => "Assertion failed: loop { break elem; } == 3"

// closure
[(|| elem)() == 3 ] => "Assertion failed: (|| elem)() == 3"

// const block

// continue

// err

// field
[ FOO.bar == 3 ] => "Assertion failed: FOO.bar == 3"

// for loop
[ { for _ in 0..elem { elem; } elem } == 3 ] => "Assertion failed: { for _ in 0..elem { elem; } elem } == 3"

// if
[ if true { elem } else { elem } == 3 ] => "Assertion failed: if true { elem } else { elem } == 3"

// inline asm

// let
[ if let true = true { elem } else { elem } == 3 ] => "Assertion failed: if let true = true { elem } else { elem } == 3"

// lit

// loop
[ loop { elem; break elem; } == 3 ] => "Assertion failed: loop { elem; break elem; } == 3"

// mac call

// match
[ match elem { _ => elem } == 3 ] => "Assertion failed: match elem { _ => elem, } == 3"

// ret
[ (|| { return elem; })() == 3 ] => "Assertion failed: (|| { return elem; })() == 3"

// try
[ (|| { Some(Some(elem)?) })() == Some(3) ] => "Assertion failed: (|| { Some(Some(elem)?) })() == Some(3)"

// try block

// underscore

// while
[ { while false { elem; break; } elem } == 3 ] => "Assertion failed: { while false { elem; break; } elem } == 3"

// yeet

// yield
);
}
Loading

0 comments on commit 9c3d283

Please sign in to comment.