Skip to content

Commit 4e263fe

Browse files
committed
Add test-examples for Cfg::simplify_with
1 parent 6f0544a commit 4e263fe

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/librustdoc/clean/cfg.rs

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ impl Cfg {
205205
/// Attempt to simplify this cfg by assuming that `assume` is already known to be true, will
206206
/// return `None` if simplification managed to completely eliminate any requirements from this
207207
/// `Cfg`.
208+
///
209+
/// See `tests::test_simplify_with` for examples.
208210
pub(crate) fn simplify_with(&self, assume: &Cfg) -> Option<Cfg> {
209211
if self == assume {
210212
return None;

src/librustdoc/clean/cfg/tests.rs

+36
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,39 @@ fn test_render_long_html() {
433433
);
434434
})
435435
}
436+
437+
#[test]
438+
fn test_simplify_with() {
439+
// This is a tiny subset of things that could be simplified, but it likely covers 90% of
440+
// real world usecases well.
441+
with_default_session_globals(|| {
442+
let foo = word_cfg("foo");
443+
let bar = word_cfg("bar");
444+
let baz = word_cfg("baz");
445+
let quux = word_cfg("quux");
446+
447+
let foobar = Cfg::All(vec![foo.clone(), bar.clone()]);
448+
let barbaz = Cfg::All(vec![bar.clone(), baz.clone()]);
449+
let foobarbaz = Cfg::All(vec![foo.clone(), bar.clone(), baz.clone()]);
450+
let bazquux = Cfg::All(vec![baz.clone(), quux.clone()]);
451+
452+
// Unrelated cfgs don't affect each other
453+
assert_eq!(foo.simplify_with(&bar).as_ref(), Some(&foo));
454+
assert_eq!(foobar.simplify_with(&bazquux).as_ref(), Some(&foobar));
455+
456+
// Identical cfgs are eliminated
457+
assert_eq!(foo.simplify_with(&foo), None);
458+
assert_eq!(foobar.simplify_with(&foobar), None);
459+
460+
// Multiple cfgs eliminate a single assumed cfg
461+
assert_eq!(foobar.simplify_with(&foo).as_ref(), Some(&bar));
462+
assert_eq!(foobar.simplify_with(&bar).as_ref(), Some(&foo));
463+
464+
// A single cfg is eliminated by multiple assumed cfg containing it
465+
assert_eq!(foo.simplify_with(&foobar), None);
466+
467+
// Multiple cfgs eliminate the matching subset of multiple assumed cfg
468+
assert_eq!(foobar.simplify_with(&barbaz).as_ref(), Some(&foo));
469+
assert_eq!(foobar.simplify_with(&foobarbaz), None);
470+
});
471+
}

0 commit comments

Comments
 (0)