Skip to content
Snippets Groups Projects
Commit 050098d6 authored by Georges Racinet's avatar Georges Racinet
Browse files

rust-index: variant of assert_py_eq with normalizer expression

The example given in doc-comment is the main use case: some methods
may require ordering insensitive comparison. This is about to be
used for `reachableroots2`
parent 62e39bef
No related branches found
No related tags found
2 merge requests!737Improvements to the Rust index's performance,!708Implement pure rust revlog Index
......@@ -823,9 +823,45 @@
}
}
/// assert two Python objects to be equal from a Python point of view
///
/// `method` is a label for the assertion error message, intended to be the
/// name of the caller.
/// `normalizer` is a function that takes a Python variable name and returns
/// an expression that the conparison will actually use.
/// Foe example: `|v| format!("sorted({})", v)`
fn assert_py_eq_normalized(
py: Python,
method: &str,
rust: &PyObject,
c: &PyObject,
normalizer: impl FnOnce(&str) -> String + Copy,
) -> PyResult<()> {
let locals = PyDict::new(py);
locals.set_item(py, "rust".into_py_object(py).into_object(), rust)?;
locals.set_item(py, "c".into_py_object(py).into_object(), c)?;
// let lhs = format!(normalizer_fmt, "rust");
// let rhs = format!(normalizer_fmt, "c");
let is_eq: PyBool = py
.eval(
&format!("{} == {}", &normalizer("rust"), &normalizer("c")),
None,
Some(&locals),
)?
.extract(py)?;
assert!(
is_eq.is_true(),
"{} results differ. Rust: {:?} C: {:?} (before any normalization)",
method,
rust,
c
);
Ok(())
}
fn assert_py_eq(
py: Python,
method: &str,
rust: &PyObject,
c: &PyObject,
) -> PyResult<()> {
......@@ -826,22 +862,10 @@
fn assert_py_eq(
py: Python,
method: &str,
rust: &PyObject,
c: &PyObject,
) -> PyResult<()> {
let locals = PyDict::new(py);
locals.set_item(py, "rust".into_py_object(py).into_object(), rust)?;
locals.set_item(py, "c".into_py_object(py).into_object(), c)?;
let is_eq: PyBool =
py.eval("rust == c", None, Some(&locals))?.extract(py)?;
assert!(
is_eq.is_true(),
"{} results differ. Rust: {:?} C: {:?}",
method,
rust,
c
);
Ok(())
assert_py_eq_normalized(py, method, rust, c, |v| v.to_owned())
}
/// Create the module, with __package__ given from parent
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment