def testModifierFromCell(self): v1, v2 = trellis.Value(42), trellis.Value(99) @trellis.modifier def do_it(): v1.value = v1.value * 2 self.assertEqual(self.ctrl.reads, {v1: 1}) def rule(): v2.value do_it() self.assertEqual(self.ctrl.reads, {v2: 1}) trellis.Cell(rule).value self.assertEqual(v1.value, 84)
def testValueUsed(self): v = trellis.Value(42) ctrl = self.mocker.replace(self.ctrl) #'peak.events.stm.ctrl') ctrl.used(v) self.mocker.replay() trellis.install_controller(ctrl) self.assertEqual(v.value, 42)
def testSelfDependencyDoesNotIncreaseLayer(self): c1 = trellis.Value(23) c2 = trellis.Cell(lambda: c1.value + c2.value, 0) self.assertEqual(c2.value, 23) self.assertEqual(c2.layer, 1) c1.value = 19 self.assertEqual(c2.value, 42) self.assertEqual(c2.layer, 1)
def testValueUndo(self): v = trellis.Value(42) self.assertEqual(v.value, 42) sp = self.ctrl.savepoint() v.value = 43 self.assertEqual(v.value, 43) self.ctrl.rollback_to(sp) self.assertEqual(v.value, 42)
def testUndoLogSpansMultipleRecalcs(self): c1 = trellis.Value(False, discrete=True) c2 = trellis.Cell(lambda: (c1.value, log.append(trellis.savepoint()))) log = [] c2.value log = [] c1.value = True self.failUnless(len(log) == 2 and log[1] > log[0], log)
def testSettingOccursForEqualObjects(self): d1 = {} d2 = {} c1 = trellis.Value() c1.value = d1 self.failUnless(c1.value is d1) c1.value = d2 self.failUnless(c1.value is d2)
def testValueUnchanged(self): v = trellis.Value(42) ctrl = self.mocker.replace(self.ctrl) ctrl.lock(v) mocker.expect(ctrl.changed(v)).count(0) self.mocker.replay() trellis.install_controller(ctrl) v.value = 42 self.assertEqual(v.value, 42)
def testValueChanged(self): v = trellis.Value(42) old_ctrl, ctrl = self.ctrl, self.mocker.replace(self.ctrl) ctrl.lock(v) ctrl.changed(v) self.mocker.replay() trellis.install_controller(ctrl) v.value = 43 self.assertEqual(v.value, 43)
def testValueBasics(self): self.failUnless(issubclass(trellis.Value, trellis.AbstractCell)) self.failUnless(issubclass(trellis.Value, stm.AbstractSubject)) v = trellis.Value() self.assertEqual(v.value, None) self.assertEqual(v._set_by, trellis._sentinel) self.assertEqual(v._reset, trellis._sentinel) v.value = 21 self.assertEqual(v._set_by, trellis._sentinel)
def testResumeOnlyOnceUntilFlushed(self): log = [] c1 = trellis.Value(1) c2 = trellis.Value(2) def f(): for i in range(3): c1.value, c2.value log.append(i) yield activity.Pause t = activity.TaskCell(f) self.assertEqual(log, []) EventLoop.flush() self.assertEqual(log, [0]) c1.value = 3 self.assertEqual(log, [0]) c2.value = 4 EventLoop.flush() self.assertEqual(log, [0, 1])
def testRuleChain(self): v = trellis.Value(0) log = [] c1 = trellis.Cell(lambda: int(v.value / 2)) c2 = trellis.Cell(lambda: log.append(c1.value)) c2.value self.assertEqual(log, [0]) v.value = 1 self.assertEqual(log, [0]) v.value = 2 self.assertEqual(log, [0, 1])
def testDependencyAndCallback(self): log = [] v = trellis.Value(42) v1 = trellis.Value(1) c1 = trellis.Cell(lambda: v1.value * 2) def f(): while v.value: log.append(v.value) v1.value = v.value yield activity.Pause t = activity.TaskCell(f) check = [] for j in 42, 57, 99, 106, 23, None: self.assertEqual(log, check) v.value = j if j: check.append(j) for i in range(5): t._loop.flush() if j: self.assertEqual(c1.value, j * 2) self.assertEqual(log, check)
def testDiscreteValue(self): log = [] v = trellis.Value(False, True) c = trellis.Cell(lambda: log.append(v.value)) self.assertEqual(log, []) c.value self.assertEqual(log, [False]) del log[:] v.value = True self.assertEqual(log, [True, False]) self.assertEqual(v.value, False) del log[:] v.value = False self.assertEqual(log, [])
def testSetAfterSchedule(self): def A(): B.value C.value A = trellis.Cell(A, None) @trellis.Cell def B(): A.value = C.value C = trellis.Value() A.value C.value = 1
def testUndoPostCommitCancelsUndoOfCommitSchedule(self): c1 = trellis.Value(False, discrete=True) def c2(): c1.value log.append(trellis.savepoint()) if len(log) == 2: raise DummyError c2 = trellis.Cell(c2) log = [] c2.value log = [] # This will raise a different error if undoing the on-commit stack # causes an underflow: self.assertRaises(DummyError, setattr, c1, 'value', True)
def testValueSetLock(self): v = trellis.Value(42) v.value = 43 self.assertEqual(v.value, 43) self.assertEqual(v._set_by, None) def go(): v.value = 99 t = TestListener() t.name = 't' t.run = go self.assertRaises(trellis.InputConflict, self.ctrl.run_rule, t) self.assertEqual(v.value, 43) def go(): v.value = 43 t = TestListener() t.name = 't' t.run = go self.ctrl.run_rule(t) self.assertEqual(v.value, 43)
def testDiscrete(self): v = trellis.Value(None, True) v.value = 42 self.assertEqual(v.value, None)