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

/src/tests/Core/OptionMapFixture.cs

https://github.com/sflanker/commandline
C# | 150 lines | 92 code | 20 blank | 38 comment | 4 complexity | 9eb45a9938781329481f21b1f8925eba MD5 | raw file
Possible License(s): Apache-2.0
  1. #region License
  2. //
  3. // Command Line Library: OptionMapFixture.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.Collections.Generic;
  31. using Xunit;
  32. using FluentAssertions;
  33. using CommandLine;
  34. using CommandLine.Core;
  35. #endregion
  36. namespace CommandLine.Tests
  37. {
  38. public class OptionMapFixture
  39. {
  40. #region Helper Nested Class
  41. class OptionMapBuilder
  42. {
  43. private readonly OptionMap _optionMap;
  44. private readonly List<OptionInfo> _options;
  45. private readonly List<string> _names;
  46. public OptionMapBuilder(int capacity)
  47. {
  48. _optionMap = new OptionMap(capacity, new ParserSettings(true));
  49. _options = new List<OptionInfo>(capacity);
  50. _names = new List<string>(capacity);
  51. }
  52. public void AppendOption(string longName)
  53. {
  54. var oa = new OptionAttribute(longName);
  55. var oi = oa.CreateOptionInfo();
  56. _optionMap[oa.UniqueName] = oi;
  57. _options.Add(oi);
  58. _names.Add(oa.UniqueName);
  59. }
  60. public void AppendOption(char shortName, string longName)
  61. {
  62. var oa = new OptionAttribute(shortName, longName);
  63. var oi = oa.CreateOptionInfo();
  64. _optionMap[oa.UniqueName] = oi;
  65. _options.Add(oi);
  66. _names.Add(oa.UniqueName);
  67. }
  68. public List<OptionInfo> Options
  69. {
  70. get { return _options; }
  71. }
  72. public List<string> Names
  73. {
  74. get { return _names; }
  75. }
  76. public OptionMap OptionMap
  77. {
  78. get { return _optionMap; }
  79. }
  80. }
  81. #endregion
  82. [Fact]
  83. public void Manage_options()
  84. {
  85. var omBuilder = new OptionMapBuilder(3);
  86. omBuilder.AppendOption('p', "pretend");
  87. omBuilder.AppendOption("newuse");
  88. omBuilder.AppendOption('D', null);
  89. var optionMap = omBuilder.OptionMap;
  90. omBuilder.Options[0].Should().BeSameAs(optionMap[omBuilder.Names[0]]);
  91. omBuilder.Options[1].Should().BeSameAs(optionMap[omBuilder.Names[1]]);
  92. omBuilder.Options[2].Should().BeSameAs(optionMap[omBuilder.Names[2]]);
  93. }
  94. //[Fact]
  95. //public void RetrieveNotExistentShortOption()
  96. //{
  97. // var shortOi = _optionMap["y"];
  98. // shortOi.Should().BeNull();
  99. //}
  100. //[Fact]
  101. //public void RetrieveNotExistentLongOption()
  102. //{
  103. // var longOi = _optionMap["nomorebugshere"];
  104. // longOi.Should().BeNull();
  105. //}
  106. private static OptionMap CreateMap (ref OptionMap map, IDictionary<string, OptionInfo> optionCache)
  107. {
  108. if (map == null)
  109. {
  110. map = new OptionMap (3, new ParserSettings (true));
  111. }
  112. var attribute1 = new OptionAttribute('p', "pretend");
  113. var attribute2 = new OptionAttribute("newuse");
  114. var attribute3 = new OptionAttribute('D', null);
  115. var option1 = attribute1.CreateOptionInfo();
  116. var option2 = attribute2.CreateOptionInfo();
  117. var option3 = attribute3.CreateOptionInfo();
  118. map[attribute1.UniqueName] = option1;
  119. map[attribute2.UniqueName] = option2;
  120. map[attribute3.UniqueName] = option3;
  121. if (optionCache != null)
  122. {
  123. optionCache[attribute1.UniqueName] = option1;
  124. optionCache[attribute1.UniqueName] = option2;
  125. optionCache[attribute2.UniqueName]= option3;
  126. }
  127. return map;
  128. }
  129. }
  130. }