Skip to content

Commit

Permalink
Merge pull request #68 from merijndejonge/Unparse-tldrules
Browse files Browse the repository at this point in the history
Add extension method and corresponding unit tests to convert (unparse…
  • Loading branch information
tinohager authored Mar 24, 2024
2 parents 75eba15 + 2d683b3 commit a2040c7
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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);

CollectionAssert.AreEqual(rules1, rules2);
Assert.AreEqual(TldRuleType.WildcardException, rules2[1].Type);
}

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

CollectionAssert.AreEqual(rules1, rules2);
Assert.AreEqual(TldRuleType.Wildcard, rules2[3].Type);
}

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);
}
}
57 changes: 57 additions & 0 deletions src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nager.PublicSuffix.Models;

namespace Nager.PublicSuffix.Extensions
{
/// <summary>
/// TldRule Extensions
/// </summary>
public static class TldRuleExtensions
{
/// <summary>
/// Converts the collection of <see cref="TldRule"/> <paramref name="rules"/> to text.
/// </summary>
/// <param name="rules">The collection of <see cref="TldRule"/> rules</param>
/// <returns></returns>
public static string UnparseRules(this IEnumerable<TldRule> rules)
{
var rulesData = new StringBuilder();
foreach (var division in rules.GroupBy(rule => rule.Division))
{
switch (division.Key)
{
case TldRuleDivision.ICANN:
rulesData.Append("\n// ===BEGIN ICANN DOMAINS===\n");
break;
case TldRuleDivision.Private:
rulesData.Append("\n// ===BEGIN PRIVATE DOMAINS===\n");
break;
}

foreach (var rule in division)
{
rulesData.Append("\n");

if (rule.Type == TldRuleType.WildcardException)
{
rulesData.Append("!");
}
rulesData.Append(rule.Name);
}

switch (division.Key)
{
case TldRuleDivision.ICANN:
rulesData.Append("\n// ===END ICANN DOMAINS===\n");
break;
case TldRuleDivision.Private:
rulesData.Append("\n// ===END PRIVATE DOMAINS===\n");
break;
}
}

return rulesData.ToString();
}
}}

0 comments on commit a2040c7

Please sign in to comment.