From cd4bc5bd6ae298d71e21e53241bad9c1cf1e2c35 Mon Sep 17 00:00:00 2001
From: Danilo Piparo <danilo.piparo@cern.ch>
Date: Thu, 1 Nov 2018 15:42:00 +0100
Subject: [PATCH] [Exp PyROOT] Add tests for TDirectory pythonizations

---
 .../PyROOT/test/CMakeLists.txt                |  1 +
 .../PyROOT/test/tdirectory_read_write.py      | 48 +++++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 bindings/pyroot_experimental/PyROOT/test/tdirectory_read_write.py

diff --git a/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt b/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt
index db15a024658..9a9a835bfe1 100644
--- a/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt
+++ b/bindings/pyroot_experimental/PyROOT/test/CMakeLists.txt
@@ -3,6 +3,7 @@ ROOT_ADD_PYUNITTEST(pyroot_pyz_pretty_printing pretty_printing.py)
 ROOT_ADD_PYUNITTEST(pyroot_pyz_array_interface array_interface.py)
 
 # TFile, TDirectory, TDirectoryFile pythonizations
+ROOT_ADD_PYUNITTEST(pyroot_pyz_tdirectory_read_write tdirectoryfile_read_write.py)
 ROOT_ADD_PYUNITTEST(pyroot_pyz_tdirectoryfile_read_write tdirectoryfile_read_write.py)
 ROOT_ADD_PYUNITTEST(pyroot_pyz_tfile_open_read_write tfile_open_read_write.py)
 
diff --git a/bindings/pyroot_experimental/PyROOT/test/tdirectory_read_write.py b/bindings/pyroot_experimental/PyROOT/test/tdirectory_read_write.py
new file mode 100644
index 00000000000..0cedafc17ac
--- /dev/null
+++ b/bindings/pyroot_experimental/PyROOT/test/tdirectory_read_write.py
@@ -0,0 +1,48 @@
+import unittest
+
+import ROOT
+from libcppyy import SetOwnership
+
+
+class TDirectoryReadWrite(unittest.TestCase):
+    """
+    Test for the attr syntax and Get method of TDirectory.
+    """
+
+    nbins = 8
+    xmin = 0
+    xmax = 4
+
+    # Setup
+    @classmethod
+    def setUpClass(cls):
+        cls.dir0 = ROOT.TDirectory("dir0", "dir0")
+        h = ROOT.TH1F("h", "h", cls.nbins, cls.xmin, cls.xmax)
+        SetOwnership(h, False)
+        # this must be there otherwise the histogram is not attached to dir0
+        h.SetDirectory(cls.dir0)
+
+        dir1 = cls.dir0.mkdir("dir1")
+        dir1.cd()
+        h1 = ROOT.TH1F("h1", "h1", cls.nbins, cls.xmin, cls.xmax)
+        SetOwnership(h1, False)
+
+        dir2 = dir1.mkdir("dir2")
+        dir2.cd()
+        h2 = ROOT.TH1F("h2", "h2", cls.nbins, cls.xmin, cls.xmax)
+        SetOwnership(h2, False)
+
+    def checkHisto(self, h):
+        xaxis = h.GetXaxis()
+        self.assertEqual(self.nbins, h.GetNbinsX())
+        self.assertEqual(self.xmin, xaxis.GetXmin())
+        self.assertEqual(self.xmax, xaxis.GetXmax())
+
+    # Tests
+    def test_readHisto_attrsyntax(self):
+        self.checkHisto(self.dir0.h)
+        self.checkHisto(self.dir0.dir1.h1)
+        self.checkHisto(self.dir0.dir1.dir2.h2)
+
+if __name__ == '__main__':
+    unittest.main()
-- 
GitLab