Skip to content

Commit 030679c

Browse files
committed
more tests for root_compiler
1 parent d77b8aa commit 030679c

3 files changed

Lines changed: 109 additions & 24 deletions

File tree

‎paths_cli/compiling/root_compiler.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def clean_input_key(key):
2929
"""
3030
key = key.lower()
3131
key = "_".join(key.split()) # whitespace to underscore
32-
key.replace("-", "_")
32+
key = key.replace("-", "_")
3333
return key
3434

3535
### Managing known compilers and aliases to the known compilers ############
@@ -63,8 +63,9 @@ def _get_compiler(compiler_name):
6363
return _COMPILERS[canonical_name]
6464

6565
def _register_compiler_plugin(plugin):
66-
DUPLICATE_ERROR = RuntimeError(f"The name {plugin.name} has been "
67-
"reserved by another compiler")
66+
DUPLICATE_ERROR = CompilerRegistrationError(
67+
f"The name {plugin.name} has been reserved by another compiler"
68+
)
6869
if plugin.name in _COMPILERS:
6970
raise DUPLICATE_ERROR
7071

@@ -175,10 +176,11 @@ def do_compile(dct):
175176
"""
176177
objs = []
177178
for category in _sort_user_categories(dct):
178-
# func = COMPILERS[category]
179+
# breakpoint()
179180
func = _get_compiler(category)
180181
yaml_objs = dct.get(category, [])
181-
print(f"{yaml_objs}")
182-
new = [func(obj) for obj in yaml_objs]
182+
logger.debug(f"{yaml_objs}")
183+
new = func(yaml_objs)
184+
# new = [func(obj) for obj in yaml_objs]
183185
objs.extend(new)
184186
return objs

‎paths_cli/tests/compiling/test_core.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,14 @@ def test_register_builder_duplicate(self):
295295

296296
@pytest.mark.parametrize('input_type', ['str', 'dict'])
297297
def test_compile(self, input_type):
298+
# the compile method should work whether the input is a dict
299+
# representing an object to be compiled or string name for an
300+
# already-compiled object
298301
pytest.skip()
299302

303+
@pytest.mark.parametrize('input_type', ['str', 'dict'])
300304
@pytest.mark.parametrize('as_list', [True, False])
301-
def test_call(self, as_list):
305+
def test_call(self, input_type, as_list):
306+
# the call method should work whether the input is a single object
307+
# or a list of objects (as well as whether string or dict)
302308
pytest.skip()

‎paths_cli/tests/compiling/test_root_compiler.py‎

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import paths_cli
33
from paths_cli.compiling.root_compiler import *
44
from 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
)
99
from unittest.mock import Mock, PropertyMock, patch
1010
from paths_cli.compiling.core import Compiler, InstanceBuilder
@@ -23,7 +23,8 @@ def foo_compiler_plugin():
2323

2424
@pytest.fixture
2525
def 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

4351
class 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

96111
def 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

145192
def 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

178225
def 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

Comments
 (0)