Skip to content

Commit

Permalink
Merge pull request #67 from merijndejonge/AddGetRules
Browse files Browse the repository at this point in the history
Add extension method to retrieve the collection of rules from a struc…
  • Loading branch information
tinohager authored Mar 24, 2024
2 parents a2040c7 + 056ec00 commit b20ee2b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nager.PublicSuffix.Extensions;
using Nager.PublicSuffix.Models;
using System.Linq;

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();

CollectionAssert.AreEqual(rulesIn, rulesOut);
}
}
2 changes: 1 addition & 1 deletion src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void AddRules(this DomainDataStructure structure, IEnumerable<TldR
/// <summary>
/// Add <paramref name="tldRule"/> to <paramref name="structure"/>.
/// </summary>
/// <param name="structure">The structure to appened the rule.</param>
/// <param name="structure">The structure to append the rule.</param>
/// <param name="tldRule">The rule to append.</param>
public static void AddRule(this DomainDataStructure structure, TldRule tldRule)
{
Expand Down Expand Up @@ -55,5 +55,26 @@ public static void AddRule(this DomainDataStructure structure, TldRule tldRule)
structure.Nested.Add(domainPart, new DomainDataStructure(domainPart, tldRule));
}
}

/// <summary>
/// Get collection of <see cref="TldRule"/> rules from <paramref name="structure"/>.
/// </summary>
/// <param name="structure">The <see cref="DomainDataStructure"/> structure from which to obtain the rules</param>
/// <returns></returns>
public static IEnumerable<TldRule> 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;
}
}
}
}
}

0 comments on commit b20ee2b

Please sign in to comment.