22import paths_cli
33from paths_cli .compiling .root_compiler import *
44from paths_cli .compiling .root_compiler import (
5- _get_compiler , _get_registration_names , _register_builder_plugin ,
6- _register_compiler_plugin , _sort_user_categories , _CompilerProxy ,
7- _COMPILERS , _ALIASES
5+ _canonical_name , _get_compiler , _get_registration_names ,
6+ _register_builder_plugin , _register_compiler_plugin ,
7+ _sort_user_categories , _CompilerProxy , _COMPILERS , _ALIASES
88)
99from unittest .mock import Mock , PropertyMock , patch
1010from paths_cli .compiling .core import Compiler , InstanceBuilder
@@ -23,7 +23,8 @@ def foo_compiler_plugin():
2323
2424@pytest .fixture
2525def foo_baz_builder_plugin ():
26- builder = InstanceBuilder (None , [], name = 'baz' )
26+ builder = InstanceBuilder (lambda : "FOO" , [], name = 'baz' ,
27+ aliases = ['qux' ])
2728 builder .compiler_name = 'foo'
2829 return builder
2930
@@ -34,11 +35,18 @@ def foo_baz_builder_plugin():
3435
3536### TESTS ##################################################################
3637
37- def clean_input_key ():
38- pytest .skip ()
38+ @pytest .mark .parametrize ('input_string' , ["foo-bar" , "FOO_bar" , "foo bar" ,
39+ "foo_bar" , "foo BAR" ])
40+ def test_clean_input_key (input_string ):
41+ assert clean_input_key (input_string ) == "foo_bar"
3942
40- def test_canonical_name ():
41- pytest .skip ()
43+ @pytest .mark .parametrize ('input_name' , ['canonical' , 'alias' ])
44+ def test_canonical_name (input_name ):
45+ compilers = {'canonical' : "FOO" }
46+ aliases = {'alias' : 'canonical' }
47+ with patch .dict (COMPILER_LOC , compilers ) as compilers_ , \
48+ patch .dict (BASE + "_ALIASES" , aliases ) as aliases_ :
49+ assert _canonical_name (input_name ) == "canonical"
4250
4351class TestCompilerProxy :
4452 def setup (self ):
@@ -83,15 +91,22 @@ def test_compiler_for_existing(foo_compiler):
8391 proxy = compiler_for ('foo' )
8492 assert proxy ._proxy is foo_compiler
8593
86- def test_compiler_for_registered ( ):
94+ def test_compiler_for_unregistered ( foo_compiler ):
8795 # if a compiler is registered after compiler_for is called, then
8896 # compiler_for should use that as its proxy
89- pytest .skip ()
97+ proxy = compiler_for ('foo' )
98+ with patch .dict (COMPILER_LOC , {'foo' : foo_compiler }):
99+ assert proxy ._proxy is foo_compiler
90100
91- def test_compiler_for_registered_alias ():
101+ def test_compiler_for_registered_alias (foo_compiler ):
92102 # if compiler_for is registered as an alias, compiler_for should still
93103 # get the correct compiler
94- pytest .skip ()
104+ compilers = {'foo' : foo_compiler }
105+ aliases = {'bar' : 'foo' }
106+ with patch .dict (COMPILER_LOC , compilers ) as compilers_ , \
107+ patch .dict (BASE + "_ALIASES" , aliases ) as aliases_ :
108+ proxy = compiler_for ('bar' )
109+ assert proxy ._proxy is foo_compiler
95110
96111def test_get_compiler_existing (foo_compiler ):
97112 # if a compiler has been registered, then _get_compiler should return the
@@ -132,15 +147,47 @@ def test_register_compiler_plugin(foo_compiler_plugin):
132147
133148 assert 'foo' not in _COMPILERS
134149
135- def test_register_compiler_plugin_duplicate ():
150+ @pytest .mark .parametrize ('duplicate_of' , ['canonical' , 'alias' ])
151+ @pytest .mark .parametrize ('duplicate_from' , ['canonical' , 'alias' ])
152+ def test_register_compiler_plugin_duplicate (duplicate_of , duplicate_from ):
136153 # if a compiler of the same name exists either in canonical or aliases,
137154 # _register_compiler_plugin should raise an error
138- pytest .skip ()
139155
140- def test_register_builder_plugin ():
156+ # duplicate_of: existing
157+ # duplicate_from: which part of the plugin has the duplicated name
158+ pytest .skip () # FIXME: same duplication problem as before, I think
159+ if duplicate_from == 'canonical' :
160+ plugin = CompilerPlugin (Mock (compiler_name = duplicate_of ),
161+ aliases = ['foo' ])
162+ else :
163+ plugin = CompilerPlugin (Mock (compiler_name = 'foo' ),
164+ aliases = [duplicate_of ])
165+
166+ compilers = {'canonical' : "FOO" }
167+ aliases = {'alias' : 'canonical' }
168+ with patch .dict (COMPILER_LOC , compilers ) as compilers_ ,\
169+ patch .dict (BASE + "_ALIASES" , aliases ) as aliases_ :
170+ with pytest .raises (CompilerRegistrationError ):
171+ _register_compiler_plugin (plugin )
172+
173+ @pytest .mark .parametrize ('compiler_exists' , [True , False ])
174+ def test_register_builder_plugin (compiler_exists , foo_baz_builder_plugin ,
175+ foo_compiler ):
141176 # _register_builder_plugin should register plugins that don't exist,
142177 # including registering the compiler if needed
143- pytest .skip ()
178+ if compiler_exists :
179+ compilers = {'foo' : foo_compiler }
180+ else :
181+ compilers = {}
182+
183+ with patch .dict (COMPILER_LOC , compilers ):
184+ if not compiler_exists :
185+ assert 'foo' not in _COMPILERS
186+ _register_builder_plugin (foo_baz_builder_plugin )
187+ assert 'foo' in _COMPILERS
188+ type_dispatch = _COMPILERS ['foo' ].type_dispatch
189+ assert type_dispatch ['baz' ] is foo_baz_builder_plugin
190+ assert type_dispatch ['qux' ] is foo_baz_builder_plugin
144191
145192def test_register_plugins_unit (foo_compiler_plugin , foo_baz_builder_plugin ):
146193 # register_plugins should correctly sort builder and compiler plugins,
@@ -177,4 +224,34 @@ def test_sort_user_categories():
177224
178225def test_do_compile ():
179226 # compiler should correctly compile a basic input dict
180- pytest .skip ()
227+ compilers = {
228+ 'foo' : Compiler ({
229+ 'baz' : lambda dct : "BAZ" * dct ['x' ]
230+ }, 'foo' ),
231+ 'bar' : Compiler ({
232+ 'qux' : lambda dct : "QUX" * dct ['x' ]
233+ }, 'bar' ),
234+ }
235+ aliases = {'baar' : 'bar' }
236+ input_dict = {
237+ 'foo' : [{
238+ 'type' : 'baz' ,
239+ 'x' : 2
240+ }],
241+ 'baar' : [{
242+ 'type' : 'qux' ,
243+ 'x' : 3
244+ }]
245+ }
246+ order = ['bar' , 'foo' ]
247+ try :
248+ paths_cli .compiling .root_compiler .COMPILE_ORDER = order
249+ with patch .dict (COMPILER_LOC , compilers ) as _compiler ,\
250+ patch .dict (BASE + "_ALIASES" , aliases ) as _alias :
251+ objs = do_compile (input_dict )
252+ finally :
253+ paths_cli .compiling .root_compiler .COMPILE_ORDER = COMPILE_ORDER
254+ # check that we unset properly (test the test)
255+ assert paths_cli .compiling .root_compiler .COMPILE_ORDER [0 ] == 'engine'
256+
257+ assert objs == ["QUXQUXQUX" , "BAZBAZ" ]
0 commit comments