Commits

Maxim Moiseev committed 704df61

removed unnecessary code, added accessors initial implementation and some tests

  • Participants
  • Parent commits 6315044

Comments (0)

Files changed (3)

foonc/accessors.py

+import operator
+from functools import partial
+
+import core
+
+def _get_one(obj, name):
+    if isinstance(obj, dict):
+        return obj[name]
+    x = getattr(obj, name)
+    if callable(x):
+        x = x()
+    return x
+
+def _get(path):
+    parts = path.split('.')
+    return partial(reduce, _get_one, parts)
+
+def _ensure_callable(x):
+    return x if callable(x) else core.const(x)
+
+def cond(f, **kwargs):
+    pass
+
+def _if(p, t, e):
+    pass
+
+def _case(f, cases, otherwise=None):
+    pass
+
+get = core.wrap(_get)
+if_ = core.wrap(_if)

foonctest/__init__.py

-import sys
-import unittest
-
-import foonctest
-
-def suite():
-    suite = unittest.TestSuite()
-    suite.addTests(foonctest.suite())
-    return suite
-
-if __name__ == '__main__':
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    sys.exit(not result.wasSuccessful())

foonctest/foonctest.py

 from foonc.core import *
 from foonc.core import _compose2
 
+from foonc import accessors
+
 def inc(x):
     return x + 1
 
         self.assertEqual(42, f('41'))
 
 
-def suite():
-    suite = unittest.TestSuite()
-    suite.addTests(unittest.makeSuite(FooncTests))
-    suite.addTests(unittest.makeSuite(FooncWrapperTests))
-    return suite
-
-
-if __name__ == '__main__':
-    unittest.main()
+class GetTests(unittest.TestCase):
+    def test_get(self):
+        obj = {'foo':{'bar':{'baz':42}}}
+        f = accessors.get('foo.bar.baz')
+        self.assertEqual(42, f(obj))