Skip to content

Commit

Permalink
Validate XML section on all countries
Browse files Browse the repository at this point in the history
  • Loading branch information
caspChristian committed Aug 23, 2024
1 parent 1030ff5 commit 2e11931
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions VatValidation.Tests/TestcasesFromXmlComments.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
using System.Xml.Linq;
using Towel;
using Xunit.Abstractions;

namespace VatValidation.Tests;

public class TestcasesFromXmlComments
{
private static bool IsLine(string line, string wanted)
{
if (!line.Contains(wanted))
return false;
if (line != wanted) throw new Exception(line);
return true;
}
private static IEnumerable<string> GetElementsLines(IEnumerable<XElement>? elms) =>
elms?.SelectMany(e => e.Nodes().SelectMany(n => n.ToString().Trim().Split('\n').Select(l => l.Trim()))) ?? [];

private static void AddTestcasesFromTypeXmlData(TheoryData<string, TestData> cases, Countries.ICountry inst)
{
Console.WriteLine($"{nameof(AddTestcasesFromTypeXmlData)} {inst.CC}");
var documentation = inst.GetType().GetDocumentation()?.Trim();
if (documentation is null)
Console.WriteLine("all implementations must at least have basic XML testcases");
Assert.NotNull(documentation);
Assert.NotEqual("", documentation);
if (string.IsNullOrEmpty(documentation))
return;
Assert.Contains("<testcases>", documentation);
Assert.Contains("</testcases>", documentation);
var inTestcasesSection = false;
foreach (var l in documentation.Split('\n'))
var xmlo = XElement.Parse($"<root>{documentation}</root>");
var testcases = xmlo.Elements("testcases");
Assert.Single(testcases);
var testcaserows = GetElementsLines(testcases)?.ToList() ?? [];

var countryElms = xmlo.Elements("country");
Assert.Single(countryElms);
var region = new System.Globalization.RegionInfo(inst.CC);
var country = region.EnglishName;
Assert.Equal(country, string.Join('\n', GetElementsLines(countryElms)));

foreach (var l in testcaserows)
{
var line = l.Trim();
if (IsLine(line, "<testcases>"))
{
inTestcasesSection = true;
continue;
}
else if (!inTestcasesSection)
continue;
if (IsLine(line, "</testcases>"))
{
break;
}

var data = new TestData(line);
var data = new TestData(l);
var test = GetTestFromTestLine(inst.CC, data);
Assert.NotNull(test);

Expand Down

0 comments on commit 2e11931

Please sign in to comment.