44 _get_parser , _get_registration_names , _register_builder_plugin ,
55 _register_parser_plugin
66)
7- from unittest .mock import Mock , patch
8- from paths_cli .parsing .core import Parser
7+ from unittest .mock import Mock , PropertyMock , patch
8+ from paths_cli .parsing .core import Parser , InstanceBuilder
9+ from paths_cli .parsing .plugins import ParserPlugin
10+
11+
12+ ### FIXTURES ###############################################################
13+
14+ @pytest .fixture
15+ def foo_parser ():
16+ return Parser (None , 'foo' )
17+
18+ @pytest .fixture
19+ def foo_parser_plugin ():
20+ return ParserPlugin (Mock (parser_name = 'foo' ), ['bar' ])
21+
22+ @pytest .fixture
23+ def foo_baz_builder_plugin ():
24+ builder = InstanceBuilder (None , [], name = 'baz' )
25+ builder .parser_name = 'foo'
26+ return builder
27+
28+ ### CONSTANTS ##############################################################
929
1030PARSER_LOC = "paths_cli.parsing.root_parser.PARSERS"
1131
32+ ### TESTS ##################################################################
33+
1234class TestParserProxy :
1335 def setup (self ):
1436 self .parser = Parser (None , "foo" )
@@ -46,10 +68,9 @@ def test_parser_for_nonexisting():
4668 with pytest .raises (KeyError ):
4769 proxy ._proxy
4870
49- def test_parser_for_existing ():
71+ def test_parser_for_existing (foo_parser ):
5072 # if a parser already exists when parser_for is called, then parser_for
5173 # should get that as its proxy
52- foo_parser = Parser (None , 'foo' )
5374 with patch .dict (PARSER_LOC , {'foo' : foo_parser }):
5475 proxy = parser_for ('foo' )
5576 assert proxy ._proxy is foo_parser
@@ -59,25 +80,42 @@ def test_parser_for_registered():
5980 # should use that as its proxy
6081 pytest .skip ()
6182
62- def test_get_parser_existing ():
83+ def test_get_parser_existing (foo_parser ):
6384 # if a parser has been registered, then _get_parser should return the
6485 # registered parser
65- pytest .skip ()
86+ with patch .dict (PARSER_LOC , {'foo' : foo_parser }):
87+ assert _get_parser ('foo' ) is foo_parser
6688
67- def test_get_parser_nonexisting ():
89+ def test_get_parser_nonexisting (foo_parser ):
6890 # if a parser has not been registered, then _get_parser should create
6991 # the parser
70- pytest .skip ()
71-
72- def test_get_registration_names ():
92+ with patch .dict (PARSER_LOC , {}):
93+ parser = _get_parser ('foo' )
94+ assert parser is not foo_parser # overkill
95+ assert parser .label == 'foo'
96+ assert 'foo' in PARSERS
97+
98+ @pytest .mark .parametrize ('canonical,aliases,expected' , [
99+ ('foo' , ['bar' , 'baz' ], ['foo' , 'bar' , 'baz' ]),
100+ ('foo' , ['baz' , 'bar' ], ['foo' , 'baz' , 'bar' ]),
101+ ('foo' , ['foo' , 'bar' ], ['foo' , 'bar' ]),
102+ ])
103+ def test_get_registration_names (canonical , aliases , expected ):
73104 # _get_registration_names should always provide the names in order
74105 # `canonical, alias1, alias2, ...` regardless of whether `canonical` is
75106 # also listed in the aliases
76- pytest .skip ()
107+ plugin = Mock (aliases = aliases )
108+ type(plugin ).name = PropertyMock (return_value = canonical )
109+ assert _get_registration_names (plugin ) == expected
77110
78- def test_register_parser_plugin ():
111+ def test_register_parser_plugin (foo_parser_plugin ):
79112 # _register_parser_plugin should register parsers that don't exist
80- pytest .skip ()
113+ parsers = {}
114+ with patch .dict (PARSER_LOC , parsers ):
115+ assert 'foo' not in parsers
116+ _register_parser_plugin (foo_parser_plugin )
117+ assert 'foo' in PARSERS
118+ assert 'bar' in ALIASES
81119
82120def test_register_parser_plugin_duplicate ():
83121 # if a parser of the same name exists either in canonical or aliases,
@@ -89,12 +127,16 @@ def test_register_builder_plugin():
89127 # including registering the parser if needed
90128 pytest .skip ()
91129
92- def test_register_plugins_unit ():
130+ def test_register_plugins_unit (foo_parser_plugin , foo_baz_builder_plugin ):
93131 # register_plugins should correctly sort builder and parser plugins, and
94132 # call the correct registration functions
95- # TODO: patch _register_builder & _register_parser; this is unit, not
96- # integration
97- pytest .skip ()
133+ BASE = "paths_cli.parsing.root_parser."
134+
135+ with patch (BASE + "_register_builder_plugin" , Mock ()) as builder , \
136+ patch (BASE + "_register_parser_plugin" , Mock ()) as parser :
137+ register_plugins ([foo_baz_builder_plugin , foo_parser_plugin ])
138+ assert builder .called_once_with (foo_baz_builder_plugin )
139+ assert parser .called_once_with (foo_parser_plugin )
98140
99141def test_register_plugins_integration ():
100142 # register_plugins should correctly register plugins
0 commit comments