aboutsummaryrefslogtreecommitdiff
path: root/crates/cfg/src/dnf.rs
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-10-23 11:14:58 +0100
committerJonas Schievink <[email protected]>2020-10-23 11:14:58 +0100
commita246d4f599dcf2612fa99720fb4ca98101ed93fb (patch)
tree5972eedd06b94860d1c87a6711766377585a04b5 /crates/cfg/src/dnf.rs
parentdab8870f5c6d90ea1a327037825dc87a46778045 (diff)
cfg: move tests to separate file
that way we don't have to re-check the entire project when a test is changed
Diffstat (limited to 'crates/cfg/src/dnf.rs')
-rw-r--r--crates/cfg/src/dnf.rs158
1 files changed, 0 insertions, 158 deletions
diff --git a/crates/cfg/src/dnf.rs b/crates/cfg/src/dnf.rs
index 7ee7b0062..580c9a9a2 100644
--- a/crates/cfg/src/dnf.rs
+++ b/crates/cfg/src/dnf.rs
@@ -318,161 +318,3 @@ fn make_nnf(expr: CfgExpr) -> CfgExpr {
318 }, 318 },
319 } 319 }
320} 320}
321
322#[cfg(test)]
323mod test {
324 use expect_test::{expect, Expect};
325 use mbe::ast_to_token_tree;
326 use syntax::{ast, AstNode};
327
328 use super::*;
329
330 fn check_dnf(input: &str, expect: Expect) {
331 let (tt, _) = {
332 let source_file = ast::SourceFile::parse(input).ok().unwrap();
333 let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
334 ast_to_token_tree(&tt).unwrap()
335 };
336 let cfg = CfgExpr::parse(&tt);
337 let actual = format!("#![cfg({})]", DnfExpr::new(cfg));
338 expect.assert_eq(&actual);
339 }
340
341 fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
342 let (tt, _) = {
343 let source_file = ast::SourceFile::parse(input).ok().unwrap();
344 let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
345 ast_to_token_tree(&tt).unwrap()
346 };
347 let cfg = CfgExpr::parse(&tt);
348 let dnf = DnfExpr::new(cfg);
349 let why_inactive = dnf.why_inactive(opts).unwrap().to_string();
350 expect.assert_eq(&why_inactive);
351 }
352
353 #[track_caller]
354 fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) {
355 let (tt, _) = {
356 let source_file = ast::SourceFile::parse(input).ok().unwrap();
357 let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
358 ast_to_token_tree(&tt).unwrap()
359 };
360 let cfg = CfgExpr::parse(&tt);
361 let dnf = DnfExpr::new(cfg);
362 let hints = dnf.compute_enable_hints(opts).map(|diff| diff.to_string()).collect::<Vec<_>>();
363 assert_eq!(hints, expected_hints);
364 }
365
366 #[test]
367 fn smoke() {
368 check_dnf("#![cfg(test)]", expect![[r#"#![cfg(test)]"#]]);
369 check_dnf("#![cfg(not(test))]", expect![[r#"#![cfg(not(test))]"#]]);
370 check_dnf("#![cfg(not(not(test)))]", expect![[r#"#![cfg(test)]"#]]);
371
372 check_dnf("#![cfg(all(a, b))]", expect![[r#"#![cfg(all(a, b))]"#]]);
373 check_dnf("#![cfg(any(a, b))]", expect![[r#"#![cfg(any(a, b))]"#]]);
374
375 check_dnf("#![cfg(not(a))]", expect![[r#"#![cfg(not(a))]"#]]);
376 }
377
378 #[test]
379 fn distribute() {
380 check_dnf("#![cfg(all(any(a, b), c))]", expect![[r#"#![cfg(any(all(a, c), all(b, c)))]"#]]);
381 check_dnf("#![cfg(all(c, any(a, b)))]", expect![[r#"#![cfg(any(all(c, a), all(c, b)))]"#]]);
382 check_dnf(
383 "#![cfg(all(any(a, b), any(c, d)))]",
384 expect![[r#"#![cfg(any(all(a, c), all(a, d), all(b, c), all(b, d)))]"#]],
385 );
386
387 check_dnf(
388 "#![cfg(all(any(a, b, c), any(d, e, f), g))]",
389 expect![[
390 r#"#![cfg(any(all(a, d, g), all(a, e, g), all(a, f, g), all(b, d, g), all(b, e, g), all(b, f, g), all(c, d, g), all(c, e, g), all(c, f, g)))]"#
391 ]],
392 );
393 }
394
395 #[test]
396 fn demorgan() {
397 check_dnf("#![cfg(not(all(a, b)))]", expect![[r#"#![cfg(any(not(a), not(b)))]"#]]);
398 check_dnf("#![cfg(not(any(a, b)))]", expect![[r#"#![cfg(all(not(a), not(b)))]"#]]);
399
400 check_dnf("#![cfg(not(all(not(a), b)))]", expect![[r#"#![cfg(any(a, not(b)))]"#]]);
401 check_dnf("#![cfg(not(any(a, not(b))))]", expect![[r#"#![cfg(all(not(a), b))]"#]]);
402 }
403
404 #[test]
405 fn nested() {
406 check_dnf(
407 "#![cfg(all(any(a), not(all(any(b)))))]",
408 expect![[r#"#![cfg(all(a, not(b)))]"#]],
409 );
410
411 check_dnf("#![cfg(any(any(a, b)))]", expect![[r#"#![cfg(any(a, b))]"#]]);
412 check_dnf("#![cfg(not(any(any(a, b))))]", expect![[r#"#![cfg(all(not(a), not(b)))]"#]]);
413 check_dnf("#![cfg(all(all(a, b)))]", expect![[r#"#![cfg(all(a, b))]"#]]);
414 check_dnf("#![cfg(not(all(all(a, b))))]", expect![[r#"#![cfg(any(not(a), not(b)))]"#]]);
415 }
416
417 #[test]
418 fn hints() {
419 let mut opts = CfgOptions::default();
420
421 check_enable_hints("#![cfg(test)]", &opts, &["enable test"]);
422 check_enable_hints("#![cfg(not(test))]", &opts, &[]);
423
424 check_enable_hints("#![cfg(any(a, b))]", &opts, &["enable a", "enable b"]);
425 check_enable_hints("#![cfg(any(b, a))]", &opts, &["enable b", "enable a"]);
426
427 check_enable_hints("#![cfg(all(a, b))]", &opts, &["enable a and b"]);
428
429 opts.insert_atom("test".into());
430
431 check_enable_hints("#![cfg(test)]", &opts, &[]);
432 check_enable_hints("#![cfg(not(test))]", &opts, &["disable test"]);
433 }
434
435 /// Tests that we don't suggest hints for cfgs that express an inconsistent formula.
436 #[test]
437 fn hints_impossible() {
438 let mut opts = CfgOptions::default();
439
440 check_enable_hints("#![cfg(all(test, not(test)))]", &opts, &[]);
441
442 opts.insert_atom("test".into());
443
444 check_enable_hints("#![cfg(all(test, not(test)))]", &opts, &[]);
445 }
446
447 #[test]
448 fn why_inactive() {
449 let mut opts = CfgOptions::default();
450 opts.insert_atom("test".into());
451 opts.insert_atom("test2".into());
452
453 check_why_inactive("#![cfg(a)]", &opts, expect![["a is disabled"]]);
454 check_why_inactive("#![cfg(not(test))]", &opts, expect![["test is enabled"]]);
455
456 check_why_inactive(
457 "#![cfg(all(not(test), not(test2)))]",
458 &opts,
459 expect![["test and test2 are enabled"]],
460 );
461 check_why_inactive("#![cfg(all(a, b))]", &opts, expect![["a and b are disabled"]]);
462 check_why_inactive(
463 "#![cfg(all(not(test), a))]",
464 &opts,
465 expect![["test is enabled and a is disabled"]],
466 );
467 check_why_inactive(
468 "#![cfg(all(not(test), test2, a))]",
469 &opts,
470 expect![["test is enabled and a is disabled"]],
471 );
472 check_why_inactive(
473 "#![cfg(all(not(test), not(test2), a))]",
474 &opts,
475 expect![["test and test2 are enabled and a is disabled"]],
476 );
477 }
478}