1*bcb5dc79SHONG Yifan# Copyright 2017 The Bazel Authors. All rights reserved. 2*bcb5dc79SHONG Yifan# 3*bcb5dc79SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License"); 4*bcb5dc79SHONG Yifan# you may not use this file except in compliance with the License. 5*bcb5dc79SHONG Yifan# You may obtain a copy of the License at 6*bcb5dc79SHONG Yifan# 7*bcb5dc79SHONG Yifan# http://www.apache.org/licenses/LICENSE-2.0 8*bcb5dc79SHONG Yifan# 9*bcb5dc79SHONG Yifan# Unless required by applicable law or agreed to in writing, software 10*bcb5dc79SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS, 11*bcb5dc79SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*bcb5dc79SHONG Yifan# See the License for the specific language governing permissions and 13*bcb5dc79SHONG Yifan# limitations under the License. 14*bcb5dc79SHONG Yifan 15*bcb5dc79SHONG Yifan"""Unit tests for dicts.bzl.""" 16*bcb5dc79SHONG Yifan 17*bcb5dc79SHONG Yifanload("//lib:dicts.bzl", "dicts") 18*bcb5dc79SHONG Yifanload("//lib:unittest.bzl", "asserts", "unittest") 19*bcb5dc79SHONG Yifan 20*bcb5dc79SHONG Yifandef _add_test(ctx): 21*bcb5dc79SHONG Yifan """Unit tests for dicts.add.""" 22*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 23*bcb5dc79SHONG Yifan 24*bcb5dc79SHONG Yifan # Test zero- and one-argument behavior. 25*bcb5dc79SHONG Yifan asserts.equals(env, {}, dicts.add()) 26*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1}, dicts.add({"a": 1})) 27*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1}, dicts.add(a = 1)) 28*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1, "b": 2}, dicts.add({"a": 1}, b = 2)) 29*bcb5dc79SHONG Yifan 30*bcb5dc79SHONG Yifan # Test simple two-argument behavior. 31*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1, "b": 2}, dicts.add({"a": 1}, {"b": 2})) 32*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1, "b": 2, "c": 3}, dicts.add({"a": 1}, {"b": 2}, c = 3)) 33*bcb5dc79SHONG Yifan 34*bcb5dc79SHONG Yifan # Test simple more-than-two-argument behavior. 35*bcb5dc79SHONG Yifan asserts.equals( 36*bcb5dc79SHONG Yifan env, 37*bcb5dc79SHONG Yifan {"a": 1, "b": 2, "c": 3, "d": 4}, 38*bcb5dc79SHONG Yifan dicts.add({"a": 1}, {"b": 2}, {"c": 3}, {"d": 4}), 39*bcb5dc79SHONG Yifan ) 40*bcb5dc79SHONG Yifan asserts.equals( 41*bcb5dc79SHONG Yifan env, 42*bcb5dc79SHONG Yifan {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}, 43*bcb5dc79SHONG Yifan dicts.add({"a": 1}, {"b": 2}, {"c": 3}, {"d": 4}, e = 5), 44*bcb5dc79SHONG Yifan ) 45*bcb5dc79SHONG Yifan 46*bcb5dc79SHONG Yifan # Test same-key overriding. 47*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 100}, dicts.add({"a": 1}, {"a": 100})) 48*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 100}, dicts.add({"a": 1}, a = 100)) 49*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {"a": 100}, {"a": 10})) 50*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {"a": 100}, a = 10)) 51*bcb5dc79SHONG Yifan asserts.equals( 52*bcb5dc79SHONG Yifan env, 53*bcb5dc79SHONG Yifan {"a": 100, "b": 10}, 54*bcb5dc79SHONG Yifan dicts.add({"a": 1}, {"a": 100}, {"b": 10}), 55*bcb5dc79SHONG Yifan ) 56*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {}, {"a": 10})) 57*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 10}, dicts.add({"a": 1}, {}, a = 10)) 58*bcb5dc79SHONG Yifan asserts.equals( 59*bcb5dc79SHONG Yifan env, 60*bcb5dc79SHONG Yifan {"a": 10, "b": 5}, 61*bcb5dc79SHONG Yifan dicts.add({"a": 1}, {"a": 10, "b": 5}), 62*bcb5dc79SHONG Yifan ) 63*bcb5dc79SHONG Yifan asserts.equals( 64*bcb5dc79SHONG Yifan env, 65*bcb5dc79SHONG Yifan {"a": 10, "b": 5}, 66*bcb5dc79SHONG Yifan dicts.add({"a": 1}, a = 10, b = 5), 67*bcb5dc79SHONG Yifan ) 68*bcb5dc79SHONG Yifan 69*bcb5dc79SHONG Yifan # Test some other boundary cases. 70*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1}, dicts.add({"a": 1}, {})) 71*bcb5dc79SHONG Yifan 72*bcb5dc79SHONG Yifan # Since dictionaries are passed around by reference, make sure that the 73*bcb5dc79SHONG Yifan # result of dicts.add is always a *copy* by modifying it afterwards and 74*bcb5dc79SHONG Yifan # ensuring that the original argument doesn't also reflect the change. We do 75*bcb5dc79SHONG Yifan # this to protect against someone who might attempt to optimize the function 76*bcb5dc79SHONG Yifan # by returning the argument itself in the one-argument case. 77*bcb5dc79SHONG Yifan original = {"a": 1} 78*bcb5dc79SHONG Yifan result = dicts.add(original) 79*bcb5dc79SHONG Yifan result["a"] = 2 80*bcb5dc79SHONG Yifan asserts.equals(env, 1, original["a"]) 81*bcb5dc79SHONG Yifan 82*bcb5dc79SHONG Yifan return unittest.end(env) 83*bcb5dc79SHONG Yifan 84*bcb5dc79SHONG Yifanadd_test = unittest.make(_add_test) 85*bcb5dc79SHONG Yifan 86*bcb5dc79SHONG Yifandef _omit_test(ctx): 87*bcb5dc79SHONG Yifan """Unit tests for dicts.omit.""" 88*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 89*bcb5dc79SHONG Yifan 90*bcb5dc79SHONG Yifan # Test empty dict, empty list. 91*bcb5dc79SHONG Yifan asserts.equals(env, {}, dicts.omit({}, [])) 92*bcb5dc79SHONG Yifan 93*bcb5dc79SHONG Yifan # Test empty dict, nonempty list. 94*bcb5dc79SHONG Yifan asserts.equals(env, {}, dicts.omit({}, ["a"])) 95*bcb5dc79SHONG Yifan 96*bcb5dc79SHONG Yifan # Test nonempty dict, empty list. 97*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1}, dicts.omit({"a": 1}, [])) 98*bcb5dc79SHONG Yifan 99*bcb5dc79SHONG Yifan # Test key in dict. 100*bcb5dc79SHONG Yifan asserts.equals(env, {}, dicts.omit({"a": 1}, ["a"])) 101*bcb5dc79SHONG Yifan 102*bcb5dc79SHONG Yifan # Test key not in dict. 103*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1}, dicts.omit({"a": 1}, ["b"])) 104*bcb5dc79SHONG Yifan 105*bcb5dc79SHONG Yifan # Since dictionaries are passed around by reference, make sure that the 106*bcb5dc79SHONG Yifan # result of dicts.omit is always a *copy* by modifying it afterwards and 107*bcb5dc79SHONG Yifan # ensuring that the original argument doesn't also reflect the change. We do 108*bcb5dc79SHONG Yifan # this to protect against someone who might attempt to optimize the function 109*bcb5dc79SHONG Yifan # by returning the argument itself in the empty list case. 110*bcb5dc79SHONG Yifan original = {"a": 1} 111*bcb5dc79SHONG Yifan result = dicts.omit(original, []) 112*bcb5dc79SHONG Yifan result["a"] = 2 113*bcb5dc79SHONG Yifan asserts.equals(env, 1, original["a"]) 114*bcb5dc79SHONG Yifan 115*bcb5dc79SHONG Yifan return unittest.end(env) 116*bcb5dc79SHONG Yifan 117*bcb5dc79SHONG Yifanomit_test = unittest.make(_omit_test) 118*bcb5dc79SHONG Yifan 119*bcb5dc79SHONG Yifandef _pick_test(ctx): 120*bcb5dc79SHONG Yifan """Unit tests for dicts.pick.""" 121*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 122*bcb5dc79SHONG Yifan 123*bcb5dc79SHONG Yifan # Test empty dict, empty list. 124*bcb5dc79SHONG Yifan asserts.equals(env, {}, dicts.pick({}, [])) 125*bcb5dc79SHONG Yifan 126*bcb5dc79SHONG Yifan # Test empty dict, nonempty list. 127*bcb5dc79SHONG Yifan asserts.equals(env, {}, dicts.pick({}, ["a"])) 128*bcb5dc79SHONG Yifan 129*bcb5dc79SHONG Yifan # Test nonempty dict, empty list. 130*bcb5dc79SHONG Yifan asserts.equals(env, {}, dicts.pick({"a": 1}, [])) 131*bcb5dc79SHONG Yifan 132*bcb5dc79SHONG Yifan # Test key in dict. 133*bcb5dc79SHONG Yifan asserts.equals(env, {"a": 1}, dicts.pick({"a": 1}, ["a"])) 134*bcb5dc79SHONG Yifan 135*bcb5dc79SHONG Yifan # Test key not in dict. 136*bcb5dc79SHONG Yifan asserts.equals(env, {}, dicts.pick({"a": 1}, ["b"])) 137*bcb5dc79SHONG Yifan 138*bcb5dc79SHONG Yifan # Since dictionaries are passed around by reference, make sure that the 139*bcb5dc79SHONG Yifan # result of dicts.pick is always a *copy* by modifying it afterwards and 140*bcb5dc79SHONG Yifan # ensuring that the original argument doesn't also reflect the change. We do 141*bcb5dc79SHONG Yifan # this to protect against someone who might attempt to optimize the function 142*bcb5dc79SHONG Yifan # by returning the argument itself. 143*bcb5dc79SHONG Yifan original = {"a": 1} 144*bcb5dc79SHONG Yifan result = dicts.pick(original, ["a"]) 145*bcb5dc79SHONG Yifan result["a"] = 2 146*bcb5dc79SHONG Yifan asserts.equals(env, 1, original["a"]) 147*bcb5dc79SHONG Yifan 148*bcb5dc79SHONG Yifan return unittest.end(env) 149*bcb5dc79SHONG Yifan 150*bcb5dc79SHONG Yifanpick_test = unittest.make(_pick_test) 151*bcb5dc79SHONG Yifan 152*bcb5dc79SHONG Yifandef dicts_test_suite(): 153*bcb5dc79SHONG Yifan """Creates the test targets and test suite for dicts.bzl tests.""" 154*bcb5dc79SHONG Yifan unittest.suite( 155*bcb5dc79SHONG Yifan "dicts_tests", 156*bcb5dc79SHONG Yifan add_test, 157*bcb5dc79SHONG Yifan omit_test, 158*bcb5dc79SHONG Yifan pick_test, 159*bcb5dc79SHONG Yifan ) 160