Skip to content

Commit

Permalink
Optimize code and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Mar 24, 2024
1 parent 07d4d95 commit 2d683b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
26 changes: 14 additions & 12 deletions src/Nager.PublicSuffix.UnitTest/TldRuleExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
27 changes: 16 additions & 11 deletions src/Nager.PublicSuffix/Extensions/TldRuleExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,52 +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)
public static string UnparseRules(this IEnumerable<TldRule> 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();
}
}}

0 comments on commit 2d683b3

Please sign in to comment.