77from test .support .os_helper import TESTFN , unlink
88from textwrap import dedent
99from unittest import TestCase
10- import collections
1110import contextlib
1211import inspect
1312import os .path
2120 from clinic import DSLParser
2221
2322
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+
2430def _expect_failure (tc , parser , code , errmsg , * , filename = None , lineno = None ):
2531 """Helper for the parser tests.
2632
@@ -41,81 +47,6 @@ def _expect_failure(tc, parser, code, errmsg, *, filename=None, lineno=None):
4147 tc .assertEqual (cm .exception .lineno , lineno )
4248
4349
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-
11950class ClinicWholeFileTest (TestCase ):
12051 maxDiff = None
12152
@@ -124,7 +55,7 @@ def expect_failure(self, raw, errmsg, *, filename=None, lineno=None):
12455 filename = filename , lineno = lineno )
12556
12657 def setUp (self ):
127- self .clinic = clinic . Clinic ( clinic . CLanguage ( None ), filename = "test.c" )
58+ self .clinic = _make_clinic ( filename = "test.c" )
12859
12960 def test_eol (self ):
13061 # regression test:
@@ -848,7 +779,7 @@ def test_clinic_1(self):
848779class ClinicParserTest (TestCase ):
849780
850781 def parse (self , text ):
851- c = FakeClinic ()
782+ c = _make_clinic ()
852783 parser = DSLParser (c )
853784 block = clinic .Block (text )
854785 parser .parse (block )
@@ -872,7 +803,7 @@ def checkDocstring(self, fn, expected):
872803 dedent (expected ).strip ())
873804
874805 def test_trivial (self ):
875- parser = DSLParser (FakeClinic ())
806+ parser = DSLParser (_make_clinic ())
876807 block = clinic .Block ("""
877808 module os
878809 os.access
@@ -1119,7 +1050,7 @@ def test_cloning_nonexistent_function_correctly_fails(self):
11191050 with support .captured_stderr () as stderr :
11201051 self .expect_failure (block , err , lineno = 0 )
11211052 expected_debug_print = dedent ("""\
1122- cls=None, module=<FakeClinic object>, existing='fooooooooooooooooo'
1053+ cls=None, module=<clinic.Clinic object>, existing='fooooooooooooooooo'
11231054 (cls or module).functions=[]
11241055 """ )
11251056 stderr = stderr .getvalue ()
@@ -1740,8 +1671,7 @@ def test_indent_stack_illegal_outdent(self):
17401671 self .expect_failure (block , err )
17411672
17421673 def test_directive (self ):
1743- c = FakeClinic ()
1744- parser = DSLParser (c )
1674+ parser = DSLParser (_make_clinic ())
17451675 parser .flag = False
17461676 parser .directives ['setflag' ] = lambda : setattr (parser , 'flag' , True )
17471677 block = clinic .Block ("setflag" )
@@ -3147,22 +3077,24 @@ def test_Block_repr(self):
31473077 self .assertEqual (repr (block3 ), expected_repr_3 )
31483078
31493079 def test_Destination_repr (self ):
3080+ c = _make_clinic ()
3081+
31503082 destination = clinic .Destination (
3151- "foo" , type = "file" , clinic = FakeClinic () , args = ("eggs" ,)
3083+ "foo" , type = "file" , clinic = c , args = ("eggs" ,)
31523084 )
31533085 self .assertEqual (
31543086 repr (destination ), "<clinic.Destination 'foo' type='file' file='eggs'>"
31553087 )
31563088
3157- destination2 = clinic .Destination ("bar" , type = "buffer" , clinic = FakeClinic () )
3089+ destination2 = clinic .Destination ("bar" , type = "buffer" , clinic = c )
31583090 self .assertEqual (repr (destination2 ), "<clinic.Destination 'bar' type='buffer'>" )
31593091
31603092 def test_Module_repr (self ):
3161- module = clinic .Module ("foo" , FakeClinic ())
3093+ module = clinic .Module ("foo" , _make_clinic ())
31623094 self .assertRegex (repr (module ), r"<clinic.Module 'foo' at \d+>" )
31633095
31643096 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' )
31663098 self .assertRegex (repr (cls ), r"<clinic.Class 'foo' at \d+>" )
31673099
31683100 def test_FunctionKind_repr (self ):
@@ -3176,7 +3108,7 @@ def test_FunctionKind_repr(self):
31763108 def test_Function_and_Parameter_reprs (self ):
31773109 function = clinic .Function (
31783110 name = 'foo' ,
3179- module = FakeClinic (),
3111+ module = _make_clinic (),
31803112 cls = None ,
31813113 c_basename = None ,
31823114 full_name = 'foofoo' ,
0 commit comments