Accept kwargs in dicts.add() (#130)

Accept kwargs in dicts.add()
This commit is contained in:
Dave Lee 2019-03-20 10:08:29 -07:00 committed by c-parsons
parent bdbedc1832
commit aeefb6531a
2 changed files with 19 additions and 1 deletions

View File

@ -14,7 +14,7 @@
"""Skylib module containing functions that operate on dictionaries."""
def _add(*dictionaries):
def _add(*dictionaries, **kwargs):
"""Returns a new `dict` that has all the entries of the given dictionaries.
If the same key is present in more than one of the input dictionaries, the
@ -27,6 +27,7 @@ def _add(*dictionaries):
Args:
*dictionaries: Zero or more dictionaries to be added.
**kwargs: Additional dictionary passed as keyword args.
Returns:
A new `dict` that has all the entries of the given dictionaries.
@ -34,6 +35,7 @@ def _add(*dictionaries):
result = {}
for d in dictionaries:
result.update(d)
result.update(kwargs)
return result
dicts = struct(

View File

@ -24,9 +24,12 @@ def _add_test(ctx):
# Test zero- and one-argument behavior.
asserts.equals(env, {}, dicts.add())
asserts.equals(env, {"a": 1}, dicts.add({"a": 1}))
asserts.equals(env, {"a": 1}, dicts.add(a = 1))
asserts.equals(env, {"a": 1, "b": 2}, dicts.add({"a": 1}, b = 2))
# Test simple two-argument behavior.
asserts.equals(env, {"a": 1, "b": 2}, dicts.add({"a": 1}, {"b": 2}))
asserts.equals(env, {"a": 1, "b": 2, "c": 3}, dicts.add({"a": 1}, {"b": 2}, c = 3))
# Test simple more-than-two-argument behavior.
asserts.equals(
@ -34,21 +37,34 @@ def _add_test(ctx):
{"a": 1, "b": 2, "c": 3, "d": 4},
dicts.add({"a": 1}, {"b": 2}, {"c": 3}, {"d": 4}),
)
asserts.equals(
env,
{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5},
dicts.add({"a": 1}, {"b": 2}, {"c": 3}, {"d": 4}, e = 5),
)
# Test same-key overriding.
asserts.equals(env, {"a": 100}, dicts.add({"a": 1}, {"a": 100}))
asserts.equals(env, {"a": 100}, dicts.add({"a": 1}, a = 100))
asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {"a": 100}, {"a": 10}))
asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {"a": 100}, a = 10))
asserts.equals(
env,
{"a": 100, "b": 10},
dicts.add({"a": 1}, {"a": 100}, {"b": 10}),
)
asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {}, {"a": 10}))
asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {}, a = 10))
asserts.equals(
env,
{"a": 10, "b": 5},
dicts.add({"a": 1}, {"a": 10, "b": 5}),
)
asserts.equals(
env,
{"a": 10, "b": 5},
dicts.add({"a": 1}, a = 10, b = 5),
)
# Test some other boundary cases.
asserts.equals(env, {"a": 1}, dicts.add({"a": 1}, {}))