Exemplo n.º 1
0
class TestAdvancedBag(unittest.TestCase):
    def setUp(self):
        self.bag = Bag()
        self.bag.setBackRef()
        self.bag["a.b.c.d"] = 4
        self.bag["a.b.e"] = 5

    def testparent(self):
        self.assertEqual(self.bag["a.b.c"].parent, self.bag["a.b"])
        c = self.bag["a.b.c"]
        self.assertEqual(c["../e"], 5)
        self.bag["a.b"].delParentRef()
        self.assertFalse(self.bag["a.b"].backref)

    def testformula(self):
        self.bag["product"] = self.bag.formula("$x*$y", x="a.b.c.d", y="a.b.e")
        self.assertEqual(self.bag["product"], self.bag["a.b.c.d"] * self.bag["a.b.e"])

        self.bag.defineFormula(calculate_perimeter="2*($base + $height)")
        self.bag.defineSymbol(base="a.b.c.d", height="a.b.e")
        self.bag["perimeter"] = self.bag.formula("calculate_perimeter")
        self.assertEqual(self.bag["perimeter"], 18)

    def testcallbackitem(self):
        def hello():
            return "Hello!"

        self.bag.setCallBackItem("say_hello", hello)
        self.assertEqual(self.bag["say_hello"], hello())

    def testnodetrigger(self):
        self.lastupdates = []

        def mycallback(node, info=None, evt=None):
            self.lastupdates.append((node.getLabel(), info, node.getValue()))

        self.bag.getNode("a.b.c.d").subscribe("lastupdates", mycallback)
        self.bag["a.b.c.d"] = 20
        self.assertEqual(self.lastupdates[-1], ("d", 4, 20))

    def testbagtriggers(self):
        self.log = []

        def log_upd(node, pathlist, oldvalue, evt):
            self.log.append((".".join(pathlist), node.getValue(), oldvalue, evt))

        def log_ins(node, pathlist, ind, evt):
            self.log.append((".".join(pathlist), node.getValue(), ind, evt))

        def log_del(node, pathlist, ind, evt):
            self.log.append((".".join(pathlist), node.getValue(), ind, evt))

        self.bag.subscribe("log", update=log_upd, insert=log_ins, delete=log_del)
        self.bag["a.b.t"] = 45
        self.assertEqual(self.log[-1], ("a.b", 45, 2, "ins"))
        self.bag["a.b.t"] = 56
        self.assertEqual(self.log[-1], ("a.b.t", 56, 45, "upd_value"))
        self.bag.delItem("a.b.t")
        self.assertEqual(self.log[-1], ("a.b", 56, 2, "del"))
Exemplo n.º 2
0
class TestAdvancedBag(unittest.TestCase):
    def setUp(self):
        self.bag = Bag()
        self.bag.setBackRef()
        self.bag['a.b.c.d'] = 4
        self.bag['a.b.e'] = 5

    def testparent(self):
        self.assertEqual(self.bag['a.b.c'].parent, self.bag['a.b'])
        c = self.bag['a.b.c']
        self.assertEqual(c['../e'], 5)
        self.bag['a.b'].delParentRef()
        self.assertFalse(self.bag['a.b'].backref)

    def testformula(self):
        self.bag['product'] = self.bag.formula('$x*$y', x='a.b.c.d', y='a.b.e')
        self.assertEqual(self.bag['product'],
                         self.bag['a.b.c.d'] * self.bag['a.b.e'])

        self.bag.defineFormula(calculate_perimeter='2*($base + $height)')
        self.bag.defineSymbol(base='a.b.c.d', height='a.b.e')
        self.bag['perimeter'] = self.bag.formula('calculate_perimeter')
        self.assertEqual(self.bag['perimeter'], 18)

    def testcallbackitem(self):
        def hello():
            return 'Hello!'

        self.bag.setCallBackItem('say_hello', hello)
        self.assertEqual(self.bag['say_hello'], hello())

    def testnodetrigger(self):
        self.lastupdates = []

        def mycallback(node, info=None, evt=None):
            self.lastupdates.append((node.getLabel(), info, node.getValue()))

        self.bag.getNode('a.b.c.d').subscribe('lastupdates', mycallback)
        self.bag['a.b.c.d'] = 20
        self.assertEqual(self.lastupdates[-1], ('d', 4, 20))

    def testbagtriggers(self):
        self.log = []

        def log_upd(node, pathlist, oldvalue, evt):
            self.log.append(
                ('.'.join(pathlist), node.getValue(), oldvalue, evt))

        def log_ins(node, pathlist, ind, evt):
            self.log.append(('.'.join(pathlist), node.getValue(), ind, evt))

        def log_del(node, pathlist, ind, evt):
            self.log.append(('.'.join(pathlist), node.getValue(), ind, evt))

        self.bag.subscribe('log',
                           update=log_upd,
                           insert=log_ins,
                           delete=log_del)
        self.bag['a.b.t'] = 45
        self.assertEqual(self.log[-1], ('a.b', 45, 2, 'ins'))
        self.bag['a.b.t'] = 56
        self.assertEqual(self.log[-1], ('a.b.t', 56, 45, 'upd_value'))
        self.bag.delItem('a.b.t')
        self.assertEqual(self.log[-1], ('a.b', 56, 2, 'del'))