File: test_accessor.py

package info (click to toggle)
nanobind 2.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,300 kB
  • sloc: cpp: 12,232; python: 6,315; ansic: 4,813; makefile: 22; sh: 15
file content (41 lines) | stat: -rw-r--r-- 1,068 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
import test_accessor_ext as t


def test_01_str_attr_inplace_mutation():
    """
    Tests that a C++ expression like obj.attr("foo") += ... 
    can actually modify the object in-place.
    """
    a = t.test_str_attr_accessor_inplace_mutation()
    assert a.value == 1


def test_02_str_item_inplace_mutation():
    """
    Similar to test 01, but tests obj["foo"] (keyed attribute access)
    on the C++ side.
    """
    d = t.test_str_item_accessor_inplace_mutation()
    assert d.keys() == {"a"}
    assert d["a"] == 1


def test_03_num_item_list_inplace_mutation():
    """
    Similar to test 01, but tests l[n] (index access)
    on the C++ side, where l is an ``nb::list``.
    """
    l = t.test_num_item_list_accessor_inplace_mutation()
    assert len(l) == 1
    assert l[0] == 1


def test_04_obj_item_inplace_mutation():
    """
    Similar to test 01, but tests obj[h] (handle access)
    on the C++ side.
    """
    d = t.test_obj_item_accessor_inplace_mutation()
    assert len(d) == 1
    assert d.keys() == {0}
    assert d[0] == 1  # dict lookup