def test_children(self): rule = children() ps = rule.run(Partial.from_obj({"foo": 5})) results = ps.getValue() values = [r[0] for r in results] self.assertEqual([['foo']], values)
def test_children_list(self): rule = children() ps = rule.run(Partial.from_obj([9, 4, 7, 1, 1])) results = ps.getValue() values = [r[0] for r in results] self.assertEqual([["0", "1", "2", "3", "4"]], values)
def test_each_ref(self): ref_obj = {"a": 7, "b": 3, "c": -1} rule = children() >> each(set_value, ref=ref_obj) ps = rule.run(Partial.from_obj({"a": 0, "b": 0, "c": 0})) results = ps.getValue() roots = [r[1].root for r in results] self.assertEqual(len(roots), 1) root = roots[0] self.assertIsInstance(root, dict) self.assertEqual(root, ref_obj)
def test_each_list(self): def increment(value): return set_value(value + 1) rule = children() >> each(increment) partial = Partial.from_obj([2, 5, 1]) ps = rule.run(partial) results = ps.getValue() roots = [r[1].root for r in results] self.assertEqual(len(roots), 1) values = roots[0] self.assertIsInstance(values, list) self.assertEqual(values, [3, 6, 2])
def test_each(self): counter = { "num": 0 } def increment_set(_): counter['num'] += 1 num = counter['num'] return set_value(num) rule = children() >> each(increment_set) ps = rule.run(Partial.from_obj({"a": 0, "b": 0, "c": 0})) results = ps.getValue() roots = [r[1].root for r in results] self.assertEqual(len(roots), 1) root = roots[0] self.assertIsInstance(root, dict) values = sorted(root.values()) self.assertEqual(values, [1, 2, 3])