Skip to content
Snippets Groups Projects
Commit a7612f9e authored by Enric Tejedor Saavedra's avatar Enric Tejedor Saavedra
Browse files

[Exp PyROOT] Test contains pythonisation for TObject

parent 5137397e
Branches
Tags
No related merge requests found
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
ROOT_ADD_PYUNITTEST(pyroot_pyz_pretty_printing pretty_printing.py) ROOT_ADD_PYUNITTEST(pyroot_pyz_pretty_printing pretty_printing.py)
ROOT_ADD_PYUNITTEST(pyroot_pyz_array_interface array_interface.py) ROOT_ADD_PYUNITTEST(pyroot_pyz_array_interface array_interface.py)
# TObject and subclasses pythonisations
ROOT_ADD_PYUNITTEST(pyroot_pyz_tobject_contains tobject_contains.py)
# TDirectory and subclasses pythonizations # TDirectory and subclasses pythonizations
ROOT_ADD_PYUNITTEST(pyroot_pyz_tdirectory_attrsyntax tdirectory_attrsyntax.py) ROOT_ADD_PYUNITTEST(pyroot_pyz_tdirectory_attrsyntax tdirectory_attrsyntax.py)
ROOT_ADD_PYUNITTEST(pyroot_pyz_tdirectoryfile_attrsyntax_get tdirectoryfile_attrsyntax_get.py) ROOT_ADD_PYUNITTEST(pyroot_pyz_tdirectoryfile_attrsyntax_get tdirectoryfile_attrsyntax_get.py)
......
import unittest
import ROOT
from libcppyy import SetOwnership
class TObjectContains(unittest.TestCase):
"""
Test for the __contains__ pythonisation of TObject and subclasses.
Such pythonisation relies on TObject::FindObject, which is redefined
in some of its subclasses, such as TCollection.
Thanks to this pythonisation, we can use the syntax `obj in col`
to know if col contains obj.
"""
num_elems = 3
# Helpers
def create_tlist(self):
l = ROOT.TList()
for _ in range(self.num_elems):
o = ROOT.TObject()
# Prevent immediate deletion of C++ TObjects
SetOwnership(o, False)
l.Add(o)
return l
# Tests
def test_contains(self):
l = self.create_tlist()
for elem in l:
self.assertTrue(elem in l)
# Make sure it does not work just because of __iter__
self.assertTrue(l.__contains__(elem))
o = ROOT.TObject()
self.assertFalse(o in l)
self.assertFalse(l.__contains__(o))
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment