示例#1
0
 def testLen(self):
     r = registry.Registry("test_registry")
     self.assertEqual(len(r), 0)
     r["a"] = lambda: None
     self.assertEqual(len(r), 1)
     r["b"] = lambda: 4
     self.assertEqual(len(r), 2)
示例#2
0
 def testGet(self):
     r = registry.Registry("test_registry",
                           value_transformer=lambda k, v: v())
     r["a"] = lambda: "xyz"
     self.assertEqual(r.get("a"), "xyz")
     self.assertEqual(r.get("a", 3), "xyz")
     self.assertIsNone(r.get("b"))
     self.assertEqual(r.get("b", 3), 3)
示例#3
0
    def testNoKeyProvided(self):
        r = registry.Registry("test")

        def f():
            return 3

        r.register(f)
        self.assertEqual(r["f"](), 3)
示例#4
0
 def testTransformer(self):
     r = registry.Registry("test_registry",
                           value_transformer=lambda x, y: x + y())
     r.register(3)(lambda: 5)
     r.register(10)(lambda: 12)
     self.assertEqual(r[3], 8)
     self.assertEqual(r[10], 22)
     self.assertEqual(set(r.values()), set((8, 22)))
     self.assertEqual(set(r.items()), set(((3, 8), (10, 22))))
示例#5
0
 def testIteration(self):
     r = registry.Registry("test_registry")
     r["a"] = lambda: None
     r["b"] = lambda: 4
     self.assertEqual(sorted(r), ["a", "b"])
示例#6
0
 def testMembership(self):
     r = registry.Registry("test_registry")
     r["a"] = lambda: None
     r["b"] = lambda: 4
     self.assertTrue("a" in r)
     self.assertTrue("b" in r)
示例#7
0
 def testDefaultKeyFn(self):
     r = registry.Registry("test", default_key_fn=lambda x: x().upper())
     r.register()(lambda: "hello")
     self.assertEqual(r["HELLO"](), "hello")
示例#8
0
 def testGetterSetter(self):
     r = registry.Registry("test_registry")
     r["hello"] = lambda: "world"
     r["a"] = lambda: "b"
     self.assertEqual(r["hello"](), "world")
     self.assertEqual(r["a"](), "b")