From 52e69550f4fe47d433a5b2e3a5ad8a2174b26a63 Mon Sep 17 00:00:00 2001 From: zyd Date: Sat, 2 Jul 2022 18:21:48 +0900 Subject: [PATCH] add method propOr --- README.md | 2 +- ramda/__init__.py | 1 + ramda/propOr.py | 5 ++++ test/test_propOr.py | 69 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 ramda/propOr.py create mode 100644 test/test_propOr.py diff --git a/README.md b/README.md index 6e1e4fb..01a20ef 100644 --- a/README.md +++ b/README.md @@ -566,7 +566,7 @@ R.propEq(1, 'v1', {'v1': 1}) # True ``` - [ ] propIs -- [ ] propOr +- [x] propOr - [x] 0.1.2 props - [ ] propSatisfies - [x] 0.1.2 range diff --git a/ramda/__init__.py b/ramda/__init__.py index e4bbf50..95b06d8 100644 --- a/ramda/__init__.py +++ b/ramda/__init__.py @@ -86,6 +86,7 @@ from .project import project from .prop import prop from .propEq import propEq +from .propOr import propOr from .props import props from .range import range from .reduce import reduce diff --git a/ramda/propOr.py b/ramda/propOr.py new file mode 100644 index 0000000..0d9e2f5 --- /dev/null +++ b/ramda/propOr.py @@ -0,0 +1,5 @@ +from .defaultTo import defaultTo +from .private._curry3 import _curry3 +from .prop import prop + +propOr = _curry3(lambda val, p, obj: defaultTo(val, prop(p, obj))) diff --git a/test/test_propOr.py b/test/test_propOr.py new file mode 100644 index 0000000..6ee6349 --- /dev/null +++ b/test/test_propOr.py @@ -0,0 +1,69 @@ + +import unittest + +import ramda as R + +""" +https://github.com/ramda/ramda/blob/master/test/propOr.js +""" + +fred = {'name': 'Fred', 'age': 23} +anon = {'age': 99} + +nm = R.propOr('Unknown', 'name') + +class TestPropOrForDict(unittest.TestCase): + def test_returns_a_function_that_fetches_the_appropriate_property(self): + self.assertEqual('Fred', nm(fred)) + + def test_returns_the_default_value_when_the_property_does_not_exist(self): + self.assertEqual('Unknown', nm(anon)) + + def test_returns_the_default_value_when_the_object_is_None(self): + self.assertEqual('Unknown', nm(None)) + + def test_uses_the_default_when_supplied_an_object_with_a_None_value(self): + self.assertEqual('foo', R.propOr('foo', 'x', {'x': None})) + +class Fred: + def __init__(self): + self.name = 'Fred' + self.age = 23 +fredObj = Fred() + +class Anon: + def __init__(self): + self.age = 99 +anonObj = Anon() + +class TestPropOrForObject(unittest.TestCase): + def test_returns_a_function_that_fetches_the_appropriate_property(self): + self.assertEqual('Fred', nm(fredObj)) + + def test_returns_the_default_value_when_the_property_does_not_exist(self): + self.assertEqual('Unknown', nm(anonObj)) + + def test_returns_the_default_value_when_the_object_is_None(self): + self.assertEqual('Unknown', nm(None)) + + def test_uses_the_default_when_supplied_an_object_with_a_None_value(self): + class NoneObj: + def __init__(self): + self.x = None + self.assertEqual('foo', R.propOr('foo', 'x', NoneObj())) + +class TestPropOtherCases(unittest.TestCase): + def test_handles_number_as_property(self): + deities = ['Cthulhu', 'Dagon', 'Yog-Sothoth'] + self.assertEqual('Cthulhu', R.propOr('Unknown', 0, deities)) + self.assertEqual('Dagon', R.propOr('Unknown', 1, deities)) + self.assertEqual('Yog-Sothoth', R.propOr('Unknown', 2, deities)) + self.assertEqual('Yog-Sothoth', R.propOr('Unknown', -1, deities)) + self.assertEqual('Unknown', R.propOr('Unknown', 3, deities)) + + def test_shows_the_same_behaviours_as_pathOr_for_a_nonexistent_property(self): + # TODO: implement pathOr + pass + +if __name__ == '__main__': + unittest.main()