PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/CommandLine.Tests/Unit/ParserTests.cs

https://github.com/the-vk/commandline
C# | 171 lines | 113 code | 25 blank | 33 comment | 0 complexity | 9ece9207e9ca17f24ca2a083bdc15958 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.IO;
  3. using System.Linq;
  4. using CommandLine.Tests.Fakes;
  5. using FluentAssertions;
  6. using Xunit;
  7. namespace CommandLine.Tests.Unit
  8. {
  9. public class ParserTests
  10. {
  11. [Fact]
  12. public void When_HelpWriter_is_set_help_screen_is_generated()
  13. {
  14. // Fixture setup
  15. var writer = new StringWriter();
  16. var sut = new Parser(with => with.HelpWriter = writer);
  17. // Exercize system
  18. sut.ParseArguments<FakeOptionWithRequired>(new string[] { });
  19. // Verify outcome
  20. var text = writer.ToString();
  21. Assert.True(text.Length > 0);
  22. // Teardown
  23. }
  24. [Fact]
  25. public void When_HelpWriter_is_set_help_screen_is_generated_in_verbs_scenario()
  26. {
  27. // Fixture setup
  28. var writer = new StringWriter();
  29. var sut = new Parser(with => with.HelpWriter = writer);
  30. // Exercize system
  31. sut.ParseArguments(new string[] { }, typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions));
  32. // Verify outcome
  33. var text = writer.ToString();
  34. Assert.True(text.Length > 0);
  35. // Teardown
  36. }
  37. [Fact]
  38. public void When_HelpWriter_is_set_help_screen_is_generated_in_verbs_scenario_using_generic_overload()
  39. {
  40. // Fixture setup
  41. var writer = new StringWriter();
  42. var sut = new Parser(with => with.HelpWriter = writer);
  43. // Exercize system
  44. sut.ParseArguments<AddOptions, CommitOptions, CloneOptions>(new string[] { });
  45. // Verify outcome
  46. var text = writer.ToString();
  47. Assert.True(text.Length > 0);
  48. // Teardown
  49. }
  50. [Fact]
  51. public void Parse_options()
  52. {
  53. // Fixture setup
  54. var expectedOptions = new FakeOptions
  55. {
  56. StringValue = "strvalue", IntSequence = new[] { 1, 2, 3 }
  57. };
  58. var sut = new Parser();
  59. // Exercize system
  60. var result = sut.ParseArguments<FakeOptions>(new[] { "--stringvalue=strvalue", "-i1", "2", "3" });
  61. // Verify outcome
  62. result.Value.ShouldHave().AllProperties().EqualTo(expectedOptions);
  63. Assert.False(result.Errors.Any());
  64. // Teardown
  65. }
  66. [Fact]
  67. public void Parse_options_with_double_dash()
  68. {
  69. // Fixture setup
  70. var expectedOptions = new FakeOptionsWithValues
  71. {
  72. StringValue = "astring",
  73. LongValue = 20L,
  74. StringSequence = new[] { "--aaa", "-b", "--ccc" },
  75. IntValue = 30
  76. };
  77. var sut = new Parser(with => with.EnableDashDash = true);
  78. // Exercize system
  79. var result = sut.ParseArguments<FakeOptionsWithValues>(
  80. new[] { "--stringvalue", "astring", "--", "20", "--aaa", "-b", "--ccc", "30" });
  81. // Verify outcome
  82. result.Value.ShouldHave().AllProperties().EqualTo(expectedOptions);
  83. Assert.False(result.Errors.Any());
  84. // Teardown
  85. }
  86. [Fact]
  87. public void Parse_options_with_double_dash_in_verbs_scenario()
  88. {
  89. // Fixture setup
  90. var expectedOptions = new AddOptions
  91. {
  92. Patch = true,
  93. FileName = "--strange-fn"
  94. };
  95. var sut = new Parser(with => with.EnableDashDash = true);
  96. // Exercize system
  97. var result = sut.ParseArguments(
  98. new[] { "add", "-p", "--", "--strange-fn" },
  99. typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions));
  100. // Verify outcome
  101. Assert.IsType<AddOptions>(result.Value);
  102. result.Value.ShouldHave().AllRuntimeProperties().EqualTo(expectedOptions);
  103. Assert.False(result.Errors.Any());
  104. // Teardown
  105. }
  106. [Fact]
  107. public void Parse_verbs()
  108. {
  109. // Fixture setup
  110. var expectedOptions = new CloneOptions
  111. {
  112. Quiet = true,
  113. Urls = new[] { "http://gsscoder.github.com/", "http://yes-to-nooo.github.com/" }
  114. };
  115. var sut = new Parser();
  116. // Exercize system
  117. var result = sut.ParseArguments(
  118. new[] { "clone", "-q", "http://gsscoder.github.com/", "http://yes-to-nooo.github.com/" },
  119. typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions));
  120. // Verify outcome
  121. Assert.IsType<CloneOptions>(result.Value);
  122. result.Value.ShouldHave().AllRuntimeProperties().EqualTo(expectedOptions);
  123. Assert.False(result.Errors.Any());
  124. // Teardown
  125. }
  126. [Fact]
  127. public void Parse_verbs_using_generic_overload()
  128. {
  129. // Fixture setup
  130. var expectedOptions = new CloneOptions
  131. {
  132. Quiet = true,
  133. Urls = new[] { "http://gsscoder.github.com/", "http://yes-to-nooo.github.com/" }
  134. };
  135. var sut = new Parser();
  136. // Exercize system
  137. var result = sut.ParseArguments<AddOptions, CommitOptions, CloneOptions>(
  138. new[] { "clone", "-q", "http://gsscoder.github.com/", "http://yes-to-nooo.github.com/" });
  139. // Verify outcome
  140. Assert.IsType<CloneOptions>(result.Value);
  141. result.Value.ShouldHave().AllRuntimeProperties().EqualTo(expectedOptions);
  142. Assert.False(result.Errors.Any());
  143. // Teardown
  144. }
  145. }
  146. }