PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/CommandLine.Tests/Unit/Core/InstanceChooserTests.cs

https://github.com/the-vk/commandline
C# | 110 lines | 73 code | 16 blank | 21 comment | 0 complexity | b7bc8c6724ba3202debb08ab6f808e4c 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.Globalization;
  4. using System.Linq;
  5. using CommandLine.Core;
  6. using CommandLine.Tests.Fakes;
  7. using FluentAssertions;
  8. using Xunit;
  9. namespace CommandLine.Tests.Unit.Core
  10. {
  11. public class InstanceChooserTests
  12. {
  13. [Fact]
  14. public void Parse_empty_array_returns_NullInstance()
  15. {
  16. // Fixture setup
  17. var expectedErrors = new[] { new NoVerbSelectedError() };
  18. // Exercize system
  19. var result = InstanceChooser.Choose(
  20. new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) },
  21. new string[] { },
  22. StringComparer.Ordinal,
  23. CultureInfo.InvariantCulture);
  24. // Verify outcome
  25. Assert.IsType<NullInstance>(result.Value);
  26. Assert.True(expectedErrors.SequenceEqual(result.Errors));
  27. // Teardown
  28. }
  29. [Fact]
  30. public void Explicit_help_request_generates_HelpVerbRequestedError()
  31. {
  32. // Fixture setup
  33. var expectedErrors = new[] { new HelpVerbRequestedError(null, null, false) };
  34. // Exercize system
  35. var result = InstanceChooser.Choose(
  36. new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) },
  37. new[] { "help" },
  38. StringComparer.Ordinal,
  39. CultureInfo.InvariantCulture);
  40. // Verify outcome
  41. Assert.IsType<NullInstance>(result.Value);
  42. Assert.True(expectedErrors.SequenceEqual(result.Errors));
  43. // Teardown
  44. }
  45. [Fact]
  46. public void Explicit_help_request_for_a_valid_verb_generates_HelpVerbRequestedError_with_appropriate_data()
  47. {
  48. // Fixture setup
  49. var expectedErrors = new[] { new HelpVerbRequestedError("commit", typeof(CommitOptions), true) };
  50. // Exercize system
  51. var result = InstanceChooser.Choose(
  52. new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) },
  53. new[] { "help", "commit" },
  54. StringComparer.Ordinal,
  55. CultureInfo.InvariantCulture);
  56. // Verify outcome
  57. Assert.IsType<NullInstance>(result.Value);
  58. Assert.True(expectedErrors.SequenceEqual(result.Errors));
  59. // Teardown
  60. }
  61. [Fact]
  62. public void Explicit_help_request_for_an_invalid_verb_generates_HelpVerbRequestedError_with_Matched_set_to_false()
  63. {
  64. // Fixture setup
  65. var expectedErrors = new[] { new HelpVerbRequestedError(null, null, false) };
  66. // Exercize system
  67. var result = InstanceChooser.Choose(
  68. new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) },
  69. new[] { "help", "earthunderalienattack" },
  70. StringComparer.Ordinal,
  71. CultureInfo.InvariantCulture);
  72. // Verify outcome
  73. Assert.IsType<NullInstance>(result.Value);
  74. Assert.True(expectedErrors.SequenceEqual(result.Errors));
  75. // Teardown
  76. }
  77. [Fact]
  78. public void Parse_existing_verb_returns_verb_instance()
  79. {
  80. // Fixture setup
  81. var expected = new AddOptions { Patch = true, FileName = "dummy.bin"};
  82. // Exercize system
  83. var result = InstanceChooser.Choose(
  84. new[] { typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions) },
  85. new[] { "add", "--patch", "dummy.bin" },
  86. StringComparer.Ordinal,
  87. CultureInfo.InvariantCulture);
  88. // Verify outcome
  89. Assert.IsType<AddOptions>(result.Value);
  90. expected.ShouldHave().AllRuntimeProperties().EqualTo(result.Value);
  91. // Teardown
  92. }
  93. }
  94. }