PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tests/Attributes/HelpOptionAttributeFixture.cs

https://github.com/sflanker/commandline
C# | 117 lines | 77 code | 14 blank | 26 comment | 0 complexity | b2db7d0ae25d234adf67a2aff3a0e594 MD5 | raw file
Possible License(s): Apache-2.0
  1. #region License
  2. //
  3. // Command Line Library: HelpOptionAttributeFixture.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.IO;
  32. using CommandLine.Text;
  33. using Xunit;
  34. using FluentAssertions;
  35. #endregion
  36. namespace CommandLine.Tests
  37. {
  38. public class HelpOptionAttributeFixture : ParserBaseFixture
  39. {
  40. #region Mock Objects
  41. private class MockOptions
  42. {
  43. [Option('i', "input", Required = true, HelpText = "Input file with equations, xml format (see manual).")]
  44. public string InputFile { get; set; }
  45. [Option('o', "output", Required=false, HelpText="Output file with results, otherwise standard output.")]
  46. public string OutputFile {get;set;}
  47. [Option("paralell", Required=false, HelpText="Paralellize processing in multiple threads.")]
  48. public bool ParalellizeProcessing{get;set;}
  49. [Option('v', null, Required=false, HelpText="Show detailed processing messages.")]
  50. public bool Verbose{get;set;}
  51. [HelpOption(HelpText="Display this screen.")]
  52. public string GetUsage()
  53. {
  54. var help = new HelpText(new HeadingInfo("MyProgram", "1.0"));
  55. help.Copyright = new CopyrightInfo("Authors, Inc.", 2007);
  56. help.AddPreOptionsLine("This software is under the terms of the XYZ License");
  57. help.AddPreOptionsLine("(http://license-text.org/show.cgi?xyz).");
  58. help.AddPreOptionsLine("Usage: myprog --input equations-file.xml -o result-file.xml");
  59. help.AddPreOptionsLine(" myprog -i equations-file.xml --paralell");
  60. help.AddPreOptionsLine(" myprog -i equations-file.xml -vo result-file.xml");
  61. help.AddOptions(this);
  62. return help;
  63. }
  64. }
  65. #endregion
  66. [Fact]
  67. public void Correct_input_not_activates_help()
  68. {
  69. var options = new MockOptions();
  70. var writer = new StringWriter();
  71. var parser = new Parser();
  72. var result = parser.ParseArguments(
  73. new string[] { "-imath.xml", "-oresult.xml" }, options, writer);
  74. result.Should().BeTrue();;
  75. writer.ToString().Length.Should().Be(0);
  76. }
  77. [Fact]
  78. public void Bad_input_activates_help()
  79. {
  80. var options = new MockOptions();
  81. var writer = new StringWriter();
  82. var parser = new Parser();
  83. var result = parser.ParseArguments(
  84. new string[] { "math.xml", "-oresult.xml" }, options, writer);
  85. result.Should().BeFalse();
  86. string helpText = writer.ToString();
  87. (helpText.Length > 0).Should().BeTrue();
  88. Console.Write(helpText);
  89. }
  90. [Fact]
  91. public void Explicit_help_activation()
  92. {
  93. var options = new MockOptions();
  94. var writer = new StringWriter();
  95. var parser = new Parser();
  96. var result = parser.ParseArguments(
  97. new string[] { "--help" }, options, writer);
  98. result.Should().BeFalse();
  99. string helpText = writer.ToString();
  100. (helpText.Length > 0).Should().BeTrue();
  101. }
  102. }
  103. }