1*bcb5dc79SHONG Yifan# Copyright 2018 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 partial.bzl.""" 16*bcb5dc79SHONG Yifan 17*bcb5dc79SHONG Yifanload("//lib:partial.bzl", "partial") 18*bcb5dc79SHONG Yifanload("//lib:unittest.bzl", "asserts", "unittest") 19*bcb5dc79SHONG Yifan 20*bcb5dc79SHONG Yifandef _make_noargs_nokwargs(): 21*bcb5dc79SHONG Yifan """Test utility for no args no kwargs case""" 22*bcb5dc79SHONG Yifan return 1 23*bcb5dc79SHONG Yifan 24*bcb5dc79SHONG Yifandef _make_args_nokwargs(arg1, arg2, arg3): 25*bcb5dc79SHONG Yifan """Test utility for args no kwargs case""" 26*bcb5dc79SHONG Yifan return arg1 + arg2 + arg3 27*bcb5dc79SHONG Yifan 28*bcb5dc79SHONG Yifandef _make_args_kwargs(arg1, arg2, arg3, **kwargs): 29*bcb5dc79SHONG Yifan """Test utility for args and kwargs case""" 30*bcb5dc79SHONG Yifan return arg1 + arg2 + arg3 + kwargs["x"] + kwargs["y"] 31*bcb5dc79SHONG Yifan 32*bcb5dc79SHONG Yifandef _call_noargs_nokwargs(call_arg1): 33*bcb5dc79SHONG Yifan """Test utility no args no kwargs case where values passed from call site""" 34*bcb5dc79SHONG Yifan return call_arg1 35*bcb5dc79SHONG Yifan 36*bcb5dc79SHONG Yifandef _call_args_nokwargs(func_arg1, call_arg1): 37*bcb5dc79SHONG Yifan """Test utility for args no kwargs case where values passed from call site""" 38*bcb5dc79SHONG Yifan return func_arg1 + call_arg1 39*bcb5dc79SHONG Yifan 40*bcb5dc79SHONG Yifandef _call_args_kwargs(func_arg1, call_arg1, func_mult, call_mult): 41*bcb5dc79SHONG Yifan """Test utility for args and kwargs case where values passed from call site""" 42*bcb5dc79SHONG Yifan return (func_arg1 + call_arg1) * func_mult * call_mult 43*bcb5dc79SHONG Yifan 44*bcb5dc79SHONG Yifandef _make_call_test(ctx): 45*bcb5dc79SHONG Yifan """Unit tests for partial.make and partial.call.""" 46*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 47*bcb5dc79SHONG Yifan 48*bcb5dc79SHONG Yifan # Test cases where there are no args (or kwargs) at the make site, only 49*bcb5dc79SHONG Yifan # at the call site. 50*bcb5dc79SHONG Yifan foo = partial.make(_make_noargs_nokwargs) 51*bcb5dc79SHONG Yifan asserts.equals(env, 1, partial.call(foo)) 52*bcb5dc79SHONG Yifan 53*bcb5dc79SHONG Yifan foo = partial.make(_make_args_nokwargs) 54*bcb5dc79SHONG Yifan asserts.equals(env, 6, partial.call(foo, 1, 2, 3)) 55*bcb5dc79SHONG Yifan 56*bcb5dc79SHONG Yifan foo = partial.make(_make_args_kwargs) 57*bcb5dc79SHONG Yifan asserts.equals(env, 15, partial.call(foo, 1, 2, 3, x = 4, y = 5)) 58*bcb5dc79SHONG Yifan 59*bcb5dc79SHONG Yifan # Test cases where there are args (and/or kwargs) at the make site and the 60*bcb5dc79SHONG Yifan # call site. 61*bcb5dc79SHONG Yifan foo = partial.make(_call_noargs_nokwargs, 100) 62*bcb5dc79SHONG Yifan asserts.equals(env, 100, partial.call(foo)) 63*bcb5dc79SHONG Yifan 64*bcb5dc79SHONG Yifan foo = partial.make(_call_args_nokwargs, 100) 65*bcb5dc79SHONG Yifan asserts.equals(env, 112, partial.call(foo, 12)) 66*bcb5dc79SHONG Yifan 67*bcb5dc79SHONG Yifan foo = partial.make(_call_args_kwargs, 100, func_mult = 10) 68*bcb5dc79SHONG Yifan asserts.equals(env, 2240, partial.call(foo, 12, call_mult = 2)) 69*bcb5dc79SHONG Yifan 70*bcb5dc79SHONG Yifan # Test case where there are args and kwargs ath the make site, and the call 71*bcb5dc79SHONG Yifan # site overrides some make site args. 72*bcb5dc79SHONG Yifan foo = partial.make(_call_args_kwargs, 100, func_mult = 10) 73*bcb5dc79SHONG Yifan asserts.equals(env, 1120, partial.call(foo, 12, func_mult = 5, call_mult = 2)) 74*bcb5dc79SHONG Yifan 75*bcb5dc79SHONG Yifan return unittest.end(env) 76*bcb5dc79SHONG Yifan 77*bcb5dc79SHONG Yifanmake_call_test = unittest.make(_make_call_test) 78*bcb5dc79SHONG Yifan 79*bcb5dc79SHONG Yifandef _is_instance_test(ctx): 80*bcb5dc79SHONG Yifan """Unit test for partial.is_instance.""" 81*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 82*bcb5dc79SHONG Yifan 83*bcb5dc79SHONG Yifan # We happen to use make_call_test here, but it could be any valid test rule. 84*bcb5dc79SHONG Yifan asserts.true(env, partial.is_instance(partial.make(make_call_test))) 85*bcb5dc79SHONG Yifan asserts.true(env, partial.is_instance(partial.make(make_call_test, timeout = "short"))) 86*bcb5dc79SHONG Yifan asserts.true(env, partial.is_instance(partial.make(make_call_test, timeout = "short", tags = ["foo"]))) 87*bcb5dc79SHONG Yifan asserts.false(env, partial.is_instance(None)) 88*bcb5dc79SHONG Yifan asserts.false(env, partial.is_instance({})) 89*bcb5dc79SHONG Yifan asserts.false(env, partial.is_instance(struct(foo = 1))) 90*bcb5dc79SHONG Yifan asserts.false(env, partial.is_instance(struct(function = "not really function"))) 91*bcb5dc79SHONG Yifan 92*bcb5dc79SHONG Yifan return unittest.end(env) 93*bcb5dc79SHONG Yifan 94*bcb5dc79SHONG Yifanis_instance_test = unittest.make(_is_instance_test) 95*bcb5dc79SHONG Yifan 96*bcb5dc79SHONG Yifandef partial_test_suite(): 97*bcb5dc79SHONG Yifan """Creates the test targets and test suite for partial.bzl tests.""" 98*bcb5dc79SHONG Yifan unittest.suite( 99*bcb5dc79SHONG Yifan "partial_tests", 100*bcb5dc79SHONG Yifan make_call_test, 101*bcb5dc79SHONG Yifan is_instance_test, 102*bcb5dc79SHONG Yifan ) 103