aa000b42ddb8abda1b1d53c831bbeed7a4a112a6
[platform/upstream/python-gobject.git] / tests / test_docstring.py
1 import unittest
2
3 import gi.docstring
4
5 from gi.repository import GIMarshallingTests
6 from gi.repository import Gio
7 from gi.repository import GObject
8
9 try:
10     import cairo
11     cairo = cairo
12     has_cairo = True
13     from gi.repository import Regress
14     from gi.repository import Gtk
15 except ImportError:
16     has_cairo = False
17
18
19 class Test(unittest.TestCase):
20     def test_api(self):
21         new_func = lambda info: 'docstring test'
22         old_func = gi.docstring.get_doc_string_generator()
23
24         gi.docstring.set_doc_string_generator(new_func)
25         self.assertEqual(gi.docstring.get_doc_string_generator(),
26                          new_func)
27         self.assertEqual(gi.docstring.generate_doc_string(None),
28                          'docstring test')
29
30         # Set back to original generator
31         gi.docstring.set_doc_string_generator(old_func)
32         self.assertEqual(gi.docstring.get_doc_string_generator(),
33                          old_func)
34
35     def test_final_signature_with_full_inout(self):
36         self.assertEqual(GIMarshallingTests.Object.full_inout.__doc__,
37                          'full_inout(object:GIMarshallingTests.Object) -> object:GIMarshallingTests.Object')
38
39     def test_overridden_doc_is_not_clobbered(self):
40         self.assertEqual(GIMarshallingTests.OverridesObject.method.__doc__,
41                          'Overridden doc string.')
42
43     def test_allow_none_with_user_data_defaults(self):
44         g_file_copy_doc = 'copy(self, destination:Gio.File, ' \
45                           'flags:Gio.FileCopyFlags, ' \
46                           'cancellable:Gio.Cancellable=None, ' \
47                           'progress_callback:Gio.FileProgressCallback=None, ' \
48                           'progress_callback_data=None) -> bool'
49
50         self.assertEqual(Gio.File.copy.__doc__, g_file_copy_doc)
51
52     def test_array_length_arg(self):
53         self.assertEqual(GIMarshallingTests.array_in.__doc__,
54                          'array_in(ints:list)')
55
56     def test_init_function(self):
57         # This tests implicit array length args along with skipping a
58         # boolean return
59         self.assertEqual(GIMarshallingTests.init_function.__doc__,
60                          'init_function(argv:list=None) -> bool, argv:list')
61
62     def test_boolean_return(self):
63         self.assertEqual(GIMarshallingTests.boolean_return_true.__doc__,
64                          'boolean_return_true() -> bool')
65
66     def test_may_return_none(self):
67         self.assertEqual(Gio.File.get_basename.__doc__,
68                          'get_basename(self) -> str or None')
69
70     def test_class_doc_constructors(self):
71         doc = GIMarshallingTests.Object.__doc__
72         self.assertTrue('new(int_:int)' in doc)
73
74     def test_struct_doc_constructors(self):
75         doc = GIMarshallingTests.BoxedStruct.__doc__
76         self.assertTrue('new()' in doc)
77         self.assertTrue('BoxedStruct()' in doc)
78
79     @unittest.skipUnless(has_cairo, 'built without cairo support')
80     def test_private_struct_constructors(self):
81         # Structs without a size or constructor should have no constructor docs.
82         doc = Regress.TestBoxedPrivate.__doc__
83         self.assertEqual(doc, '')
84
85     def test_array_inout_etc(self):
86         self.assertEqual(GIMarshallingTests.array_inout_etc.__doc__,
87                          'array_inout_etc(first:int, ints:list, last:int) -> ints:list, sum:int')
88
89     def test_array_out_etc(self):
90         self.assertEqual(GIMarshallingTests.array_out_etc.__doc__,
91                          'array_out_etc(first:int, last:int) -> ints:list, sum:int')
92
93     @unittest.skipUnless(has_cairo, 'built without cairo support')
94     def test_shared_array_length_with_prior_out_arg(self):
95         # Test the 'iter' out argument does not effect length argument skipping.
96         self.assertEqual(Gtk.ListStore.insert_with_valuesv.__doc__,
97                          'insert_with_valuesv(self, position:int, columns:list, values:list) -> iter:Gtk.TreeIter')
98
99     def test_sub_class_doc(self):
100         class A(GObject.Object):
101             """first doc"""
102             pass
103
104         class B(A):
105             """second doc"""
106             pass
107
108         self.assertEqual(A.__doc__, "first doc")
109         self.assertEqual(B.__doc__, "second doc")
110
111     def test_sub_class_no_doc(self):
112         class A(GObject.Object):
113             pass
114
115         class B(A):
116             """sub-class doc"""
117
118         self.assertEqual(A.__doc__, None)
119         self.assertEqual(B.__doc__, "sub-class doc")
120
121     @unittest.expectedFailure  # https://bugzilla.gnome.org/show_bug.cgi?id=734926
122     def test_sub_class_doc_setattr(self):
123         class A(GObject.Object):
124             pass
125
126         class B(A):
127             pass
128
129         A.__doc__ = 'custom doc'
130
131         self.assertEqual(A.__doc__, "custom doc")
132         self.assertEqual(B.__doc__, "custom doc")