Skip to content

Commit

Permalink
Accept kwargs in dicts.add() (#130)
Browse files Browse the repository at this point in the history
Accept kwargs in dicts.add()
  • Loading branch information
kastiglione authored and c-parsons committed Mar 20, 2019
1 parent bdbedc1 commit aeefb65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/dicts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,13 +27,15 @@ 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.
"""
result = {}
for d in dictionaries:
result.update(d)
result.update(kwargs)
return result

dicts = struct(
Expand Down
16 changes: 16 additions & 0 deletions tests/dicts_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,47 @@ 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(
env,
{"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}, {}))
Expand Down

0 comments on commit aeefb65

Please sign in to comment.