From 42da99ff92949936e7f85786dd18b723c6b1d5eb Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Thu, 21 Nov 2024 08:26:55 -0500 Subject: [PATCH] uv-pep508: add a clarifying test This test demonstrates the difference between `extra != "foo"` and `sys_platform != "foo"`. I wrote this test down to test the extra simplification logic was correct. And I also wanted to test whether we could somehow hackily encode `group` (as opposed to just `extra`) logic into marker expressions by reusing another field. But I don't think we can. --- crates/uv-pep508/src/marker/tree.rs | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crates/uv-pep508/src/marker/tree.rs b/crates/uv-pep508/src/marker/tree.rs index 5c5502e31..e312022d9 100644 --- a/crates/uv-pep508/src/marker/tree.rs +++ b/crates/uv-pep508/src/marker/tree.rs @@ -2451,6 +2451,39 @@ mod test { assert!(m("sys_platform == 'win32' and sys_platform != 'win32'").is_false()); } + /// This tests the difference between simplifying extras and simplifying + /// other stringly-valued marker attributes. + /// + /// In particular, this demonstrates that `extra != "foo"` would actually + /// be more clearly written as `"foo" not in extras`. And similarly, `extra + /// == "foo"` would be written as `"foo" in extras`. This is different from + /// other attributes, like `sys_platform`, where the test is just string + /// equality. That is, `extra` is a set where as `sys_platform` is just a + /// single value. + #[test] + fn test_simplification_extra_versus_other() { + // Here, the `extra != 'foo'` cannot be simplified out, because + // `extra == 'foo'` can be true even when `extra == 'bar`' is true. + assert_simplifies( + r#"extra != "foo" and (extra == "bar" or extra == "baz")"#, + "(extra == 'bar' and extra != 'foo') or (extra == 'baz' and extra != 'foo')", + ); + // But here, the `sys_platform != 'foo'` can be simplified out, because + // it is strictly disjoint with + // `sys_platform == "bar" or sys_platform == "baz"`. + assert_simplifies( + r#"sys_platform != "foo" and (sys_platform == "bar" or sys_platform == "baz")"#, + "sys_platform == 'bar' or sys_platform == 'baz'", + ); + + // Another case I experimented with and wanted to verify. + assert_simplifies( + r#"(extra != "bar" and (extra == "foo" or extra == "baz")) + or ((extra != "foo" and extra != "bar") and extra == "baz")"#, + "(extra != 'bar' and extra == 'baz') or (extra != 'bar' and extra == 'foo')", + ); + } + #[test] fn test_marker_negation() { assert_eq!(