-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotNetConsoleBuilderTests.cs
More file actions
117 lines (100 loc) · 4.29 KB
/
DotNetConsoleBuilderTests.cs
File metadata and controls
117 lines (100 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
namespace Neolution.DotNet.Console.UnitTests
{
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Neolution.DotNet.Console.UnitTests.Fakes;
using Neolution.DotNet.Console.UnitTests.Stubs;
using Shouldly;
using Xunit;
/// <summary>
/// Command line arguments grammar tests
/// </summary>
public class DotNetConsoleBuilderTests
{
/// <summary>
/// The argument string for the internal check-deps command
/// </summary>
private const string CheckDependenciesArgumentString = DotNetConsoleDefaults.CheckDependenciesCommand;
/// <summary>
/// Given a mistyped verb, when a default verb is defined, then should throw on console building.
/// </summary>
[Fact]
public void GivenMistypedVerb_WhenDefaultVerbIsDefined_ThenShouldThrowOnBuilding()
{
// Arrange
const string args = "eho"; // mistyped verb
// Act
// Assert
Should.Throw(() => DotNetConsole.CreateBuilderWithReference(Assembly.GetAssembly(typeof(EchoCommand))!, args.Split(" ")), typeof(DotNetConsoleException));
}
/// <summary>
/// Given a valid argument, when default verb is defined, then should not throw on creating.
/// </summary>
/// <param name="args">The arguments.</param>
[Theory]
[InlineData("")]
[InlineData("echo")]
[InlineData("--silent")]
[InlineData("-s")]
public void GivenValidArgument_WhenDefaultVerbIsDefined_ThenShouldNotThrowOnCreating(string args)
{
// Arrange
var servicesAssembly = Assembly.GetAssembly(typeof(EchoCommand))!;
// Act
// Assert
Should.NotThrow(() => DotNetConsole.CreateBuilderWithReference(servicesAssembly, args.Split(" ")));
}
/// <summary>
/// Given a reserved argument name, when default verb is defined, then should not throw on creating.
/// </summary>
/// <param name="args">The arguments.</param>
[Theory]
[InlineData("help")]
[InlineData("version")]
[InlineData("--help")]
[InlineData("--version")]
[InlineData("help echo")]
public void GivenReservedArgumentName_WhenDefaultVerbIsDefined_ThenShouldNotThrowOnCreating(string args)
{
// Arrange
var servicesAssembly = Assembly.GetAssembly(typeof(EchoCommand))!;
// Act
// Assert
Should.NotThrow(() => DotNetConsole.CreateBuilderWithReference(servicesAssembly, args.Split(" ")));
}
/// <summary>
/// Given invalid arguments, when no default verb is defined, then should not throw on console creating.
/// </summary>
/// <param name="args">The arguments.</param>
[Theory]
[InlineData("")]
[InlineData("verb-that-does-not-exist")]
[InlineData("--option-that-does-not-exist")]
[InlineData("-o")]
public void GivenInvalidArguments_WhenNoDefaultVerbIsDefined_ThenShouldNotThrowOnCreating(string args)
{
// Arrange
var servicesAssembly = Assembly.GetAssembly(typeof(EchoCommand))!;
var verbTypes = new List<Type> { typeof(EchoOptions) }.ToArray();
// Act
// Assert
Should.NotThrow(() => DotNetConsole.CreateBuilderWithReference(servicesAssembly, verbTypes, args.Split(" ")));
}
/// <summary>
/// Given the check-deps builder, when registration is missing, then should throw on console building.
/// </summary>
[Fact]
public void GivenCheckDependenciesCommand_WhenRegistrationIsMissing_ThenShouldThrow()
{
// Arrange
var builder = DotNetConsole.CreateBuilderWithReference(Assembly.GetAssembly(typeof(DefaultCommand))!, [CheckDependenciesArgumentString]);
// Intentionally only registering the transient service and not the scoped and singleton services.
builder.Services.AddTransient<ITransientServiceStub, TransientServiceStub>();
// Act
// Assert
Should.Throw(() => builder.Build(), typeof(AggregateException));
}
}
}