35
35
from powerapi .cli .parser import Parser , MainParser , ComponentSubParser
36
36
from powerapi .cli .parser import store_true
37
37
from powerapi .cli .parser import AlreadyAddedArgumentException , BadTypeException
38
- from powerapi .cli .parser import UnknowArgException , BadContextException , MissingValueException
38
+ from powerapi .cli .parser import UnknowArgException , BadContextException , MissingValueException , ComponentAlreadyExistException
39
+ from powerapi .cli .parser import SubParserWithoutNameArgumentException , NoNameSpecifiedForComponentException
39
40
from powerapi .cli .parser import TooManyArgumentNamesException , BadValueException
40
41
41
42
@@ -200,19 +201,21 @@ def test_subparser():
200
201
- "" : {}
201
202
- "-z" : UnknowArgException(z)
202
203
- "-a" : {a: True}
203
- - "-a --sub toto -b" : {a:True, sub: {'toto' : {b: True}}}
204
+ - "-a --sub toto -b" : NoNameSpecifiedForComponentException
205
+ - "-a --sub toto -b --name titi" : {a:True, sub: { titi: {'toto' : {b: True}}}}
204
206
- "-b" : BadContextException(b, [toto])
205
207
206
208
Parser description :
207
209
208
210
- base parser arguments : -a
209
- - subparser toto binded to the argument sub with sub arguments : -b
211
+ - subparser toto binded to the argument sub with sub arguments : -b and --name
210
212
"""
211
213
parser = MainParser (help_arg = False )
212
214
parser .add_argument ('a' , flag = True , action = store_true )
213
215
214
216
subparser = ComponentSubParser ('toto' )
215
217
subparser .add_argument ('b' , flag = True , action = store_true )
218
+ subparser .add_argument ('n' , 'name' )
216
219
parser .add_component_subparser ('sub' , subparser )
217
220
218
221
check_parsing_result (parser , '' , {})
@@ -222,13 +225,53 @@ def test_subparser():
222
225
223
226
check_parsing_result (parser , '-a' , {'a' : True })
224
227
225
- check_parsing_result (parser , '-a --sub toto -b' ,
226
- {'a' : True , 'sub' : {'toto' : { 'b' : True }}})
228
+ with pytest .raises (NoNameSpecifiedForComponentException ):
229
+ check_parsing_result (parser , '-a --sub toto -b' , {})
230
+
231
+ check_parsing_result (parser , '-a --sub toto -b --name titi' , {'a' : True , 'sub' : {'toto' : {'titi' : {'name' : 'titi' , 'b' : True }}}})
227
232
228
233
with pytest .raises (BadContextException ):
229
234
check_parsing_result (parser , '-b' , None )
230
235
231
236
237
+ def test_create_two_component ():
238
+ """
239
+ Create two component of the same type with the following cli :
240
+ --sub toto --name titi --sub toto -b --name tutu
241
+
242
+ test if the result is :
243
+ {sub:{'toto' : {'titi': {'name': 'titi'}, 'tutu': {'name': 'tutu', 'b':False}}}}
244
+
245
+ """
246
+ parser = MainParser (help_arg = False )
247
+
248
+ subparser = ComponentSubParser ('toto' )
249
+ subparser .add_argument ('b' , flag = True , action = store_true )
250
+ subparser .add_argument ('n' , 'name' )
251
+ parser .add_component_subparser ('sub' , subparser )
252
+
253
+ check_parsing_result (parser , '--sub toto --name titi --sub toto -b --name tutu' , {'sub' : {'toto' : {'titi' : {'name' : 'titi' }, 'tutu' : {'name' : 'tutu' , 'b' : True }}}})
254
+
255
+
256
+ def test_create_component_that_already_exist ():
257
+ """
258
+ Create two component with the same name with the following cli
259
+ --sub toto --name titi --sub toto --name titi
260
+
261
+ test if an ComponentAlreadyExistException is raised
262
+
263
+
264
+ """
265
+ parser = MainParser (help_arg = False )
266
+
267
+ subparser = ComponentSubParser ('toto' )
268
+ subparser .add_argument ('b' , flag = True , action = store_true )
269
+ subparser .add_argument ('n' , 'name' )
270
+ parser .add_component_subparser ('sub' , subparser )
271
+
272
+ with pytest .raises (ComponentAlreadyExistException ):
273
+ check_parsing_result (parser , '--sub toto --name titi --sub toto --name titi' , None )
274
+
232
275
def test_argument_with_val ():
233
276
"""
234
277
test to parse strings with a parser and retrieve the following results :
@@ -348,9 +391,11 @@ def test_add_component_subparser_that_aldready_exists():
348
391
"""
349
392
parser = MainParser (help_arg = False )
350
393
subparser = ComponentSubParser ('titi' )
394
+ subparser .add_argument ('n' , 'name' )
351
395
parser .add_component_subparser ('toto' , subparser )
352
396
subparser2 = ComponentSubParser ('titi' )
353
-
397
+ subparser2 .add_argument ('n' , 'name' )
398
+
354
399
with pytest .raises (AlreadyAddedArgumentException ):
355
400
parser .add_component_subparser ('toto' , subparser2 )
356
401
@@ -363,8 +408,21 @@ def test_add_component_subparser_with_two_name():
363
408
parser = MainParser (help_arg = False )
364
409
subparser = ComponentSubParser ('titi' )
365
410
subparser .add_argument ('a' , 'aaa' , flag = True , action = store_true , default = False )
411
+ subparser .add_argument ('n' , 'name' )
366
412
parser .add_component_subparser ('sub' , subparser )
367
- check_parsing_result (parser , '--sub titi -a' , {'sub' : {'titi' : {'aaa' : True }}})
413
+ check_parsing_result (parser , '--sub titi -a --name tutu' , {'sub' : {'titi' : {'tutu' : {'aaa' : True , 'name' : 'tutu' }}}})
414
+
415
+
416
+ def test_add_component_subparser_that_aldready_exists ():
417
+ """
418
+ Add a component_subparser with no argument 'name'
419
+ test if a SubParserWithoutNameArgumentException is raised
420
+ """
421
+ parser = MainParser (help_arg = False )
422
+ subparser = ComponentSubParser ('titi' )
423
+
424
+ with pytest .raises (SubParserWithoutNameArgumentException ):
425
+ parser .add_component_subparser ('toto' , subparser )
368
426
369
427
370
428
def test_parse_empty_string_default_value ():
0 commit comments