Skip to content

Commit 8fd522a

Browse files
Add unit test for WebBrowserUriTypeConverter.cs file (#13523)
* Add unit test for WebBrowserUriTypeConverter.cs file * Update * Update WebBrowserUriTypeConverterTests.cs * Replease“” with string.Empty --------- Co-authored-by: Leaf Shi <132890443+LeafShi1@users.noreply.github.com>
1 parent 8f3ff3b commit 8fd522a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Globalization;
5+
6+
namespace System.Windows.Forms.Tests;
7+
8+
public class WebBrowserUriTypeConverterTests
9+
{
10+
private readonly WebBrowserUriTypeConverter _converter = new();
11+
12+
[Theory]
13+
[InlineData("ftp.microsoft.com", "http://ftp.microsoft.com/")]
14+
[InlineData(" www.example.com ", "http://www.example.com/")]
15+
[InlineData("HTTPS://secure.com", "https://secure.com/")]
16+
[InlineData("localhost", "http://localhost/")]
17+
[InlineData("sub.domain.com/path", "http://sub.domain.com/path")]
18+
public void WebBrowserUri_ConvertFrom_RelativeUri_PrependsHttp(string input, string expected)
19+
{
20+
Uri result = (Uri)_converter.ConvertFrom(null, CultureInfo.InvariantCulture, input)!;
21+
22+
result.Should().NotBeNull();
23+
result.Should().BeOfType<Uri>();
24+
result.Should().Be(expected);
25+
}
26+
27+
[Theory]
28+
[InlineData("http://valid.com")]
29+
[InlineData("https://secure.com")]
30+
[InlineData("file:///C:/path/to/file.txt")]
31+
public void WebBrowserUri_ConvertFrom_AbsoluteUri_ReturnsSame(string input)
32+
{
33+
Uri inputUri = new(input);
34+
Uri result = (Uri)_converter.ConvertFrom(null, CultureInfo.InvariantCulture, inputUri)!;
35+
36+
result.Should().Be(inputUri);
37+
}
38+
39+
[Fact]
40+
public void WebBrowserUri_ConvertFrom_EmptyString_ReturnsNull()
41+
{
42+
object? result = _converter.ConvertFrom(null, CultureInfo.InvariantCulture, string.Empty);
43+
44+
result.Should().BeNull();
45+
}
46+
47+
[Fact]
48+
public void WebBrowserUri_ConvertFrom_InvalidUri_ThrowsUriFormatException()
49+
{
50+
Action action = () => _converter.ConvertFrom(null, CultureInfo.InvariantCulture, "http://inv@lid uri");
51+
52+
action.Should().Throw<UriFormatException>();
53+
}
54+
55+
[Fact]
56+
public void WebBrowserUri_ConvertFrom_AlreadyUri_ReturnsSameUri()
57+
{
58+
Uri uri = new("http://www.microsoft.com/");
59+
Uri result = (Uri)_converter.ConvertFrom(null, CultureInfo.InvariantCulture, uri)!;
60+
61+
result.Should().Be(uri);
62+
}
63+
}

0 commit comments

Comments
 (0)