def test_conditional_long(self): state = dict(target=[], condition=True) tree = behaviours.conditional("condition", condition=behaviours.evalb("check", lambda state: state['condition']), true=behaviours.repeat(behaviours.do("success", lambda state: state['target'].append(True))), false=behaviours.repeat(behaviours.do("fail", lambda state: state['target'].append(False))), ) running, success = tree.tick(state) self.assertEqual(running, True) self.assertEqual(state['target'], [True]) running, success = tree.tick(state) self.assertEqual(running, True) self.assertEqual(state['target'], [True, True]) state['condition'] = False running, success = tree.tick(state) self.assertEqual(running, True) self.assertEqual(state['target'], [True, True, False]) state['condition'] = True running, success = tree.tick(state) self.assertEqual(running, True) self.assertEqual(state['target'], [True, True, False, True])
def test_conditional_long(self): state = dict(target=[], condition=True) tree = behaviours.conditional( "condition", condition=behaviours.evalb("check", lambda state: state['condition']), true=behaviours.repeat( behaviours.do("success", lambda state: state['target'].append(True))), false=behaviours.repeat( behaviours.do("fail", lambda state: state['target'].append(False))), ) running, success = tree.tick(state) self.assertEqual(running, True) self.assertEqual(state['target'], [True]) running, success = tree.tick(state) self.assertEqual(running, True) self.assertEqual(state['target'], [True, True]) state['condition'] = False running, success = tree.tick(state) self.assertEqual(running, True) self.assertEqual(state['target'], [True, True, False]) state['condition'] = True running, success = tree.tick(state) self.assertEqual(running, True) self.assertEqual(state['target'], [True, True, False, True])
def test_conditional(self): tree = behaviours.conditional("condition", condition=behaviours.evalb("check", lambda state: True), true=behaviours.do("exito", lambda state: state.append(True)), false=behaviours.do("fail", lambda state: state.append(False)), ) target = [] running, success = tree.tick(target) self.assertEqual(running, False) self.assertEqual(success, True) self.assertEqual(target, [True])
def test_conditional(self): tree = behaviours.conditional( "condition", condition=behaviours.evalb("check", lambda state: True), true=behaviours.do("exito", lambda state: state.append(True)), false=behaviours.do("fail", lambda state: state.append(False)), ) target = [] running, success = tree.tick(target) self.assertEqual(running, False) self.assertEqual(success, True) self.assertEqual(target, [True])
def test_sequence(self): target = [] tree = behaviours.sequence("append two", behaviours.do("call it1", lambda state: state.append(1)), behaviours.wait(1), behaviours.do("call it2", lambda state: state.append(2)), ) running, success = tree.tick(target) self.assertEqual(running, True) self.assertEqual(target, [1]) running, success = tree.tick(target) self.assertEqual(running, False) self.assertEqual(success, True) self.assertEqual(target, [1, 2])
def test_do(self): target = [] tree = behaviours.do("call_it", lambda state: state.append(True)) running, success = tree.tick(target) self.assertEqual(running, False) self.assertEqual(success, True) self.assertEqual(target, [True])
def test_sequence(self): target = [] tree = behaviours.sequence( "append two", behaviours.do("call it1", lambda state: state.append(1)), behaviours.wait(1), behaviours.do("call it2", lambda state: state.append(2)), ) running, success = tree.tick(target) self.assertEqual(running, True) self.assertEqual(target, [1]) running, success = tree.tick(target) self.assertEqual(running, False) self.assertEqual(success, True) self.assertEqual(target, [1, 2])
def test_repeat(self): target = [] tree = behaviours.repeat(behaviours.do("success", lambda state: state.append(True))) running, success = tree.tick(target) self.assertEqual(running, True) self.assertEqual(target, [True]) running, success = tree.tick(target) self.assertEqual(running, True) self.assertEqual(target, [True, True])
def test_sequence_fail(self): target = [] tree = behaviours.sequence("append two", behaviours.evalb("fail", lambda s: False), behaviours.do("call it", lambda s: target.append(2)), ) running, success = tree.tick() self.assertEqual(running, False) self.assertEqual(success, False)
def test_repeat(self): target = [] tree = behaviours.repeat( behaviours.do("success", lambda state: state.append(True))) running, success = tree.tick(target) self.assertEqual(running, True) self.assertEqual(target, [True]) running, success = tree.tick(target) self.assertEqual(running, True) self.assertEqual(target, [True, True])
def test_sequence_fail(self): target = [] tree = behaviours.sequence( "append two", behaviours.evalb("fail", lambda s: False), behaviours.do("call it", lambda s: target.append(2)), ) running, success = tree.tick() self.assertEqual(running, False) self.assertEqual(success, False)
def test_parallel_until(self): s = dict(stop=False, count=0) tree = behaviours.untilb("repeat until", behaviours.evalb("check", lambda state: state['stop']), behaviours.notb(behaviours.do("incr", lambda state: state.__setitem__("count", state["count"] + 1))), ) running, success = tree.tick(s) self.assertEqual(running, True) self.assertEqual(s['count'], 1) running, success = tree.tick(s) self.assertEqual(running, True) self.assertEqual(s['count'], 2) s['stop'] = True running, success = tree.tick(s) self.assertEqual(running, False) self.assertEqual(success, True) self.assertEqual(s['count'], 3)
def test_parallel_while(self): s = dict(stop=False, count=0) tree = behaviours.whileb( "repeat while", behaviours.evalb("check", lambda state: not state['stop']), behaviours.do( "count", lambda state: state.__setitem__("count", state["count"] + 1))) running, success = tree.tick(s) self.assertEqual(running, True) self.assertEqual(s['count'], 1) running, success = tree.tick(s) self.assertEqual(running, True) self.assertEqual(s['count'], 2) s['stop'] = True running, success = tree.tick(s) self.assertEqual(running, False) self.assertEqual(success, True) self.assertEqual(s['count'], 3)
def test_do_fail(self): tree = behaviours.do("call_it", lambda state: 1/0) running, success = tree.tick() self.assertEqual(running, False) self.assertEqual(success, False)
def test_do_fail(self): tree = behaviours.do("call_it", lambda state: 1 / 0) running, success = tree.tick() self.assertEqual(running, False) self.assertEqual(success, False)