PageRenderTime 153ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

https://github.com/the-vk/commandline
C# | 332 lines | 207 code | 61 blank | 64 comment | 0 complexity | 945bc15028e49b15fc83768b1de36849 MD5 | raw file
  1. // Copyright 2005-2013 Giacomo Stelluti Scala & Contributors. All rights reserved. See doc/License.md in the project root for license information.
  2. using System;
  3. using System.Globalization;
  4. using System.Linq;
  5. using CommandLine.Core;
  6. using CommandLine.Tests.Fakes;
  7. using FluentAssertions;
  8. using Xunit;
  9. namespace CommandLine.Tests.Unit.Core
  10. {
  11. public class InstanceBuilderTests
  12. {
  13. [Fact]
  14. public void Explicit_help_request_generates_help_requested_error()
  15. {
  16. // Fixture setup
  17. var fakeOptions = new FakeOptions();
  18. var expectedResult = ParserResult.Create(
  19. ParserResultType.Options,
  20. fakeOptions, new Error[] { new HelpRequestedError() });
  21. // Exercize system
  22. var result = InstanceBuilder.Build(
  23. () => fakeOptions,
  24. new[] { "--help" },
  25. StringComparer.Ordinal,
  26. CultureInfo.InvariantCulture);
  27. // Verify outcome
  28. Assert.True(expectedResult.Equals(result));
  29. // Teardown
  30. }
  31. [Fact]
  32. public void Parse_negative_int_value()
  33. {
  34. // Fixture setup
  35. var expectedResult = -123;
  36. // Exercize system
  37. var result = InstanceBuilder.Build(
  38. () => new FakeOptions(),
  39. new[] { "-123" },
  40. StringComparer.Ordinal,
  41. CultureInfo.InvariantCulture);
  42. // Verify outcome
  43. Assert.Equal(expectedResult, result.Value.LongValue);
  44. // Teardown
  45. }
  46. [Fact]
  47. public void Parse_double_value()
  48. {
  49. // Fixture setup
  50. var expectedResult = .123D;
  51. // Exercize system
  52. var result = InstanceBuilder.Build(
  53. () => new FakeOptionsWithDouble(),
  54. new[] { "0.123" },
  55. StringComparer.Ordinal,
  56. CultureInfo.InvariantCulture);
  57. // Verify outcome
  58. Assert.Equal(expectedResult, result.Value.DoubleValue);
  59. // Teardown
  60. }
  61. [Fact]
  62. public void Parse_negative_double_value()
  63. {
  64. // Fixture setup
  65. var expectedResult = -.123D;
  66. // Exercize system
  67. var result = InstanceBuilder.Build(
  68. () => new FakeOptionsWithDouble(),
  69. new[] { "-0.123" },
  70. StringComparer.Ordinal,
  71. CultureInfo.InvariantCulture);
  72. // Verify outcome
  73. Assert.Equal(expectedResult, result.Value.DoubleValue);
  74. // Teardown
  75. }
  76. [Fact]
  77. public void Parse_int_sequence_with_range()
  78. {
  79. // Fixture setup
  80. var expectedResult = new[] { 10, 20, 30, 40 };
  81. // Exercize system
  82. var result = InstanceBuilder.Build(
  83. () => new FakeOptions(),
  84. new[] { "-i", "10", "20", "30", "40" },
  85. StringComparer.Ordinal,
  86. CultureInfo.InvariantCulture);
  87. // Verify outcome
  88. Assert.True(expectedResult.SequenceEqual(result.Value.IntSequence));
  89. // Teardown
  90. }
  91. [Fact]
  92. public void Parse_enum_value()
  93. {
  94. // Fixture setup
  95. var expectedResult = new FakeOptionsWithEnum
  96. {
  97. Colors = Colors.Green
  98. };
  99. // Exercize system
  100. var result = InstanceBuilder.Build(
  101. () => new FakeOptionsWithEnum(),
  102. new[] { "--colors", "Green" },
  103. StringComparer.Ordinal,
  104. CultureInfo.InvariantCulture);
  105. // Verify outcome
  106. expectedResult.ShouldHave().AllProperties().EqualTo(result.Value);
  107. // Teardown
  108. }
  109. [Fact]
  110. public void Parse_values_partitioned_between_sequence_and_scalar()
  111. {
  112. // Fixture setup
  113. var expectedResult = new FakeOptionsWithValues
  114. {
  115. StringValue = string.Empty,
  116. LongValue = 10L,
  117. StringSequence = new[] { "a", "b", "c" },
  118. IntValue = 20
  119. };
  120. // Exercize system
  121. var result = InstanceBuilder.Build(
  122. () => new FakeOptionsWithValues(),
  123. new[] { "10", "a", "b", "c", "20" },
  124. StringComparer.Ordinal,
  125. CultureInfo.InvariantCulture);
  126. // Verify outcome
  127. expectedResult.ShouldHave().AllProperties().EqualTo(result.Value);
  128. // Teardown
  129. }
  130. [Fact]
  131. public void Parse_sequence_value_without_range_constraints()
  132. {
  133. // Fixture setup
  134. var expectedResult = new FakeOptionsWithSequenceWithoutRange
  135. {
  136. LongSequence = new[] { 1L, 2L, 3L, 4L, 5L, 6L }
  137. };
  138. // Exercize system
  139. var result = InstanceBuilder.Build(
  140. () => new FakeOptionsWithSequenceWithoutRange(),
  141. new[] { "1", "2", "3", "4", "5", "6" },
  142. StringComparer.Ordinal,
  143. CultureInfo.InvariantCulture);
  144. // Verify outcome
  145. expectedResult.ShouldHave().AllProperties().EqualTo(result.Value);
  146. // Teardown
  147. }
  148. /// <summary>
  149. /// https://github.com/gsscoder/commandline/issues/31
  150. /// </summary>
  151. [Fact]
  152. public void Double_dash_force_subsequent_arguments_as_values()
  153. {
  154. // Fixture setup
  155. var expectedResult = new FakeOptionsWithValues
  156. {
  157. StringValue = "str1",
  158. LongValue = 10L,
  159. StringSequence = new[] { "-a", "--bee", "-c" },
  160. IntValue = 20
  161. };
  162. var arguments = new[] { "--stringvalue", "str1", "--", "10", "-a", "--bee", "-c", "20" };
  163. // Exercize system
  164. var result = InstanceBuilder.Build(
  165. () => new FakeOptionsWithValues(),
  166. (a, optionSpecs) =>
  167. Tokenizer.PreprocessDashDash(a,
  168. args => Tokenizer.Tokenize(args, name => NameLookup.Contains(name, optionSpecs, StringComparer.Ordinal))),
  169. arguments,
  170. StringComparer.Ordinal,
  171. CultureInfo.InvariantCulture);
  172. // Verify outcome
  173. expectedResult.ShouldHave().AllProperties().EqualTo(result.Value);
  174. // Teardown
  175. }
  176. [Fact]
  177. public void Parse_option_from_different_sets_gererates_MutuallyExclusiveSetError()
  178. {
  179. // Fixture setup
  180. var expectedResult = new[]
  181. {
  182. new MutuallyExclusiveSetError(new NameInfo("", "weburl")),
  183. new MutuallyExclusiveSetError(new NameInfo("", "ftpurl"))
  184. };
  185. // Exercize system
  186. var result = InstanceBuilder.Build(
  187. () => new FakeOptionsWithSets(),
  188. new[] { "--weburl", "http://mywebsite.org/", "--ftpurl", "fpt://ftpsite.org/" },
  189. StringComparer.Ordinal,
  190. CultureInfo.InvariantCulture);
  191. // Verify outcome
  192. Assert.True(expectedResult.SequenceEqual(result.Errors));
  193. // Teardown
  194. }
  195. [Fact]
  196. public void Omitting_required_option_gererates_MissingRequiredOptionError()
  197. {
  198. // Fixture setup
  199. var expectedResult = new[] { new MissingRequiredOptionError(new NameInfo("", "str")) };
  200. // Exercize system
  201. var result = InstanceBuilder.Build(
  202. () => new FakeOptionWithRequired(),
  203. new string[] { },
  204. StringComparer.Ordinal,
  205. CultureInfo.InvariantCulture);
  206. // Verify outcome
  207. Assert.True(expectedResult.SequenceEqual(result.Errors));
  208. // Teardown
  209. }
  210. [Fact]
  211. public void Wrong_range_in_sequence_gererates_SequenceOutOfRangeError()
  212. {
  213. // Fixture setup
  214. var expectedResult = new[] { new SequenceOutOfRangeError(new NameInfo("i", "")) };
  215. // Exercize system
  216. var result = InstanceBuilder.Build(
  217. () => new FakeOptions(),
  218. new [] { "-i", "10" },
  219. StringComparer.Ordinal,
  220. CultureInfo.InvariantCulture);
  221. // Verify outcome
  222. Assert.True(expectedResult.SequenceEqual(result.Errors));
  223. // Teardown
  224. }
  225. [Fact]
  226. public void Parse_unknown_long_option_gererates_UnknownOptionError()
  227. {
  228. // Fixture setup
  229. var expectedResult = new[] { new UnknownOptionError("xyz") };
  230. // Exercize system
  231. var result = InstanceBuilder.Build(
  232. () => new FakeOptions(),
  233. new[] { "--stringvalue", "abc", "--xyz" },
  234. StringComparer.Ordinal,
  235. CultureInfo.InvariantCulture);
  236. // Verify outcome
  237. Assert.True(expectedResult.SequenceEqual(result.Errors));
  238. // Teardown
  239. }
  240. [Fact]
  241. public void Parse_unknown_short_option_gererates_UnknownOptionError()
  242. {
  243. // Fixture setup
  244. var expectedResult = new[] { new UnknownOptionError("z") };
  245. // Exercize system
  246. var result = InstanceBuilder.Build(
  247. () => new FakeOptions(),
  248. new[] { "-z", "-x" },
  249. StringComparer.Ordinal,
  250. CultureInfo.InvariantCulture);
  251. // Verify outcome
  252. Assert.True(expectedResult.SequenceEqual(result.Errors));
  253. // Teardown
  254. }
  255. [Fact]
  256. public void Parse_unknown_short_option_in_option_group_gererates_UnknownOptionError()
  257. {
  258. // Fixture setup
  259. var expectedResult = new[] { new UnknownOptionError("z") };
  260. // Exercize system
  261. var result = InstanceBuilder.Build(
  262. () => new FakeOptions(),
  263. new[] { "-zx" },
  264. StringComparer.Ordinal,
  265. CultureInfo.InvariantCulture);
  266. // Verify outcome
  267. Assert.True(expectedResult.SequenceEqual(result.Errors));
  268. // Teardown
  269. }
  270. }
  271. }