| 1 |
import unittest |
| 2 |
|
| 3 |
import gi.docstring |
| 4 |
|
| 5 |
from gi.repository import Regress |
| 6 |
from gi.repository import GIMarshallingTests |
| 7 |
from gi.repository import Gio |
| 8 |
from gi.repository import GObject |
| 9 |
|
| 10 |
try: |
| 11 |
from gi.repository import Gtk |
| 12 |
except ImportError: |
| 13 |
Gtk = None |
| 14 |
|
| 15 |
|
| 16 |
class Test(unittest.TestCase): |
| 17 |
def test_api(self): |
| 18 |
def new_func(info): |
| 19 |
return "docstring test" |
| 20 |
|
| 21 |
old_func = gi.docstring.get_doc_string_generator() |
| 22 |
|
| 23 |
gi.docstring.set_doc_string_generator(new_func) |
| 24 |
self.assertEqual(gi.docstring.get_doc_string_generator(), new_func) |
| 25 |
self.assertEqual(gi.docstring.generate_doc_string(None), "docstring test") |
| 26 |
|
| 27 |
# Set back to original generator |
| 28 |
gi.docstring.set_doc_string_generator(old_func) |
| 29 |
self.assertEqual(gi.docstring.get_doc_string_generator(), old_func) |
| 30 |
|
| 31 |
def test_final_signature_with_full_inout(self): |
| 32 |
self.assertEqual( |
| 33 |
GIMarshallingTests.Object.full_inout.__doc__, |
| 34 |
"full_inout(object:GIMarshallingTests.Object) -> object:GIMarshallingTests.Object", |
| 35 |
) |
| 36 |
|
| 37 |
def test_overridden_doc_is_not_clobbered(self): |
| 38 |
self.assertEqual( |
| 39 |
GIMarshallingTests.OverridesObject.method.__doc__, "Overridden doc string." |
| 40 |
) |
| 41 |
|
| 42 |
def test_allow_none_with_user_data_defaults(self): |
| 43 |
g_file_copy_doc = ( |
| 44 |
"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 |
|
| 51 |
self.assertEqual(Gio.File.copy.__doc__, g_file_copy_doc) |
| 52 |
|
| 53 |
def test_array_length_arg(self): |
| 54 |
self.assertEqual(GIMarshallingTests.array_in.__doc__, "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( |
| 60 |
GIMarshallingTests.init_function.__doc__, |
| 61 |
"init_function(argv:list=None) -> bool, argv:list", |
| 62 |
) |
| 63 |
|
| 64 |
def test_boolean_return(self): |
| 65 |
self.assertEqual( |
| 66 |
GIMarshallingTests.boolean_return_true.__doc__, |
| 67 |
"boolean_return_true() -> bool", |
| 68 |
) |
| 69 |
|
| 70 |
# https://bugzilla.gnome.org/show_bug.cgi?id=740301 |
| 71 |
def test_may_return_none(self): |
| 72 |
self.assertEqual( |
| 73 |
Gio.File.get_basename.__doc__, "get_basename(self) -> str or None" |
| 74 |
) |
| 75 |
|
| 76 |
def test_class_doc_constructors(self): |
| 77 |
doc = GIMarshallingTests.Object.__doc__ |
| 78 |
self.assertTrue("new(int_:int)" in doc) |
| 79 |
|
| 80 |
def test_struct_doc_constructors(self): |
| 81 |
doc = GIMarshallingTests.BoxedStruct.__doc__ |
| 82 |
self.assertTrue("new()" in doc) |
| 83 |
self.assertTrue("BoxedStruct()" in doc) |
| 84 |
|
| 85 |
def test_private_struct_constructors(self): |
| 86 |
# Structs without a size or constructor should have no constructor docs. |
| 87 |
doc = Regress.TestBoxedPrivate.__doc__ |
| 88 |
self.assertEqual(doc, "") |
| 89 |
|
| 90 |
def test_array_inout_etc(self): |
| 91 |
self.assertEqual( |
| 92 |
GIMarshallingTests.array_inout_etc.__doc__, |
| 93 |
"array_inout_etc(first:int, ints:list, last:int) -> ints:list, sum:int", |
| 94 |
) |
| 95 |
|
| 96 |
def test_array_out_etc(self): |
| 97 |
self.assertEqual( |
| 98 |
GIMarshallingTests.array_out_etc.__doc__, |
| 99 |
"array_out_etc(first:int, last:int) -> ints:list, sum:int", |
| 100 |
) |
| 101 |
|
| 102 |
@unittest.skipUnless(Gtk, "no Gtk") |
| 103 |
def test_shared_array_length_with_prior_out_arg(self): |
| 104 |
# Test the 'iter' out argument does not effect length argument skipping. |
| 105 |
self.assertRegex( |
| 106 |
Gtk.ListStore.insert_with_valuesv.__doc__, |
| 107 |
"insert_with_values.*\\(self, position:int, columns:list, values:list\\) -> iter:Gtk.TreeIter", |
| 108 |
) |
| 109 |
|
| 110 |
def test_sub_class_doc(self): |
| 111 |
class A(GObject.Object): |
| 112 |
"""first doc.""" |
| 113 |
|
| 114 |
pass |
| 115 |
|
| 116 |
class B(A): |
| 117 |
"""second doc.""" |
| 118 |
|
| 119 |
pass |
| 120 |
|
| 121 |
self.assertEqual(A.__doc__, "first doc.") |
| 122 |
self.assertEqual(B.__doc__, "second doc.") |
| 123 |
|
| 124 |
def test_sub_class_no_doc(self): |
| 125 |
class A(GObject.Object): |
| 126 |
pass |
| 127 |
|
| 128 |
class B(A): |
| 129 |
"""sub-class doc.""" |
| 130 |
|
| 131 |
self.assertEqual(A.__doc__, None) |
| 132 |
self.assertEqual(B.__doc__, "sub-class doc.") |
| 133 |
|
| 134 |
@unittest.expectedFailure # https://bugzilla.gnome.org/show_bug.cgi?id=734926 |
| 135 |
def test_sub_class_doc_setattr(self): |
| 136 |
class A(GObject.Object): |
| 137 |
pass |
| 138 |
|
| 139 |
class B(A): |
| 140 |
pass |
| 141 |
|
| 142 |
A.__doc__ = "custom doc" |
| 143 |
|
| 144 |
self.assertEqual(A.__doc__, "custom doc") |
| 145 |
self.assertEqual(B.__doc__, "custom doc") |
| 146 |
|
| 147 |
def test_return_array_with_length_argument(self): |
| 148 |
self.assertEqual( |
| 149 |
GIMarshallingTests.enum_array_return_type.__doc__, |
| 150 |
"enum_array_return_type() -> list", |
| 151 |
) |