-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from merijndejonge/Unparse-tldrules
Add extension method and corresponding unit tests to convert (unparse…
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}} |