Initialized repository with current sources.

This commit is contained in:
Matthias Koefferlein
2017-02-12 13:21:08 +01:00
parent bf6ea90905
commit 1b98f9b0f9
3061 changed files with 1992657 additions and 0 deletions
+2688
View File
File diff suppressed because it is too large Load Diff
+1070
View File
File diff suppressed because it is too large Load Diff
+341
View File
@@ -0,0 +1,341 @@
import pya
import unittest
import sys
class BoxPCell(pya.PCellDeclaration):
def display_text(self, parameters):
# provide a descriptive text for the cell
return "Box(L=" + str(parameters[0]) + ",W=" + ('%.3f' % parameters[1]) + ",H=" + ('%.3f' % parameters[2]) + ")"
def get_parameters(self):
# prepare a set of parameter declarations
param = []
param.append(pya.PCellParameterDeclaration("l", pya.PCellParameterDeclaration.TypeLayer, "Layer", pya.LayerInfo(0, 0)))
param.append(pya.PCellParameterDeclaration("w", pya.PCellParameterDeclaration.TypeDouble, "Width", 1.0))
param.append(pya.PCellParameterDeclaration("h", pya.PCellParameterDeclaration.TypeDouble, "Height", 1.0))
return param
def get_layers(self, parameters):
return [ parameters[0] ]
def produce(self, layout, layers, parameters, cell):
dbu = layout.dbu
# fetch the parameters
l = parameters[0]
w = parameters[1] / layout.dbu
h = parameters[2] / layout.dbu
# create the shape
cell.shapes(layers[0]).insert(pya.Box(-w / 2, -h / 2, w / 2, h / 2))
class PCellTestLib(pya.Library):
boxpcell = None
def __init__(self):
# set the description
self.description = "PCell test lib"
# create the PCell declarations
boxpcell = BoxPCell()
self.layout().register_pcell("Box", boxpcell)
sb_index = self.layout().add_cell("StaticBox")
l10 = self.layout().insert_layer(pya.LayerInfo(10, 0))
sb_cell = self.layout().cell(sb_index)
sb_cell.shapes(l10).insert(pya.Box(0, 0, 100, 200))
# register us with the name "MyLib"
self.register("PCellTestLib")
def inspect_LayerInfo(self):
return "<" + str(self) + ">"
pya.LayerInfo.__repr__ = inspect_LayerInfo
def find_layer(ly, lp):
for li in ly.layer_indices():
if str(ly.get_info(li)) == lp:
return li
return None
class DBPCellTests(unittest.TestCase):
def test_1(self):
# instantiate and register the library
tl = PCellTestLib()
ly = pya.Layout(True)
ly.dbu = 0.01
li1 = find_layer(ly, "1/0")
self.assertEqual(li1 == None, True)
ci1 = ly.add_cell("c1")
c1 = ly.cell(ci1)
lib = pya.Library.library_by_name("NoLib")
self.assertEqual(lib == None, True)
lib = pya.Library.library_by_name("PCellTestLib")
self.assertEqual(lib != None, True)
pcell_decl = lib.layout().pcell_declaration("x")
self.assertEqual(pcell_decl == None, True)
pcell_decl = lib.layout().pcell_declaration("Box")
self.assertEqual(pcell_decl != None, True)
pcell_decl_id = lib.layout().pcell_id("Box")
self.assertEqual(pcell_decl.id(), pcell_decl_id)
self.assertEqual(":".join(lib.layout().pcell_names()), "Box")
self.assertEqual(lib.layout().pcell_ids(), [ pcell_decl_id ])
self.assertEqual(lib.layout().pcell_declaration(pcell_decl_id).id(), pcell_decl_id)
param = [ pya.LayerInfo(1, 0) ] # rest is filled with defaults
pcell_var_id = ly.add_pcell_variant(lib, pcell_decl_id, param)
pcell_var = ly.cell(pcell_var_id)
pcell_inst = c1.insert(pya.CellInstArray(pcell_var_id, pya.Trans()))
self.assertEqual(pcell_var.layout().__repr__(), ly.__repr__())
self.assertEqual(pcell_var.library().__repr__(), lib.__repr__())
self.assertEqual(pcell_var.is_pcell_variant(), True)
self.assertEqual(pcell_var.display_title(), "PCellTestLib.Box(L=1/0,W=1.000,H=1.000)")
self.assertEqual(pcell_var.basic_name(), "Box")
self.assertEqual(c1.is_pcell_variant(), False)
self.assertEqual(c1.is_pcell_variant(pcell_inst), True)
self.assertEqual(pcell_var.pcell_id(), pcell_decl_id)
self.assertEqual(pcell_var.pcell_library().__repr__(), lib.__repr__())
self.assertEqual(pcell_var.pcell_parameters().__repr__(), "[<1/0>, 1.0, 1.0]")
self.assertEqual(pcell_var.pcell_parameters_by_name().__repr__(), "{'h': 1.0, 'l': <1/0>, 'w': 1.0}")
self.assertEqual(pcell_var.pcell_parameter("h").__repr__(), "1.0")
self.assertEqual(c1.pcell_parameters(pcell_inst).__repr__(), "[<1/0>, 1.0, 1.0]")
self.assertEqual(c1.pcell_parameters_by_name(pcell_inst).__repr__(), "{'h': 1.0, 'l': <1/0>, 'w': 1.0}")
self.assertEqual(c1.pcell_parameter(pcell_inst, "h").__repr__(), "1.0")
self.assertEqual(pcell_inst.pcell_parameters_by_name().__repr__(), "{'h': 1.0, 'l': <1/0>, 'w': 1.0}")
self.assertEqual(pcell_inst["h"].__repr__(), "1.0")
self.assertEqual(pcell_inst["i"].__repr__(), "None")
self.assertEqual(pcell_inst.pcell_parameter("h").__repr__(), "1.0")
self.assertEqual(pcell_var.pcell_declaration().__repr__(), pcell_decl.__repr__())
self.assertEqual(c1.pcell_declaration(pcell_inst).__repr__(), pcell_decl.__repr__())
self.assertEqual(pcell_inst.pcell_declaration().__repr__(), pcell_decl.__repr__())
pcell_inst.change_pcell_parameter("h", 2.0)
self.assertEqual(pcell_inst.pcell_parameters_by_name().__repr__(), "{'h': 2.0, 'l': <1/0>, 'w': 1.0}")
pcell_inst.set_property("abc", "a property")
self.assertEqual(pcell_inst.property("abc").__repr__(), "'a property'")
c1.clear()
param = [ pya.LayerInfo(1, 0), 5.0, 10.0 ]
pcell_var_id = ly.add_pcell_variant(lib, pcell_decl_id, param)
pcell_var = ly.cell(pcell_var_id)
pcell_inst = c1.insert(pya.CellInstArray(pcell_var_id, pya.Trans()))
self.assertEqual(pcell_var.layout().__repr__(), ly.__repr__())
self.assertEqual(pcell_var.library().__repr__(), lib.__repr__())
self.assertEqual(pcell_var.is_pcell_variant(), True)
self.assertEqual(pcell_var.display_title(), "PCellTestLib.Box(L=1/0,W=5.000,H=10.000)")
self.assertEqual(pcell_var.basic_name(), "Box")
self.assertEqual(c1.is_pcell_variant(), False)
self.assertEqual(c1.is_pcell_variant(pcell_inst), True)
self.assertEqual(pcell_var.pcell_id(), pcell_decl_id)
self.assertEqual(pcell_var.pcell_library().__repr__(), lib.__repr__())
self.assertEqual(pcell_var.pcell_parameters().__repr__(), "[<1/0>, 5.0, 10.0]")
self.assertEqual(c1.pcell_parameters(pcell_inst).__repr__(), "[<1/0>, 5.0, 10.0]")
self.assertEqual(pcell_inst.pcell_parameters().__repr__(), "[<1/0>, 5.0, 10.0]")
self.assertEqual(pcell_var.pcell_declaration().__repr__(), pcell_decl.__repr__())
self.assertEqual(c1.pcell_declaration(pcell_inst).__repr__(), pcell_decl.__repr__())
li1 = find_layer(ly, "1/0")
self.assertEqual(li1 != None, True)
self.assertEqual(ly.is_valid_layer(li1), True)
self.assertEqual(str(ly.get_info(li1)), "1/0")
lib_proxy_id = ly.add_lib_cell(lib, lib.layout().cell_by_name("StaticBox"))
lib_proxy = ly.cell(lib_proxy_id)
self.assertEqual(lib_proxy.display_title(), "PCellTestLib.StaticBox")
self.assertEqual(lib_proxy.basic_name(), "StaticBox")
self.assertEqual(lib_proxy.layout().__repr__(), ly.__repr__())
self.assertEqual(lib_proxy.library().__repr__(), lib.__repr__())
self.assertEqual(lib_proxy.is_pcell_variant(), False)
self.assertEqual(lib.layout().cell(lib.layout().cell_by_name("StaticBox")).library().__repr__(), "None")
li2 = find_layer(ly, "10/0")
self.assertEqual(li2 != None, True)
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-250,-500;250,500)")
self.assertEqual(ly.begin_shapes(lib_proxy.cell_index(), li2).shape().__str__(), "box (0,0;10,20)")
param = { "w": 1, "h": 2 }
c1.change_pcell_parameters(pcell_inst, param)
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-50,-100;50,100)")
param = [ pya.LayerInfo(1, 0), 5.0, 5.0 ]
c1.change_pcell_parameters(pcell_inst, param)
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-250,-250;250,250)")
pcell_inst.change_pcell_parameters({ "w": 2.0, "h": 10.0 })
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-100,-500;100,500)")
pcell_inst.change_pcell_parameters([ pya.LayerInfo(1, 0), 5.0, 5.0 ])
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-250,-250;250,250)")
pcell_inst.change_pcell_parameter("w", 5.0)
pcell_inst.change_pcell_parameter("h", 1.0)
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-250,-50;250,50)")
c1.change_pcell_parameter(pcell_inst, "w", 10.0)
c1.change_pcell_parameter(pcell_inst, "h", 2.0)
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-500,-100;500,100)")
self.assertEqual(ly.cell(pcell_inst.cell_index).is_pcell_variant(), True)
self.assertEqual(pcell_inst.is_pcell(), True)
new_id = ly.convert_cell_to_static(pcell_inst.cell_index)
self.assertEqual(new_id == pcell_inst.cell_index, False)
self.assertEqual(ly.cell(new_id).is_pcell_variant(), False)
param = [ pya.LayerInfo(1, 0), 5.0, 5.0 ]
c1.change_pcell_parameters(pcell_inst, param)
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-250,-250;250,250)")
pcell_inst.cell_index = new_id
self.assertEqual(ly.begin_shapes(c1.cell_index(), li1).shape().__str__(), "box (-500,-100;500,100)")
def test_2(self):
# instantiate and register the library
tl = PCellTestLib()
ly = pya.Layout(True)
ly.dbu = 0.01
ci1 = ly.add_cell("c1")
c1 = ly.cell(ci1)
lib = pya.Library.library_by_name("PCellTestLib")
pcell_decl_id = lib.layout().pcell_id("Box")
param = [ pya.LayerInfo(1, 0), 10.0, 2.0 ]
pcell_var_id = ly.add_pcell_variant(lib, pcell_decl_id, param)
pcell_var = ly.cell(pcell_var_id)
pcell_inst = c1.insert(pya.CellInstArray(pcell_var_id, pya.Trans()))
li1 = find_layer(ly, "1/0")
self.assertEqual(li1 != None, True)
self.assertEqual(ly.is_valid_layer(li1), True)
self.assertEqual(str(ly.get_info(li1)), "1/0")
self.assertEqual(pcell_inst.is_pcell(), True)
self.assertEqual(ly.begin_shapes(pcell_inst.cell_index, li1).shape().__str__(), "box (-500,-100;500,100)")
pcell_inst.convert_to_static()
self.assertEqual(pcell_inst.is_pcell(), False)
self.assertEqual(ly.begin_shapes(pcell_inst.cell_index, li1).shape().__str__(), "box (-500,-100;500,100)")
pcell_inst.convert_to_static()
self.assertEqual(pcell_inst.is_pcell(), False)
self.assertEqual(ly.begin_shapes(pcell_inst.cell_index, li1).shape().__str__(), "box (-500,-100;500,100)")
def test_3(self):
# instantiate and register the library
tl = PCellTestLib()
ly = pya.Layout(True)
ly.dbu = 0.01
c1 = ly.create_cell("c1")
lib = pya.Library.library_by_name("PCellTestLib")
pcell_decl_id = lib.layout().pcell_id("Box")
param = { "w": 4.0, "h": 8.0, "l": pya.LayerInfo(1, 0) }
pcell_var_id = ly.add_pcell_variant(lib, pcell_decl_id, param)
pcell_var = ly.cell(pcell_var_id)
pcell_inst = c1.insert(pya.CellInstArray(pcell_var_id, pya.Trans()))
self.assertEqual(ly.begin_shapes(c1.cell_index(), ly.layer(1, 0)).shape().__str__(), "box (-200,-400;200,400)")
def test_4(self):
# instantiate and register the library
tl = PCellTestLib()
lib = pya.Library.library_by_name("PCellTestLib")
pcell_decl_id = lib.layout().pcell_id("Box")
param = { "w": 4.0, "h": 8.0, "l": pya.LayerInfo(1, 0) }
pcell_var_id = lib.layout().add_pcell_variant(pcell_decl_id, param)
self.assertEqual(lib.layout().begin_shapes(pcell_var_id, lib.layout().layer(1, 0)).shape().__str__(), "box (-2000,-4000;2000,4000)")
def test_5(self):
# instantiate and register the library
tl = PCellTestLib()
lib = pya.Library.library_by_name("PCellTestLib")
pcell_decl_id = lib.layout().pcell_id("Box")
param = { "w": 3.0, "h": 7.0, "l": pya.LayerInfo(2, 0) }
pcell_var_id = lib.layout().add_pcell_variant(pcell_decl_id, param)
self.assertEqual(lib.layout().begin_shapes(pcell_var_id, lib.layout().layer(2, 0)).shape().__str__(), "box (-1500,-3500;1500,3500)")
def test_6(self):
# instantiate and register the library
tl = PCellTestLib()
lib = pya.Library.library_by_name("PCellTestLib")
param = { "w": 3.0, "h": 8.0, "l": pya.LayerInfo(3, 0) }
pcell_var = lib.layout().create_cell("Box", param)
self.assertEqual(lib.layout().begin_shapes(pcell_var.cell_index(), lib.layout().layer(3, 0)).shape().__str__(), "box (-1500,-4000;1500,4000)")
def test_7(self):
# instantiate and register the library
tl = PCellTestLib()
ly = pya.Layout(True)
ly.dbu = 0.01
param = { "w": 4.0, "h": 8.0, "l": pya.LayerInfo(4, 0) }
cell = ly.create_cell("Box", "PCellTestLib", param)
self.assertEqual(ly.begin_shapes(cell, ly.layer(4, 0)).shape().__str__(), "box (-200,-400;200,400)")
def test_8(self):
# instantiate and register the library
tl = PCellTestLib()
lib = pya.Library.library_by_name("PCellTestLib")
ly = pya.Layout(True)
ly.dbu = 0.01
param = { "w": 2.0, "h": 6.0, "l": pya.LayerInfo(5, 0) }
pcell_var = lib.layout().create_cell("Box", param)
pcell_var.name = "BOXVAR"
cell = ly.create_cell("BOXVAR", "PCellTestLib")
self.assertEqual(cell.begin_shapes_rec(ly.layer(5, 0)).shape().__str__(), "box (-100,-300;100,300)")
# run unit tests
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(DBPCellTests)
if not unittest.TextTestRunner(verbosity = 1).run(suite).wasSuccessful():
sys.exit(1)
+31
View File
@@ -0,0 +1,31 @@
import pya
import unittest
import sys
class DBRegionTest(unittest.TestCase):
def test_1_Region(self):
r = pya.Region()
self.assertEqual(str(r), "")
r.insert(pya.Box(0, 100, 200, 300))
self.assertEqual(str(r), "(0,100;0,300;200,300;200,100)")
r2 = pya.Region(pya.Box(50, 150, 250, 350))
self.assertEqual(str(r2), "(50,150;50,350;250,350;250,150)")
r += r2
self.assertEqual(str(r), "(0,100;0,300;200,300;200,100);(50,150;50,350;250,350;250,150)")
r.merge()
self.assertEqual(str(r), "(0,100;0,300;50,300;50,350;250,350;250,150;200,150;200,100)")
# run unit tests
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(DBRegionTest)
if not unittest.TextTestRunner(verbosity = 1).run(suite).wasSuccessful():
sys.exit(1)
+465
View File
@@ -0,0 +1,465 @@
import pya
import unittest
import os
import sys
import gc
testlog = ""
# an event filter class
class EventFilter(pya.QObject):
_log = []
def log(self):
return self._log
def eventFilter(self, obj, event):
self._log.append(type(event).__name__ + ": " + repr(event.type()))
pya.QObject.eventFilter(self, obj, event)
# QAction implementation
class MyAction(pya.QAction):
ce = None
def __init__(self, p, n):
pya.QAction.__init__(self, p)
self.objectName = n
ce = None
def childEvent(self, ev):
if self.ce:
self.ce(ev)
def on_child_event(self, _ce):
self.ce = _ce
class MyStandardItemModel(pya.QStandardItemModel):
# make this method public
def srn(self, rn):
if hasattr(self.__class__, "setRoleNames"):
self.setRoleNames(rn)
else:
self.setItemRoleNames(rn)
# Another event filter
class MyObject(pya.QObject):
ef = None
def eventFilter(self, watched, event):
if self.ef(watched, event):
return True
return pya.QObject.eventFilter(self, watched, event)
def on_event_filter(self, ef):
self.ef = ef
# The Qt binding tests
class QtBindingTest(unittest.TestCase):
def test_00(self):
# all references of PA are released now:
pass
# ...
def test_10(self):
a = MyAction(None, "a")
a.text = "mytext"
a.checkable = True
self.assertEqual(a.isChecked(), False)
a.checked = True
self.assertEqual(a.text, "mytext")
self.assertEqual(a.objectName, "a")
a.text += "."
self.assertEqual(a.text, "mytext.")
self.assertEqual(a.checked, True)
global testlog
testlog = ""
def f(checked):
global testlog
testlog += "[" + str(checked) + "]"
a.triggered(f)
self.assertEqual(testlog, "")
a.trigger() # also toggles checked state
self.assertEqual(testlog, "[False]")
testlog = ""
a.trigger() # also toggles checked state
self.assertEqual(testlog, "[True]")
def test_11(self):
a = pya.QAction(None)
aa = MyAction(a, "aa")
self.assertEqual(aa.objectName, "aa")
# destroying a will also destroy aa
a.destroy()
self.assertEqual(a.destroyed(), True)
self.assertEqual(aa.destroyed(), True)
def test_12(self):
a = pya.QAction(None)
aa = pya.QAction(a)
aa.objectName = "aa"
# destroying a will also destroy aa
a = None
self.assertEqual(aa.destroyed(), True)
def test_13(self):
a = pya.QAction(None)
aa = pya.QAction(a)
aa.objectName = "aa"
aa.text = "aatext"
cc = []
for c in a.children():
cc.append(c.objectName)
self.assertEqual(",".join(cc), "aa")
# aa now is kept by a
aa = None
# fetch aa again
for c in a.children():
if c.objectName == "aa":
aa = c
self.assertEqual(aa != None, True)
self.assertEqual(type(aa), pya.QAction)
self.assertEqual(aa.text, "aatext")
self.assertEqual(aa.destroyed(), False)
def test_20(self):
global no_event
global ce_log
no_event = False
def event_filter(watched, event):
global no_event
return no_event
ef = MyObject()
ef.on_event_filter(event_filter)
def child_event(ce):
global ce_log
ce_log.append(str(ce.added()) + ":" + ce.child().objectName)
ce_log = []
a = MyAction(None, "a")
a.on_child_event(child_event)
a.installEventFilter(ef)
aa = MyAction(None, "aa")
self.assertEqual(",".join(ce_log), "")
aa.setParent(a)
self.assertEqual(",".join(ce_log), "True:aa")
ce_log = []
# destroy aa
aa.destroy()
aa = None
self.assertEqual(",".join(ce_log), "False:aa")
ce_log = []
no_event = True
aa = MyAction(None, "aa")
aa.setParent(a)
self.assertEqual(",".join(ce_log), "")
ce_log = []
no_event = False
aa.destroy()
aa = None
self.assertEqual(",".join(ce_log), "False:aa")
ce_log = []
def test_30(self):
# dialog construction, cleanup, object dependency ...
mw = None
dialog = pya.QDialog(mw)
label = pya.QLabel(dialog)
layout = pya.QHBoxLayout(dialog)
layout.addWidget(label)
dialog = pya.QDialog(mw)
label = pya.QLabel(dialog)
layout = pya.QHBoxLayout(dialog)
layout.addWidget(label)
label.destroy()
dialog = pya.QDialog(mw)
label = pya.QLabel(dialog)
layout = pya.QHBoxLayout(dialog)
layout.addWidget(label)
layout.destroy()
dialog = pya.QDialog(mw)
label = pya.QLabel(dialog)
layout = pya.QHBoxLayout(dialog)
layout.addWidget(label)
dialog.destroy()
dialog = pya.QDialog(mw)
label = pya.QLabel(dialog)
layout = pya.QHBoxLayout(dialog)
layout.addWidget(label)
dialog = None
label = None
layout = None
def test_31(self):
# Optional arguments, enums, QFlag's
mw = None
mb = pya.QMessageBox(pya.QMessageBox.Critical, "title", "text")
self.assertEqual(mb.icon.to_i() != pya.QMessageBox.Warning.to_i(), True)
self.assertEqual(mb.icon.to_i() == pya.QMessageBox.Critical.to_i(), True)
self.assertEqual(mb.standardButtons.to_i() == pya.QMessageBox.NoButton.to_i(), True)
mb = pya.QMessageBox(pya.QMessageBox.Critical, "title", "text", pya.QMessageBox.Ok)
self.assertEqual(mb.standardButtons.to_i() == pya.QMessageBox.Ok.to_i(), True)
mb = pya.QMessageBox(pya.QMessageBox.Critical, "title", "text", pya.QMessageBox.Ok | pya.QMessageBox.Cancel)
self.assertEqual(mb.standardButtons.to_i() == pya.QMessageBox.Ok.to_i() + pya.QMessageBox.Cancel.to_i(), True)
def test_40(self):
# Lifetime management of objects/methods not using QObject.parent
# QTreeWidget (parent)/QTreeWidgetItem (child)
# constructor with parent-like argument:
tw = pya.QTreeWidget()
ti = pya.QTreeWidgetItem(tw)
# strange, but true:
self.assertEqual(ti.parent(), None)
self.assertEqual(tw.topLevelItemCount, 1)
ti = None
# gives 1, because the tree widget item is kept by
# the tree widget:
self.assertEqual(tw.topLevelItemCount, 1)
# the tree item belongs to the widget, hence it's destroyed with
# the widget
ti = tw.topLevelItem(0)
tw._destroy()
# gives true, because tw owns ti too.
self.assertEqual(ti._destroyed(), True)
# The same works for insert too
tw = pya.QTreeWidget()
ti = pya.QTreeWidgetItem()
tw.insertTopLevelItem(0, ti)
self.assertEqual(tw.topLevelItemCount, 1)
ti = None
# gives 1, because the tree widget item is kept by
# the tree widget:
self.assertEqual(tw.topLevelItemCount, 1)
# the tree item belongs to the widget, hence it's destroyed with
# the widget
ti = tw.topLevelItem(0)
tw._destroy()
# gives true, because tw owns ti
self.assertEqual(ti._destroyed(), True)
# And add:
tw = pya.QTreeWidget()
ti = pya.QTreeWidgetItem()
tw.addTopLevelItem(ti)
self.assertEqual(tw.topLevelItemCount, 1)
ti = None
# gives 1, because the tree widget item is kept by
# the tree widget:
self.assertEqual(tw.topLevelItemCount, 1)
# the tree item belongs to the widget, hence it's destroyed with
# the widget
ti = tw.topLevelItem(0)
tw._destroy()
# gives true, because tw owns ti
self.assertEqual(ti._destroyed(), True)
# But the item is released when we take it and add:
tw = pya.QTreeWidget()
ti = pya.QTreeWidgetItem()
tw.addTopLevelItem(ti)
self.assertEqual(tw.topLevelItemCount, 1)
ti = None
# gives 1, because the tree widget item is kept by
# the tree widget:
self.assertEqual(tw.topLevelItemCount, 1)
ti = tw.takeTopLevelItem(0)
tw._destroy()
# gives false, because we took ti and tw no longer owns it
self.assertEqual(ti._destroyed(), False)
# And we can destroy a child too
tw = pya.QTreeWidget()
ti = pya.QTreeWidgetItem()
tw.addTopLevelItem(ti)
self.assertEqual(tw.topLevelItemCount, 1)
ti._destroy()
self.assertEqual(tw.topLevelItemCount, 0)
def test_41(self):
# Lifetime management of objects/methods not using QObject.parent
# QTreeWidgetItem (parent)/QTreeWidgetItem (child)
# constructor with parent-like argument (supported by QObject parent/child relationship):
tw = pya.QTreeWidgetItem()
ti = pya.QTreeWidgetItem(tw)
# that's not QObject.parent - this one still is 0 (not seen by RBA)
self.assertEqual(ti.parent(), tw)
self.assertEqual(tw.childCount(), 1)
ti = None
# gives 1, because the tree widget item is kept by
# the tree widget:
self.assertEqual(tw.childCount(), 1)
# the tree item belongs to the widget, hence it's destroyed with
# the widget
ti = tw.child(0)
tw._destroy()
# gives true, because tw owns ti too.
self.assertEqual(ti._destroyed(), True)
# The same works for insert too
tw = pya.QTreeWidgetItem()
ti = pya.QTreeWidgetItem()
tw.insertChild(0, ti)
self.assertEqual(tw.childCount(), 1)
ti = None
# gives 1, because the tree widget item is kept by
# the tree widget:
self.assertEqual(tw.childCount(), 1)
# the tree item belongs to the widget, hence it's destroyed with
# the widget
ti = tw.child(0)
tw._destroy()
# gives true, because tw owns ti
self.assertEqual(ti._destroyed(), True)
# And add:
tw = pya.QTreeWidgetItem()
ti = pya.QTreeWidgetItem()
tw.addChild(ti)
self.assertEqual(tw.childCount(), 1)
ti = None
# gives 1, because the tree widget item is kept by
# the tree widget:
self.assertEqual(tw.childCount(), 1)
# the tree item belongs to the widget, hence it's destroyed with
# the widget
ti = tw.child(0)
tw._destroy()
# gives true, because tw owns ti
self.assertEqual(ti._destroyed(), True)
# But the item is released when we take it and add:
tw = pya.QTreeWidgetItem()
ti = pya.QTreeWidgetItem()
tw.addChild(ti)
self.assertEqual(tw.childCount(), 1)
ti = None
# gives 1, because the tree widget item is kept by
# the tree widget:
self.assertEqual(tw.childCount(), 1)
ti = tw.takeChild(0)
tw._destroy()
# gives false, because we took ti and tw no longer owns it
self.assertEqual(ti._destroyed(), False)
# And we can destroy a child too
tw = pya.QTreeWidgetItem()
ti = pya.QTreeWidgetItem()
tw.addChild(ti)
self.assertEqual(tw.childCount(), 1)
ti._destroy()
self.assertEqual(tw.childCount(), 0)
def test_42(self):
# QKeyEvent and related issues
ef = EventFilter()
widget = pya.QLineEdit()
widget.setText("ABC")
pya.QApplication.processEvents()
widget.installEventFilter(ef)
ke = pya.QKeyEvent(pya.QEvent.KeyPress, pya.Qt.Key_O.to_i(), pya.Qt.ShiftModifier, "O")
pya.QCoreApplication.postEvent(widget, ke)
ke = pya.QKeyEvent(pya.QEvent.KeyPress, pya.Qt.Key_Left.to_i(), pya.Qt.NoModifier)
pya.QCoreApplication.postEvent(widget, ke)
ke = pya.QKeyEvent(pya.QEvent.KeyPress, pya.Qt.Key_P.to_i(), pya.Qt.NoModifier, "p")
pya.QCoreApplication.postEvent(widget, ke)
pya.QApplication.processEvents()
s1 = "QKeyEvent: ShortcutOverride (51)\nQKeyEvent: KeyPress (6)\nQKeyEvent: ShortcutOverride (51)\nQKeyEvent: KeyPress (6)\nQKeyEvent: ShortcutOverride (51)\nQKeyEvent: KeyPress (6)"
s2 = "QKeyEvent: KeyPress (6)\nQKeyEvent: KeyPress (6)\nQKeyEvent: KeyPress (6)"
self.assertEqual("\n".join(ef.log()) == s1 or "\n".join(ef.log()) == s2, True)
ef = None
self.assertEqual(widget.text, "ABCpO")
widget = None
def test_43(self):
# QHash bindings
slm = MyStandardItemModel()
r1 = "{0: \'display\', 1: \'decoration\', 2: \'edit\', 3: \'toolTip\', 4: \'statusTip\', 5: \'whatsThis\'}"
r2 = "{0L: \'display\', 1L: \'decoration\', 2L: \'edit\', 3L: \'toolTip\', 4L: \'statusTip\', 5L: \'whatsThis\'}"
self.assertEqual(str(slm.roleNames()) == r1 or str(slm.roleNames()) == r2, True)
rn = slm.roleNames()
rn[7] = "blabla"
slm.srn(rn)
r1 = "{0: \'display\', 1: \'decoration\', 2: \'edit\', 3: \'toolTip\', 4: \'statusTip\', 5: \'whatsThis\', 7: \'blabla\'}"
r2 = "{0L: \'display\', 1L: \'decoration\', 2L: \'edit\', 3L: \'toolTip\', 4L: \'statusTip\', 5L: \'whatsThis\', 7L: \'blabla\'}"
self.assertEqual(str(slm.roleNames()) == r1 or str(slm.roleNames()) == r2, True)
# run unit tests
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(QtBindingTest)
if not unittest.TextTestRunner(verbosity = 1).run(suite).wasSuccessful():
sys.exit(1)
+198
View File
@@ -0,0 +1,198 @@
import pya
import unittest
import sys
class TLTest(unittest.TestCase):
def test_1(self):
expr = pya.Expression()
res = expr.eval()
self.assertEqual(str(type(res)).replace("class", "type"), "<type 'NoneType'>")
self.assertEqual(repr(res), "None")
expr = pya.Expression.eval("1+2")
self.assertEqual(str(type(expr)).replace("class", "type"), "<type 'float'>")
self.assertEqual(repr(expr), "3.0")
expr = pya.Expression()
expr.text = "1+2"
res = expr.eval()
self.assertEqual(str(type(res)).replace("class", "type"), "<type 'float'>")
self.assertEqual(str(res), "3.0")
expr = pya.Expression()
expr.var("a", 5)
expr.text = "a+to_i(2)"
res = expr.eval()
self.assertEqual(str(type(res)).replace("class", "type").replace("int", "long"), "<type 'long'>")
self.assertEqual(str(res), "7")
expr.var("a", 7)
res = expr.eval()
self.assertEqual(str(res), "9")
pya.Expression.global_var("xxx", 17.5)
expr = pya.Expression("xxx+1")
res = expr.eval()
self.assertEqual(str(type(res)).replace("class", "type"), "<type 'float'>")
self.assertEqual(str(res), "18.5")
expr = pya.Expression("a+b*2", { "a": 18, "b": 2.5 })
res = expr.eval()
self.assertEqual(str(type(res)).replace("class", "type"), "<type 'float'>")
self.assertEqual(str(res), "23.0")
expr = pya.Expression("[a[1],a[2],a[0],a[4],a[3]]", { "a": [ 17, "a", None, [ 2, 7 ], { 8: "x", "u": 42 } ] })
res = expr.eval()
self.assertEqual(str(type(res)).replace("class", "type"), "<type 'list'>")
self.assertEqual(str(res) == "['a', None, 17L, {8L: 'x', 'u': 42L}, [2L, 7L]]" or str(res) == "['a', None, 17, {8: 'x', 'u': 42}, [2, 7]]", True)
expr = pya.Expression("a[1]", { "a": [ 17, "a", None, [ 2, 7 ], { 8: "x", "u": 42 } ] })
res = expr.eval()
self.assertEqual(str(type(res)).replace("class", "type"), "<type 'str'>")
self.assertEqual(str(res), "a")
expr = pya.Expression("a[4]", { "a": [ 17, "a", None, [ 2, 7 ], { 8: "x", "u": 42 } ] })
res = expr.eval()
self.assertEqual(str(type(res)).replace("class", "type"), "<type 'dict'>")
self.assertEqual(str(res) == "{8L: 'x', 'u': 42L}" or str(res) == "{8: 'x', 'u': 42}", True)
# Advanced expressions
def test_2_Expression(self):
box1 = pya.Box(0, 100, 200, 300)
box2 = pya.Box(50, 150, 250, 350)
expr = pya.Expression("a", { "a": box1, "b": box2 })
res = expr.eval()
self.assertEqual(str(res), "(0,100;200,300)")
# boxes are non-managed objects -> passing the object through the expression does not persist their ID
self.assertNotEqual(id(res), id(box1))
self.assertNotEqual(id(res), id(box2))
# -------------------------------------------------
box1 = pya.Box(0, 100, 200, 300)
box2 = pya.Box(50, 150, 250, 350)
expr = pya.Expression("a&b", { "a": box1, "b": box2 })
res = expr.eval()
self.assertEqual(str(res), "(50,150;200,300)")
# computed objects are entirely new ones
self.assertNotEqual(id(res), id(box1))
self.assertNotEqual(id(res), id(box2))
# -------------------------------------------------
box1 = pya.Box(0, 100, 200, 300)
box2 = pya.Box(50, 150, 250, 350)
expr = pya.Expression("x=a&b; y=x; z=y; [x,y,z]", { "a": box1, "b": box2, "x": None, "y": None, "z": None })
res = expr.eval()
self.assertEqual(str(res), "[(50,150;200,300), (50,150;200,300), (50,150;200,300)]")
# all objects are individual copies
self.assertNotEqual(id(res[0]), id(box1))
self.assertNotEqual(id(res[0]), id(box2))
self.assertNotEqual(id(res[1]), id(res[0]))
self.assertNotEqual(id(res[2]), id(res[0]))
# -------------------------------------------------
box1 = pya.Box(0, 100, 200, 300)
box2 = pya.Box(50, 150, 250, 350)
expr = pya.Expression("var x=a&b; var y=x; var z=y; [x,y,z]", { "a": box1, "b": box2 })
res = expr.eval()
self.assertEqual(str(res), "[(50,150;200,300), (50,150;200,300), (50,150;200,300)]")
# all objects are individual copies
self.assertNotEqual(id(res[0]), id(box1))
self.assertNotEqual(id(res[0]), id(box2))
self.assertNotEqual(id(res[1]), id(res[0]))
self.assertNotEqual(id(res[2]), id(res[0]))
# destruction of the expression's object space does not matter since we have copies
expr._destroy()
self.assertEqual(str(res), "[(50,150;200,300), (50,150;200,300), (50,150;200,300)]")
# -------------------------------------------------
region1 = pya.Region()
region1 |= pya.Box(0, 100, 200, 300)
region2 = pya.Region()
region2 |= pya.Box(50, 150, 250, 350)
expr = pya.Expression("a", { "a": region1, "b": region2 })
res = expr.eval()
# regions are managed objects -> passing the object through the expression persists it's object ID
self.assertEqual(id(res), id(region1))
self.assertNotEqual(id(res), id(region2))
# -------------------------------------------------
region1 = pya.Region()
region1 |= pya.Box(0, 100, 200, 300)
region2 = pya.Region()
region2 |= pya.Box(50, 150, 250, 350)
expr = pya.Expression("a&b", { "a": region1, "b": region2, "x": None, "y": None, "z": None })
res = expr.eval()
self.assertEqual(str(res), "(50,150;50,300;200,300;200,150)")
# The returned object (as a new one) is an entirely fresh one
self.assertNotEqual(id(res), id(region1))
self.assertNotEqual(id(res), id(region2))
# -------------------------------------------------
region1 = pya.Region()
region1 |= pya.Box(0, 100, 200, 300)
region2 = pya.Region()
region2 |= pya.Box(50, 150, 250, 350)
expr = pya.Expression("x=a&b; y=x; z=y; [x,y,z]", { "a": region1, "b": region2, "x": None, "y": None, "z": None })
res = expr.eval()
self.assertEqual(str(res), "[(50,150;50,300;200,300;200,150), (50,150;50,300;200,300;200,150), (50,150;50,300;200,300;200,150)]")
# regions are managed objects -> passing the object through the expression persists it's object ID
self.assertNotEqual(id(res[0]), id(region1))
self.assertNotEqual(id(res[0]), id(region2))
self.assertEqual(id(res[1]), id(res[0]))
self.assertEqual(id(res[2]), id(res[0]))
# -------------------------------------------------
region1 = pya.Region()
region1 |= pya.Box(0, 100, 200, 300)
region2 = pya.Region()
region2 |= pya.Box(50, 150, 250, 350)
expr = pya.Expression("var x=a&b; var y=x; var z=y; [x,y,z]", { "a": region1, "b": region2 })
res = expr.eval()
self.assertEqual(str(res), "[(50,150;50,300;200,300;200,150), (50,150;50,300;200,300;200,150), (50,150;50,300;200,300;200,150)]")
# regions are managed objects -> passing the object through the expression persists it's object ID
self.assertNotEqual(id(res[0]), id(region1))
self.assertNotEqual(id(res[0]), id(region2))
self.assertEqual(id(res[1]), id(res[0]))
self.assertEqual(id(res[2]), id(res[0]))
# the result objects live in the expression object space and are destroyed with the expression
expr._destroy()
self.assertEqual(len(res), 3)
self.assertEqual(res[0].destroyed(), True)
self.assertEqual(res[1].destroyed(), True)
self.assertEqual(res[2].destroyed(), True)
# run unit tests
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TLTest)
if not unittest.TextTestRunner(verbosity = 1).run(suite).wasSuccessful():
sys.exit(1)