Skip to content

Commit

Permalink
improve tuple methods test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 26, 2023
1 parent 57b5232 commit 6897f66
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ tuple_conversion!(
#[cfg(test)]
#[allow(deprecated)] // TODO: remove allow when GIL Pool is removed
mod tests {
use crate::types::{PyAny, PyList, PyTuple};
use crate::types::{any::PyAnyMethods, tuple::PyTupleMethods, PyAny, PyList, PyTuple};
use crate::{Python, ToPyObject};
use std::collections::HashSet;

Expand All @@ -830,11 +830,21 @@ mod tests {
let ob = (1, 2, 3).to_object(py);
let tuple: &PyTuple = ob.downcast(py).unwrap();
assert_eq!(3, tuple.len());
assert!(!tuple.is_empty());
let ob: &PyAny = tuple.into();
assert_eq!((1, 2, 3), ob.extract().unwrap());
});
}

#[test]
fn test_empty() {
Python::with_gil(|py| {
let tuple = PyTuple::empty(py);
assert!(tuple.is_empty());
assert_eq!(0, tuple.len());
});
}

#[test]
fn test_slice() {
Python::with_gil(|py| {
Expand Down Expand Up @@ -894,6 +904,52 @@ mod tests {
});
}

#[test]
fn test_bound_iter() {
Python::with_gil(|py| {
let tuple = PyTuple::new_bound(py, [1, 2, 3]);
assert_eq!(3, tuple.len());
let mut iter = tuple.iter();

assert_eq!(iter.size_hint(), (3, Some(3)));

assert_eq!(1_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (2, Some(2)));

assert_eq!(2_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (1, Some(1)));

assert_eq!(3_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (0, Some(0)));

assert!(iter.next().is_none());
assert!(iter.next().is_none());
});
}

#[test]
fn test_bound_iter_rev() {
Python::with_gil(|py| {
let tuple = PyTuple::new_bound(py, [1, 2, 3]);
assert_eq!(3, tuple.len());
let mut iter = tuple.iter().rev();

assert_eq!(iter.size_hint(), (3, Some(3)));

assert_eq!(3_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (2, Some(2)));

assert_eq!(2_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (1, Some(1)));

assert_eq!(1_i32, iter.next().unwrap().extract::<'_, i32>().unwrap());
assert_eq!(iter.size_hint(), (0, Some(0)));

assert!(iter.next().is_none());
assert!(iter.next().is_none());
});
}

#[test]
fn test_into_iter() {
Python::with_gil(|py| {
Expand Down

0 comments on commit 6897f66

Please sign in to comment.