24
24
using chocolatey . infrastructure . commandline ;
25
25
using Moq ;
26
26
using FluentAssertions ;
27
+ using chocolatey . infrastructure . app . domain ;
27
28
28
29
namespace chocolatey . tests . infrastructure . app . commands
29
30
{
@@ -38,6 +39,8 @@ public abstract class ChocolateySearchCommandSpecsBase : TinySpec
38
39
39
40
public override void Context ( )
40
41
{
42
+ MockLogger . Reset ( ) ;
43
+
41
44
Configuration . Sources = "bob" ;
42
45
Command = new ChocolateySearchCommand ( PackageService . Object ) ;
43
46
}
@@ -259,6 +262,24 @@ public void Should_add_short_version_of_password_to_the_option_set()
259
262
{
260
263
_optionSet . Contains ( "p" ) . Should ( ) . BeTrue ( ) ;
261
264
}
265
+
266
+ [ Fact ]
267
+ public void Should_add_order_by_to_the_option_set ( )
268
+ {
269
+ _optionSet . Contains ( "order-by" ) . Should ( ) . BeTrue ( ) ;
270
+ }
271
+
272
+ [ Fact ]
273
+ public void Should_add_order_by_popularity_to_the_option_set ( )
274
+ {
275
+ _optionSet . Contains ( "order-by-popularity" ) . Should ( ) . BeTrue ( ) ;
276
+ }
277
+
278
+ [ Fact ]
279
+ public void Should_have_marked_order_by_popularity_as_deprecated ( )
280
+ {
281
+ _optionSet [ "order-by-popularity" ] . Description . Should ( ) . Contain ( "Deprecated" ) ;
282
+ }
262
283
}
263
284
264
285
public class When_handling_additional_argument_parsing : ChocolateySearchCommandSpecsBase
@@ -316,6 +337,192 @@ public void Should_call_service_list_noop()
316
337
}
317
338
}
318
339
340
+ [ NUnit . Framework . TestFixtureSource ( nameof ( TestOrders ) ) ]
341
+ public class When_handling_order_by_parameter_correctly_parses_argument : ChocolateySearchCommandSpecsBase
342
+ {
343
+ private OptionSet _optionSet ;
344
+ private OptionContext _optionContext ;
345
+ private PackageOrder _order ;
346
+ private string _orderString ;
347
+
348
+ public When_handling_order_by_parameter_correctly_parses_argument ( string orderString )
349
+ {
350
+ _orderString = orderString ;
351
+ _order = ( PackageOrder ) Enum . Parse ( typeof ( PackageOrder ) , _orderString ) ;
352
+ }
353
+
354
+ private static object [ ] TestOrders
355
+ {
356
+ get
357
+ {
358
+ var values = Enum . GetNames ( typeof ( PackageOrder ) ) ;
359
+ var result = new List < object > ( ) ;
360
+
361
+ foreach ( var value in values )
362
+ {
363
+ result . Add ( new object [ ] { value } ) ;
364
+ }
365
+
366
+ return result . ToArray ( ) ;
367
+ }
368
+ }
369
+
370
+ public override void Context ( )
371
+ {
372
+ base . Context ( ) ;
373
+
374
+ _optionSet = new OptionSet ( ) ;
375
+ Command . ConfigureArgumentParser ( _optionSet , Configuration ) ;
376
+ _optionContext = new OptionContext ( _optionSet )
377
+ {
378
+ Option = _optionSet [ "order-by" ]
379
+ } ;
380
+ _optionContext . OptionName = _optionContext . Option . Names . First ( ) ;
381
+ _optionContext . OptionValues . Add ( _orderString ) ;
382
+ }
383
+
384
+ public override void Because ( )
385
+ {
386
+ _optionContext . Option . Invoke ( _optionContext ) ;
387
+ }
388
+
389
+ [ Fact ]
390
+ public void Should_have_set_expected_package_sort_on_config ( )
391
+ {
392
+ Configuration . ListCommand . OrderBy . Should ( ) . Be ( _order ) ;
393
+ }
394
+ }
395
+
396
+ [ NUnit . Framework . TestFixture ( null ) ]
397
+ [ NUnit . Framework . TestFixture ( "" ) ]
398
+ public class When_handling_order_by_with_empty_values : ChocolateySearchCommandSpecsBase
399
+ {
400
+ private OptionSet _optionSet ;
401
+ private OptionContext _optionContext ;
402
+ private Exception _ex = null ;
403
+ private string _testValue ;
404
+
405
+ public When_handling_order_by_with_empty_values ( string testValue )
406
+ {
407
+ _testValue = testValue ;
408
+ }
409
+
410
+ public override void Context ( )
411
+ {
412
+ base . Context ( ) ;
413
+
414
+ _optionSet = new OptionSet ( ) ;
415
+ Command . ConfigureArgumentParser ( _optionSet , Configuration ) ;
416
+ _optionContext = new OptionContext ( _optionSet )
417
+ {
418
+ Option = _optionSet [ "order-by" ]
419
+ } ;
420
+ _optionContext . OptionName = _optionContext . Option . Names . First ( ) ;
421
+ _optionContext . OptionValues . Add ( _testValue ) ;
422
+ }
423
+
424
+ public override void Because ( )
425
+ {
426
+ try
427
+ {
428
+ _optionContext . Option . Invoke ( _optionContext ) ;
429
+ }
430
+ catch ( Exception ex )
431
+ {
432
+ _ex = ex ;
433
+ }
434
+ }
435
+
436
+ [ Fact ]
437
+ public void Should_have_thrown_expected_exception ( )
438
+ {
439
+ _ex . Should ( ) . NotBeNull ( )
440
+ . And . BeOfType < ApplicationException > ( )
441
+ . Which . Message . Should ( ) . StartWith ( "No '--order-by' clause was provided. Specify one of the supported clauses:" ) ;
442
+ }
443
+ }
444
+
445
+ public class When_handling_order_by_with_unsupported_value : ChocolateySearchCommandSpecsBase
446
+ {
447
+ private OptionSet _optionSet ;
448
+ private OptionContext _optionContext ;
449
+ private Exception _ex = null ;
450
+
451
+ public override void Context ( )
452
+ {
453
+ base . Context ( ) ;
454
+
455
+ _optionSet = new OptionSet ( ) ;
456
+ Command . ConfigureArgumentParser ( _optionSet , Configuration ) ;
457
+ _optionContext = new OptionContext ( _optionSet )
458
+ {
459
+ Option = _optionSet [ "order-by" ]
460
+ } ;
461
+ _optionContext . OptionName = _optionContext . Option . Names . First ( ) ;
462
+ _optionContext . OptionValues . Add ( "NotExisting" ) ;
463
+ }
464
+
465
+ public override void Because ( )
466
+ {
467
+ try
468
+ {
469
+ _optionContext . Option . Invoke ( _optionContext ) ;
470
+ }
471
+ catch ( Exception ex )
472
+ {
473
+ _ex = ex ;
474
+ }
475
+ }
476
+
477
+ [ Fact ]
478
+ public void Should_have_thrown_expected_exception ( )
479
+ {
480
+ _ex . Should ( ) . NotBeNull ( )
481
+ . And . BeOfType < ApplicationException > ( )
482
+ . Which . Message . Should ( ) . StartWith ( "The '--order-by' clause 'NotExisting' is not recognized. Use one of the supported clauses:" ) ;
483
+ }
484
+ }
485
+
486
+ public class When_handling_order_by_popularity : ChocolateySearchCommandSpecsBase
487
+ {
488
+ private OptionSet _optionSet ;
489
+ private OptionContext _optionContext ;
490
+
491
+ public override void Context ( )
492
+ {
493
+ base . Context ( ) ;
494
+
495
+ _optionSet = new OptionSet ( ) ;
496
+ Command . ConfigureArgumentParser ( _optionSet , Configuration ) ;
497
+ _optionContext = new OptionContext ( _optionSet )
498
+ {
499
+ Option = _optionSet [ "order-by-popularity" ]
500
+ } ;
501
+ _optionContext . OptionName = _optionContext . Option . Names . First ( ) ;
502
+ _optionContext . OptionValues . Add ( bool . TrueString ) ;
503
+ }
504
+
505
+ public override void Because ( )
506
+ {
507
+ _optionContext . Option . Invoke ( _optionContext ) ;
508
+ }
509
+
510
+ [ Fact ]
511
+ public void Should_have_set_expected_order_by_property_value ( )
512
+ {
513
+ Configuration . ListCommand . OrderBy . Should ( ) . Be ( PackageOrder . Popularity ) ;
514
+ }
515
+
516
+ [ Fact ]
517
+ public void Should_have_outputted_warning_message ( )
518
+ {
519
+ MockLogger . Messages . Should ( )
520
+ . ContainKey ( LogLevel . Warn . ToString ( ) )
521
+ . WhoseValue . Should ( ) . Contain ( @"'--order-by-popularity' is deprecated and will be removed in a future release.
522
+ Use '--order-by='Popularity'' instead." ) ;
523
+ }
524
+ }
525
+
319
526
public class When_noop_is_called : ChocolateySearchCommandSpecsBase
320
527
{
321
528
public override void Because ( )
0 commit comments