From d2ca5ddd7f4742938314d6e1f0e9bee26d6dcf8a Mon Sep 17 00:00:00 2001 From: Merijn de Jonge Date: Thu, 29 Feb 2024 18:52:25 +0100 Subject: [PATCH 1/2] Add extension method and corresponding unit tests to convert (unparse) a collection of TldRules to text --- .../TldRuleExtensionsTests.cs | 55 +++++++++++++++++++ .../Extensions/TldRuleExtensions.cs | 52 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs create mode 100644 src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs diff --git a/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs b/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs new file mode 100644 index 0000000..a1fde0a --- /dev/null +++ b/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs @@ -0,0 +1,55 @@ +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Nager.PublicSuffix.Extensions; +using Nager.PublicSuffix.Models; +using Nager.PublicSuffix.RuleParsers; + +namespace Nager.PublicSuffix.UnitTest; + +[TestClass] +public class TldRuleExtensionsTests +{ + [TestMethod] + public void UnParseWithExceptionTest() + { + const string rulesInText = """ + foo.com + !bar.com + !foo.bar.com + """; + var (rules1, rules2) = ParseUnParseRules(rulesInText); + + Assert.IsTrue(rules1.SequenceEqual(rules2)); + Assert.IsTrue(rules2[1].Type == TldRuleType.WildcardException); + } + + [TestMethod] + public void UnParseWithWildCardTest() + { + const string rulesInText = """ + natal.br + net.br + niteroi.br + *.nom.br + not.br + ntr.br + odo.br + ong.br + org.br + """; + var (rules1, rules2) = ParseUnParseRules(rulesInText); + Assert.IsTrue(rules1.SequenceEqual(rules2)); + Assert.IsTrue(rules2[3].Type == TldRuleType.Wildcard); + } + + private static (TldRule[] rules1, TldRule[] rules2) ParseUnParseRules(string rulesText) + { + var ruleParser = new TldRuleParser(); + + var rules1 = ruleParser.ParseRules(rulesText).ToArray(); + var rulesUnParsedText = rules1.UnParseRules(); + var rules2 = ruleParser.ParseRules(rulesUnParsedText).ToArray(); + + return (rules1, rules2); + } +} \ No newline at end of file diff --git a/src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs b/src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs new file mode 100644 index 0000000..e3beb66 --- /dev/null +++ b/src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.Linq; +using Nager.PublicSuffix.Models; + +namespace Nager.PublicSuffix.Extensions +{ + public static class TldRuleExtensions + { + /// + /// Converts the collection of to text. + /// + /// The collection of rules + /// + public static string UnParseRules(this IEnumerable rules) + { + var rulesData = ""; + foreach (var division in rules.GroupBy(x=>x.Division)) + { + switch (division.Key) + { + case TldRuleDivision.ICANN: + rulesData += "\n// ===BEGIN ICANN DOMAINS===\n"; + break; + case TldRuleDivision.Private: + rulesData += "\n// ===BEGIN PRIVATE DOMAINS===\n"; + break; + } + + foreach (var rule in division) + { + rulesData += "\n"; + if (rule.Type == TldRuleType.WildcardException) + { + rulesData += "!"; + } + rulesData += rule.Name; + } + + switch (division.Key) + { + case TldRuleDivision.ICANN: + rulesData += "\n// ===END ICANN DOMAINS===\n"; + break; + case TldRuleDivision.Private: + rulesData += "\n// ===END PRIVATE DOMAINS===\n"; + break; + } + } + + return rulesData; + } + }} \ No newline at end of file From 2d683b320aeee4562dcaacd6820301baf29a1f62 Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Sun, 24 Mar 2024 22:28:34 +0100 Subject: [PATCH 2/2] Optimize code and unit tests --- .../TldRuleExtensionsTests.cs | 26 +++++++++--------- .../Extensions/TldRuleExtensions.cs | 27 +++++++++++-------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs b/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs index a1fde0a..04b1216 100644 --- a/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs +++ b/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs @@ -10,21 +10,22 @@ namespace Nager.PublicSuffix.UnitTest; public class TldRuleExtensionsTests { [TestMethod] - public void UnParseWithExceptionTest() + public void UnparseWithExceptionTest() { const string rulesInText = """ foo.com !bar.com !foo.bar.com """; - var (rules1, rules2) = ParseUnParseRules(rulesInText); - - Assert.IsTrue(rules1.SequenceEqual(rules2)); - Assert.IsTrue(rules2[1].Type == TldRuleType.WildcardException); + + var (rules1, rules2) = ParseUnparseRules(rulesInText); + + CollectionAssert.AreEqual(rules1, rules2); + Assert.AreEqual(TldRuleType.WildcardException, rules2[1].Type); } [TestMethod] - public void UnParseWithWildCardTest() + public void UnparseWithWildCardTest() { const string rulesInText = """ natal.br @@ -37,19 +38,20 @@ public void UnParseWithWildCardTest() ong.br org.br """; - var (rules1, rules2) = ParseUnParseRules(rulesInText); - Assert.IsTrue(rules1.SequenceEqual(rules2)); - Assert.IsTrue(rules2[3].Type == TldRuleType.Wildcard); + var (rules1, rules2) = ParseUnparseRules(rulesInText); + + CollectionAssert.AreEqual(rules1, rules2); + Assert.AreEqual(TldRuleType.Wildcard, rules2[3].Type); } - private static (TldRule[] rules1, TldRule[] rules2) ParseUnParseRules(string rulesText) + private static (TldRule[] rules1, TldRule[] rules2) ParseUnparseRules(string rulesText) { var ruleParser = new TldRuleParser(); var rules1 = ruleParser.ParseRules(rulesText).ToArray(); - var rulesUnParsedText = rules1.UnParseRules(); + var rulesUnParsedText = rules1.UnparseRules(); var rules2 = ruleParser.ParseRules(rulesUnParsedText).ToArray(); return (rules1, rules2); } -} \ No newline at end of file +} diff --git a/src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs b/src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs index e3beb66..5a164a6 100644 --- a/src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs +++ b/src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs @@ -1,9 +1,13 @@ using System.Collections.Generic; using System.Linq; +using System.Text; using Nager.PublicSuffix.Models; namespace Nager.PublicSuffix.Extensions { + /// + /// TldRule Extensions + /// public static class TldRuleExtensions { /// @@ -11,42 +15,43 @@ public static class TldRuleExtensions /// /// The collection of rules /// - public static string UnParseRules(this IEnumerable rules) + public static string UnparseRules(this IEnumerable rules) { - var rulesData = ""; - foreach (var division in rules.GroupBy(x=>x.Division)) + var rulesData = new StringBuilder(); + foreach (var division in rules.GroupBy(rule => rule.Division)) { switch (division.Key) { case TldRuleDivision.ICANN: - rulesData += "\n// ===BEGIN ICANN DOMAINS===\n"; + rulesData.Append("\n// ===BEGIN ICANN DOMAINS===\n"); break; case TldRuleDivision.Private: - rulesData += "\n// ===BEGIN PRIVATE DOMAINS===\n"; + rulesData.Append("\n// ===BEGIN PRIVATE DOMAINS===\n"); break; } foreach (var rule in division) { - rulesData += "\n"; + rulesData.Append("\n"); + if (rule.Type == TldRuleType.WildcardException) { - rulesData += "!"; + rulesData.Append("!"); } - rulesData += rule.Name; + rulesData.Append(rule.Name); } switch (division.Key) { case TldRuleDivision.ICANN: - rulesData += "\n// ===END ICANN DOMAINS===\n"; + rulesData.Append("\n// ===END ICANN DOMAINS===\n"); break; case TldRuleDivision.Private: - rulesData += "\n// ===END PRIVATE DOMAINS===\n"; + rulesData.Append("\n// ===END PRIVATE DOMAINS===\n"); break; } } - return rulesData; + return rulesData.ToString(); } }} \ No newline at end of file