From aeefb6531aa30f678f0d8d40c6b5bf095408cdb5 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Wed, 20 Mar 2019 10:08:29 -0700 Subject: [PATCH] Accept kwargs in dicts.add() (#130) Accept kwargs in dicts.add() --- lib/dicts.bzl | 4 +++- tests/dicts_tests.bzl | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/dicts.bzl b/lib/dicts.bzl index f702fa92..3f8e661a 100644 --- a/lib/dicts.bzl +++ b/lib/dicts.bzl @@ -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( diff --git a/tests/dicts_tests.bzl b/tests/dicts_tests.bzl index 58353239..2fc407d9 100644 --- a/tests/dicts_tests.bzl +++ b/tests/dicts_tests.bzl @@ -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}, {}))