示例#1
0
    def test_should_get_source_for_hello_module(self):
        # Given
        import hello

        # When
        source = getsource(hello)

        # Then
        self.assertEqual(source, self._get_code_from_hello_module())
    def test_should_get_source_for_hello_module(self):
        # Given
        import hello

        # When
        source = getsource(hello)

        # Then
        self.assertEqual(source, self._get_code_from_hello_module())
    def test_should_get_source_for_module(self):
        # Given
        module = gc

        # When
        source = getsource(module)

        # Then
        self.assertGreaterEqual(len(source.splitlines()), 1)
        self.assertIn('Reference Cycle Garbage Collection', source)
示例#4
0
    def test_should_get_source_for_module_with_longname(self):
        # Given
        module = audioop

        # When
        source = getsource(module)

        # Then
        self.assertGreaterEqual(len(source.splitlines()), 1)
        self.assertIn('peak values', source)
示例#5
0
    def test_should_get_source_for_module(self):
        # Given
        module = gc

        # When
        source = getsource(module)

        # Then
        self.assertGreaterEqual(len(source.splitlines()), 1)
        self.assertIn('Reference Cycle Garbage Collection', source)
    def test_should_get_source_for_module_with_longname(self):
        # Given
        module = audioop

        # When
        source = getsource(module)

        # Then
        self.assertGreaterEqual(len(source.splitlines()), 1)
        self.assertIn('peak values', source)
示例#7
0
    def test_should_get_source_for_say_hello(self):
        # Given
        import hello

        # When
        source = getsource(hello.say_hello)

        # Then
        self.assertEqual(source.strip(),
                         self._get_code_from_hello_module("say_hello"))
    def test_should_get_source_for_builtin_methods(self):
        # Given
        functions = self._get_builtin_methods()

        for function in functions:
            # When
            source = getsource(function)
            type_name = BuiltinMethod(function).type_name

            # Then
            self.assertIsMethod(source, type_name)
    def test_should_get_source_for_builtin_functions(self):
        # Given
        functions = self._get_builtin_functions()

        for function in functions:
            # When
            source = getsource(function)

            # Then
            self.assertIsFunction(source)
            self.assertIn(function.__name__, source.splitlines()[1])
    def test_should_get_source_for_instance_of_a_type(self):
        # Given
        objects = [], {}, set([])

        # Given
        for obj in objects:
            # When/Then
            source = getsource(obj)
            name = type(obj).__name__
            self.assertIn('PyTypeObject Py%s_Type' % name.capitalize(), source)
            self.assertIn('"%s"' % name, source)
示例#11
0
    def test_should_get_source_for_say_hello(self):
        # Given
        import hello

        # When
        source = getsource(hello.say_hello)

        # Then
        self.assertEqual(
            source.strip(), self._get_code_from_hello_module("say_hello")
        )
示例#12
0
    def test_should_get_source_for_builtin_methods(self):
        # Given
        functions = self._get_builtin_methods()

        for function in functions:
            # When
            source = getsource(function)
            type_name = BuiltinMethod(function).type_name

            # Then
            self.assertIsMethod(source, type_name)
示例#13
0
    def test_should_get_source_for_builtin_functions(self):
        # Given
        functions = self._get_builtin_functions()

        for function in functions:
            # When
            source = getsource(function)

            # Then
            self.assertIsFunction(source)
            self.assertIn(function.__name__, source.splitlines()[1])
示例#14
0
    def test_should_get_source_for_instance_of_a_type(self):
        # Given
        objects = [], {}, set([])

        # Given
        for obj in objects:
            # When/Then
            source = getsource(obj)
            name = type(obj).__name__
            self.assertIn('PyTypeObject Py%s_Type' % name.capitalize(), source)
            self.assertIn('"%s"' % name, source)
示例#15
0
    def test_patching_inspect_should_work(self):
        # Given
        inspect.getsource = getsource
        inspect.getfile = getfile

        # When
        t = getfile(unittest)
        s = getsource(unittest.main)

        # Then
        self.assertGreater(len(t), 0)
        self.assertGreater(len(s), 0)
    def test_should_get_source_for_type(self):
        # Given
        types = list, dict, set, str, unicode

        # Given
        for t in types:
            # When/Then
            source = getsource(t)
            name = t.__name__
            type_name = name.capitalize() if name != 'str' else 'String'
            self.assertIn('PyTypeObject Py%s_Type' % type_name.capitalize(), source)
            self.assertIn('"%s"' % name, source)
示例#17
0
    def test_should_get_source_for_method_descriptors(self):
        # Given
        for function in self._get_method_descriptors():
            # When
            source = getsource(function)
            try:
                type_name = MethodDescriptor(function).type_name
            except:
                type_name = BuiltinMethod(function).type_name

            # Then
            self.assertIsMethod(source, type_name)
    def test_should_get_source_for_method_descriptors(self):
        # Given
        for function in self._get_method_descriptors():
            # When
            source = getsource(function)
            try:
                type_name = MethodDescriptor(function).type_name
            except:
                type_name = BuiltinMethod(function).type_name

            # Then
            self.assertIsMethod(source, type_name)
示例#19
0
    def test_should_get_source_for_type(self):
        # Given
        types = list, dict, set, str, unicode

        # Given
        for t in types:
            # When/Then
            source = getsource(t)
            name = t.__name__
            type_name = name.capitalize() if name != 'str' else 'String'
            self.assertIn('PyTypeObject Py%s_Type' % type_name.capitalize(),
                          source)
            self.assertIn('"%s"' % name, source)
示例#20
0
def patch_getsource(obj, is_binary=False):
    if is_binary:
        return cast_unicode(getsource(obj))

    else:
        return old_getsource(obj, is_binary)
示例#21
0
    def test_should_get_source_for_method_from_any_module(self):
        # Given
        function = gc.collect

        # When/Then
        self.assertIsFunction(getsource(function))
    def test_should_get_source_for_method_from_any_module(self):
        # Given
        function = gc.collect

        # When/Then
        self.assertIsFunction(getsource(function))