Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension method and corresponding unit tests to convert (unparse… #68

Merged
merged 3 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}}