PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tests/Parser/OptionArrayAttributeParsingFixture.cs

https://github.com/sflanker/commandline
C# | 425 lines | 326 code | 68 blank | 31 comment | 0 complexity | cea519aa353fe4b065ec383732c23c5d MD5 | raw file
Possible License(s): Apache-2.0
  1. #region License
  2. //
  3. // Command Line Library: OptionArrayAttributeParsingFixture.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. using System.Threading;
  29. using System.Globalization;
  30. #endregion
  31. #region Using Directives
  32. using System;
  33. using System.Collections.Generic;
  34. using Xunit;
  35. using FluentAssertions;
  36. using CommandLine.Tests.Mocks;
  37. #endregion
  38. namespace CommandLine.Tests
  39. {
  40. public class OptionArrayAttributeParsingFixture : ParserBaseFixture
  41. {
  42. [Fact]
  43. public void Parse_string_array_option_using_short_name()
  44. {
  45. var options = new SimpleOptionsWithArray();
  46. var parser = new Parser();
  47. var result = parser.ParseArguments(new string[] { "-z", "alfa", "beta", "gamma" }, options);
  48. result.Should().BeTrue();
  49. base.ElementsShouldBeEqual(new string[] { "alfa", "beta", "gamma" }, options.StringArrayValue);
  50. }
  51. [Fact]
  52. public void Parse_string_array_option_using_long_name()
  53. {
  54. var options = new SimpleOptionsWithArray();
  55. var parser = new Parser();
  56. var result = parser.ParseArguments(new string[] { "--strarr", "alfa", "beta", "gamma" }, options);
  57. result.Should().BeTrue();
  58. base.ElementsShouldBeEqual(new string[] { "alfa", "beta", "gamma" }, options.StringArrayValue);
  59. }
  60. [Fact]
  61. public void Parse_string_array_option_using_short_name_with_value_adjacent()
  62. {
  63. var options = new SimpleOptionsWithArray();
  64. var parser = new Parser();
  65. var result = parser.ParseArguments(new string[] { "-zapple", "kiwi" }, options);
  66. result.Should().BeTrue();
  67. base.ElementsShouldBeEqual(new string[] { "apple", "kiwi" }, options.StringArrayValue);
  68. }
  69. [Fact]
  70. public void Parse_string_array_option_using_long_name_with_equal_sign()
  71. {
  72. var options = new SimpleOptionsWithArray();
  73. var parser = new Parser();
  74. var result = parser.ParseArguments(new string[] { "--strarr=apple", "kiwi" }, options);
  75. result.Should().BeTrue();
  76. base.ElementsShouldBeEqual(new string[] { "apple", "kiwi" }, options.StringArrayValue);
  77. }
  78. [Fact]
  79. public void Parse_string_array_option_using_short_name_and_string_option_after()
  80. {
  81. var options = new SimpleOptionsWithArray();
  82. var parser = new Parser();
  83. var result = parser.ParseArguments(new string[] { "-z", "one", "two", "three", "-s", "after" }, options);
  84. result.Should().BeTrue();
  85. base.ElementsShouldBeEqual(new string[] { "one", "two", "three" }, options.StringArrayValue);
  86. options.StringValue.Should().Be("after");
  87. }
  88. [Fact]
  89. public void Parse_string_array_option_using_short_name_and_string_option_before()
  90. {
  91. var options = new SimpleOptionsWithArray();
  92. var parser = new Parser();
  93. var result = parser.ParseArguments(new string[] { "-s", "before", "-z", "one", "two", "three" }, options);
  94. result.Should().BeTrue();
  95. options.StringValue.Should().Be("before");
  96. base.ElementsShouldBeEqual(new string[] { "one", "two", "three" }, options.StringArrayValue);
  97. }
  98. [Fact]
  99. public void Parse_string_array_option_using_short_name_with_options_before_and_after()
  100. {
  101. var options = new SimpleOptionsWithArray();
  102. var parser = new Parser();
  103. var result = parser.ParseArguments(new string[] {
  104. "-i", "191919", "-z", "one", "two", "three", "--switch", "--string=near" }, options);
  105. result.Should().BeTrue();
  106. options.IntegerValue.Should().Be(191919);
  107. base.ElementsShouldBeEqual(new string[] { "one", "two", "three" }, options.StringArrayValue);
  108. options.BooleanValue.Should().BeTrue();
  109. options.StringValue.Should().Be("near");
  110. }
  111. [Fact]
  112. public void Parse_string_array_option_using_long_name_with_value_list()
  113. {
  114. var options = new SimpleOptionsWithArrayAndValueList();
  115. var parser = new Parser();
  116. var result = parser.ParseArguments(new string[] {
  117. "-shere", "-i999", "--strarr=0", "1", "2", "3", "4", "5", "6", "7", "8", "9" , "--switch", "f1.xml", "f2.xml"}, options);
  118. result.Should().BeTrue();
  119. options.StringValue.Should().Be("here");
  120. options.IntegerValue.Should().Be(999);
  121. base.ElementsShouldBeEqual(new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }, options.StringArrayValue);
  122. options.BooleanValue.Should().BeTrue();
  123. base.ElementsShouldBeEqual(new string[] { "f1.xml", "f2.xml" }, options.Items);
  124. }
  125. [Fact]
  126. public void Passing_no_value_to_a_string_array_option_fails()
  127. {
  128. var options = new SimpleOptionsWithArray();
  129. var parser = new Parser();
  130. var result = parser.ParseArguments(new string[] { "-z" }, options);
  131. result.Should().BeFalse();
  132. options = new SimpleOptionsWithArray();
  133. result = parser.ParseArguments(new string[] { "--strarr" }, options);
  134. result.Should().BeFalse();
  135. }
  136. /****************************************************************************************************/
  137. [Fact]
  138. public void Parse_integer_array_option_using_short_name()
  139. {
  140. var options = new SimpleOptionsWithArray();
  141. var parser = new Parser();
  142. var result = parser.ParseArguments(new string[] { "-y", "1", "2", "3" }, options);
  143. result.Should().BeTrue();
  144. base.ElementsShouldBeEqual(new int[] { 1, 2, 3 }, options.IntegerArrayValue);
  145. }
  146. [Fact]
  147. public void Passing_bad_value_to_an_integer_array_option_fails()
  148. {
  149. var options = new SimpleOptionsWithArray();
  150. var parser = new Parser();
  151. var result = parser.ParseArguments(new string[] { "-y", "one", "2", "3" }, options);
  152. result.Should().BeFalse();
  153. options = new SimpleOptionsWithArray();
  154. parser = new Parser();
  155. result = parser.ParseArguments(new string[] { "-yone", "2", "3" }, options);
  156. result.Should().BeFalse();
  157. options = new SimpleOptionsWithArray();
  158. parser = new Parser();
  159. result = parser.ParseArguments(new string[] { "--intarr", "1", "two", "3" }, options);
  160. result.Should().BeFalse();
  161. options = new SimpleOptionsWithArray();
  162. parser = new Parser();
  163. result = parser.ParseArguments(new string[] { "--intarr=1", "2", "three" }, options);
  164. result.Should().BeFalse();
  165. }
  166. [Fact]
  167. public void Passing_no_value_to_an_integer_array_option_fails()
  168. {
  169. var options = new SimpleOptionsWithArray();
  170. var parser = new Parser();
  171. var result = parser.ParseArguments(new string[] { "-y" }, options);
  172. result.Should().BeFalse();
  173. options = new SimpleOptionsWithArray();
  174. result = parser.ParseArguments(new string[] { "--intarr" }, options);
  175. result.Should().BeFalse();
  176. }
  177. /****************************************************************************************************/
  178. [Fact]
  179. public void Parse_double_array_option_using_short_name()
  180. {
  181. var options = new SimpleOptionsWithArray();
  182. var parser = new Parser();
  183. var result = parser.ParseArguments(new string[] { "-q", "0.1", "2.3", "0.9" }, options);
  184. result.Should().BeTrue();
  185. base.ElementsShouldBeEqual(new double[] { .1, 2.3, .9 }, options.DoubleArrayValue);
  186. }
  187. /****************************************************************************************************/
  188. [Fact]
  189. public void Parse_different_arrays_together__combination_one()
  190. {
  191. var options = new SimpleOptionsWithArray();
  192. var parser = new Parser();
  193. var result = parser.ParseArguments(new string[] {
  194. "-z", "one", "two", "three", "four",
  195. "-y", "1", "2", "3", "4",
  196. "-q", "0.1", "0.2", "0.3", "0.4"
  197. }, options);
  198. result.Should().BeTrue();
  199. base.ElementsShouldBeEqual(new string[] { "one", "two", "three", "four" }, options.StringArrayValue);
  200. base.ElementsShouldBeEqual(new int[] { 1, 2, 3, 4 }, options.IntegerArrayValue);
  201. base.ElementsShouldBeEqual(new double[] { .1, .2, .3, .4 }, options.DoubleArrayValue);
  202. options = new SimpleOptionsWithArray();
  203. parser = new Parser();
  204. result = parser.ParseArguments(new string[] {
  205. "-y", "1", "2", "3", "4",
  206. "-z", "one", "two", "three", "four",
  207. "-q", "0.1", "0.2", "0.3", "0.4"
  208. }, options);
  209. result.Should().BeTrue();
  210. base.ElementsShouldBeEqual(new int[] { 1, 2, 3, 4 }, options.IntegerArrayValue);
  211. base.ElementsShouldBeEqual(new string[] { "one", "two", "three", "four" }, options.StringArrayValue);
  212. base.ElementsShouldBeEqual(new double[] { .1, .2, .3, .4 }, options.DoubleArrayValue);
  213. options = new SimpleOptionsWithArray();
  214. parser = new Parser();
  215. result = parser.ParseArguments(new string[] {
  216. "-q", "0.1", "0.2", "0.3", "0.4",
  217. "-y", "1", "2", "3", "4",
  218. "-z", "one", "two", "three", "four"
  219. }, options);
  220. result.Should().BeTrue();
  221. base.ElementsShouldBeEqual(new double[] { .1, .2, .3, .4 }, options.DoubleArrayValue);
  222. base.ElementsShouldBeEqual(new int[] { 1, 2, 3, 4 }, options.IntegerArrayValue);
  223. base.ElementsShouldBeEqual(new string[] { "one", "two", "three", "four" }, options.StringArrayValue);
  224. }
  225. [Fact]
  226. public void Parse_different_arrays_together__combination_two()
  227. {
  228. var options = new SimpleOptionsWithArray();
  229. var parser = new Parser();
  230. var result = parser.ParseArguments(new string[] {
  231. "-z", "one", "two", "three", "four",
  232. "-y", "1", "2", "3", "4",
  233. "-q", "0.1", "0.2", "0.3", "0.4",
  234. "--string=after"
  235. }, options);
  236. result.Should().BeTrue();
  237. base.ElementsShouldBeEqual(new string[] { "one", "two", "three", "four" }, options.StringArrayValue);
  238. base.ElementsShouldBeEqual(new int[] { 1, 2, 3, 4 }, options.IntegerArrayValue);
  239. base.ElementsShouldBeEqual(new double[] { .1, .2, .3, .4 }, options.DoubleArrayValue);
  240. options.StringValue.Should().Be("after");
  241. options = new SimpleOptionsWithArray();
  242. parser = new Parser();
  243. result = parser.ParseArguments(new string[] {
  244. "--string", "before",
  245. "-y", "1", "2", "3", "4",
  246. "-z", "one", "two", "three", "four",
  247. "-q", "0.1", "0.2", "0.3", "0.4"
  248. }, options);
  249. result.Should().BeTrue();
  250. options.StringValue.Should().Be("before");
  251. base.ElementsShouldBeEqual(new int[] { 1, 2, 3, 4 }, options.IntegerArrayValue);
  252. base.ElementsShouldBeEqual(new string[] { "one", "two", "three", "four" }, options.StringArrayValue);
  253. base.ElementsShouldBeEqual(new double[] { .1, .2, .3, .4 }, options.DoubleArrayValue);
  254. options = new SimpleOptionsWithArray();
  255. parser = new Parser();
  256. result = parser.ParseArguments(new string[] {
  257. "-q", "0.1", "0.2", "0.3", "0.4",
  258. "-y", "1", "2", "3", "4",
  259. "-s", "near-the-center",
  260. "-z", "one", "two", "three", "four"
  261. }, options);
  262. result.Should().BeTrue();
  263. base.ElementsShouldBeEqual(new double[] { .1, .2, .3, .4 }, options.DoubleArrayValue);
  264. base.ElementsShouldBeEqual(new int[] { 1, 2, 3, 4 }, options.IntegerArrayValue);
  265. options.StringValue.Should().Be("near-the-center");
  266. base.ElementsShouldBeEqual(new string[] { "one", "two", "three", "four" }, options.StringArrayValue);
  267. options = new SimpleOptionsWithArray();
  268. parser = new Parser();
  269. result = parser.ParseArguments(new string[] {
  270. "--switch",
  271. "-z", "one", "two", "three", "four",
  272. "-y", "1", "2", "3", "4",
  273. "-i", "1234",
  274. "-q", "0.1", "0.2", "0.3", "0.4",
  275. "--string", "I'm really playing with the parser!"
  276. }, options);
  277. result.Should().BeTrue();
  278. options.BooleanValue.Should().BeTrue();
  279. base.ElementsShouldBeEqual(new string[] { "one", "two", "three", "four" }, options.StringArrayValue);
  280. base.ElementsShouldBeEqual(new int[] { 1, 2, 3, 4 }, options.IntegerArrayValue);
  281. options.IntegerValue.Should().Be(1234);
  282. base.ElementsShouldBeEqual(new double[] { .1, .2, .3, .4 }, options.DoubleArrayValue);
  283. options.StringValue.Should().Be("I'm really playing with the parser!");
  284. }
  285. /****************************************************************************************************/
  286. [Fact]
  287. public void Will_throw_exception_if_option_array_attribute_bound_to_string_with_short_name()
  288. {
  289. Assert.Throws<ParserException>(
  290. () => new Parser().ParseArguments(new string[] { "-v", "a", "b", "c" }, new SimpleOptionsWithBadOptionArray()));
  291. }
  292. [Fact]
  293. public void Will_throw_exception_if_option_array_attribute_bound_to_string_with_long_name()
  294. {
  295. Assert.Throws<ParserException>(
  296. () => new Parser().ParseArguments(new string[] { "--bstrarr", "a", "b", "c" }, new SimpleOptionsWithBadOptionArray()));
  297. }
  298. [Fact]
  299. public void Will_throw_exception_if_option_array_attribute_bound_to_integer_with_short_name()
  300. {
  301. Assert.Throws<ParserException>(
  302. () => new Parser().ParseArguments(new string[] { "-w", "1", "2", "3" }, new SimpleOptionsWithBadOptionArray()));
  303. }
  304. [Fact]
  305. public void Will_throw_exception_if_option_array_attribute_bound_to_integer_with_long_name()
  306. {
  307. Assert.Throws<ParserException>(
  308. () => new Parser().ParseArguments(new string[] { "--bintarr", "1", "2", "3" }, new SimpleOptionsWithBadOptionArray()));
  309. }
  310. [Fact]
  311. public void Parse_culture_specific_number()
  312. {
  313. var actualCulture = Thread.CurrentThread.CurrentCulture;
  314. Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
  315. var options = new SimpleOptionsWithArray();
  316. var parser = new Parser();
  317. var result = parser.ParseArguments(new string[] { "-q", "1,2", "1,23", "1,234" }, options);
  318. result.Should().BeTrue();
  319. base.ElementsShouldBeEqual(new double[] { 1.2, 1.23, 1.234 }, options.DoubleArrayValue);
  320. Thread.CurrentThread.CurrentCulture = actualCulture;
  321. }
  322. /****************************************************************************************************/
  323. [Fact]
  324. public void Parse_two_uint_consecutive_array()
  325. {
  326. var options = new OptionsWithUIntArray();
  327. var parser = new Parser();
  328. var result = Parser.Default.ParseArguments(new string[] {
  329. "--somestr", "just a string",
  330. "--arrayone", "10", "20", "30", "40",
  331. "--arraytwo", "11", "22", "33", "44",
  332. "--somebool"
  333. }, options);
  334. result.Should().BeTrue();
  335. options.SomeStringValue.Should().Be("just a string");
  336. base.ElementsShouldBeEqual(new uint[] {10, 20, 30, 40}, options.ArrayOne);
  337. base.ElementsShouldBeEqual(new uint[] {11, 22, 33, 44}, options.ArrayTwo);
  338. options.SomeBooleanValue.Should().BeTrue();
  339. }
  340. [Fact]
  341. public void Parse_two_uint_consecutive_array_using_short_names()
  342. {
  343. var options = new OptionsWithUIntArray();
  344. var result = Parser.Default.ParseArguments(new string[] {
  345. "-s", "just a string",
  346. "-o", "10", "20", "30", "40",
  347. "-t", "11", "22", "33", "44",
  348. "-b"
  349. }, options);
  350. result.Should().BeTrue();
  351. options.SomeStringValue.Should().Be("just a string");
  352. base.ElementsShouldBeEqual(new uint[] {10, 20, 30, 40}, options.ArrayOne);
  353. base.ElementsShouldBeEqual(new uint[] {11, 22, 33, 44}, options.ArrayTwo);
  354. options.SomeBooleanValue.Should().BeTrue();
  355. }
  356. }
  357. }