Skip to content

Commit 7421844

Browse files
csnoverkevinmehall
authored andcommitted
Run infallible tests natively
trybuild is VERY SLOW and it hides the test content from tooling (clippy, fmt, rust-analyzer, etc.). These tests must compile and pass so there is no reason to run them through trybuild. This change reduces test run time from 8.7s seconds to 0.7s. The recent-rustc tests are still run through trybuild even though they do not need to be simply because this avoids adding a build.rs and build dependency for versioning. Once the MSRV is increased this can also just go away.
1 parent e872b7d commit 7421844

39 files changed

+237
-168
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
extern crate peg;
21
use arithmetic::expression;
32

43
peg::parser!( grammar arithmetic() for str {
@@ -21,6 +20,7 @@ peg::parser!( grammar arithmetic() for str {
2120
= n:$(['0'..='9']+) { n.parse().unwrap() }
2221
});
2322

23+
#[test]
2424
fn main() {
2525
assert_eq!(expression("1+1"), Ok(2));
2626
assert_eq!(expression("5*5"), Ok(25));
Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
extern crate peg;
21
use peg::parser;
32

43
#[derive(Clone, PartialEq, Eq, Debug)]
@@ -8,7 +7,7 @@ pub enum Expression {
87
Product(Box<Expression>, Box<Expression>),
98
}
109

11-
parser!{
10+
parser! {
1211
/// Doc comment
1312
grammar arithmetic() for str {
1413
/// Top level parser rule
@@ -34,29 +33,42 @@ grammar arithmetic() for str {
3433
= n:$(['0'..='9']+) { Expression::Number(n.parse().unwrap()) }
3534
}}
3635

36+
#[test]
3737
fn main() {
38-
assert_eq!(arithmetic::expression("1+1"), Ok(Expression::Sum(
39-
Box::new(Expression::Number(1)),
40-
Box::new(Expression::Number(1)))
41-
));
42-
assert_eq!(arithmetic::expression("5*5"), Ok(Expression::Product(
43-
Box::new(Expression::Number(5)),
44-
Box::new(Expression::Number(5)))
45-
));
46-
assert_eq!(arithmetic::expression("2+3*4"), Ok(Expression::Sum(
47-
Box::new(Expression::Number(2)),
48-
Box::new(Expression::Product(
49-
Box::new(Expression::Number(3)),
50-
Box::new(Expression::Number(4))
51-
)),
52-
)));
53-
assert_eq!(arithmetic::expression("(2+3) * 4"), Ok(Expression::Product(
54-
Box::new(Expression::Sum(
38+
assert_eq!(
39+
arithmetic::expression("1+1"),
40+
Ok(Expression::Sum(
41+
Box::new(Expression::Number(1)),
42+
Box::new(Expression::Number(1))
43+
))
44+
);
45+
assert_eq!(
46+
arithmetic::expression("5*5"),
47+
Ok(Expression::Product(
48+
Box::new(Expression::Number(5)),
49+
Box::new(Expression::Number(5))
50+
))
51+
);
52+
assert_eq!(
53+
arithmetic::expression("2+3*4"),
54+
Ok(Expression::Sum(
5555
Box::new(Expression::Number(2)),
56-
Box::new(Expression::Number(3)),
57-
)),
58-
Box::new(Expression::Number(4))
59-
)));
56+
Box::new(Expression::Product(
57+
Box::new(Expression::Number(3)),
58+
Box::new(Expression::Number(4))
59+
)),
60+
))
61+
);
62+
assert_eq!(
63+
arithmetic::expression("(2+3) * 4"),
64+
Ok(Expression::Product(
65+
Box::new(Expression::Sum(
66+
Box::new(Expression::Number(2)),
67+
Box::new(Expression::Number(3)),
68+
)),
69+
Box::new(Expression::Number(4))
70+
))
71+
);
6072
assert!(arithmetic::expression("(22+)+1").is_err());
6173
assert!(arithmetic::expression("1++1").is_err());
6274
assert!(arithmetic::expression("3)+1").is_err());
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
extern crate peg;
3-
41
peg::parser!( grammar arithmetic() for str {
52
rule number() -> i64
63
= n:$(['0'..='9']+) { n.parse().unwrap() }
@@ -21,11 +18,12 @@ peg::parser!( grammar arithmetic() for str {
2118
}
2219
});
2320

21+
#[test]
2422
fn main() {
2523
assert_eq!(arithmetic::calculate("3+3*3+3"), Ok(15));
2624
assert_eq!(arithmetic::calculate("2+2^2^2^2/2+2"), Ok(32772));
2725
assert_eq!(arithmetic::calculate("1024/2/2/2+1"), Ok(129));
2826
assert_eq!(arithmetic::calculate("1024/(1+1)/2/2+1"), Ok(129));
2927
assert_eq!(arithmetic::calculate("-1-2*-2"), Ok(3));
3028
assert_eq!(arithmetic::calculate("1+3!+1"), Ok(8));
31-
}
29+
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate peg;
2-
31
peg::parser!( grammar arithmetic() for str {
42
rule ident() -> &'input str = $(['a'..='z']+)
53
rule haskell_op() -> String = "`" i:ident() "`" [' '|'\n']* { i.to_owned() }
@@ -18,17 +16,20 @@ peg::parser!( grammar arithmetic() for str {
1816
pub enum InfixAst {
1917
Ident(String),
2018
Add(Box<InfixAst>, Box<InfixAst>),
21-
Op(String, Box<InfixAst>, Box<InfixAst>)
19+
Op(String, Box<InfixAst>, Box<InfixAst>),
2220
}
2321

24-
fn main(){
25-
assert_eq!(arithmetic::expression("a + b `x` c").unwrap(),
22+
#[test]
23+
fn main() {
24+
assert_eq!(
25+
arithmetic::expression("a + b `x` c").unwrap(),
2626
InfixAst::Add(
2727
Box::new(InfixAst::Ident("a".to_owned())),
28-
Box::new(InfixAst::Op("x".to_owned(),
28+
Box::new(InfixAst::Op(
29+
"x".to_owned(),
2930
Box::new(InfixAst::Ident("b".to_owned())),
3031
Box::new(InfixAst::Ident("c".to_owned()))
3132
))
3233
)
3334
)
34-
}
35+
}

tests/run-pass/arithmetic_infix_ast_span.rs renamed to tests/pass/arithmetic_infix_ast_span.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate peg;
2-
31
peg::parser!( grammar arithmetic() for str {
42
rule ident() -> &'input str = $(['a'..='z']+)
53

@@ -28,8 +26,10 @@ pub enum Op {
2826
Mul(Box<Node>, Box<Node>),
2927
}
3028

31-
fn main(){
32-
assert_eq!(arithmetic::expression("a+b*c").unwrap(),
29+
#[test]
30+
fn main() {
31+
assert_eq!(
32+
arithmetic::expression("a+b*c").unwrap(),
3333
Node {
3434
start: 0,
3535
end: 5,
@@ -58,4 +58,4 @@ fn main(){
5858
)
5959
}
6060
);
61-
}
61+
}

tests/run-pass/arithmetic_with_left_recursion.rs renamed to tests/pass/arithmetic_with_left_recursion.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate peg;
2-
31
use arithmetic::sum;
42

53
peg::parser!( grammar arithmetic() for str {
@@ -12,6 +10,7 @@ peg::parser!( grammar arithmetic() for str {
1210
= n:$(['0'..='9']+) { n.parse().unwrap() }
1311
});
1412

13+
#[test]
1514
fn main() {
1615
assert_eq!(sum("1"), Ok(1));
1716
assert_eq!(sum("1+1"), Ok(2));

tests/run-pass/assembly_ast_dyn_type_param_bounds.rs renamed to tests/pass/assembly_ast_dyn_type_param_bounds.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extern crate peg;
1+
#![allow(unused)]
22
use peg::parser;
33

44
// C++ in Rust
@@ -13,14 +13,12 @@ trait AsDynOperand<'a> {
1313
fn as_dyn_operand(self: Box<Self>) -> Box<dyn Operand<'a> + 'a>;
1414
}
1515

16-
impl<'a, T: /* Sized + */ Operand<'a> + 'a> AsDynOperand<'a> for T {
16+
impl<'a, T: Operand<'a> + 'a> AsDynOperand<'a> for T {
1717
fn as_dyn_operand(self: Box<Self>) -> Box<dyn Operand<'a> + 'a> {
1818
self
1919
}
2020
}
2121

22-
23-
2422
#[derive(Debug)]
2523
pub struct Program<'a>(Vec<Box<dyn Operation<'a> + 'a>>);
2624

@@ -52,7 +50,7 @@ impl<'a> Location<'a> for Global<'a> {}
5250
struct Literal(i32);
5351
impl<'a> Operand<'a> for Literal {}
5452

55-
parser!{
53+
parser! {
5654
grammar assembly() for str {
5755
pub rule program() -> Program<'input>
5856
= op:operation() ** "\n" { Program(op) }
@@ -82,7 +80,7 @@ grammar assembly() for str {
8280

8381
rule operand() -> Box<dyn Operand<'input> + 'input>
8482
= l:location() {l.as_dyn_operand()} / l:literal() {Box::new(l)}
85-
83+
8684
rule literal() -> Literal
8785
= n:$(['0'..='9']+) {?
8886
let n = n.parse::<i32>().map_err(|_| "invalid int literal")?;
@@ -91,31 +89,35 @@ grammar assembly() for str {
9189

9290
}}
9391

92+
#[test]
9493
fn main() {
95-
let parsed = assembly::program("%apple = add 1 @g
94+
let parsed = assembly::program(
95+
"%apple = add 1 @g
9696
@b = add 2 %a
9797
%c = sub 82 @b
98-
@dog = sub @b 12").unwrap();
98+
@dog = sub @b 12",
99+
)
100+
.unwrap();
99101
let expected = Program(vec![
100-
Box::new(Add{
102+
Box::new(Add {
101103
result: Box::new(Register("apple")),
102104
lhs: Box::new(Literal(1)),
103-
rhs: Box::new(Global("g"))
105+
rhs: Box::new(Global("g")),
104106
}),
105-
Box::new(Add{
107+
Box::new(Add {
106108
result: Box::new(Global("b")),
107109
lhs: Box::new(Literal(2)),
108-
rhs: Box::new(Register("a"))
110+
rhs: Box::new(Register("a")),
109111
}),
110-
Box::new(Sub{
112+
Box::new(Sub {
111113
result: Box::new(Register("c")),
112114
lhs: Box::new(Literal(82)),
113-
rhs: Box::new(Global("b"))
115+
rhs: Box::new(Global("b")),
114116
}),
115-
Box::new(Sub{
117+
Box::new(Sub {
116118
result: Box::new(Global("dog")),
117119
lhs: Box::new(Global("b")),
118-
rhs: Box::new(Literal(12))
120+
rhs: Box::new(Literal(12)),
119121
}),
120122
]);
121123
assert_eq!(format!("{parsed:?}"), format!("{expected:?}"));
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
extern crate peg;
2-
31
peg::parser!(grammar borrows() for str {
42
use std::borrow::{ToOwned, Cow};
5-
3+
64
pub rule borrowed() -> &'input str
75
= $(['a'..='z']+)
86

@@ -13,8 +11,9 @@ peg::parser!(grammar borrows() for str {
1311

1412
use self::borrows::*;
1513

14+
#[test]
1615
fn main() {
1716
assert_eq!(borrowed("abcd"), Ok("abcd"));
1817
assert_eq!(&*lifetime_parameter("abcd").unwrap(), "abcd");
1918
assert_eq!(&*lifetime_parameter("COW").unwrap(), "cow");
20-
}
19+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
extern crate peg;
21
use peg::parser;
32

4-
parser!{
3+
parser! {
54
grammar byteparser() for [u8] {
65
pub rule commands() -> Vec<&'input[u8]> = command()*
76
rule command() -> &'input [u8] = ">" val:$([b' ' ..= b'~']+) [0] { val }
87
}
98
}
109

10+
#[test]
1111
fn main() {
12-
assert_eq!(byteparser::commands(b">asdf\0>xyz\0"), Ok(vec![&b"asdf"[..], &b"xyz"[..]]));
12+
assert_eq!(
13+
byteparser::commands(b">asdf\0>xyz\0"),
14+
Ok(vec![&b"asdf"[..], &b"xyz"[..]])
15+
);
1316
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate peg;
2-
31
peg::parser!( grammar parse() for str {
42

53
pub rule dec_byte() -> u8
@@ -37,6 +35,7 @@ peg::parser!( grammar parse() for str {
3735
}
3836
});
3937

38+
#[test]
4039
fn main() {
4140
assert_eq!(parse::dec_byte("0"), Ok(0));
4241
assert_eq!(parse::dec_byte("255"), Ok(255));
@@ -49,7 +48,15 @@ fn main() {
4948
assert!(parse::xml("<a><b><c></b></c></a>").is_err());
5049
assert!(parse::xml("<a><b></c><c></b></a>").is_err());
5150

52-
assert!(parse::return_early("a").unwrap_err().expected.tokens().any(|e| e == "number"));
53-
assert!(parse::return_early("123").unwrap_err().expected.tokens().any(|e| e == "smaller number"));
51+
assert!(parse::return_early("a")
52+
.unwrap_err()
53+
.expected
54+
.tokens()
55+
.any(|e| e == "number"));
56+
assert!(parse::return_early("123")
57+
.unwrap_err()
58+
.expected
59+
.tokens()
60+
.any(|e| e == "smaller number"));
5461
assert_eq!(parse::return_early("99").unwrap(), 99);
5562
}

0 commit comments

Comments
 (0)