pycell: add more test coverage

This commit is contained in:
David Hewitt 2022-06-12 08:51:47 +01:00
parent 7725f17c46
commit 2fd5364646
2 changed files with 127 additions and 0 deletions

View File

@ -1117,4 +1117,32 @@ a = A()
Ok(())
})
}
#[cfg(feature = "macros")]
mod using_macros {
use super::*;
#[crate::pyclass]
#[pyo3(crate = "crate")]
struct SomeClass(i32);
#[test]
fn instance_borrow_methods() {
// More detailed tests of the underlying semantics in pycell.rs
Python::with_gil(|py| {
let instance = Py::new(py, SomeClass(0)).unwrap();
assert_eq!(instance.borrow(py).0, 0);
assert_eq!(instance.try_borrow(py).unwrap().0, 0);
assert_eq!(instance.borrow_mut(py).0, 0);
assert_eq!(instance.try_borrow_mut(py).unwrap().0, 0);
instance.borrow_mut(py).0 = 123;
assert_eq!(instance.borrow(py).0, 123);
assert_eq!(instance.try_borrow(py).unwrap().0, 123);
assert_eq!(instance.borrow_mut(py).0, 123);
assert_eq!(instance.try_borrow_mut(py).unwrap().0, 123);
})
}
}
}

View File

@ -1086,3 +1086,102 @@ where
<T::BaseType as PyClassBaseType>::LayoutAsBase::tp_dealloc(slf, py)
}
}
#[cfg(test)]
#[cfg(feature = "macros")]
mod tests {
use super::*;
#[crate::pyclass]
#[pyo3(crate = "crate")]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
struct SomeClass(i32);
#[test]
fn pycell_replace() {
Python::with_gil(|py| {
let cell = PyCell::new(py, SomeClass(0)).unwrap();
assert_eq!(*cell.borrow(), SomeClass(0));
let previous = cell.replace(SomeClass(123));
assert_eq!(previous, SomeClass(0));
assert_eq!(*cell.borrow(), SomeClass(123));
})
}
#[test]
#[should_panic(expected = "Already borrowed: PyBorrowMutError")]
fn pycell_replace_panic() {
Python::with_gil(|py| {
let cell = PyCell::new(py, SomeClass(0)).unwrap();
let _guard = cell.borrow();
cell.replace(SomeClass(123));
})
}
#[test]
fn pycell_replace_with() {
Python::with_gil(|py| {
let cell = PyCell::new(py, SomeClass(0)).unwrap();
assert_eq!(*cell.borrow(), SomeClass(0));
let previous = cell.replace_with(|value| {
*value = SomeClass(2);
SomeClass(123)
});
assert_eq!(previous, SomeClass(2));
assert_eq!(*cell.borrow(), SomeClass(123));
})
}
#[test]
#[should_panic(expected = "Already borrowed: PyBorrowMutError")]
fn pycell_replace_with_panic() {
Python::with_gil(|py| {
let cell = PyCell::new(py, SomeClass(0)).unwrap();
let _guard = cell.borrow();
cell.replace_with(|_| SomeClass(123));
})
}
#[test]
fn pycell_swap() {
Python::with_gil(|py| {
let cell = PyCell::new(py, SomeClass(0)).unwrap();
let cell2 = PyCell::new(py, SomeClass(123)).unwrap();
assert_eq!(*cell.borrow(), SomeClass(0));
assert_eq!(*cell2.borrow(), SomeClass(123));
cell.swap(cell2);
assert_eq!(*cell.borrow(), SomeClass(123));
assert_eq!(*cell2.borrow(), SomeClass(0));
})
}
#[test]
#[should_panic(expected = "Already borrowed: PyBorrowMutError")]
fn pycell_swap_panic() {
Python::with_gil(|py| {
let cell = PyCell::new(py, SomeClass(0)).unwrap();
let cell2 = PyCell::new(py, SomeClass(123)).unwrap();
let _guard = cell.borrow();
cell.swap(cell2);
})
}
#[test]
#[should_panic(expected = "Already borrowed: PyBorrowMutError")]
fn pycell_swap_panic_other_borrowed() {
Python::with_gil(|py| {
let cell = PyCell::new(py, SomeClass(0)).unwrap();
let cell2 = PyCell::new(py, SomeClass(123)).unwrap();
let _guard = cell2.borrow();
cell.swap(cell2);
})
}
}