pyo3/tests/test_variable_arguments.rs

50 lines
1.2 KiB
Rust
Raw Permalink Normal View History

2021-12-03 00:03:32 +00:00
#![cfg(feature = "macros")]
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyTuple};
2023-09-24 12:34:53 +00:00
#[path = "../src/tests/common.rs"]
mod common;
#[pyclass]
struct MyClass {}
#[pymethods]
impl MyClass {
#[staticmethod]
#[pyo3(signature = (*args))]
fn test_args(args: Bound<'_, PyTuple>) -> Bound<'_, PyTuple> {
2021-02-11 21:37:38 +00:00
args
}
#[staticmethod]
#[pyo3(signature = (**kwargs))]
fn test_kwargs(kwargs: Option<Bound<'_, PyDict>>) -> Option<Bound<'_, PyDict>> {
2021-02-11 21:37:38 +00:00
kwargs
}
}
#[test]
fn variable_args() {
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let my_obj = py.get_type_bound::<MyClass>();
2022-07-19 17:34:23 +00:00
py_assert!(py, my_obj, "my_obj.test_args() == ()");
py_assert!(py, my_obj, "my_obj.test_args(1) == (1,)");
py_assert!(py, my_obj, "my_obj.test_args(1, 2) == (1, 2)");
});
}
#[test]
fn variable_kwargs() {
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let my_obj = py.get_type_bound::<MyClass>();
2022-07-19 17:34:23 +00:00
py_assert!(py, my_obj, "my_obj.test_kwargs() == None");
py_assert!(py, my_obj, "my_obj.test_kwargs(test=1) == {'test': 1}");
py_assert!(
py,
my_obj,
"my_obj.test_kwargs(test1=1, test2=2) == {'test1':1, 'test2':2}"
);
});
}