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"""decorator_utils tests.""" 16 17# pylint: disable=unused-import 18from __future__ import absolute_import 19from __future__ import division 20from __future__ import print_function 21 22import functools 23 24from tensorflow.python.platform import test 25from tensorflow.python.platform import tf_logging as logging 26from tensorflow.python.util import decorator_utils 27 28 29def _test_function(unused_arg=0): 30 pass 31 32 33class GetQualifiedNameTest(test.TestCase): 34 35 def test_method(self): 36 self.assertEqual( 37 "GetQualifiedNameTest.test_method", 38 decorator_utils.get_qualified_name(GetQualifiedNameTest.test_method)) 39 40 def test_function(self): 41 self.assertEqual("_test_function", 42 decorator_utils.get_qualified_name(_test_function)) 43 44 45class AddNoticeToDocstringTest(test.TestCase): 46 47 def _check(self, doc, expected): 48 self.assertEqual( 49 decorator_utils.add_notice_to_docstring( 50 doc=doc, 51 instructions="Instructions", 52 no_doc_str="Nothing here", 53 suffix_str="(suffix)", 54 notice=["Go away"]), 55 expected) 56 57 def test_regular(self): 58 expected = ( 59 "Brief (suffix)\n\nWarning: Go away\nInstructions\n\nDocstring\n\n" 60 "Args:\n arg1: desc") 61 # No indent for main docstring 62 self._check("Brief\n\nDocstring\n\nArgs:\n arg1: desc", expected) 63 # 2 space indent for main docstring, blank lines not indented 64 self._check("Brief\n\n Docstring\n\n Args:\n arg1: desc", expected) 65 # 2 space indent for main docstring, blank lines indented as well. 66 self._check("Brief\n \n Docstring\n \n Args:\n arg1: desc", expected) 67 # No indent for main docstring, first line blank. 68 self._check("\n Brief\n \n Docstring\n \n Args:\n arg1: desc", 69 expected) 70 # 2 space indent, first line blank. 71 self._check("\n Brief\n \n Docstring\n \n Args:\n arg1: desc", 72 expected) 73 74 def test_brief_only(self): 75 expected = "Brief (suffix)\n\nWarning: Go away\nInstructions" 76 self._check("Brief", expected) 77 self._check("Brief\n", expected) 78 self._check("Brief\n ", expected) 79 self._check("\nBrief\n ", expected) 80 self._check("\n Brief\n ", expected) 81 82 def test_no_docstring(self): 83 expected = "Nothing here\n\nWarning: Go away\nInstructions" 84 self._check(None, expected) 85 self._check("", expected) 86 87 def test_no_empty_line(self): 88 expected = "Brief (suffix)\n\nWarning: Go away\nInstructions\n\nDocstring" 89 # No second line indent 90 self._check("Brief\nDocstring", expected) 91 # 2 space second line indent 92 self._check("Brief\n Docstring", expected) 93 # No second line indent, first line blank 94 self._check("\nBrief\nDocstring", expected) 95 # 2 space second line indent, first line blank 96 self._check("\n Brief\n Docstring", expected) 97 98 99class ValidateCallableTest(test.TestCase): 100 101 def test_function(self): 102 decorator_utils.validate_callable(_test_function, "test") 103 104 def test_method(self): 105 decorator_utils.validate_callable(self.test_method, "test") 106 107 def test_callable(self): 108 109 class TestClass(object): 110 111 def __call__(self): 112 pass 113 114 decorator_utils.validate_callable(TestClass(), "test") 115 116 def test_partial(self): 117 partial = functools.partial(_test_function, unused_arg=7) 118 decorator_utils.validate_callable(partial, "test") 119 120 def test_fail_non_callable(self): 121 x = 0 122 self.assertRaises(ValueError, decorator_utils.validate_callable, x, "test") 123 124 125if __name__ == "__main__": 126 test.main() 127