test_print_signatures.py 2.7 KB
Newer Older
1 2 3
#! /usr/bin/env python

# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
4
#
5 6 7
# 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
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 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.
"""
TestCases for print_signatures.py

sample lines from API_DEV.spec:
    paddle.autograd.backward (ArgSpec(args=['tensors', 'grad_tensors', 'retain_graph'], varargs=None, keywords=None, defaults=(None, False)), ('document', '33a4434c9d123331499334fbe0274870'))
    paddle.autograd.PyLayer (paddle.autograd.py_layer.PyLayer, ('document', 'c26adbbf5f1eb43d16d4a399242c979e'))
    paddle.autograd.PyLayer.apply (ArgSpec(args=['cls'], varargs=args, keywords=kwargs, defaults=None), ('document', 'cb78696dc032fb8af2cba8504153154d'))
"""
import functools
25 26 27 28
import hashlib
import unittest

from print_signatures import is_primitive, md5
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44


def func_example(param_a, param_b):
    """
    example function
    """
    pass


def func_example_2(func=functools.partial(func_example, 1)):
    """
    example function 2
    """
    pass


45
class ClassExample:
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    """
    example Class
    """

    def example_method(self):
        """
        class method
        """
        pass


class Test_all_in_print_signatures(unittest.TestCase):
    def test_md5(self):
        algo = hashlib.md5()
        algo.update(func_example.__doc__.encode('utf-8'))
        digest = algo.hexdigest()
        self.assertEqual(digest, md5(func_example.__doc__))


65 66 67 68 69
class Test_is_primitive(unittest.TestCase):
    def test_single(self):
        self.assertTrue(is_primitive(2))
        self.assertTrue(is_primitive(2.1))
        self.assertTrue(is_primitive("2.1.1"))
70
        self.assertFalse(is_primitive("hello paddle".encode('UTF-8')))
71 72 73 74 75 76 77 78 79 80
        self.assertFalse(is_primitive(1j))
        self.assertTrue(is_primitive(True))

    def test_collection(self):
        self.assertTrue(is_primitive([]))
        self.assertTrue(is_primitive(tuple()))
        self.assertTrue(is_primitive(set()))
        self.assertTrue(is_primitive([1, 2]))
        self.assertTrue(is_primitive((1.1, 2.2)))
        self.assertTrue(is_primitive(set([1, 2.3])))
81
        self.assertFalse(is_primitive(range(3)))
82 83
        self.assertFalse(is_primitive({}))
        self.assertFalse(is_primitive([1, 1j]))
84 85 86 87


if __name__ == '__main__':
    unittest.main()