From 6591f7f29abeaa40fbdb83c067fab7f3ccc062b8 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Fri, 24 Sep 2010 10:32:43 +0000 Subject: [PATCH] -Fixed test when tests run in UTC timezone --- .../Converters/IsoDateTimeConverterTests.cs | 11 +++++-- .../Serialization/TypeNameHandlingTests.cs | 32 ++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs index 092a14e7..2424c0e0 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/IsoDateTimeConverterTests.cs @@ -262,14 +262,21 @@ namespace Newtonsoft.Json.Tests.Converters [Test] public void SerializeShouldChangeNonUTCDates() { + DateTime localDateTime = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Local); + DateTimeTestClass c = new DateTimeTestClass(); - c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Local); + c.DateTimeField = localDateTime; c.PreField = "Pre"; c.PostField = "Post"; string json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); //note that this fails without the Utc converter... c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc); string json2 = JsonConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); - Assert.AreNotEqual(json, json2); + + // if the current timezone is utc then local already equals utc + if (TimeZone.CurrentTimeZone.GetUtcOffset(localDateTime) == TimeSpan.Zero) + Assert.AreEqual(json, json2); + else + Assert.AreNotEqual(json, json2); } #endif diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index 1a1dd32e..50d67fae 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -579,12 +579,18 @@ namespace Newtonsoft.Json.Tests.Serialization Dictionary collection = new Dictionary() { {"First", new UrlStatus{ Status = 404, Url = @"http://www.bing.com"}}, - {"Second", new UrlStatus{Status = 400, Url = @"http://www.google.com"}} + {"Second", new UrlStatus{Status = 400, Url = @"http://www.google.com"}}, + {"List", new List + { + new UrlStatus {Status = 300, Url = @"http://www.yahoo.com"}, + new UrlStatus {Status = 200, Url = @"http://www.askjeeves.com"} + } + } }; string json = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings { - TypeNameHandling = TypeNameHandling.Objects, + TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }); @@ -599,20 +605,38 @@ namespace Newtonsoft.Json.Tests.Serialization ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 400, ""Url"": ""http://www.google.com"" + }, + ""List"": { + ""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests]], mscorlib"", + ""$values"": [ + { + ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", + ""Status"": 300, + ""Url"": ""http://www.yahoo.com"" + }, + { + ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", + ""Status"": 200, + ""Url"": ""http://www.askjeeves.com"" + } + ] } }", json); object c = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { - TypeNameHandling = TypeNameHandling.Objects, + TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }); Assert.IsInstanceOfType(typeof(Dictionary), c); Dictionary newCollection = (Dictionary)c; - Assert.AreEqual(2, newCollection.Count); + Assert.AreEqual(3, newCollection.Count); Assert.AreEqual(@"http://www.bing.com", ((UrlStatus)newCollection["First"]).Url); + + List statues = (List) newCollection["List"]; + Assert.AreEqual(2, statues.Count); } }