2018-04-17 16:33:38 +00:00
|
|
|
# Copyright 2018 The Bazel Authors. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""Unit tests for partial.bzl."""
|
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
load("//:lib.bzl", "asserts", "partial", "unittest")
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
def _make_noargs_nokwargs():
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Test utility for no args no kwargs case"""
|
|
|
|
return 1
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
def _make_args_nokwargs(arg1, arg2, arg3):
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Test utility for args no kwargs case"""
|
|
|
|
return arg1 + arg2 + arg3
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
def _make_args_kwargs(arg1, arg2, arg3, **kwargs):
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Test utility for args and kwargs case"""
|
|
|
|
return arg1 + arg2 + arg3 + kwargs["x"] + kwargs["y"]
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
def _call_noargs_nokwargs(call_arg1):
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Test utility no args no kwargs case where values passed from call site"""
|
|
|
|
return call_arg1
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
def _call_args_nokwargs(func_arg1, call_arg1):
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Test utility for args no kwargs case where values passed from call site"""
|
|
|
|
return func_arg1 + call_arg1
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
def _call_args_kwargs(func_arg1, call_arg1, func_mult, call_mult):
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Test utility for args and kwargs case where values passed from call site"""
|
|
|
|
return (func_arg1 + call_arg1) * func_mult * call_mult
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
def _make_call_test(ctx):
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Unit tests for partial.make and partial.call."""
|
|
|
|
env = unittest.begin(ctx)
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
# Test cases where there are no args (or kwargs) at the make site, only
|
|
|
|
# at the call site.
|
|
|
|
foo = partial.make(_make_noargs_nokwargs)
|
|
|
|
asserts.equals(env, 1, partial.call(foo))
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
foo = partial.make(_make_args_nokwargs)
|
|
|
|
asserts.equals(env, 6, partial.call(foo, 1, 2, 3))
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
foo = partial.make(_make_args_kwargs)
|
|
|
|
asserts.equals(env, 15, partial.call(foo, 1, 2, 3, x = 4, y = 5))
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
# Test cases where there are args (and/or kwargs) at the make site and the
|
|
|
|
# call site.
|
|
|
|
foo = partial.make(_call_noargs_nokwargs, 100)
|
|
|
|
asserts.equals(env, 100, partial.call(foo))
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
foo = partial.make(_call_args_nokwargs, 100)
|
|
|
|
asserts.equals(env, 112, partial.call(foo, 12))
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
foo = partial.make(_call_args_kwargs, 100, func_mult = 10)
|
|
|
|
asserts.equals(env, 2240, partial.call(foo, 12, call_mult = 2))
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
# Test case where there are args and kwargs ath the make site, and the call
|
|
|
|
# site overrides some make site args.
|
|
|
|
foo = partial.make(_call_args_kwargs, 100, func_mult = 10)
|
|
|
|
asserts.equals(env, 1120, partial.call(foo, 12, func_mult = 5, call_mult = 2))
|
2018-04-17 16:33:38 +00:00
|
|
|
|
2018-06-12 17:09:57 +00:00
|
|
|
unittest.end(env)
|
2018-04-17 16:33:38 +00:00
|
|
|
|
|
|
|
make_call_test = unittest.make(_make_call_test)
|
|
|
|
|
|
|
|
def partial_test_suite():
|
2018-06-12 17:09:57 +00:00
|
|
|
"""Creates the test targets and test suite for partial.bzl tests."""
|
|
|
|
unittest.suite(
|
|
|
|
"partial_tests",
|
|
|
|
make_call_test,
|
|
|
|
)
|