From f3ec28a6b360b45558321e946b99eb002b16fa2d Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 28 Apr 2013 12:49:23 +1200 Subject: [PATCH] -Fixed build --- Src/Newtonsoft.Json.Tests/JsonConvertTest.cs | 161 +++++++++++-------- Src/Newtonsoft.Json.Tests/TestFixtureBase.cs | 2 +- 2 files changed, 99 insertions(+), 64 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs index 1794d5f6..afdf77d2 100644 --- a/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonConvertTest.cs @@ -53,142 +53,177 @@ namespace Newtonsoft.Json.Tests [Test] public void DefaultSettings() { - JsonConvert.DefaultSettings = () => new JsonSerializerSettings + try { - Formatting = Formatting.Indented - }; + JsonConvert.DefaultSettings = () => new JsonSerializerSettings + { + Formatting = Formatting.Indented + }; - string json = JsonConvert.SerializeObject(new { test = new[] { 1, 2, 3 } }); + string json = JsonConvert.SerializeObject(new { test = new[] { 1, 2, 3 } }); - Assert.AreEqual(@"{ + Assert.AreEqual(@"{ ""test"": [ 1, 2, 3 ] }", json); + } + finally + { + JsonConvert.DefaultSettings = null; + } } [Test] public void DefaultSettings_Override() { - JsonConvert.DefaultSettings = () => new JsonSerializerSettings + try { - Formatting = Formatting.Indented - }; + JsonConvert.DefaultSettings = () => new JsonSerializerSettings + { + Formatting = Formatting.Indented + }; - string json = JsonConvert.SerializeObject(new { test = new[] { 1, 2, 3 } }, new JsonSerializerSettings + string json = JsonConvert.SerializeObject(new { test = new[] { 1, 2, 3 } }, new JsonSerializerSettings + { + Formatting = Formatting.None + }); + + Assert.AreEqual(@"{""test"":[1,2,3]}", json); + } + finally { - Formatting = Formatting.None - }); - - Assert.AreEqual(@"{""test"":[1,2,3]}", json); + JsonConvert.DefaultSettings = null; + } } [Test] public void DefaultSettings_Override_JsonConverterOrder() { - JsonConvert.DefaultSettings = () => new JsonSerializerSettings + try { - Formatting = Formatting.Indented, - Converters = { new IsoDateTimeConverter { DateTimeFormat = "yyyy" } } - }; + JsonConvert.DefaultSettings = () => new JsonSerializerSettings + { + Formatting = Formatting.Indented, + Converters = { new IsoDateTimeConverter { DateTimeFormat = "yyyy" } } + }; - string json = JsonConvert.SerializeObject(new[] { new DateTime(2000, 12, 12, 4, 2, 4, DateTimeKind.Utc) }, new JsonSerializerSettings - { - Formatting = Formatting.None, - Converters = + string json = JsonConvert.SerializeObject(new[] { new DateTime(2000, 12, 12, 4, 2, 4, DateTimeKind.Utc) }, new JsonSerializerSettings + { + Formatting = Formatting.None, + Converters = { // should take precedence new JavaScriptDateTimeConverter(), new IsoDateTimeConverter { DateTimeFormat = "dd" } } - }); + }); - Assert.AreEqual(@"[new Date(976593724000)]", json); + Assert.AreEqual(@"[new Date(976593724000)]", json); + } + finally + { + JsonConvert.DefaultSettings = null; + } } [Test] public void DefaultSettings_Create() { - JsonConvert.DefaultSettings = () => new JsonSerializerSettings + try { - Formatting = Formatting.Indented - }; + JsonConvert.DefaultSettings = () => new JsonSerializerSettings + { + Formatting = Formatting.Indented + }; - IList l = new List { 1, 2, 3 }; + IList l = new List { 1, 2, 3 }; - StringWriter sw = new StringWriter(); - JsonSerializer serializer = JsonSerializer.CreateDefault(); - serializer.Serialize(sw, l); + StringWriter sw = new StringWriter(); + JsonSerializer serializer = JsonSerializer.CreateDefault(); + serializer.Serialize(sw, l); - Assert.AreEqual(@"[ + Assert.AreEqual(@"[ 1, 2, 3 ]", sw.ToString()); - sw = new StringWriter(); - serializer.Formatting = Formatting.None; - serializer.Serialize(sw, l); + sw = new StringWriter(); + serializer.Formatting = Formatting.None; + serializer.Serialize(sw, l); - Assert.AreEqual(@"[1,2,3]", sw.ToString()); + Assert.AreEqual(@"[1,2,3]", sw.ToString()); - sw = new StringWriter(); - serializer = new JsonSerializer(); - serializer.Serialize(sw, l); + sw = new StringWriter(); + serializer = new JsonSerializer(); + serializer.Serialize(sw, l); - Assert.AreEqual(@"[1,2,3]", sw.ToString()); + Assert.AreEqual(@"[1,2,3]", sw.ToString()); - sw = new StringWriter(); - serializer = JsonSerializer.Create(); - serializer.Serialize(sw, l); + sw = new StringWriter(); + serializer = JsonSerializer.Create(); + serializer.Serialize(sw, l); - Assert.AreEqual(@"[1,2,3]", sw.ToString()); + Assert.AreEqual(@"[1,2,3]", sw.ToString()); + } + finally + { + JsonConvert.DefaultSettings = null; + } } [Test] public void DefaultSettings_CreateWithSettings() { - JsonConvert.DefaultSettings = () => new JsonSerializerSettings + try { - Formatting = Formatting.Indented - }; - - IList l = new List { 1, 2, 3 }; - - StringWriter sw = new StringWriter(); - JsonSerializer serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings + JsonConvert.DefaultSettings = () => new JsonSerializerSettings { - Converters = { new IntConverter() } - }); - serializer.Serialize(sw, l); + Formatting = Formatting.Indented + }; - Assert.AreEqual(@"[ + IList l = new List { 1, 2, 3 }; + + StringWriter sw = new StringWriter(); + JsonSerializer serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings + { + Converters = { new IntConverter() } + }); + serializer.Serialize(sw, l); + + Assert.AreEqual(@"[ 2, 4, 6 ]", sw.ToString()); - sw = new StringWriter(); - serializer.Converters.Clear(); - serializer.Serialize(sw, l); + sw = new StringWriter(); + serializer.Converters.Clear(); + serializer.Serialize(sw, l); - Assert.AreEqual(@"[ + Assert.AreEqual(@"[ 1, 2, 3 ]", sw.ToString()); - sw = new StringWriter(); - serializer = JsonSerializer.Create(new JsonSerializerSettings { Formatting = Formatting.Indented }); - serializer.Serialize(sw, l); + sw = new StringWriter(); + serializer = JsonSerializer.Create(new JsonSerializerSettings { Formatting = Formatting.Indented }); + serializer.Serialize(sw, l); - Assert.AreEqual(@"[ + Assert.AreEqual(@"[ 1, 2, 3 ]", sw.ToString()); + } + finally + { + JsonConvert.DefaultSettings = null; + } } public class IntConverter : JsonConverter diff --git a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs index 3c60c72f..94dead46 100644 --- a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs +++ b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs @@ -41,7 +41,7 @@ using NUnit.Framework; using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; using TestMethod = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute; -using Setup = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitialize; +using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute; #endif using Newtonsoft.Json.Utilities; using System.Collections;