Skip to content

Commit

Permalink
Add CommandLineParser tests
Browse files Browse the repository at this point in the history
Change WebViewOptions to handle default values by itself
  • Loading branch information
ZKRobi committed Mar 11, 2023
1 parent da2305c commit d45fed9
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 12 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}") = "ShellTests", "tests\ShellTests\ShellTests.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
8 changes: 4 additions & 4 deletions src/shell/dotnet/Shell/WebWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public WebWindow(WebWindowOptions options)
InitializeComponent();

// TODO: When no title is set from options, we should show the HTML document's title instead
Title = options.Title ?? WebWindowOptions.DefaultTitle;
Width = options.Width ?? WebWindowOptions.DefaultWidth;
Height = options.Height ?? WebWindowOptions.DefaultHeight;
Title = options.Title!;
Width = options.Width!.Value;
Height = options.Height!.Value;
TrySetIconUrl(options);

_ = InitializeAsync();
Expand Down Expand Up @@ -59,7 +59,7 @@ private void TrySetIconUrl(WebWindowOptions webWindowOptions)
return;

// TODO: What's the default URL if the app is running from a manifest? We should probably not allow relative urls in that case.
var appUrl = new Uri(webWindowOptions.Url ?? WebWindowOptions.DefaultUrl);
var appUrl = new Uri(webWindowOptions.Url!);

var iconUrl = webWindowOptions.IconUrl != null
? new Uri(webWindowOptions.IconUrl, UriKind.RelativeOrAbsolute)
Expand Down
36 changes: 28 additions & 8 deletions src/shell/dotnet/Shell/WebWindowOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@ namespace Shell
{
public sealed class WebWindowOptions
{
private double? _height = null;
[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; }

[Display(Description = $"Set the url for the web view. Default: {DefaultUrl}")]
public string? Url { get; set; }
public double? Height
{
get { return _height ?? DefaultHeight; }
set { _height = value; }
}

[Display(Name = "icon", Description = $"Set the icon url for the window.")]
public string? IconUrl { get; set; }

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

private string? _url;
[Display(Description = $"Set the url for the web view. Default: {DefaultUrl}")]
public string? Url
{
get { return _url ?? DefaultUrl; }
set { _url = value; }
}

private double? _width = null;
[Display(Description = $"Set the width of the window. Default: 800")]
public double? Width { get; set; }
public double? Width
{
get { return _width ?? DefaultWidth; }
set { _width = value; }
}

public const double DefaultHeight = 450;
public const string DefaultTitle = "Compose Web Container";
Expand Down
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
{
public 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);
}
}
}
Loading

0 comments on commit d45fed9

Please sign in to comment.