Skip to content

Commit

Permalink
pycell: add more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Jun 12, 2022
1 parent a758495 commit 1f7037a
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,4 +1117,27 @@ a = A()
Ok(())
})
}

#[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);
})
}
}
97 changes: 97 additions & 0 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,3 +1125,100 @@ where
<T::BaseType as PyClassBaseType>::LayoutAsBase::tp_dealloc(slf, py)
}
}

#[cfg(test)]
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);
})
}
}

0 comments on commit 1f7037a

Please sign in to comment.