From a5c1d78c394d8b625a7f0453595c02ef7d023e9e Mon Sep 17 00:00:00 2001 From: Merijn de Jonge Date: Thu, 29 Feb 2024 18:48:20 +0100 Subject: [PATCH 1/2] Add extension method to retrieve the collection of rules from a structure --- .../DomainDataStructureExtensionsTests.cs | 30 +++++++++++++++++++ .../DomainDataStructureExtensions.cs | 23 +++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/Nager.PublicSuffix.UnitTest/DomainDataStructureExtensionsTests.cs diff --git a/src/Nager.PublicSuffix.UnitTest/DomainDataStructureExtensionsTests.cs b/src/Nager.PublicSuffix.UnitTest/DomainDataStructureExtensionsTests.cs new file mode 100644 index 0000000..ffd0819 --- /dev/null +++ b/src/Nager.PublicSuffix.UnitTest/DomainDataStructureExtensionsTests.cs @@ -0,0 +1,30 @@ +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Nager.PublicSuffix.Extensions; +using Nager.PublicSuffix.Models; + +namespace Nager.PublicSuffix.UnitTest; + +[TestClass] +public class DomainDataStructureExtensionsTests +{ + [TestMethod] + public void GetRulesTest() + { + var structure = new DomainDataStructure(""); + var rulesIn = new[] + { + new TldRule("foo", TldRuleDivision.Private), + new TldRule("bar", TldRuleDivision.Private ), + new TldRule("to.gov.br", TldRuleDivision.ICANN), + new TldRule("*.bd", TldRuleDivision.ICANN), + new TldRule("un.known", TldRuleDivision.Unknown) + }; + + structure.AddRules(rulesIn); + + var rulesOut = structure.GetRules().ToArray(); + + Assert.IsTrue(rulesIn.SequenceEqual(rulesOut)); + } +} \ No newline at end of file diff --git a/src/Nager.PublicSuffix/Extensions/DomainDataStructureExtensions.cs b/src/Nager.PublicSuffix/Extensions/DomainDataStructureExtensions.cs index 0ccf984..ed24db9 100644 --- a/src/Nager.PublicSuffix/Extensions/DomainDataStructureExtensions.cs +++ b/src/Nager.PublicSuffix/Extensions/DomainDataStructureExtensions.cs @@ -25,7 +25,7 @@ public static void AddRules(this DomainDataStructure structure, IEnumerable /// Add to . /// - /// The structure to appened the rule. + /// The structure to append the rule. /// The rule to append. public static void AddRule(this DomainDataStructure structure, TldRule tldRule) { @@ -55,5 +55,26 @@ public static void AddRule(this DomainDataStructure structure, TldRule tldRule) structure.Nested.Add(domainPart, new DomainDataStructure(domainPart, tldRule)); } } + + /// + /// Get collection of rules from . + /// + /// The structure from which to obtain the rules + /// + public static IEnumerable GetRules(this DomainDataStructure structure) + { + foreach (var dataStructure in structure.Nested.Values) + { + if (dataStructure.TldRule != null) + { + yield return dataStructure.TldRule; + } + + foreach (var tldRule in dataStructure.GetRules()) + { + yield return tldRule; + } + } + } } } From 056ec0021ea0759dbf1c5cf0fc5c848653953c1f Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Sun, 24 Mar 2024 22:35:18 +0100 Subject: [PATCH 2/2] Optimize unit test, cleanup code --- .../DomainDataStructureExtensionsTests.cs | 4 ++-- src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs | 2 +- .../Extensions/DomainDataStructureExtensions.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Nager.PublicSuffix.UnitTest/DomainDataStructureExtensionsTests.cs b/src/Nager.PublicSuffix.UnitTest/DomainDataStructureExtensionsTests.cs index ffd0819..e9004a4 100644 --- a/src/Nager.PublicSuffix.UnitTest/DomainDataStructureExtensionsTests.cs +++ b/src/Nager.PublicSuffix.UnitTest/DomainDataStructureExtensionsTests.cs @@ -1,7 +1,7 @@ -using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Nager.PublicSuffix.Extensions; using Nager.PublicSuffix.Models; +using System.Linq; namespace Nager.PublicSuffix.UnitTest; @@ -25,6 +25,6 @@ public void GetRulesTest() var rulesOut = structure.GetRules().ToArray(); - Assert.IsTrue(rulesIn.SequenceEqual(rulesOut)); + CollectionAssert.AreEqual(rulesIn, rulesOut); } } \ No newline at end of file diff --git a/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs b/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs index 04b1216..39fcc5b 100644 --- a/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs +++ b/src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs @@ -1,8 +1,8 @@ -using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Nager.PublicSuffix.Extensions; using Nager.PublicSuffix.Models; using Nager.PublicSuffix.RuleParsers; +using System.Linq; namespace Nager.PublicSuffix.UnitTest; diff --git a/src/Nager.PublicSuffix/Extensions/DomainDataStructureExtensions.cs b/src/Nager.PublicSuffix/Extensions/DomainDataStructureExtensions.cs index ed24db9..bb93c7d 100644 --- a/src/Nager.PublicSuffix/Extensions/DomainDataStructureExtensions.cs +++ b/src/Nager.PublicSuffix/Extensions/DomainDataStructureExtensions.cs @@ -55,7 +55,7 @@ public static void AddRule(this DomainDataStructure structure, TldRule tldRule) structure.Nested.Add(domainPart, new DomainDataStructure(domainPart, tldRule)); } } - + /// /// Get collection of rules from . ///