Skip to content

Commit

Permalink
Merge pull request #24 from AlexRadch/AllCulturesTests
Browse files Browse the repository at this point in the history
Fixed parsing of DateTime and Double
  • Loading branch information
Turnerj authored Jul 22, 2020
2 parents 06db808 + af5c447 commit 4116fcc
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 85 deletions.
10 changes: 4 additions & 6 deletions src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace TurnerSoftware.SitemapTools.Parser
Expand Down Expand Up @@ -77,7 +75,7 @@ private SitemapIndexEntry ParseSitemapIndex(XmlNode sitemapNode)
}
else if (nodeName.Equals("lastmod", StringComparison.InvariantCultureIgnoreCase))
{
if (DateTime.TryParse(nodeValue, out var tmpLastModified))
if (DateTime.TryParse(nodeValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var tmpLastModified))
{
result.LastModified = tmpLastModified;
}
Expand All @@ -103,7 +101,7 @@ private SitemapEntry ParseSitemapEntry(XmlNode urlNode)
}
else if (nodeName.Equals("lastmod", StringComparison.InvariantCultureIgnoreCase))
{
if (DateTime.TryParse(nodeValue, out var tmpLastModified))
if (DateTime.TryParse(nodeValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var tmpLastModified))
{
result.LastModified = tmpLastModified;
}
Expand All @@ -114,7 +112,7 @@ private SitemapEntry ParseSitemapEntry(XmlNode urlNode)
}
else if (nodeName.Equals("priority", StringComparison.InvariantCultureIgnoreCase))
{
if (double.TryParse(nodeValue, out var tmpPriority))
if (double.TryParse(nodeValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var tmpPriority))
{
result.Priority = tmpPriority;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="TurnerSoftware.RobotsExclusionTools" Version="0.3.0" />
<PackageReference Include="TurnerSoftware.RobotsExclusionTools" Version="0.4.0" />
</ItemGroup>

</Project>
61 changes: 40 additions & 21 deletions tests/TurnerSoftware.SitemapTools.Tests/SitemapQueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TurnerSoftware.SitemapTools;

namespace TurnerSoftware.SitemapTools.Tests
{
Expand All @@ -14,45 +13,65 @@ public class SitemapQueryTests : TestBase
[TestMethod]
public async Task GetSitemapAsync()
{
var sitemapQuery = GetSitemapQuery();
var uriBuilder = GetTestServerUriBuilder();
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
Thread.CurrentThread.CurrentCulture = culture;

uriBuilder.Path = "basic-sitemap.xml";
var sitemap = await sitemapQuery.GetSitemapAsync(uriBuilder.Uri);
var sitemapQuery = GetSitemapQuery();
var uriBuilder = GetTestServerUriBuilder();

Assert.AreEqual(0, sitemap.Sitemaps.Count());
Assert.AreEqual(12, sitemap.Urls.Count());
uriBuilder.Path = "basic-sitemap.xml";
var sitemap = await sitemapQuery.GetSitemapAsync(uriBuilder.Uri);

Assert.AreEqual(0, sitemap.Sitemaps.Count());
Assert.AreEqual(12, sitemap.Urls.Count());
}
}

[TestMethod]
public async Task DiscoverSitemapsAsync()
{
var sitemapQuery = GetSitemapQuery();
var discoveredSitemaps = await sitemapQuery.DiscoverSitemapsAsync("localhost");
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
Thread.CurrentThread.CurrentCulture = culture;

var sitemapQuery = GetSitemapQuery();
var discoveredSitemaps = await sitemapQuery.DiscoverSitemapsAsync("localhost");

Assert.AreEqual(3, discoveredSitemaps.Count());
Assert.AreEqual(3, discoveredSitemaps.Count());
}
}

[TestMethod]
public async Task GetAllSitemapsForDomainAsync()
{
var sitemapQuery = GetSitemapQuery();
var sitemaps = await sitemapQuery.GetAllSitemapsForDomainAsync("localhost");
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
Thread.CurrentThread.CurrentCulture = culture;

Assert.AreEqual(7, sitemaps.Count());
var sitemapQuery = GetSitemapQuery();
var sitemaps = await sitemapQuery.GetAllSitemapsForDomainAsync("localhost");

Assert.AreEqual(7, sitemaps.Count());
}
}

