use crate::{cfg_expr::expr::Expression, resolve::CfgMap};
#[test]
#[cfg_attr(miri, ignore)] fn target_family() {
let matches_any_family =
Expression::parse("any(unix, target_family = \"windows\", target_family = \"wasm\")")
.unwrap();
let impossible = Expression::parse("all(windows, target_family = \"unix\")").unwrap();
let mut map = CfgMap::default();
let rustc = &|| cmd!("rustc");
for target in [
"aarch64-apple-darwin",
"x86_64-pc-windows-msvc",
"wasm32-unknown-unknown",
"thumbv7m-none-eabi",
] {
let t = map.eval_cfg(&matches_any_family, &target.into(), rustc).unwrap();
if target.contains("-none") {
assert!(!t, "{target}");
} else {
assert!(t, "{target}");
}
assert!(!map.eval_cfg(&impossible, &target.into(), rustc).unwrap());
}
}
#[test]
fn tiny() {
assert!(Expression::parse("all()").unwrap().eval(|_| false));
assert!(!Expression::parse("any()").unwrap().eval(|_| true));
assert!(!Expression::parse("not(all())").unwrap().eval(|_| false));
assert!(Expression::parse("not(any())").unwrap().eval(|_| true));
assert!(Expression::parse("all(not(blah))").unwrap().eval(|_| false));
assert!(!Expression::parse("any(not(blah))").unwrap().eval(|_| true));
}
#[test]
#[cfg_attr(miri, ignore)] fn very_specific() {
let specific = Expression::parse(
r#"all(
target_os = "windows",
target_arch = "x86",
windows,
target_env = "msvc",
target_feature = "fxsr",
target_feature = "sse",
target_feature = "sse2",
target_pointer_width = "32",
target_endian = "little",
not(target_vendor = "uwp"),
target_has_atomic = "8",
target_has_atomic = "16",
target_has_atomic = "32",
target_has_atomic = "64",
not(target_has_atomic = "128"),
target_has_atomic = "ptr",
panic = "unwind",
not(panic = "abort"),
)"#,
)
.unwrap();
let mut map = CfgMap::default();
let rustc = &|| cmd!("rustc");
for target in ["i686-pc-windows-msvc", "i686-pc-windows-gnu"] {
let t = map.eval_cfg(&specific, &target.into(), rustc).unwrap();
assert_eq!(
target == "i686-pc-windows-msvc",
t,
"expected true for i686-pc-windows-msvc, but got true for {target}",
);
}
}
#[test]
#[cfg_attr(miri, ignore)] fn complex() {
let complex = Expression::parse(r#"cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))"#).unwrap();
let mut map = CfgMap::default();
let rustc = &|| cmd!("rustc");
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-gnu".into(), rustc).unwrap());
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-musl".into(), rustc).unwrap());
assert!(!map.eval_cfg(&complex, &"x86_64-pc-windows-msvc".into(), rustc).unwrap());
assert!(!map.eval_cfg(&complex, &"x86_64-apple-darwin".into(), rustc).unwrap());
assert!(!map.eval_cfg(&complex, &"aarch64-linux-android".into(), rustc).unwrap());
let complex =
Expression::parse(r#"all(not(target_os = "ios"), not(target_os = "android"))"#).unwrap();
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-gnu".into(), rustc).unwrap());
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-musl".into(), rustc).unwrap());
assert!(map.eval_cfg(&complex, &"x86_64-pc-windows-msvc".into(), rustc).unwrap());
assert!(map.eval_cfg(&complex, &"x86_64-apple-darwin".into(), rustc).unwrap());
assert!(!map.eval_cfg(&complex, &"aarch64-linux-android".into(), rustc).unwrap());
let complex = Expression::parse(r#"all(any(unix, target_arch="x86"), not(any(target_os="android", target_os="emscripten")))"#).unwrap();
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-gnu".into(), rustc).unwrap());
assert!(map.eval_cfg(&complex, &"x86_64-unknown-linux-musl".into(), rustc).unwrap());
assert!(map.eval_cfg(&complex, &"x86_64-apple-darwin".into(), rustc).unwrap());
assert!(!map.eval_cfg(&complex, &"x86_64-pc-windows-msvc".into(), rustc).unwrap());
assert!(!map.eval_cfg(&complex, &"aarch64-linux-android".into(), rustc).unwrap());
let complex = Expression::parse(r#"all(target_os="none")"#).unwrap();
assert!(!map.eval_cfg(&complex, &"x86_64-unknown-linux-gnu".into(), rustc).unwrap());
assert!(map.eval_cfg(&complex, &"armebv7r-none-eabi".into(), rustc).unwrap());
}
#[test]
#[cfg_attr(miri, ignore)] fn wasm_family() {
let wasm = Expression::parse(r#"cfg(target_family = "wasm")"#).unwrap();
let mut map = CfgMap::default();
let rustc = &|| cmd!("rustc");
for target in [
"wasm32-unknown-unknown",
"wasm32-unknown-emscripten",
"wasm32-wasip1",
"wasm64-unknown-unknown",
] {
assert!(map.eval_cfg(&wasm, &target.into(), rustc).unwrap(), "{target}");
}
}