From c2b0aaf0b428b459cb23dcf5c1e023c55070c7ab Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Wed, 22 Jul 2020 00:02:39 +0700 Subject: [PATCH 1/2] Changed tests for all cultures and fixed parsing of DateTime and Double --- .../Parser/XmlSitemapParser.cs | 10 +- .../TurnerSoftware.SitemapTools.csproj | 2 +- .../SitemapQueryTests.cs | 61 ++++++---- .../TextSitemapParserTests.cs | 29 +++-- .../XmlSitemapParserTests.cs | 105 ++++++++++-------- 5 files changed, 122 insertions(+), 85 deletions(-) diff --git a/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs b/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs index 367dd3b..e967bb2 100644 --- a/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs +++ b/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs @@ -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 @@ -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.AllowWhiteSpaces, out var tmpLastModified)) { result.LastModified = tmpLastModified; } @@ -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.AllowWhiteSpaces, out var tmpLastModified)) { result.LastModified = tmpLastModified; } @@ -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; } diff --git a/src/TurnerSoftware.SitemapTools/TurnerSoftware.SitemapTools.csproj b/src/TurnerSoftware.SitemapTools/TurnerSoftware.SitemapTools.csproj index 2df7f48..e82482b 100644 --- a/src/TurnerSoftware.SitemapTools/TurnerSoftware.SitemapTools.csproj +++ b/src/TurnerSoftware.SitemapTools/TurnerSoftware.SitemapTools.csproj @@ -14,7 +14,7 @@ all runtime; build; native; contentfiles; analyzers - + diff --git a/tests/TurnerSoftware.SitemapTools.Tests/SitemapQueryTests.cs b/tests/TurnerSoftware.SitemapTools.Tests/SitemapQueryTests.cs index b6ab811..d81c62d 100644 --- a/tests/TurnerSoftware.SitemapTools.Tests/SitemapQueryTests.cs +++ b/tests/TurnerSoftware.SitemapTools.Tests/SitemapQueryTests.cs @@ -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 { @@ -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)); + } } } } diff --git a/tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs b/tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs index 0559ab7..bffc284 100644 --- a/tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs +++ b/tests/TurnerSoftware.SitemapTools.Tests/TextSitemapParserTests.cs @@ -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; @@ -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); + } } } } diff --git a/tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs b/tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs index 9107a1c..71f5d3d 100644 --- a/tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs +++ b/tests/TurnerSoftware.SitemapTools.Tests/XmlSitemapParserTests.cs @@ -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; @@ -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); + } } } } From af5c447940b71049095a8c95c9598ea25e43cd58 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Wed, 22 Jul 2020 11:47:32 +0700 Subject: [PATCH 2/2] Changed DateTime.TryParse style to DateTimeStyles.None --- src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs b/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs index e967bb2..0d24dd9 100644 --- a/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs +++ b/src/TurnerSoftware.SitemapTools/Parser/XmlSitemapParser.cs @@ -75,7 +75,7 @@ private SitemapIndexEntry ParseSitemapIndex(XmlNode sitemapNode) } else if (nodeName.Equals("lastmod", StringComparison.InvariantCultureIgnoreCase)) { - if (DateTime.TryParse(nodeValue, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out var tmpLastModified)) + if (DateTime.TryParse(nodeValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var tmpLastModified)) { result.LastModified = tmpLastModified; } @@ -101,7 +101,7 @@ private SitemapEntry ParseSitemapEntry(XmlNode urlNode) } else if (nodeName.Equals("lastmod", StringComparison.InvariantCultureIgnoreCase)) { - if (DateTime.TryParse(nodeValue, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out var tmpLastModified)) + if (DateTime.TryParse(nodeValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var tmpLastModified)) { result.LastModified = tmpLastModified; }