Skip to content

Add unit test for WebBrowserUriTypeConverter.cs file #13523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;

namespace System.Windows.Forms.Tests;

public class WebBrowserUriTypeConverterTests
{
private readonly WebBrowserUriTypeConverter _converter = new();

[Theory]
[InlineData("ftp.microsoft.com", "http://ftp.microsoft.com/")]
[InlineData(" www.example.com ", "http://www.example.com/")]
[InlineData("HTTPS://secure.com", "https://secure.com/")]
[InlineData("localhost", "http://localhost/")]
[InlineData("sub.domain.com/path", "http://sub.domain.com/path")]
public void WebBrowserUri_ConvertFrom_RelativeUri_PrependsHttp(string input, string expected)
{
Uri result = (Uri)_converter.ConvertFrom(null, CultureInfo.InvariantCulture, input)!;

result.Should().NotBeNull();
result.Should().BeOfType<Uri>();
result.Should().Be(expected);
}

[Theory]
[InlineData("http://valid.com")]
[InlineData("https://secure.com")]
[InlineData("file:///C:/path/to/file.txt")]
public void WebBrowserUri_ConvertFrom_AbsoluteUri_ReturnsSame(string input)
{
Uri inputUri = new(input);
Uri result = (Uri)_converter.ConvertFrom(null, CultureInfo.InvariantCulture, inputUri)!;

result.Should().Be(inputUri);
}

[Fact]
public void WebBrowserUri_ConvertFrom_EmptyString_ReturnsNull()
{
object? result = _converter.ConvertFrom(null, CultureInfo.InvariantCulture, "");

result.Should().BeNull();
}

[Fact]
public void WebBrowserUri_ConvertFrom_InvalidUri_ThrowsUriFormatException()
{
Action action = () => _converter.ConvertFrom(null, CultureInfo.InvariantCulture, "http://inv@lid uri");

action.Should().Throw<UriFormatException>();
}

[Fact]
public void WebBrowserUri_ConvertFrom_AlreadyUri_ReturnsSameUri()
{
Uri uri = new("http://www.microsoft.com/");
Uri result = (Uri)_converter.ConvertFrom(null, CultureInfo.InvariantCulture, uri)!;

result.Should().Be(uri);
}
}