7
7
from test .support .os_helper import TESTFN , unlink
8
8
from textwrap import dedent
9
9
from unittest import TestCase
10
- import collections
11
10
import contextlib
12
11
import inspect
13
12
import os .path
21
20
from clinic import DSLParser
22
21
23
22
23
+ def _make_clinic (* , filename = 'clinic_tests' ):
24
+ clang = clinic .CLanguage (None )
25
+ c = clinic .Clinic (clang , filename = filename )
26
+ c .block_parser = clinic .BlockParser ('' , clang )
27
+ return c
28
+
29
+
24
30
def _expect_failure (tc , parser , code , errmsg , * , filename = None , lineno = None ):
25
31
"""Helper for the parser tests.
26
32
@@ -41,81 +47,6 @@ def _expect_failure(tc, parser, code, errmsg, *, filename=None, lineno=None):
41
47
tc .assertEqual (cm .exception .lineno , lineno )
42
48
43
49
44
- class FakeConverter :
45
- def __init__ (self , name , args ):
46
- self .name = name
47
- self .args = args
48
-
49
-
50
- class FakeConverterFactory :
51
- def __init__ (self , name ):
52
- self .name = name
53
-
54
- def __call__ (self , name , default , ** kwargs ):
55
- return FakeConverter (self .name , kwargs )
56
-
57
-
58
- class FakeConvertersDict :
59
- def __init__ (self ):
60
- self .used_converters = {}
61
-
62
- def get (self , name , default ):
63
- return self .used_converters .setdefault (name , FakeConverterFactory (name ))
64
-
65
- c = clinic .Clinic (language = 'C' , filename = "file" )
66
-
67
- class FakeClinic :
68
- def __init__ (self ):
69
- self .converters = FakeConvertersDict ()
70
- self .legacy_converters = FakeConvertersDict ()
71
- self .language = clinic .CLanguage (None )
72
- self .filename = "clinic_tests"
73
- self .destination_buffers = {}
74
- self .block_parser = clinic .BlockParser ('' , self .language )
75
- self .modules = collections .OrderedDict ()
76
- self .classes = collections .OrderedDict ()
77
- clinic .clinic = self
78
- self .name = "FakeClinic"
79
- self .line_prefix = self .line_suffix = ''
80
- self .destinations = {}
81
- self .add_destination ("block" , "buffer" )
82
- self .add_destination ("file" , "buffer" )
83
- self .add_destination ("suppress" , "suppress" )
84
- d = self .destinations .get
85
- self .field_destinations = collections .OrderedDict ((
86
- ('docstring_prototype' , d ('suppress' )),
87
- ('docstring_definition' , d ('block' )),
88
- ('methoddef_define' , d ('block' )),
89
- ('impl_prototype' , d ('block' )),
90
- ('parser_prototype' , d ('suppress' )),
91
- ('parser_definition' , d ('block' )),
92
- ('impl_definition' , d ('block' )),
93
- ))
94
- self .functions = []
95
-
96
- def get_destination (self , name ):
97
- d = self .destinations .get (name )
98
- if not d :
99
- sys .exit ("Destination does not exist: " + repr (name ))
100
- return d
101
-
102
- def add_destination (self , name , type , * args ):
103
- if name in self .destinations :
104
- sys .exit ("Destination already exists: " + repr (name ))
105
- self .destinations [name ] = clinic .Destination (name , type , self , * args )
106
-
107
- def is_directive (self , name ):
108
- return name == "module"
109
-
110
- def directive (self , name , args ):
111
- self .called_directives [name ] = args
112
-
113
- _module_and_class = clinic .Clinic ._module_and_class
114
-
115
- def __repr__ (self ):
116
- return "<FakeClinic object>"
117
-
118
-
119
50
class ClinicWholeFileTest (TestCase ):
120
51
maxDiff = None
121
52
@@ -124,7 +55,7 @@ def expect_failure(self, raw, errmsg, *, filename=None, lineno=None):
124
55
filename = filename , lineno = lineno )
125
56
126
57
def setUp (self ):
127
- self .clinic = clinic . Clinic ( clinic . CLanguage ( None ), filename = "test.c" )
58
+ self .clinic = _make_clinic ( filename = "test.c" )
128
59
129
60
def test_eol (self ):
130
61
# regression test:
@@ -848,7 +779,7 @@ def test_clinic_1(self):
848
779
class ClinicParserTest (TestCase ):
849
780
850
781
def parse (self , text ):
851
- c = FakeClinic ()
782
+ c = _make_clinic ()
852
783
parser = DSLParser (c )
853
784
block = clinic .Block (text )
854
785
parser .parse (block )
@@ -872,7 +803,7 @@ def checkDocstring(self, fn, expected):
872
803
dedent (expected ).strip ())
873
804
874
805
def test_trivial (self ):
875
- parser = DSLParser (FakeClinic ())
806
+ parser = DSLParser (_make_clinic ())
876
807
block = clinic .Block ("""
877
808
module os
878
809
os.access
@@ -1119,7 +1050,7 @@ def test_cloning_nonexistent_function_correctly_fails(self):
1119
1050
with support .captured_stderr () as stderr :
1120
1051
self .expect_failure (block , err , lineno = 0 )
1121
1052
expected_debug_print = dedent ("""\
1122
- cls=None, module=<FakeClinic object>, existing='fooooooooooooooooo'
1053
+ cls=None, module=<clinic.Clinic object>, existing='fooooooooooooooooo'
1123
1054
(cls or module).functions=[]
1124
1055
""" )
1125
1056
stderr = stderr .getvalue ()
@@ -1740,8 +1671,7 @@ def test_indent_stack_illegal_outdent(self):
1740
1671
self .expect_failure (block , err )
1741
1672
1742
1673
def test_directive (self ):
1743
- c = FakeClinic ()
1744
- parser = DSLParser (c )
1674
+ parser = DSLParser (_make_clinic ())
1745
1675
parser .flag = False
1746
1676
parser .directives ['setflag' ] = lambda : setattr (parser , 'flag' , True )
1747
1677
block = clinic .Block ("setflag" )
@@ -3147,22 +3077,24 @@ def test_Block_repr(self):
3147
3077
self .assertEqual (repr (block3 ), expected_repr_3 )
3148
3078
3149
3079
def test_Destination_repr (self ):
3080
+ c = _make_clinic ()
3081
+
3150
3082
destination = clinic .Destination (
3151
- "foo" , type = "file" , clinic = FakeClinic () , args = ("eggs" ,)
3083
+ "foo" , type = "file" , clinic = c , args = ("eggs" ,)
3152
3084
)
3153
3085
self .assertEqual (
3154
3086
repr (destination ), "<clinic.Destination 'foo' type='file' file='eggs'>"
3155
3087
)
3156
3088
3157
- destination2 = clinic .Destination ("bar" , type = "buffer" , clinic = FakeClinic () )
3089
+ destination2 = clinic .Destination ("bar" , type = "buffer" , clinic = c )
3158
3090
self .assertEqual (repr (destination2 ), "<clinic.Destination 'bar' type='buffer'>" )
3159
3091
3160
3092
def test_Module_repr (self ):
3161
- module = clinic .Module ("foo" , FakeClinic ())
3093
+ module = clinic .Module ("foo" , _make_clinic ())
3162
3094
self .assertRegex (repr (module ), r"<clinic.Module 'foo' at \d+>" )
3163
3095
3164
3096
def test_Class_repr (self ):
3165
- cls = clinic .Class ("foo" , FakeClinic (), None , 'some_typedef' , 'some_type_object' )
3097
+ cls = clinic .Class ("foo" , _make_clinic (), None , 'some_typedef' , 'some_type_object' )
3166
3098
self .assertRegex (repr (cls ), r"<clinic.Class 'foo' at \d+>" )
3167
3099
3168
3100
def test_FunctionKind_repr (self ):
@@ -3176,7 +3108,7 @@ def test_FunctionKind_repr(self):
3176
3108
def test_Function_and_Parameter_reprs (self ):
3177
3109
function = clinic .Function (
3178
3110
name = 'foo' ,
3179
- module = FakeClinic (),
3111
+ module = _make_clinic (),
3180
3112
cls = None ,
3181
3113
c_basename = None ,
3182
3114
full_name = 'foofoo' ,
0 commit comments