PageRenderTime 69ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tests/Parser/ValueOptionAttributeParsingFixture.cs

https://github.com/sflanker/commandline
C# | 80 lines | 64 code | 13 blank | 3 comment | 0 complexity | 3c45555280357f6e7083fccb822d8610 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CommandLine.Tests.Mocks;
  6. using Xunit;
  7. using FluentAssertions;
  8. namespace CommandLine.Tests
  9. {
  10. /// <summary>
  11. /// [Enhancement] https://github.com/gsscoder/commandline/issues/33
  12. /// </summary>
  13. public class ValueOptionAttributeParsingFixture : ParserBaseFixture
  14. {
  15. [Fact]
  16. public void Value_option_attribute_isolates_non_option_values()
  17. {
  18. var options = new SimpleOptionsWithValueOption();
  19. var parser = new Parser();
  20. var result = parser.ParseArguments(
  21. new string[] { "--switch", "file.ext", "1000", "0.1234", "-s", "out.ext" }, options);
  22. result.Should().BeTrue();
  23. options.BooleanValue.Should().BeTrue();
  24. options.StringItem.Should().Be("file.ext");
  25. options.IntegerItem.Should().Be(1000);
  26. options.NullableDoubleItem.Should().Be(0.1234D);
  27. options.StringValue.Should().Be("out.ext");
  28. }
  29. [Fact]
  30. public void Value_option_attribute_values_are_not_mandatory()
  31. {
  32. var options = new SimpleOptionsWithValueOption();
  33. var parser = new Parser();
  34. var result = parser.ParseArguments(
  35. new string[] { "--switch" }, options);
  36. result.Should().BeTrue();
  37. options.BooleanValue.Should().BeTrue();
  38. options.StringItem.Should().BeNull();
  39. options.IntegerItem.Should().Be(0);
  40. options.NullableDoubleItem.Should().NotHaveValue();
  41. }
  42. [Fact]
  43. public void Value_option_takes_precedence_on_value_list_regardless_declaration_order()
  44. {
  45. var options = new SimpleOptionsWithValueOptionAndValueList();
  46. var parser = new Parser();
  47. var result = parser.ParseArguments(
  48. new string[] { "ofvalueoption", "-1234", "4321", "forvaluelist1", "forvaluelist2", "forvaluelist3" }, options);
  49. result.Should().BeTrue();
  50. options.StringItem.Should().Be("ofvalueoption");
  51. options.NullableInteger.Should().Be(-1234);
  52. options.UnsignedIntegerItem.Should().Be(4321U);
  53. options.Items[0].Should().Be("forvaluelist1");
  54. options.Items[1].Should().Be("forvaluelist2");
  55. options.Items[2].Should().Be("forvaluelist3");
  56. }
  57. [Fact]
  58. public void Between_value_options_order_matters()
  59. {
  60. var options = new SimpleOptionsWithValueOptionAndValueList();
  61. var parser = new Parser();
  62. var result = parser.ParseArguments(
  63. new string[] { "4321", "ofvalueoption", "-1234", "forvaluelist1", "forvaluelist2", "forvaluelist3" }, options);
  64. result.Should().BeFalse();
  65. }
  66. }
  67. }