File: test_variable_arguments.rs

package info (click to toggle)
rust-pyo3 0.27.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,648 kB
  • sloc: javascript: 59; makefile: 58; python: 39; sh: 1
file content (48 lines) | stat: -rw-r--r-- 1,171 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#![cfg(feature = "macros")]

use pyo3::prelude::*;
use pyo3::types::{PyDict, PyTuple};

mod test_utils;

#[pyclass]
struct MyClass {}

#[pymethods]
impl MyClass {
    #[staticmethod]
    #[pyo3(signature = (*args))]
    fn test_args(args: Bound<'_, PyTuple>) -> Bound<'_, PyTuple> {
        args
    }

    #[staticmethod]
    #[pyo3(signature = (**kwargs))]
    fn test_kwargs(kwargs: Option<Bound<'_, PyDict>>) -> Option<Bound<'_, PyDict>> {
        kwargs
    }
}

#[test]
fn variable_args() {
    Python::attach(|py| {
        let my_obj = py.get_type::<MyClass>();
        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() {
    Python::attach(|py| {
        let my_obj = py.get_type::<MyClass>();
        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}"
        );
    });
}