Skip to content

Commit e00db53

Browse files
committed
Optimize Unit Tests
1 parent 9e5f676 commit e00db53

File tree

5 files changed

+26
-29
lines changed

5 files changed

+26
-29
lines changed

src/Nager.PublicSuffix.UnitTest/DomainNormalizerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class DomainNormalizerTest
1111
[DataRow("HTTPS://microsoft.com")]
1212
[DataRow("HTTPS://Microsoft.com")]
1313
[DataRow("HTTPS://MICROSOFT.COM")]
14-
[DataTestMethod]
14+
[TestMethod]
1515
public void UriDomainNormalizerTest(string domain)
1616
{
1717
var domainNormalizer = new UriDomainNormalizer();
@@ -26,7 +26,7 @@ public void UriDomainNormalizerTest(string domain)
2626

2727
[DataRow("xn--frisr-mua.com")]
2828
[DataRow("XN--frisr-mua.com")]
29-
[DataTestMethod]
29+
[TestMethod]
3030
public void IdnMappingDomainNormalizerTest(string domain)
3131
{
3232
var domainNormalizer = new IdnMappingDomainNormalizer();

src/Nager.PublicSuffix.UnitTest/NormalizationVariationTest.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ public void UnderscoreBasedVariationsValid()
5252
}
5353

5454
[TestMethod]
55-
[ExpectedException(typeof(ParseException))]
5655
public void UnderscoreBasedVariationsInvalid()
5756
{
5857
// These domains are treated as invalid and produce null via the Uri normalization method.
5958
// def._ghi.jkl.com is valid but
6059
// abc.def._ghi.jkl.com is invalid.
6160
// It can't be correct that adding abc. in front of a valid domain makes it invalid
62-
this.PerformParsingCheck("abc.def._ghi.jkl.com", null, "jkl.com");
61+
Assert.ThrowsExactly<ParseException>(() => this.PerformParsingCheck("abc.def._ghi.jkl.com", null, "jkl.com"));
6362
}
6463

6564
[TestMethod]
@@ -96,17 +95,16 @@ public void DashBasedVariationsValid()
9695
}
9796

9897
[TestMethod]
99-
[ExpectedException(typeof(ParseException))]
10098
public void DashBasedVariationsInvalid()
10199
{
102100
// Adding sub domains to these examples demonstrates how the Uri validation behaves in an unexpected way.
103101
// For example "sub.-example.com" is valid but "double.sub.-example.com" is not.
104102

105-
this.PerformParsingCheck("sub.example.-com", null, "example.-com");
106-
this.PerformParsingCheck("sub.example.-com-", null, "example.-com-");
107-
this.PerformParsingCheck("double.sub.-example.com", null, "-example.com");
108-
this.PerformParsingCheck("double.sub.example.-com", null, "example.-com");
109-
this.PerformParsingCheck("double.sub.example.-com-", null, "example.-com-");
103+
Assert.ThrowsExactly<ParseException>(() => this.PerformParsingCheck("sub.example.-com", null, "example.-com"));
104+
Assert.ThrowsExactly<ParseException>(() => this.PerformParsingCheck("sub.example.-com-", null, "example.-com-"));
105+
Assert.ThrowsExactly<ParseException>(() => this.PerformParsingCheck("double.sub.-example.com", null, "-example.com"));
106+
Assert.ThrowsExactly<ParseException>(() => this.PerformParsingCheck("double.sub.example.-com", null, "example.-com"));
107+
Assert.ThrowsExactly<ParseException>(() => this.PerformParsingCheck("double.sub.example.-com-", null, "example.-com-"));
110108
}
111109

112110
private void PerformParsingCheck(string domain, string expectedRegistrableDomain_UriVersion, string expectedRegistrableDomain_IdnVersion)

src/Nager.PublicSuffix.UnitTest/RealRules/PublicSuffixTest.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,25 @@ protected void CheckPublicSuffix(string domain, string expected)
2525
}
2626
}
2727

28-
[DataTestMethod]
2928
[DataRow(null)]
29+
[TestMethod]
3030
public void ParseNullDomainCheck(string domain)
3131
{
3232
this.CheckPublicSuffix(domain, null);
3333
}
3434

