-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add unit tests for Hosts ValidationHelper and ColorPicker format conversions #46679
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
Open
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/increase-test-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+640
−0
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cbc845a
Add unit tests for Hosts ValidationHelper and ColorPicker format conv…
Copilot 8d2f5fb
Fix code review feedback: use dynamic MaxHostsCount, add missing usin…
Copilot 65296b3
Add SRGBTo to spell-check allow list
Copilot fe081df
Fix CI failures: remove SA1512 section comments, use char overload fo…
Copilot c39d000
Fix SA1515: add blank line before single-line comment in ConvertToOkl…
Copilot a0e8a20
Fix swapped Decimal expected values for Red and Blue in GetStringRepr…
Copilot db73018
Apply code review fixes: rename test methods for accuracy, use dynami…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ OLIVEGREEN | |
| PALEBLUE | ||
| PArgb | ||
| Pbgra | ||
| SRGBTo | ||
| WHITEONBLACK | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| // Copyright (c) Microsoft Corporation | ||
| // The Microsoft Corporation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Linq; | ||
|
|
||
| using HostsUILib; | ||
| using HostsUILib.Helpers; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace Hosts.Tests | ||
| { | ||
| [TestClass] | ||
| public class ValidationHelperTest | ||
| { | ||
| [DataTestMethod] | ||
| [DataRow("0.0.0.0")] | ||
| [DataRow("127.0.0.1")] | ||
| [DataRow("192.168.1.1")] | ||
| [DataRow("255.255.255.255")] | ||
| [DataRow("10.0.0.1")] | ||
| [DataRow("172.16.0.1")] | ||
| [DataRow("1.2.3.4")] | ||
| [DataRow("01.01.01.01")] | ||
| [DataRow("0.0.0.1")] | ||
| public void ValidIPv4_ValidAddresses_ReturnsTrue(string address) | ||
| { | ||
| Assert.IsTrue(ValidationHelper.ValidIPv4(address)); | ||
| } | ||
|
|
||
| [DataTestMethod] | ||
| [DataRow("256.0.0.0")] | ||
| [DataRow("0.256.0.0")] | ||
| [DataRow("0.0.256.0")] | ||
| [DataRow("0.0.0.256")] | ||
| [DataRow("999.999.999.999")] | ||
| [DataRow("1.2.3")] | ||
| [DataRow("1.2.3.4.5")] | ||
| [DataRow("1.2.3.")] | ||
| [DataRow(".1.2.3")] | ||
| [DataRow("1..2.3")] | ||
| [DataRow("abc.def.ghi.jkl")] | ||
| [DataRow("192.168.1.1/24")] | ||
| [DataRow("192.168.1.1:80")] | ||
| [DataRow("192.168.1")] | ||
| [DataRow("-1.0.0.0")] | ||
| public void ValidIPv4_InvalidAddresses_ReturnsFalse(string address) | ||
| { | ||
| Assert.IsFalse(ValidationHelper.ValidIPv4(address)); | ||
| } | ||
|
|
||
| [DataTestMethod] | ||
| [DataRow(null)] | ||
| [DataRow("")] | ||
| [DataRow(" ")] | ||
| [DataRow("\t")] | ||
| [DataRow("\n")] | ||
| public void ValidIPv4_NullOrWhitespace_ReturnsFalse(string address) | ||
| { | ||
| Assert.IsFalse(ValidationHelper.ValidIPv4(address)); | ||
| } | ||
|
|
||
| [DataTestMethod] | ||
| [DataRow("::1")] | ||
| [DataRow("::")] | ||
| [DataRow("2001:0db8:85a3:0000:0000:8a2e:0370:7334")] | ||
| [DataRow("2001:db8:85a3:0:0:8a2e:370:7334")] | ||
| [DataRow("2001:db8:85a3::8a2e:370:7334")] | ||
| [DataRow("fe80::1")] | ||
| [DataRow("ff02::1")] | ||
| [DataRow("2001:db8::1")] | ||
| [DataRow("::ffff:192.168.1.1")] | ||
| [DataRow("fe80::1%eth0")] | ||
| public void ValidIPv6_ValidAddresses_ReturnsTrue(string address) | ||
| { | ||
| Assert.IsTrue(ValidationHelper.ValidIPv6(address)); | ||
| } | ||
|
|
||
| [DataTestMethod] | ||
| [DataRow("2001:db8:85a3:0:0:8a2e:370:7334:extra")] | ||
| [DataRow("gggg::1")] | ||
| [DataRow("12345::1")] | ||
| [DataRow("192.168.1.1")] | ||
| [DataRow("::ffff:999.999.999.999")] | ||
| [DataRow("hello")] | ||
| [DataRow("2001:db8:85a3::8a2e:370:7334:1234:5678")] | ||
| public void ValidIPv6_InvalidAddresses_ReturnsFalse(string address) | ||
| { | ||
| Assert.IsFalse(ValidationHelper.ValidIPv6(address)); | ||
| } | ||
|
|
||
| [DataTestMethod] | ||
| [DataRow(null)] | ||
| [DataRow("")] | ||
| [DataRow(" ")] | ||
| [DataRow("\t")] | ||
| public void ValidIPv6_NullOrWhitespace_ReturnsFalse(string address) | ||
| { | ||
| Assert.IsFalse(ValidationHelper.ValidIPv6(address)); | ||
| } | ||
|
|
||
| [DataTestMethod] | ||
| [DataRow("localhost")] | ||
| [DataRow("example.com")] | ||
| [DataRow("sub.domain.example.com")] | ||
| [DataRow("my-host")] | ||
| [DataRow("host1 host2")] | ||
| [DataRow("host1 host2 host3")] | ||
| [DataRow("example.com www.example.com")] | ||
| public void ValidHosts_ValidHostnames_ReturnsTrue(string hosts) | ||
| { | ||
| Assert.IsTrue(ValidationHelper.ValidHosts(hosts, validateHostsLength: false)); | ||
| } | ||
|
|
||
| [DataTestMethod] | ||
| [DataRow(null)] | ||
| [DataRow("")] | ||
| [DataRow(" ")] | ||
| [DataRow("\t")] | ||
| public void ValidHosts_NullOrWhitespace_ReturnsFalse(string hosts) | ||
| { | ||
| Assert.IsFalse(ValidationHelper.ValidHosts(hosts, validateHostsLength: false)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidHosts_WithLengthValidation_ExceedsMaxCount_ReturnsFalse() | ||
| { | ||
| // Create a host string with one more than MaxHostsCount hosts | ||
| var hosts = string.Join(" ", Enumerable.Range(1, Consts.MaxHostsCount + 1).Select(i => $"h{i}")); | ||
| Assert.IsFalse(ValidationHelper.ValidHosts(hosts, validateHostsLength: true)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidHosts_WithLengthValidation_AtMaxCount_ReturnsTrue() | ||
| { | ||
| // Create a host string with exactly MaxHostsCount hosts | ||
| var hosts = string.Join(" ", Enumerable.Range(1, Consts.MaxHostsCount).Select(i => $"h{i}")); | ||
| Assert.IsTrue(ValidationHelper.ValidHosts(hosts, validateHostsLength: true)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidHosts_WithLengthValidation_BelowMaxCount_ReturnsTrue() | ||
| { | ||
| string hosts = "host1 host2 host3"; | ||
| Assert.IsTrue(ValidationHelper.ValidHosts(hosts, validateHostsLength: true)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidHosts_WithoutLengthValidation_ExceedsMaxCount_ReturnsTrue() | ||
| { | ||
| // When validateHostsLength is false, exceeding max count should still return true | ||
| string hosts = "h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12"; | ||
| Assert.IsTrue(ValidationHelper.ValidHosts(hosts, validateHostsLength: false)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidHosts_SingleHost_ReturnsTrue() | ||
| { | ||
| Assert.IsTrue(ValidationHelper.ValidHosts("localhost", validateHostsLength: true)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidHosts_InvalidHostname_ReturnsFalse() | ||
| { | ||
| Assert.IsFalse(ValidationHelper.ValidHosts("host_with!invalid@chars", validateHostsLength: false)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidHosts_HostWithSubdomains_ReturnsTrue() | ||
| { | ||
| Assert.IsTrue(ValidationHelper.ValidHosts("sub.domain.example.com", validateHostsLength: true)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ValidHosts_MultipleValidHosts_WithLengthValidation_ReturnsTrue() | ||
| { | ||
| Assert.IsTrue(ValidationHelper.ValidHosts("example.com www.example.com api.example.com", validateHostsLength: true)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.