PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tests/Parser/ParserFixture.cs

https://github.com/sflanker/commandline
C# | 520 lines | 412 code | 82 blank | 26 comment | 0 complexity | f4fc91511128a000e499566482be01e5 MD5 | raw file
Possible License(s): Apache-2.0
  1. #region License
  2. //
  3. // Command Line Library: ParserFixture.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.Globalization;
  32. using System.IO;
  33. using System.Threading;
  34. using CommandLine.Tests.Mocks;
  35. using Xunit;
  36. using FluentAssertions;
  37. #endregion
  38. namespace CommandLine.Tests
  39. {
  40. public class ParserFixture : ParserBaseFixture
  41. {
  42. [Fact]
  43. public void Will_throw_exception_if_arguments_array_is_null()
  44. {
  45. Assert.Throws<ArgumentNullException>(
  46. () => new Parser().ParseArguments(null, new SimpleOptions()));
  47. }
  48. [Fact]
  49. public void Will_throw_exception_if_options_instance_is_null()
  50. {
  51. Assert.Throws<ArgumentNullException>(
  52. () => new Parser().ParseArguments(new string[] {}, null));
  53. }
  54. [Fact]
  55. public void Parse_string_option()
  56. {
  57. var options = new SimpleOptions();
  58. var parser = new Parser();
  59. var result = parser.ParseArguments(new string[] { "-s", "something" }, options);
  60. result.Should().BeTrue();
  61. options.StringValue.Should().Be("something");
  62. Console.WriteLine(options);
  63. }
  64. [Fact]
  65. public void Parse_string_integer_bool_options()
  66. {
  67. var options = new SimpleOptions();
  68. var parser = new Parser();
  69. var result = parser.ParseArguments(
  70. new string[] { "-s", "another string", "-i100", "--switch" }, options);
  71. result.Should().BeTrue();
  72. options.StringValue.Should().Be("another string");
  73. options.IntegerValue.Should().Be(100);
  74. options.BooleanValue.Should().BeTrue();
  75. Console.WriteLine(options);
  76. }
  77. [Fact]
  78. public void Parse_short_adjacent_options()
  79. {
  80. var options = new BooleanSetOptions();
  81. var parser = new Parser();
  82. var result = parser.ParseArguments(new string[] { "-ca", "-d65" }, options);
  83. result.Should().BeTrue();
  84. options.BooleanThree.Should().BeTrue();
  85. options.BooleanOne.Should().BeTrue();
  86. options.BooleanTwo.Should().BeFalse();
  87. options.NonBooleanValue.Should().Be(65D);
  88. Console.WriteLine(options);
  89. }
  90. [Fact]
  91. public void Parse_short_long_options()
  92. {
  93. var options = new BooleanSetOptions();
  94. var parser = new Parser();
  95. var result = parser.ParseArguments(new string[] { "-b", "--double=9" }, options);
  96. result.Should().BeTrue();
  97. options.BooleanTwo.Should().BeTrue();
  98. options.BooleanOne.Should().BeFalse();
  99. options.BooleanThree.Should().BeFalse();
  100. options.NonBooleanValue.Should().Be(9D);
  101. Console.WriteLine(options);
  102. }
  103. [Fact]
  104. public void Parse_option_list()
  105. {
  106. var options = new SimpleOptionsWithOptionList();
  107. var parser = new Parser();
  108. var result = parser.ParseArguments(new string[] {
  109. "-k", "string1:stringTwo:stringIII", "-s", "test-file.txt" }, options);
  110. result.Should().BeTrue();
  111. options.SearchKeywords[0].Should().Be("string1");
  112. Console.WriteLine(options.SearchKeywords[0]);
  113. options.SearchKeywords[1].Should().Be("stringTwo");
  114. Console.WriteLine(options.SearchKeywords[1]);
  115. options.SearchKeywords[2].Should().Be("stringIII");
  116. Console.WriteLine(options.SearchKeywords[2]);
  117. options.StringValue.Should().Be("test-file.txt");
  118. Console.WriteLine(options.StringValue);
  119. }
  120. #region #BUG0000
  121. [Fact]
  122. public void Short_option_refuses_equal_token()
  123. {
  124. var options = new SimpleOptions();
  125. var parser = new Parser();
  126. var result = parser.ParseArguments(new string[] { "-i=10" }, options);
  127. result.Should().BeFalse();
  128. Console.WriteLine(options);
  129. }
  130. #endregion
  131. [Fact]
  132. public void Parse_enum_options()
  133. {
  134. var options = new SimpleOptionsWithEnum();
  135. var parser = new Parser();
  136. var result = parser.ParseArguments(new string[] { "-s", "data.bin", "-a", "ReadWrite" }, options);
  137. result.Should().BeTrue();
  138. options.StringValue.Should().Be("data.bin");
  139. options.FileAccess.Should().Be(FileAccess.ReadWrite);
  140. Console.WriteLine(options);
  141. }
  142. [Fact]
  143. public void Parse_culture_specific_number()
  144. {
  145. var actualCulture = Thread.CurrentThread.CurrentCulture;
  146. Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
  147. var options = new NumberSetOptions();
  148. var parser = new Parser();
  149. var result = parser.ParseArguments(new string[] { "-d", "10,986" }, options);
  150. result.Should().BeTrue();
  151. options.DoubleValue.Should().Be(10.986D);
  152. Thread.CurrentThread.CurrentCulture = actualCulture;
  153. }
  154. [Fact]
  155. public void Parse_culture_specific_nullable_number()
  156. {
  157. var actualCulture = Thread.CurrentThread.CurrentCulture;
  158. Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
  159. var options = new NumberSetOptions();
  160. var parser = new Parser();
  161. var result = parser.ParseArguments(new string[] { "--n-double", "12,32982" }, options);
  162. result.Should().BeTrue();
  163. options.NullableDoubleValue.Should().Be(12.32982D);
  164. Thread.CurrentThread.CurrentCulture = actualCulture;
  165. }
  166. [Fact]
  167. public void Parse_options_with_defaults()
  168. {
  169. var options = new SimpleOptionsWithDefaults();
  170. var parser = new Parser();
  171. var result = parser.ParseArguments(new string[] {}, options);
  172. result.Should().BeTrue();
  173. options.StringValue.Should().Be("str");
  174. options.IntegerValue.Should().Be(9);
  175. options.BooleanValue.Should().BeTrue();
  176. }
  177. [Fact]
  178. public void Parse_options_with_default_array()
  179. {
  180. var options = new SimpleOptionsWithDefaultArray();
  181. var parser = new Parser();
  182. var result = parser.ParseArguments(new [] { "-y", "4", "5", "6" }, options);
  183. result.Should().BeTrue();
  184. options.StringArrayValue.Should().Equal(new [] { "a", "b", "c" });
  185. options.IntegerArrayValue.Should().Equal(new [] { 4, 5, 6 });
  186. options.DoubleArrayValue.Should().Equal(new [] { 1.1, 2.2, 3.3 });
  187. }
  188. [Fact]
  189. public void Parse_options_with_bad_defaults()
  190. {
  191. var options = new SimpleOptionsWithBadDefaults();
  192. Assert.Throws<ParserException>(
  193. () => new Parser().ParseArguments(new string[] {}, options));
  194. }
  195. #region #BUG0002
  196. [Fact]
  197. public void Parsing_non_existent_short_option_fails_without_throwing_an_exception()
  198. {
  199. var options = new SimpleOptions();
  200. var parser = new Parser();
  201. var result = parser.ParseArguments(new string[] { "-x" }, options);
  202. result.Should().BeFalse();
  203. }
  204. [Fact]
  205. public void Parsing_non_existent_long_option_fails_without_throwing_an_exception()
  206. {
  207. var options = new SimpleOptions();
  208. var parser = new Parser();
  209. var result = parser.ParseArguments(new string[] { "--extend" }, options);
  210. result.Should().BeFalse();
  211. }
  212. #endregion
  213. #region #REQ0000
  214. [Fact]
  215. public void Default_parsing_is_case_sensitive()
  216. {
  217. var parser = new Parser();
  218. var options = new MixedCaseOptions();
  219. var result = parser.ParseArguments(new string[] { "-a", "alfa", "--beta-OPTION", "beta" }, options);
  220. result.Should().BeTrue();
  221. options.AlfaValue.Should().Be("alfa");
  222. options.BetaValue.Should().Be("beta");
  223. }
  224. [Fact]
  225. public void Using_wrong_case_with_default_fails()
  226. {
  227. var parser = new Parser();
  228. var options = new MixedCaseOptions();
  229. var result = parser.ParseArguments(new string[] { "-A", "alfa", "--Beta-Option", "beta" }, options);
  230. result.Should().BeFalse();
  231. }
  232. [Fact]
  233. public void Disabling_case_sensitive()
  234. {
  235. var parser = new Parser(new ParserSettings(false)); //Ref.: #DGN0001
  236. var options = new MixedCaseOptions();
  237. var result = parser.ParseArguments(new string[] { "-A", "alfa", "--Beta-Option", "beta" }, options);
  238. result.Should().BeTrue();
  239. options.AlfaValue.Should().Be("alfa");
  240. options.BetaValue.Should().Be("beta");
  241. }
  242. #endregion
  243. #region #BUG0003
  244. [Fact]
  245. public void Passing_no_value_to_a_string_type_long_option_fails()
  246. {
  247. var options = new SimpleOptions();
  248. var parser = new Parser();
  249. var result = parser.ParseArguments(new string[] { "--string" }, options);
  250. result.Should().BeFalse();
  251. }
  252. [Fact]
  253. public void Passing_no_value_to_a_byte_type_long_option_fails()
  254. {
  255. var options = new NumberSetOptions();
  256. var parser = new Parser();
  257. var result = parser.ParseArguments(new string[] { "--byte" }, options);
  258. result.Should().BeFalse();
  259. }
  260. [Fact]
  261. public void Passing_no_value_to_a_short_type_long_option_fails()
  262. {
  263. var options = new NumberSetOptions();
  264. var parser = new Parser();
  265. var result = parser.ParseArguments(new string[] { "--short" }, options);
  266. result.Should().BeFalse();
  267. }
  268. [Fact]
  269. public void Passing_no_value_to_an_integer_type_long_option_fails()
  270. {
  271. var options = new NumberSetOptions();
  272. var parser = new Parser();
  273. var result = parser.ParseArguments(new string[] { "--int" }, options);
  274. result.Should().BeFalse();
  275. }
  276. [Fact]
  277. public void Passing_no_value_to_a_long_type_long_option_fails()
  278. {
  279. var options = new NumberSetOptions();
  280. var parser = new Parser();
  281. var result = parser.ParseArguments(new string[] { "--long" }, options);
  282. result.Should().BeFalse();
  283. }
  284. [Fact]
  285. public void Passing_no_value_to_a_float_type_long_option_fails()
  286. {
  287. var options = new NumberSetOptions();
  288. var parser = new Parser();
  289. var result = parser.ParseArguments(new string[] { "--float" }, options);
  290. result.Should().BeFalse();
  291. }
  292. [Fact]
  293. public void Passing_no_value_to_a_double_type_long_option_fails()
  294. {
  295. var options = new NumberSetOptions();
  296. var parser = new Parser();
  297. var result = parser.ParseArguments(new string[] { "--double" }, options);
  298. result.Should().BeFalse();
  299. }
  300. #endregion
  301. #region #REQ0001
  302. [Fact]
  303. public void Allow_single_dash_as_option_input_value()
  304. {
  305. var options = new SimpleOptions();
  306. var parser = new Parser();
  307. var result = parser.ParseArguments(new string[] { "--string", "-" }, options);
  308. result.Should().BeTrue();
  309. options.StringValue.Should().Be("-");
  310. }
  311. [Fact]
  312. public void Allow_single_dash_as_non_option_value()
  313. {
  314. var options = new SimpleOptionsWithValueList();
  315. var parser = new Parser();
  316. var result = parser.ParseArguments(new string[] { "-sparser.xml", "-", "--switch" }, options);
  317. result.Should().BeTrue();
  318. options.StringValue.Should().Be("parser.xml");
  319. options.BooleanValue.Should().BeTrue();
  320. options.Items.Count.Should().Be(1);
  321. options.Items[0].Should().Be("-");
  322. }
  323. #endregion
  324. #region #BUG0004
  325. [Fact]
  326. public void Parse_negative_integer_value()
  327. {
  328. var options = new SimpleOptions();
  329. var parser = new Parser();
  330. var result = parser.ParseArguments(new string[] { "-i", "-4096" }, options);
  331. result.Should().BeTrue();
  332. options.IntegerValue.Should().Be(-4096);
  333. }
  334. public void ParseNegativeIntegerValue_InputStyle2()
  335. {
  336. var options = new NumberSetOptions();
  337. var parser = new Parser();
  338. var result = parser.ParseArguments(new string[] { "-i-4096" }, options);
  339. result.Should().BeTrue();
  340. options.IntegerValue.Should().Be(-4096);
  341. }
  342. public void ParseNegativeIntegerValue_InputStyle3()
  343. {
  344. var options = new NumberSetOptions();
  345. var parser = new Parser();
  346. var result = parser.ParseArguments(new string[] { "--int", "-4096" }, options);
  347. result.Should().BeTrue();
  348. options.IntegerValue.Should().Be(-4096);
  349. }
  350. public void ParseNegativeIntegerValue_InputStyle4()
  351. {
  352. var options = new NumberSetOptions();
  353. var parser = new Parser();
  354. var result = parser.ParseArguments(new string[] { "--int=-4096" }, options);
  355. result.Should().BeTrue();
  356. options.IntegerValue.Should().Be(-4096);
  357. }
  358. [Fact]
  359. public void Parse_negative_floating_point_value()
  360. {
  361. var options = new NumberSetOptions();
  362. var parser = new Parser();
  363. var result = parser.ParseArguments(new string[] { "-d", "-4096.1024" }, options);
  364. result.Should().BeTrue();
  365. options.DoubleValue.Should().Be(-4096.1024D);
  366. }
  367. [Fact]
  368. public void Parse_negative_floating_point_value_input_style2()
  369. {
  370. var options = new NumberSetOptions();
  371. var parser = new Parser();
  372. var result = parser.ParseArguments(new string[] { "-d-4096.1024" }, options);
  373. result.Should().BeTrue();
  374. options.DoubleValue.Should().Be(-4096.1024D);
  375. }
  376. [Fact]
  377. public void Parse_negative_floating_point_value_input_style3()
  378. {
  379. var options = new NumberSetOptions();
  380. var parser = new Parser();
  381. var result = parser.ParseArguments(new string[] { "--double", "-4096.1024" }, options);
  382. result.Should().BeTrue();
  383. options.DoubleValue.Should().Be(-4096.1024D);
  384. }
  385. [Fact]
  386. public void Parse_negative_floating_point_value_input_style4()
  387. {
  388. var options = new NumberSetOptions();
  389. var parser = new Parser();
  390. var result = parser.ParseArguments(new string[] { "--double=-4096.1024" }, options);
  391. result.Should().BeTrue();
  392. options.DoubleValue.Should().Be(-4096.1024D);
  393. }
  394. #endregion
  395. #region #BUG0005
  396. [Fact]
  397. public void Passing_short_value_to_byte_option_must_fail_gracefully()
  398. {
  399. var options = new NumberSetOptions();
  400. var parser = new Parser();
  401. var result = parser.ParseArguments(new string[] { "-b", short.MaxValue.ToString(CultureInfo.InvariantCulture) }, options);
  402. result.Should().BeFalse();
  403. }
  404. [Fact]
  405. public void Passing_integer_value_to_short_option_must_fail_gracefully()
  406. {
  407. var options = new NumberSetOptions();
  408. var parser = new Parser();
  409. var result = parser.ParseArguments(new string[] { "-s", int.MaxValue.ToString(CultureInfo.InvariantCulture) }, options);
  410. result.Should().BeFalse();
  411. }
  412. [Fact]
  413. public void Passing_long_value_to_integer_option_must_fail_gracefully()
  414. {
  415. var options = new NumberSetOptions();
  416. var parser = new Parser();
  417. var result = parser.ParseArguments(new string[] { "-i", long.MaxValue.ToString(CultureInfo.InvariantCulture) }, options);
  418. result.Should().BeFalse();
  419. }
  420. [Fact]
  421. public void Passing_float_value_to_long_option_must_fail_gracefully()
  422. {
  423. var options = new NumberSetOptions();
  424. var parser = new Parser();
  425. var result = parser.ParseArguments(new string[] { "-l", float.MaxValue.ToString(CultureInfo.InvariantCulture) }, options);
  426. result.Should().BeFalse();
  427. }
  428. [Fact]
  429. public void Passing_double_value_to_float_option_must_fail_gracefully()
  430. {
  431. var options = new NumberSetOptions();
  432. var parser = new Parser();
  433. var result = parser.ParseArguments(new string[] { "-f", double.MaxValue.ToString(CultureInfo.InvariantCulture) }, options);
  434. result.Should().BeFalse();
  435. }
  436. #endregion
  437. }
  438. }