[TestMethod]
public async Task SupportsGzippedSitemapAsync()
{
var sitemapQuery = GetSitemapQuery();
var uriBuilder = GetTestServerUriBuilder();
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
Thread.CurrentThread.CurrentCulture = culture;

var sitemapQuery = GetSitemapQuery();
var uriBuilder = GetTestServerUriBuilder();

uriBuilder.Path = "gzipped-sitemap.xml.gz";
var sitemap = await sitemapQuery.GetSitemapAsync(uriBuilder.Uri);
uriBuilder.Path = "gzipped-sitemap.xml.gz";
var sitemap = await sitemapQuery.GetSitemapAsync(uriBuilder.Uri);

var gzipSitemapReference = new Uri("http://www.example.com/gzipped/");
Assert.IsTrue(sitemap.Urls.Any(u => u.Location == gzipSitemapReference));
var gzipSitemapReference = new Uri("http://www.example.com/gzipped/");
Assert.IsTrue(sitemap.Urls.Any(u => u.Location == gzipSitemapReference));
}
}
}
}
29 changes: 17 additions & 12 deletions tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TurnerSoftware.SitemapTools.Parser;

Expand All @@ -13,19 +13,24 @@ public class TextSitemapParserTests : TestBase
[TestMethod]
public void ParseTextSitemap()
{
using (var reader = LoadResource("text-sitemap.txt"))
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
var parser = new TextSitemapParser();
var sitemapFile = parser.ParseSitemap(reader);
Thread.CurrentThread.CurrentCulture = culture;

Assert.AreEqual(3, sitemapFile.Urls.Count());
using (var reader = LoadResource("text-sitemap.txt"))
{
var parser = new TextSitemapParser();
var sitemapFile = parser.ParseSitemap(reader);

var entry = sitemapFile.Urls.ElementAt(0);
Assert.AreEqual(new Uri("http://www.example.com/"), entry.Location);
entry = sitemapFile.Urls.ElementAt(1);
Assert.AreEqual(new Uri("http://www.example.com/about"), entry.Location);
entry = sitemapFile.Urls.ElementAt(2);
Assert.AreEqual(new Uri("http://www.example.com/contact-us"), entry.Location);
Assert.AreEqual(3, sitemapFile.Urls.Count());

var entry = sitemapFile.Urls.ElementAt(0);
Assert.AreEqual(new Uri("http://www.example.com/"), entry.Location);
entry = sitemapFile.Urls.ElementAt(1);
Assert.AreEqual(new Uri("http://www.example.com/about"), entry.Location);
entry = sitemapFile.Urls.ElementAt(2);
Assert.AreEqual(new Uri("http://www.example.com/contact-us"), entry.Location);
}
}
}
}
Expand Down
105 changes: 60 additions & 45 deletions tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TurnerSoftware.SitemapTools.Parser;

Expand All @@ -13,77 +13,92 @@ public class XmlSitemapParserTests : TestBase
[TestMethod]
public void ChangeFrequenciesAreSetCorrectly()
{
using (var reader = LoadResource("basic-sitemap.xml"))
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
var parser = new XmlSitemapParser();
var sitemapFile = parser.ParseSitemap(reader);
Thread.CurrentThread.CurrentCulture = culture;

var entries = sitemapFile.Urls.Where(e => e.Location.AbsolutePath.Contains("frequency/"));
using (var reader = LoadResource("basic-sitemap.xml"))
{
var parser = new XmlSitemapParser();
var sitemapFile = parser.ParseSitemap(reader);

var alwaysEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("always"));
Assert.IsNotNull(alwaysEntry);
Assert.AreEqual(ChangeFrequency.Always, alwaysEntry.ChangeFrequency);
var entries = sitemapFile.Urls.Where(e => e.Location.AbsolutePath.Contains("frequency/"));

var hourlyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("hourly"));
Assert.IsNotNull(hourlyEntry);
Assert.AreEqual(ChangeFrequency.Hourly, hourlyEntry.ChangeFrequency);
var alwaysEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("always"));
Assert.IsNotNull(alwaysEntry);
Assert.AreEqual(ChangeFrequency.Always, alwaysEntry.ChangeFrequency);

var dailyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("daily"));
Assert.IsNotNull(dailyEntry);
Assert.AreEqual(ChangeFrequency.Daily, dailyEntry.ChangeFrequency);
var hourlyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("hourly"));
Assert.IsNotNull(hourlyEntry);
Assert.AreEqual(ChangeFrequency.Hourly, hourlyEntry.ChangeFrequency);

var weeklyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("weekly"));
Assert.IsNotNull(weeklyEntry);
Assert.AreEqual(ChangeFrequency.Weekly, weeklyEntry.ChangeFrequency);
var dailyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("daily"));
Assert.IsNotNull(dailyEntry);
Assert.AreEqual(ChangeFrequency.Daily, dailyEntry.ChangeFrequency);

var monthlyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("monthly"));
Assert.IsNotNull(monthlyEntry);
Assert.AreEqual(ChangeFrequency.Monthly, monthlyEntry.ChangeFrequency);
var weeklyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("weekly"));
Assert.IsNotNull(weeklyEntry);
Assert.AreEqual(ChangeFrequency.Weekly, weeklyEntry.ChangeFrequency);

var yearlyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("yearly"));
Assert.IsNotNull(yearlyEntry);
Assert.AreEqual(ChangeFrequency.Yearly, yearlyEntry.ChangeFrequency);
var monthlyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("monthly"));
Assert.IsNotNull(monthlyEntry);
Assert.AreEqual(ChangeFrequency.Monthly, monthlyEntry.ChangeFrequency);

var neverEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("never"));
Assert.IsNotNull(neverEntry);
Assert.AreEqual(ChangeFrequency.Never, neverEntry.ChangeFrequency);
var yearlyEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("yearly"));
Assert.IsNotNull(yearlyEntry);
Assert.AreEqual(ChangeFrequency.Yearly, yearlyEntry.ChangeFrequency);

var neverEntry = entries.FirstOrDefault(e => e.Location.AbsolutePath.Contains("never"));
Assert.IsNotNull(neverEntry);
Assert.AreEqual(ChangeFrequency.Never, neverEntry.ChangeFrequency);
}
}
}

[TestMethod]
public void ParseIndexFile()
{
using (var reader = LoadResource("another-indexed-sitemap.xml"))
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
var parser = new XmlSitemapParser();
var sitemapFile = parser.ParseSitemap(reader);
Thread.CurrentThread.CurrentCulture = culture;

using (var reader = LoadResource("another-indexed-sitemap.xml"))
{
var parser = new XmlSitemapParser();
var sitemapFile = parser.ParseSitemap(reader);

Assert.AreEqual(1, sitemapFile.Sitemaps.Count());
Assert.AreEqual(1, sitemapFile.Sitemaps.Count());

var indexEntry = sitemapFile.Sitemaps.FirstOrDefault();
Assert.AreEqual(new Uri("http://localhost/last-text-sitemap.txt"), indexEntry.Location);
Assert.AreEqual(new DateTime(2005, 1, 1), indexEntry.LastModified);
var indexEntry = sitemapFile.Sitemaps.FirstOrDefault();
Assert.AreEqual(new Uri("http://localhost/last-text-sitemap.txt"), indexEntry.Location);
Assert.AreEqual(new DateTime(2005, 1, 1), indexEntry.LastModified);
}
}
}

[TestMethod]
public void ParseSitemapFile()
{
using (var reader = LoadResource("basic-sitemap.xml"))
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
var parser = new XmlSitemapParser();
var sitemapFile = parser.ParseSitemap(reader);
Thread.CurrentThread.CurrentCulture = culture;

using (var reader = LoadResource("basic-sitemap.xml"))
{
var parser = new XmlSitemapParser();
var sitemapFile = parser.ParseSitemap(reader);

Assert.AreEqual(12, sitemapFile.Urls.Count());
Assert.AreEqual(12, sitemapFile.Urls.Count());

var sitemapEntry = sitemapFile.Urls.FirstOrDefault();
Assert.AreEqual(new Uri("http://www.example.com/"), sitemapEntry.Location);
Assert.AreEqual(new DateTime(2005, 1, 2), sitemapEntry.LastModified);
Assert.AreEqual(0.8, sitemapEntry.Priority);
var sitemapEntry = sitemapFile.Urls.FirstOrDefault();
Assert.AreEqual(new Uri("http://www.example.com/"), sitemapEntry.Location);
Assert.AreEqual(new DateTime(2005, 1, 2), sitemapEntry.LastModified);
Assert.AreEqual(0.8, sitemapEntry.Priority);

sitemapEntry = sitemapFile.Urls.ElementAt(1);
Assert.AreEqual(new Uri("http://www.example.com/catalog?item=12&desc=vacation_hawaii"), sitemapEntry.Location);
Assert.AreEqual(0.5, sitemapEntry.Priority);
sitemapEntry = sitemapFile.Urls.ElementAt(1);
Assert.AreEqual(new Uri("http://www.example.com/catalog?item=12&desc=vacation_hawaii"), sitemapEntry.Location);
Assert.AreEqual(0.5, sitemapEntry.Priority);
}
}
}
}
Expand Down

0 comments on commit 4116fcc

Please sign in to comment.