cfg: move tests to separate file

that way we don't have to re-check the entire project when a test is
changed
This commit is contained in:
Jonas Schievink 2020-10-23 12:14:58 +02:00
parent dab8870f5c
commit a246d4f599
4 changed files with 195 additions and 208 deletions

View file

@ -128,53 +128,3 @@ fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> {
}
Some(ret)
}
#[cfg(test)]
mod tests {
use super::*;
use mbe::ast_to_token_tree;
use syntax::ast::{self, AstNode};
fn assert_parse_result(input: &str, expected: CfgExpr) {
let (tt, _) = {
let source_file = ast::SourceFile::parse(input).ok().unwrap();
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
ast_to_token_tree(&tt).unwrap()
};
let cfg = CfgExpr::parse(&tt);
assert_eq!(cfg, expected);
}
#[test]
fn test_cfg_expr_parser() {
assert_parse_result("#![cfg(foo)]", CfgAtom::Flag("foo".into()).into());
assert_parse_result("#![cfg(foo,)]", CfgAtom::Flag("foo".into()).into());
assert_parse_result(
"#![cfg(not(foo))]",
CfgExpr::Not(Box::new(CfgAtom::Flag("foo".into()).into())),
);
assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid);
// Only take the first
assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgAtom::Flag("foo".into()).into());
assert_parse_result(
r#"#![cfg(all(foo, bar = "baz"))]"#,
CfgExpr::All(vec![
CfgAtom::Flag("foo".into()).into(),
CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(),
]),
);
assert_parse_result(
r#"#![cfg(any(not(), all(), , bar = "baz",))]"#,
CfgExpr::Any(vec![
CfgExpr::Not(Box::new(CfgExpr::Invalid)),
CfgExpr::All(vec![]),
CfgExpr::Invalid,
CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(),
]),
);
}
}