/src/Tests/UnitTests/Parser/AnswerFileParsingTests.cs

https://github.com/reubeno/NClap · C# · 156 lines · 128 code · 28 blank · 0 comment · 0 complexity · 37bc600a9b7c1e43008a78ddcdca5700 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using FluentAssertions;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using NClap.Metadata;
  8. using NSubstitute;
  9. namespace NClap.Tests.Parser
  10. {
  11. [TestClass]
  12. public class AnswerFileParsingTests
  13. {
  14. #pragma warning disable 0649 // Field is never assigned to, and will always have its default value
  15. [ArgumentSet(
  16. Style = ArgumentSetStyle.WindowsCommandLine,
  17. AnswerFileArgumentPrefix = "@")]
  18. class Arguments
  19. {
  20. [NamedArgument(ArgumentFlags.AtMostOnce)]
  21. public int IntValue;
  22. [NamedArgument(ArgumentFlags.AtMostOnce)]
  23. public string StringValue;
  24. }
  25. [ArgumentSet(
  26. Style = ArgumentSetStyle.WindowsCommandLine,
  27. AnswerFileArgumentPrefix = "#!")]
  28. class AlternateSyntaxArguments
  29. {
  30. [NamedArgument(ArgumentFlags.AtMostOnce)]
  31. public string Value;
  32. }
  33. #pragma warning restore 0649
  34. [TestMethod]
  35. public void FileDoesNotExist()
  36. {
  37. var reader = CreateReaderThatThrows(new FileNotFoundException());
  38. var options = new CommandLineParserOptions { FileSystemReader = reader };
  39. TryParse(
  40. new[] { "@foo" },
  41. new Arguments(),
  42. options).Should().BeFalse();
  43. reader.Received().GetLines("foo");
  44. }
  45. [TestMethod]
  46. public void FileIsEmpty()
  47. {
  48. var args = new Arguments();
  49. var reader = CreateReaderThatReturns(Enumerable.Empty<string>());
  50. var options = new CommandLineParserOptions { FileSystemReader = reader };
  51. TryParse(
  52. new[] { "@foo" },
  53. args,
  54. options).Should().BeTrue();
  55. reader.Received().GetLines("foo");
  56. args.IntValue.Should().Be(0);
  57. args.StringValue.Should().BeNull();
  58. }
  59. [TestMethod]
  60. public void FileWithValidArgs()
  61. {
  62. var args = new Arguments();
  63. var reader = CreateReaderThatReturns(new[] { "/intvalue:17", "/stringvalue:a b" });
  64. var options = new CommandLineParserOptions { FileSystemReader = reader };
  65. TryParse(
  66. new[] { "@foo" },
  67. args,
  68. options).Should().BeTrue();
  69. reader.Received().GetLines("foo");
  70. args.IntValue.Should().Be(17);
  71. args.StringValue.Should().Be("a b");
  72. }
  73. [TestMethod]
  74. public void AlternateSyntax()
  75. {
  76. var args = new AlternateSyntaxArguments();
  77. var reader = CreateReaderThatReturns(new[] { "/value:abc" });
  78. var options = new CommandLineParserOptions { FileSystemReader = reader };
  79. TryParse(new[] { "@foo" }, args, options).Should().BeFalse();
  80. reader.DidNotReceive().GetLines("foo");
  81. TryParse(new[] { "#!foo" }, args, options).Should().BeTrue();
  82. reader.Received().GetLines("foo");
  83. args.Value.Should().Be("abc");
  84. }
  85. [TestMethod]
  86. public void FileWithComments()
  87. {
  88. var args = new Arguments();
  89. var reader = CreateReaderThatReturns(new[] { "# /intvalue:17", " #asd" });
  90. var options = new CommandLineParserOptions { FileSystemReader = reader };
  91. TryParse(
  92. new[] { "@foo" },
  93. args,
  94. options).Should().BeTrue();
  95. reader.Received().GetLines("foo");
  96. args.IntValue.Should().Be(0);
  97. args.StringValue.Should().BeNull();
  98. }
  99. [TestMethod]
  100. public void FileWithEmptyLine()
  101. {
  102. var args = new Arguments();
  103. var reader = CreateReaderThatReturns(new[] { string.Empty });
  104. var options = new CommandLineParserOptions { FileSystemReader = reader };
  105. TryParse(
  106. new[] { "@foo" },
  107. args,
  108. options).Should().BeTrue();
  109. reader.Received().GetLines("foo");
  110. args.IntValue.Should().Be(0);
  111. args.StringValue.Should().BeNull();
  112. }
  113. private static IFileSystemReader CreateReaderThatThrows(Exception exception)
  114. {
  115. var reader = Substitute.For<IFileSystemReader>();
  116. reader.GetLines(null).ReturnsForAnyArgs(path => { throw exception; });
  117. return reader;
  118. }
  119. private static IFileSystemReader CreateReaderThatReturns(IEnumerable<string> lines)
  120. {
  121. var reader = Substitute.For<IFileSystemReader>();
  122. reader.GetLines(null).ReturnsForAnyArgs(lines);
  123. return reader;
  124. }
  125. private static bool TryParse<T>(IEnumerable<string> args, T dest, CommandLineParserOptions options = null) where T : class =>
  126. CommandLineParser.TryParse(args, dest, options ?? new CommandLineParserOptions { DisplayUsageInfoOnError = false });
  127. }
  128. }