Skip to content

Commit

Permalink
Added tests for CommandLineParser
Browse files Browse the repository at this point in the history
  • Loading branch information
ZKRobi committed Mar 16, 2023
1 parent f438155 commit 0f7969a
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/shell/dotnet/Shell.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MorganStanley.ComposeUI.Mes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MorganStanley.ComposeUI.Messaging.Server", "..\..\messaging\dotnet\src\Server\MorganStanley.ComposeUI.Messaging.Server.csproj", "{F0DFAB55-14F5-4FF6-A12F-37B1745D4551}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{BEA71FE5-7EC8-4BEA-819B-C0DB33125D91}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shell.Tests", "tests\Shell.Tests\Shell.Tests.csproj", "{70491F36-A27B-4E8C-AFD5-B49480EC1343}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,13 +33,18 @@ Global
{F0DFAB55-14F5-4FF6-A12F-37B1745D4551}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0DFAB55-14F5-4FF6-A12F-37B1745D4551}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0DFAB55-14F5-4FF6-A12F-37B1745D4551}.Release|Any CPU.Build.0 = Release|Any CPU
{70491F36-A27B-4E8C-AFD5-B49480EC1343}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70491F36-A27B-4E8C-AFD5-B49480EC1343}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70491F36-A27B-4E8C-AFD5-B49480EC1343}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70491F36-A27B-4E8C-AFD5-B49480EC1343}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B2AA5676-2776-4BDA-95F8-BE09D7642393} = {E7A2C581-4BF4-47A5-8A11-59B2DEBADCA7}
{F0DFAB55-14F5-4FF6-A12F-37B1745D4551} = {E7A2C581-4BF4-47A5-8A11-59B2DEBADCA7}
{70491F36-A27B-4E8C-AFD5-B49480EC1343} = {BEA71FE5-7EC8-4BEA-819B-C0DB33125D91}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4C901E6C-4B9A-48C2-AB16-461040DC57B4}
Expand Down
4 changes: 2 additions & 2 deletions src/shell/dotnet/Shell/WebWindowOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public sealed class WebWindowOptions
{
[Display(Description = "Set the height of the window. Default: 450")]
public double? Height { get; set; }

[Display(Description = $"Set the title of the window. Default: {DefaultTitle}")]
public string? Title { get; set; }

Expand All @@ -24,4 +24,4 @@ public sealed class WebWindowOptions
public const string DefaultUrl = "about:blank";
public const double DefaultWidth = 800;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Shell.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShellTests
{
public class CommandLineParserAttributedPropertyTests
{
public class AttributedOptions
{
[Display(Name = "opt", Description = "Option with overwritten name")]
public string RenamedOption { get; set; }

[Display(Description = "Just a description")]
public string Option { get; set; }
}

[Fact]
public void TestParsingRenamedOption()
{
var testValue = Guid.NewGuid().ToString();
var options = CommandLineParser.Parse<AttributedOptions>(new[] { "--opt", testValue });
Assert.NotNull(options);
Assert.Equal(testValue.ToString(), options.RenamedOption);
}

[Fact]
public void TestParsingRenamedOptionWithOriginalName()
{
var testValue = Guid.NewGuid().ToString();
var options = CommandLineParser.Parse<AttributedOptions>(new[] { "--renamedOption", testValue });
Assert.NotNull(options);
Assert.Null(options.RenamedOption);
}

[Fact]
public void TestParsingOptionWithDescription()
{
var testValue = Guid.NewGuid().ToString();
var options = CommandLineParser.Parse<AttributedOptions>(new[] { "--option", testValue });
Assert.NotNull(options);
Assert.Equal(testValue.ToString(), options.Option);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Shell.Utilities;

namespace ShellTests
{
public class CommandLineParserCustomPropertyDoubleTests
{
private static readonly Random Random = new Random();
private static readonly double Default = Random.NextDouble();

/// <summary>
/// Get a test value that's different from the default value
/// </summary>
/// <returns></returns>
private static double GetTestValue()
{
var x = Random.NextDouble();
while (x == Default) { x = Random.NextDouble(); }
return x;
}

public class CustomPropertyDoubleOptions
{
private double _option = Default;
public double? Option
{
get { return _option; }
set { _option = value ?? Default; }
}
}

[Fact]
public void TestParseCustomPropertyDoubleWithEmptyParameterList()
{
var options = CommandLineParser.Parse<CustomPropertyDoubleOptions>(new string[0]);
Assert.NotNull(options);
Assert.Equal(Default, options.Option);
}

[Fact]
public void TestParseCustomPropertyDoubleWithValueProvided()
{
var testValue = GetTestValue();
var options = CommandLineParser.Parse<CustomPropertyDoubleOptions>(new[] { "--option", testValue.ToString() });
Assert.NotNull(options);
Assert.Equal(testValue, options.Option);
}

[Fact]
public void TestParseCustomPropertyDoubleWithoutValue()
{
Assert.Throws<InvalidOperationException>(() => CommandLineParser.Parse<CustomPropertyDoubleOptions>(new[] { "--option" }));
}

[Fact]
public void TestParseCustomPropertyDoubleWithOnlyDifferentParameter()
{
var options = CommandLineParser.Parse<CustomPropertyDoubleOptions>(new[] { "--stuff", GetTestValue().ToString() });
Assert.NotNull(options);
Assert.Equal(Default, options.Option);
}

[Fact]
public void TestParseCustomPropertyDoubleWithOtherParameters()
{
var testValue = GetTestValue();
var options = CommandLineParser.Parse<CustomPropertyDoubleOptions>(new[] { "--firstParam", GetTestValue().ToString(), "--option", testValue.ToString(), "--lastParam", GetTestValue().ToString() });
Assert.NotNull(options);
Assert.Equal(testValue, options.Option);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Shell.Utilities;

namespace ShellTests
{
public class CommandLineParserCustomPropertyStringTests
{
private const string Default = "default";
public class CustomPropertyStringOptions
{
private string? _option = null;
public string? Option
{
get { return _option ?? Default; }
set { _option = value; }
}
}

[Fact]
public void TestParseSimpleStringWithEmptyParameterList()
{
var options = CommandLineParser.Parse<CustomPropertyStringOptions>(new string[0]);
Assert.NotNull(options);
Assert.Equal(Default, options.Option);
}

[Fact]
public void TestParseSimpleStringWithValueProvided()
{
var testValue = Guid.NewGuid();
var options = CommandLineParser.Parse<CustomPropertyStringOptions>(new[] { "--option", testValue.ToString() });
Assert.NotNull(options);
Assert.Equal(testValue.ToString(), options.Option);
}

[Fact]
public void TestParseSimpleStringWithoutValue()
{
var testValue = Guid.NewGuid();
Assert.Throws<InvalidOperationException>(() => CommandLineParser.Parse<CustomPropertyStringOptions>(new[] { "--option" }));
}

[Fact]
public void TestParseSimpleStringWithOnlyDifferentParameter()
{
var options = CommandLineParser.Parse<CustomPropertyStringOptions>(new[] { "--stuff", "irrelevant" });
Assert.NotNull(options);
Assert.Equal(Default, options.Option);
}

[Fact]
public void TestParseSimpleStringWithOtherParameters()
{
var testValue = Guid.NewGuid();
var options = CommandLineParser.Parse<CustomPropertyStringOptions>(new[] { "--firstParam", "irrelevant", "--option", testValue.ToString(), "--lastParam", "irrelevant" });
Assert.NotNull(options);
Assert.Equal(testValue.ToString(), options.Option);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Shell.Utilities;
using System.Globalization;

namespace ShellTests
{
public class CommandLineParserSimpleDoubleTests
{
private static readonly Random Random = new Random();

public class SimpleDoubleOptions
{
public double? Option { get; set; }
}

[Fact]
public void TestParseSimpleDoubleWithEmptyParameterList()
{
var options = CommandLineParser.Parse<SimpleDoubleOptions>(new string[0]);
Assert.NotNull(options);
Assert.Null(options.Option);
}

[Fact]
public void TestParseSimpleDoubleWithValueProvided()
{
var testValue = Random.NextDouble();
var options = CommandLineParser.Parse<SimpleDoubleOptions>(new[] { "--option", testValue.ToString() });
Assert.NotNull(options);
Assert.Equal(testValue, options.Option);
}

[Fact]
public void TestParseSimpleDoubleWithoutValue()
{
Assert.Throws<InvalidOperationException>(() => CommandLineParser.Parse<SimpleDoubleOptions>(new[] { "--option" }));
}

[Fact]
public void TestParseSimpleDoubleWithOnlyDifferentParameter()
{
var options = CommandLineParser.Parse<SimpleDoubleOptions>(new[] { "--stuff", Random.NextDouble().ToString() });
Assert.NotNull(options);
Assert.Null(options.Option);
}

[Fact]
public void TestParseSimpleDoubleWithOtherParameters()
{
var testValue = Random.NextDouble();
var options = CommandLineParser.Parse<SimpleDoubleOptions>(new[] { "--firstParam", Random.NextDouble().ToString(), "--option", testValue.ToString(), "--lastParam", Random.NextDouble().ToString() });
Assert.NotNull(options);
Assert.Equal(testValue, options.Option);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Shell.Utilities;

namespace ShellTests
{
public class CommandLineParserSimpleStringTests
{
public class SimpleStringOptions
{
public string? Option { get; set; }
}

[Fact]
public void TestParseSimpleStringWithEmptyParameterList()
{
var options = CommandLineParser.Parse<SimpleStringOptions>(new string[0]);
Assert.NotNull(options);
Assert.Null(options.Option);
}

[Fact]
public void TestParseSimpleStringWithValueProvided()
{
var testValue = Guid.NewGuid();
var options = CommandLineParser.Parse<SimpleStringOptions>(new[] { "--option", testValue.ToString() });
Assert.NotNull(options);
Assert.Equal(testValue.ToString(), options.Option);
}

[Fact]
public void TestParseSimpleStringWithoutValue()
{
var testValue = Guid.NewGuid();
Assert.Throws<InvalidOperationException>(() => CommandLineParser.Parse<SimpleStringOptions>(new[] { "--option" }));
}

[Fact]
public void TestParseSimpleStringWithOnlyDifferentParameter()
{
var options = CommandLineParser.Parse<SimpleStringOptions>(new[] { "--stuff", "irrelevant" });
Assert.NotNull(options);
Assert.Null(options.Option);
}

[Fact]
public void TestParseSimpleStringWithOtherParameters()
{
var testValue = Guid.NewGuid();
var options = CommandLineParser.Parse<SimpleStringOptions>(new[] { "--firstParam", "irrelevant", "--option", testValue.ToString(), "--lastParam", "irrelevant" });
Assert.NotNull(options);
Assert.Equal(testValue.ToString(), options.Option);
}
}
}
28 changes: 28 additions & 0 deletions src/shell/dotnet/tests/Shell.Tests/Shell.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Shell\Shell.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/shell/dotnet/tests/Shell.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;

0 comments on commit 0f7969a

Please sign in to comment.