def test_regarding_scoping(self): assertEqual = self.assertEqual @policy_rule_func def expect_scope(expected="/", msg=None): def for_actual(actual): def checker(): return assertEqual(actual, expected, msg) return check(checker) return scope() >> for_actual rule = regarding( "", expect_scope("/", 0), regarding("a", expect_scope("/a", 1)), expect_scope("/", 2), regarding( "b", expect_scope("/b", 3), regarding("c", expect_scope("/b/c", 4)), expect_scope("/b", 5), ), ) rule.run(Partial.from_obj({}))
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_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_regarding_return(self): rule = regarding("/foo") ps = rule.run(Partial.from_obj({"foo": 5})) results = ps.getValue() values = [r[0] for r in results] self.assertEqual([5], 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])