PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tests/Parser/NullableTypesParsingFixture.cs

https://github.com/sflanker/commandline
C# | 180 lines | 124 code | 30 blank | 26 comment | 0 complexity | 4ef30371a67468bf227656532212d247 MD5 | raw file
Possible License(s): Apache-2.0
  1. #region License
  2. //
  3. // Command Line Library: NullableTypesParsingFixture.cs
  4. //
  5. // Author:
  6. // Giacomo Stelluti Scala (gsscoder@gmail.com)
  7. //
  8. // Copyright (C) 2005 - 2013 Giacomo Stelluti Scala
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy
  11. // of this software and associated documentation files (the "Software"), to deal
  12. // in the Software without restriction, including without limitation the rights
  13. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. // copies of the Software, and to permit persons to whom the Software is
  15. // furnished to do so, subject to the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. // THE SOFTWARE.
  27. //
  28. #endregion
  29. #region Using Directives
  30. using System;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using Xunit;
  34. using FluentAssertions;
  35. using CommandLine.Tests.Mocks;
  36. #endregion
  37. namespace CommandLine.Tests
  38. {
  39. public class NullableTypesParsingFixture : ParserBaseFixture
  40. {
  41. [Fact]
  42. public void Parse_nullable_integer_option()
  43. {
  44. var options = new NullableTypesOptions();
  45. var parser = new Parser();
  46. var result = parser.ParseArguments(new string[] { "-i", "99" }, options);
  47. result.Should().BeTrue();
  48. options.IntegerValue.Should().Be(99);
  49. options = new NullableTypesOptions();
  50. parser = new Parser();
  51. result = parser.ParseArguments(new string[] { }, options);
  52. result.Should().BeTrue();
  53. options.IntegerValue.Should().NotHaveValue();
  54. }
  55. [Fact]
  56. public void Passing_bad_value_to_a_nullable_integer_option_fails()
  57. {
  58. var options = new NullableTypesOptions();
  59. var parser = new Parser();
  60. var result = parser.ParseArguments(new string[] { "-i", "string-value" }, options);
  61. result.Should().BeFalse();
  62. }
  63. [Fact]
  64. public void Passing_no_value_to_a_nullable_integer_option_fails()
  65. {
  66. var options = new NullableTypesOptions();
  67. var parser = new Parser();
  68. var result = parser.ParseArguments(new string[] { "-int" }, options);
  69. result.Should().BeFalse();
  70. }
  71. [Fact]
  72. public void Parse_nullable_enumeration_option()
  73. {
  74. var options = new NullableTypesOptions();
  75. var parser = new Parser();
  76. var result = parser.ParseArguments(new string[] { "--enum=ReadWrite" }, options);
  77. result.Should().BeTrue();
  78. options.EnumValue.Should().Be(FileAccess.ReadWrite);
  79. options = new NullableTypesOptions();
  80. parser = new Parser();
  81. result = parser.ParseArguments(new string[] { }, options);
  82. result.Should().BeTrue();
  83. options.EnumValue.Should().BeNull();
  84. }
  85. [Fact]
  86. public void Passing_bad_value_to_a_nullable_enumeration_option_fails()
  87. {
  88. var options = new NullableTypesOptions();
  89. var parser = new Parser();
  90. var result = parser.ParseArguments(new string[] { "-e", "Overwrite" }, options);
  91. result.Should().BeFalse();
  92. }
  93. [Fact]
  94. public void Passing_no_value_to_a_nullable_enumeration_option_fails()
  95. {
  96. var options = new NullableTypesOptions();
  97. var parser = new Parser();
  98. var result = parser.ParseArguments(new string[] { "--enum" }, options);
  99. result.Should().BeFalse();
  100. }
  101. [Fact]
  102. public void Parse_nullable_double_option()
  103. {
  104. var options = new NullableTypesOptions();
  105. var parser = new Parser();
  106. var result = parser.ParseArguments(new string[] { "-d9.999" }, options);
  107. result.Should().BeTrue();
  108. options.DoubleValue.Should().Be(9.999);
  109. options = new NullableTypesOptions();
  110. parser = new Parser();
  111. result = parser.ParseArguments(new string[] { }, options);
  112. result.Should().BeTrue();
  113. options.DoubleValue.Should().NotHaveValue();
  114. }
  115. [Fact]
  116. public void Passing_bad_value_to_a_nullable_double_option_fails()
  117. {
  118. var options = new NullableTypesOptions();
  119. var parser = new Parser();
  120. var result = parser.ParseArguments(new string[] { "--double", "9,999" }, options);
  121. result.Should().BeFalse();
  122. }
  123. [Fact]
  124. public void Passing_no_value_to_a_nullable_double_option_fails()
  125. {
  126. var options = new NullableTypesOptions();
  127. var parser = new Parser();
  128. var result = parser.ParseArguments(new string[] { "-d" }, options);
  129. result.Should().BeFalse();
  130. }
  131. [Fact]
  132. public void Parse_string_option_and_nullable_value_types()
  133. {
  134. var options = new NullableTypesOptions();
  135. var parser = new Parser();
  136. var result = parser.ParseArguments(new string[] { "--string", "alone" }, options);
  137. result.Should().BeTrue();
  138. options.StringValue.Should().Be("alone");
  139. options = new NullableTypesOptions();
  140. parser = new Parser();
  141. result = parser.ParseArguments(
  142. new string[] { "-d1.789", "--int", "10099", "-stogether", "--enum", "Read" }, options);
  143. result.Should().BeTrue();
  144. options.DoubleValue.Should().Be(1.789D);
  145. options.IntegerValue.Should().Be(10099);
  146. options.StringValue.Should().Be("together");
  147. options.EnumValue.Should().Be(FileAccess.Read);
  148. }
  149. }
  150. }