PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tests/Parser/StrictFixture.cs

https://github.com/sflanker/commandline
C# | 137 lines | 84 code | 19 blank | 34 comment | 4 complexity | 5f43d7621a054c557840b506e8459619 MD5 | raw file
Possible License(s): Apache-2.0
  1. #region License
  2. //
  3. // Command Line Library: StrictFixture.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 System.Linq;
  34. using System.Reflection;
  35. using System.Text;
  36. using CommandLine.Tests.Mocks;
  37. using CommandLine.Helpers;
  38. using Xunit;
  39. using FluentAssertions;
  40. #endregion
  41. namespace CommandLine.Tests
  42. {
  43. public class StrictFixture : ParserBaseFixture
  44. {
  45. [Fact]
  46. public void Parse_strict_bad_input_fails_and_exits()
  47. {
  48. var options = new SimpleOptions();
  49. var testWriter = new StringWriter();
  50. ReflectionUtil.AssemblyFromWhichToPullInformation = Assembly.GetExecutingAssembly();
  51. var parser = new Parser();
  52. var result = parser.ParseArgumentsStrict(new string[] {"--bad", "--input"}, options, testWriter);
  53. result.Should().BeFalse();
  54. var helpText = testWriter.ToString();
  55. Console.WriteLine(helpText);
  56. var lines = helpText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  57. // Did we really produced all help?
  58. lines.Should().HaveCount(n => n == 8);
  59. // Verify just significant output
  60. lines[5].Trim().Should().Be("-s, --string");
  61. lines[6].Trim().Should().Be("-i");
  62. lines[7].Trim().Should().Be("--switch");
  63. }
  64. [Fact]
  65. public void Parse_strict_bad_input_fails_and_exits_when_get_usage_is_defined()
  66. {
  67. var options = new SimpleOptionsForStrict();
  68. var testWriter = new StringWriter();
  69. ReflectionUtil.AssemblyFromWhichToPullInformation = Assembly.GetExecutingAssembly();
  70. var parser = new Parser();
  71. var result = parser.ParseArgumentsStrict(new string[] { "--bad", "--input" }, options, testWriter);
  72. result.Should().BeFalse();
  73. var helpText = testWriter.ToString();
  74. Console.WriteLine(helpText);
  75. var lines = helpText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  76. // Did we really called user help method?
  77. lines.Should().HaveCount(n => n == 1);
  78. // Verify just significant output
  79. lines[0].Trim().Should().Be("SimpleOptionsForStrict (user defined)");
  80. }
  81. [Fact]
  82. public void Parse_strict_bad_input_fails_and_exits_with_verbs()
  83. {
  84. var options = new OptionsWithVerbsNoHelp();
  85. var testWriter = new StringWriter();
  86. ReflectionUtil.AssemblyFromWhichToPullInformation = Assembly.GetExecutingAssembly();
  87. var parser = new Parser();
  88. var result = parser.ParseArgumentsStrict(new string[] { "bad", "input" }, options, testWriter);
  89. result.Should().BeFalse();
  90. var helpText = testWriter.ToString();
  91. Console.WriteLine(helpText);
  92. var lines = helpText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  93. // Did we really produced all help?
  94. lines.Should().HaveCount(n => n == 8);
  95. // Verify just significant output
  96. lines[5].Trim().Should().Be("add Add file contents to the index.");
  97. lines[6].Trim().Should().Be("commit Record changes to the repository.");
  98. lines[7].Trim().Should().Be("clone Clone a repository into a new directory.");
  99. }
  100. [Fact]
  101. public void Parse_strict_bad_input_fails_and_exits_with_verbs_when_get_usage_is_defined()
  102. {
  103. var options = new OptionsWithVerbs();
  104. var testWriter = new StringWriter();
  105. ReflectionUtil.AssemblyFromWhichToPullInformation = Assembly.GetExecutingAssembly();
  106. var parser = new Parser();
  107. var result = parser.ParseArgumentsStrict(new string[] { "bad", "input" }, options, testWriter);
  108. result.Should().BeFalse();
  109. var helpText = testWriter.ToString();
  110. Console.WriteLine(helpText);
  111. var lines = helpText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  112. // Did we really produced all help?
  113. lines.Should().HaveCount(n => n == 1);
  114. // Verify just significant output
  115. lines[0].Trim().Should().Be("verbs help index");
  116. }
  117. }
  118. }