| 1 |
use cfg_expr::{ |
| 2 |
Expression, ParseError, |
| 3 |
error::Reason, |
| 4 |
expr::{Predicate as P, TargetPredicate as TP}, |
| 5 |
targets::*, |
| 6 |
}; |
| 7 |
|
| 8 |
macro_rules! test_validate { |
| 9 |
(ok [$($text:expr => [$($expected:expr),*$(,)?]),+$(,)?]) => { |
| 10 |
$( |
| 11 |
let val_expr = Expression::parse($text).unwrap(); |
| 12 |
let mut preds = val_expr.predicates().enumerate(); |
| 13 |
|
| 14 |
$( |
| 15 |
let actual = preds.next().unwrap(); |
| 16 |
|
| 17 |
similar_asserts::assert_eq!($expected, actual.1, "failed @ index {}", actual.0); |
| 18 |
)* |
| 19 |
|
| 20 |
if let Some((_, additional)) = preds.next() { |
| 21 |
assert!(false, "found additional requirement {:?}", additional); |
| 22 |
} |
| 23 |
)+ |
| 24 |
}; |
| 25 |
} |
| 26 |
|
| 27 |
macro_rules! err { |
| 28 |
($text:expr => $reason:ident @ $range:expr) => { |
| 29 |
let act_err = Expression::parse($text).unwrap_err(); |
| 30 |
|
| 31 |
let expected = ParseError { |
| 32 |
original: $text.to_owned(), |
| 33 |
span: $range, |
| 34 |
reason: Reason::$reason, |
| 35 |
}; |
| 36 |
|
| 37 |
similar_asserts::assert_eq!(expected, act_err); |
| 38 |
}; |
| 39 |
|
| 40 |
($text:expr => $unexpected:expr; $range:expr) => { |
| 41 |
let act_err = Expression::parse($text).unwrap_err(); |
| 42 |
|
| 43 |
let expected = ParseError { |
| 44 |
original: $text.to_owned(), |
| 45 |
span: $range, |
| 46 |
reason: Reason::Unexpected($unexpected), |
| 47 |
}; |
| 48 |
|
| 49 |
similar_asserts::assert_eq!(expected, act_err); |
| 50 |
}; |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn fails_empty() { |
| 55 |
err!("" => Empty @ 0..0); |
| 56 |
err!(" " => Empty @ 0..1); |
| 57 |
err!("\n\t\n" => Empty @ 0..3); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn fails_malformed() { |
| 62 |
err!("," => &["<key>", "all", "any", "not"]; 0..1); |
| 63 |
// Keys can't begin with a number |
| 64 |
err!("8" => &["<key>", "all", "any", "not"]; 0..1); |
| 65 |
err!("=" => &["<key>", "all", "any", "not"]; 0..1); |
| 66 |
err!("(" => &["<key>", "all", "any", "not"]; 0..1); |
| 67 |
err!("key =" => &["\"<value>\""]; 5..5); |
| 68 |
err!("key1, key2" => MultipleRootPredicates @ 0..10); |
| 69 |
err!("key1, key2, " => MultipleRootPredicates @ 0..16); |
| 70 |
err!("key1 = \"v\", key2" => MultipleRootPredicates @ 0..16); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn fails_unbalanced_parens() { |
| 75 |
err!("not(key" => UnclosedParens @ 3..7); |
| 76 |
err!("key)" => UnopenedParens @ 3..4); |
| 77 |
err!("foo (" => &["=", ",", ")"]; 4..5); |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn fails_unbalanced_quotes() { |
| 82 |
err!("key = \"value" => UnclosedQuotes @ 6..12); |
| 83 |
err!("key = \"" => UnclosedQuotes @ 6..7); |
| 84 |
err!("key = \"value, key = \"value\"" => &[",", ")"]; 21..26); |
| 85 |
err!("all(key = \"value), key = \"value\"" => &[",", ")"]; 26..31); |
| 86 |
err!("not(key = \"value)" => UnclosedQuotes @ 10..17); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
#[allow(clippy::cognitive_complexity)] |
| 91 |
fn handles_single_predicate() { |
| 92 |
test_validate!(ok [ |
| 93 |
"cfg(key)" => [P::Flag("key")], |
| 94 |
"unix" => [P::Target(TP::Family(Family::unix))], |
| 95 |
"target_arch = \"mips\"" => [P::Target(TP::Arch(Arch::mips))], |
| 96 |
"feature = \"awesome\"" => [P::Feature("awesome")], |
| 97 |
"_key" => [P::Flag("_key")], |
| 98 |
" key" => [P::Flag("key")], |
| 99 |
" key " => [P::Flag("key")], |
| 100 |
" key = \"val\"" => [P::KeyValue{ key: "key", val: "val" }], |
| 101 |
"key=\"\"" => [P::KeyValue{ key: "key", val: "" }], |
| 102 |
" key=\"7\" " => [P::KeyValue{ key: "key", val: "7" }], |
| 103 |
"key = \"7 q\" " => [P::KeyValue{ key: "key", val: "7 q" }], |
| 104 |
"target_has_atomic = \"ptr\"" => [P::Target(TP::HasAtomic(HasAtomic::Pointer))], |
| 105 |
"target_has_atomic = \"4\"" => [P::Target(TP::HasAtomic(HasAtomic::IntegerSize(4)))], |
| 106 |
"target_has_atomic = \"64\"" => [P::Target(TP::HasAtomic(HasAtomic::IntegerSize(64)))], |
| 107 |
"target_has_atomic = \"128\" " => [P::Target(TP::HasAtomic(HasAtomic::IntegerSize(128)))], |
| 108 |
"panic = \"unwind\"" => [P::Target(TP::Panic(Panic("unwind".into())))], |
| 109 |
"panic = \"abort\"" => [P::Target(TP::Panic(Panic("abort".into())))], |
| 110 |
]); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn handles_simple_funcs() { |
| 115 |
test_validate!(ok [ |
| 116 |
"any()" => [], |
| 117 |
"all()" => [], |
| 118 |
"not(key = \"value\")" => [P::KeyValue { key: "key", val: "value" }], |
| 119 |
]); |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn fails_invalid_funcs() { |
| 124 |
err!("nope()" => &["=", ",", ")"]; 4..5); |
| 125 |
err!("all(nope())" => &["=", ",", ")"]; 8..9); |
| 126 |
err!("any(,)" => &["<key>", ")", "all", "any", "not"]; 4..5); |
| 127 |
err!("blah(key)" => &["=", ",", ")"]; 4..5); |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn ensures_not_has_one_predicate() { |
| 132 |
assert_eq!( |
| 133 |
Expression::parse("not()").unwrap_err(), |
| 134 |
ParseError { |
| 135 |
original: "not()".to_owned(), |
| 136 |
span: 0..5, |
| 137 |
reason: Reason::InvalidNot(0), |
| 138 |
} |
| 139 |
); |
| 140 |
|
| 141 |
assert_eq!( |
| 142 |
Expression::parse("not(key_one, key_two)").unwrap_err(), |
| 143 |
ParseError { |
| 144 |
original: "not(key_one, key_two)".to_owned(), |
| 145 |
span: 0..21, |
| 146 |
reason: Reason::InvalidNot(2), |
| 147 |
} |
| 148 |
); |
| 149 |
|
| 150 |
assert_eq!( |
| 151 |
Expression::parse("any(not(not(key_one, key_two)))").unwrap_err(), |
| 152 |
ParseError { |
| 153 |
original: "any(not(not(key_one, key_two)))".to_owned(), |
| 154 |
span: 8..29, |
| 155 |
reason: Reason::InvalidNot(2), |
| 156 |
} |
| 157 |
); |
| 158 |
|
| 159 |
test_validate!(ok [ |
| 160 |
"not(key)" => [P::Flag("key")], |
| 161 |
"not(key,)" => [P::Flag("key")], |
| 162 |
"not(key = \"value\")" => [P::KeyValue { key: "key", val: "value" }], |
| 163 |
"not(key = \"value\",)" => [P::KeyValue { key: "key", val: "value" }], |
| 164 |
"not(not(not(key = \"value\",)))" => [P::KeyValue { key: "key", val: "value" }], |
| 165 |
]); |
| 166 |
} |