35-
[DataTestMethod]
3635
[DataRow(".com")]
3736
[DataRow(".example")]
3837
[DataRow(".example.com")]
3938
[DataRow(".example.example")]
40-
[ExpectedException(typeof(ParseException))]
39+
[TestMethod]
4140
public void ParseInvalidDomainCheck(string domain)
4241
{
43-
this.CheckPublicSuffix(domain, null);
42+
Assert.ThrowsExactly<ParseException>(() => this.CheckPublicSuffix(domain, null));
4443
}
4544

46-
[DataTestMethod]
4745
[DataRow("example")]
46+
[TestMethod]
4847
public void ParseNotUsedTopLevelDomain_(string domain)
4948
{
5049
this.CheckPublicSuffix(domain, null);
@@ -120,7 +119,6 @@ public void ComprehensiveCheck()
120119
this.CheckPublicSuffix("www.test.k12.ak.us", "test.k12.ak.us");
121120
}
122121

123-
[DataTestMethod]
124122
[DataRow("COM")]
125123
[DataRow("com")]
126124
[DataRow("公司.cn")]
@@ -150,6 +148,7 @@ public void ComprehensiveCheck()
150148
[DataRow("ec2-34-206-8-177.compute-1.amazonaws.com")]
151149
[DataRow("s3-website.us-east-2.amazonaws.com")]
152150
[DataRow("test.stg.dev")]
151+
[TestMethod]
153152
public void TldCheck(string domain)
154153
{
155154
this.CheckPublicSuffix(domain, null);

src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task FileTldRuleProviderTest()
3737

3838
var domainDataStructure = localFileRuleProvider.GetDomainDataStructure();
3939

40-
Assert.AreEqual(1460, domainDataStructure.Nested.Count);
40+
Assert.HasCount(1460, domainDataStructure.Nested);
4141
}
4242
}
4343
}

src/Nager.PublicSuffix.UnitTest/TldRuleTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,45 @@ namespace Nager.PublicSuffix.UnitTest
88
public class TldRuleTest
99
{
1010
[TestMethod]
11-
[ExpectedException(typeof(ArgumentException), "RuleData is empty")]
1211
public void InvalidRuleTest1()
1312
{
14-
new TldRule("");
13+
var ex = Assert.ThrowsExactly<ArgumentException>(() => new TldRule(""));
14+
Assert.AreEqual("RuleData is empty", ex.Message);
1515
}
1616

1717
[TestMethod]
18-
[ExpectedException(typeof(ArgumentException), "RuleData is empty")]
1918
public void InvalidRuleTest2()
2019
{
21-
new TldRule(null);
20+
var ex = Assert.ThrowsExactly<ArgumentException>(() => new TldRule(null));
21+
Assert.AreEqual("RuleData is empty", ex.Message);
2222
}
2323

2424
[TestMethod]
25-
[ExpectedException(typeof(FormatException), "Wildcard syntax not correct")]
2625
public void InvalidRuleTest3()
2726
{
28-
new TldRule("*com");
27+
var ex = Assert.ThrowsExactly<FormatException>(() => new TldRule("*com"));
28+
Assert.AreEqual("Wildcard syntax not correct", ex.Message);
2929
}
3030

3131
[TestMethod]
32-
[ExpectedException(typeof(FormatException), "Wildcard syntax not correct")]
3332
public void InvalidRuleTest4()
3433
{
35-
new TldRule("*bar.foo");
34+
var ex = Assert.ThrowsExactly<FormatException>(() => new TldRule("*bar.foo"));
35+
Assert.AreEqual("Wildcard syntax not correct", ex.Message);
3636
}
3737

3838
[TestMethod]
39-
[ExpectedException(typeof(FormatException), "Rule contains invalid empty part")]
4039
public void InvalidRuleTest5()
4140
{
42-
new TldRule(".com");
41+
var ex = Assert.ThrowsExactly<FormatException>(() => new TldRule(".com"));
42+
Assert.AreEqual("Rule contains empty part", ex.Message);
4343
}
4444

4545
[TestMethod]
46-
[ExpectedException(typeof(FormatException), "Rule contains invalid empty part")]
4746
public void InvalidRuleTest6()
4847
{
49-
new TldRule("www..com");
48+
var ex = Assert.ThrowsExactly<FormatException>(() => new TldRule("www..com"));
49+
Assert.AreEqual("Rule contains empty part", ex.Message);
5050
}
5151

5252
[TestMethod]

0 commit comments

Comments
 (0)