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

/src/CommandLine.Tests/Unit/Text/HelpTextTests.cs

https://github.com/the-vk/commandline
C# | 299 lines | 223 code | 34 blank | 42 comment | 1 complexity | 5a54c4c2d0dc6545cbdb053759f55e49 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;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using CommandLine.Infrastructure;
  6. using CommandLine.Tests.Fakes;
  7. using CommandLine.Text;
  8. using FluentAssertions;
  9. using Xunit;
  10. namespace CommandLine.Tests.Unit.Text
  11. {
  12. public class HelpTextTests
  13. {
  14. [Fact]
  15. public void Create_empty_instance()
  16. {
  17. Assert.Equal(string.Empty, new HelpText().ToString());
  18. }
  19. [Fact]
  20. public void Create_instance_without_options()
  21. {
  22. // Fixture setup
  23. // Exercize system
  24. var sut =
  25. new HelpText(new HeadingInfo("Unit-tests", "2.0"), new CopyrightInfo(true, "Author", 2005, 2013))
  26. .AddPreOptionsLine("pre-options line 1")
  27. .AddPreOptionsLine("pre-options line 2")
  28. .AddPostOptionsLine("post-options line 1")
  29. .AddPostOptionsLine("post-options line 2");
  30. // Verify outcome
  31. var lines = sut.ToString().ToNotEmptyLines();
  32. Assert.Equal("Unit-tests 2.0", lines[0]);
  33. Assert.Equal("Copyright (C) 2005 - 2013 Author", lines[1]);
  34. Assert.Equal("pre-options line 1", lines[2]);
  35. Assert.Equal("pre-options line 2", lines[3]);
  36. Assert.Equal("post-options line 1", lines[4]);
  37. Assert.Equal("post-options line 2", lines[5]);
  38. // Teardown
  39. }
  40. [Fact]
  41. public void Create_instance_with_options()
  42. {
  43. // Fixture setup
  44. // Exercize system
  45. var sut = new HelpText { AddDashesToOption = true }
  46. .AddPreOptionsLine("pre-options")
  47. .AddOptions(new FakeOptions())
  48. .AddPostOptionsLine("post-options");
  49. // Verify outcome
  50. var lines = sut.ToString().ToNotEmptyLines().TrimStringArray();
  51. Assert.Equal("pre-options", lines[0]);
  52. Assert.Equal("--stringvalue Define a string value here.", lines[1]);
  53. Assert.Equal("-i Define a int sequence here.", lines[2]);
  54. Assert.Equal("-x Define a boolean or switch value here.", lines[3]);
  55. Assert.Equal("--help Display this help screen.", lines[4]);
  56. Assert.Equal( "post-options", lines[5]);
  57. // Teardown
  58. }
  59. [Fact]
  60. public void When_defined_MetaValue_should_be_rendered()
  61. {
  62. // Fixture setup
  63. // Exercize system
  64. var sut = new HelpText("Meta Value.")
  65. .AddOptions(new FakeOptionsWithMetaValue());
  66. // Verify outcome
  67. var lines = sut.ToString().ToNotEmptyLines().TrimStringArray();
  68. Assert.Equal("i FILE, input-file=FILE Required. Specify input FILE to be processed.", lines[2]);
  69. // Teardown
  70. }
  71. [Fact]
  72. public void When_help_text_is_longer_than_width_it_will_wrap_around_as_if_in_a_column()
  73. {
  74. // Fixture setup
  75. // Exercize system
  76. var sut = new HelpText(new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131"));
  77. sut.MaximumDisplayWidth = 40;
  78. sut.AddOptions(new FakeOptionsWithLongDescription());
  79. // Verify outcome
  80. var lines = sut.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
  81. lines[2].Should().Be(" v, verbose This is the description"); //"The first line should have the arguments and the start of the Help Text.");
  82. //string formattingMessage = "Beyond the second line should be formatted as though it's in a column.";
  83. lines[3].Should().Be(" of the verbosity to ");
  84. lines[4].Should().Be(" test out the wrapping ");
  85. lines[5].Should().Be(" capabilities of the ");
  86. lines[6].Should().Be(" Help Text.");
  87. // Teardown
  88. }
  89. [Fact]
  90. public void Long_help_text_without_spaces()
  91. {
  92. // Fixture setup
  93. // Exercize system
  94. var sut = new HelpText(new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131"));
  95. sut.MaximumDisplayWidth = 40;
  96. sut.AddOptions(new FakeOptionsWithLongDescriptionAndNoSpaces());
  97. // Verify outcome
  98. var lines = sut.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
  99. lines[2].Should().Be(" v, verbose Before ");
  100. lines[3].Should().Be(" 012345678901234567890123");
  101. lines[4].Should().Be(" After");
  102. lines[5].Should().Be(" input-file Before ");
  103. lines[6].Should().Be(" 012345678901234567890123");
  104. lines[7].Should().Be(" 456789 After");
  105. // Teardown
  106. }
  107. [Fact]
  108. public void Long_pre_and_post_lines_without_spaces()
  109. {
  110. // Fixture setup
  111. // Exercize system
  112. var sut = new HelpText("Heading Info.");
  113. sut.MaximumDisplayWidth = 40;
  114. sut.AddPreOptionsLine("Before 0123456789012345678901234567890123456789012 After")
  115. .AddOptions(new FakeOptionsForHelp())
  116. .AddPostOptionsLine("Before 0123456789012345678901234567890123456789 After");
  117. // Verify outcome
  118. var lines = sut.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
  119. lines[1].Should().Be("Before ");
  120. lines[2].Should().Be("0123456789012345678901234567890123456789");
  121. lines[3].Should().Be("012 After");
  122. lines[lines.Length - 3].Should().Be("Before ");
  123. lines[lines.Length - 2].Should().Be("0123456789012345678901234567890123456789");
  124. lines[lines.Length - 1].Should().Be(" After");
  125. // Teardown
  126. }
  127. [Fact]
  128. public void Invoking_RenderParsingErrorsText_returns_appropriate_formatted_text()
  129. {
  130. // Fixture setup
  131. var fakeResult = new ParserResult<NullInstance>(
  132. ParserResultType.Options,
  133. new NullInstance(),
  134. new Error[]
  135. {
  136. new BadFormatTokenError("badtoken"),
  137. new MissingValueOptionError(new NameInfo("x", "switch")),
  138. new UnknownOptionError("unknown"),
  139. new MissingRequiredOptionError(new NameInfo("", "missing")),
  140. new MutuallyExclusiveSetError(new NameInfo("z", "")),
  141. new SequenceOutOfRangeError(new NameInfo("s", "sequence")),
  142. new NoVerbSelectedError(),
  143. new BadVerbSelectedError("badverb"),
  144. new HelpRequestedError(), // should be ignored
  145. new HelpVerbRequestedError(null, null, false), // should be ignored
  146. },
  147. Maybe.Nothing<IEnumerable<Type>>());
  148. Func<Error, string> fakeRenderer = err =>
  149. {
  150. switch (err.Tag)
  151. {
  152. case ErrorType.BadFormatTokenError:
  153. return "ERR " + ((BadFormatTokenError)err).Token;
  154. case ErrorType.MissingValueOptionError:
  155. return "ERR " + ((MissingValueOptionError)err).NameInfo.NameText;
  156. case ErrorType.UnknownOptionError:
  157. return "ERR " + ((UnknownOptionError)err).Token;
  158. case ErrorType.MissingRequiredOptionError:
  159. return "ERR " + ((MissingRequiredOptionError)err).NameInfo.NameText;
  160. case ErrorType.MutuallyExclusiveSetError:
  161. return "ERR " + ((MutuallyExclusiveSetError)err).NameInfo.NameText;
  162. case ErrorType.SequenceOutOfRangeError:
  163. return "ERR " + ((SequenceOutOfRangeError)err).NameInfo.NameText;
  164. case ErrorType.NoVerbSelectedError:
  165. return "ERR no-verb-selected";
  166. case ErrorType.BadVerbSelectedError:
  167. return "ERR " + ((BadVerbSelectedError)err).Token;
  168. default:
  169. throw new InvalidOperationException();
  170. }
  171. };
  172. // Exercize system
  173. var errorsText = HelpText.RenderParsingErrorsText(fakeResult, fakeRenderer, 2);
  174. // Verify outcome
  175. var lines = errorsText.ToNotEmptyLines();
  176. Assert.Equal(" ERR badtoken", lines[0]);
  177. Assert.Equal(" ERR x, switch", lines[1]);
  178. Assert.Equal(" ERR unknown", lines[2]);
  179. Assert.Equal(" ERR missing", lines[3]);
  180. Assert.Equal(" ERR z", lines[4]);
  181. Assert.Equal(" ERR s, sequence", lines[5]);
  182. Assert.Equal(" ERR no-verb-selected", lines[6]);
  183. Assert.Equal(" ERR badverb", lines[7]);
  184. // Teardown
  185. }
  186. [Fact]
  187. public void Invoke_AutoBuild_for_Options_returns_appropriate_formatted_text()
  188. {
  189. // Fixture setup
  190. var fakeResult = new ParserResult<FakeOptions>(
  191. ParserResultType.Options,
  192. new FakeOptions(),
  193. new Error[]
  194. {
  195. new BadFormatTokenError("badtoken"),
  196. new SequenceOutOfRangeError(new NameInfo("i", ""))
  197. },
  198. Maybe.Nothing<IEnumerable<Type>>());
  199. // Exercize system
  200. var helpText = HelpText.AutoBuild(fakeResult);
  201. // Verify outcome
  202. var lines = helpText.ToString().ToNotEmptyLines().TrimStringArray();
  203. Assert.True(lines[0].StartsWith("CommandLine", StringComparison.Ordinal));
  204. Assert.True(lines[1].StartsWith("Copyright (c)", StringComparison.Ordinal));
  205. Assert.Equal("ERROR(S):", lines[2]);
  206. Assert.Equal("Token 'badtoken' is not recognized.", lines[3]);
  207. Assert.Equal("A sequence option 'i' is defined with few items than required.", lines[4]);
  208. Assert.Equal("--stringvalue Define a string value here.", lines[5]);
  209. Assert.Equal("-i Define a int sequence here.", lines[6]);
  210. Assert.Equal("-x Define a boolean or switch value here.", lines[7]);
  211. Assert.Equal("--help Display this help screen.", lines[8]);
  212. // Teardown
  213. }
  214. [Fact]
  215. public void Invoke_AutoBuild_for_Verbs_with_specific_verb_returns_appropriate_formatted_text()
  216. {
  217. // Fixture setup
  218. var fakeResult = new ParserResult<object>(
  219. ParserResultType.Verbs,
  220. new NullInstance(),
  221. new Error[]
  222. {
  223. new HelpVerbRequestedError("commit", typeof(CommitOptions), true)
  224. },
  225. Maybe.Nothing<IEnumerable<Type>>());
  226. // Exercize system
  227. var helpText = HelpText.AutoBuild(fakeResult);
  228. // Verify outcome
  229. var lines = helpText.ToString().ToNotEmptyLines().TrimStringArray();
  230. Assert.True(lines[0].StartsWith("CommandLine", StringComparison.Ordinal));
  231. Assert.True(lines[1].StartsWith("Copyright (c)", StringComparison.Ordinal));
  232. Assert.Equal("-p, --patch Use the interactive patch selection interface to chose which", lines[2]);
  233. Assert.Equal("changes to commit.", lines[3]);
  234. Assert.Equal("--amend Used to amend the tip of the current branch.", lines[4]);
  235. Assert.Equal("--help Display this help screen.", lines[5]);
  236. // Teardown
  237. }
  238. [Fact]
  239. public void Invoke_AutoBuild_for_Verbs_with_unknown_verb_returns_appropriate_formatted_text()
  240. {
  241. // Fixture setup
  242. var verbTypes = Enumerable.Empty<Type>().Concat(
  243. new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) });
  244. var fakeResult = new ParserResult<object>(
  245. ParserResultType.Verbs,
  246. new NullInstance(),
  247. new Error[]
  248. {
  249. new HelpVerbRequestedError(null, null, false)
  250. },
  251. Maybe.Just(verbTypes));
  252. // Exercize system
  253. var helpText = HelpText.AutoBuild(fakeResult);
  254. // Verify outcome
  255. var lines = helpText.ToString().ToNotEmptyLines().TrimStringArray();
  256. Assert.True(lines[0].StartsWith("CommandLine", StringComparison.Ordinal));
  257. Assert.True(lines[1].StartsWith("Copyright (c)", StringComparison.Ordinal));
  258. Assert.Equal("add Add file contents to the index.", lines[2]);
  259. Assert.Equal("commit Record changes to the repository.", lines[3]);
  260. Assert.Equal("clone Clone a repository into a new directory.", lines[4]);
  261. Assert.Equal("help Display more information on a specific command.", lines[5]);
  262. // Teardown
  263. }
  264. }
  265. }