1 # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ============================================================================== 15 """Keyword args tests.""" 16 17 from __future__ import absolute_import 18 from __future__ import division 19 from __future__ import print_function 20 21 from tensorflow.python.platform import test 22 from tensorflow.python.util import keyword_args 23 24 25 class KeywordArgsTest(test.TestCase): 26 27 def test_keyword_args_only(self): 28 29 def func_without_decorator(a, b): 30 return a + b 31 32 @keyword_args.keyword_args_only 33 def func_with_decorator(a, b): 34 return func_without_decorator(a, b) 35 36 self.assertEqual(3, func_without_decorator(1, 2)) 37 self.assertEqual(3, func_without_decorator(a=1, b=2)) 38 self.assertEqual(3, func_with_decorator(a=1, b=2)) 39 40 # Providing non-keyword args should fail. 41 with self.assertRaisesRegexp( 42 ValueError, "Must use keyword args to call func_with_decorator."): 43 self.assertEqual(3, func_with_decorator(1, 2)) 44 45 # Partially providing keyword args should fail. 46 with self.assertRaisesRegexp( 47 ValueError, "Must use keyword args to call func_with_decorator."): 48 self.assertEqual(3, func_with_decorator(1, b=2)) 49 50 51 if __name__ == "__main__": 52 test.main() 53