-Explicit culture for tests

-Tests
This commit is contained in:
James Newton-King
2014-12-22 00:11:57 +13:00
parent 55ccac4d92
commit e9dc993fc3
23 changed files with 442 additions and 74 deletions
@@ -462,6 +462,85 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual("234", ds.Customers[0].CustomerID);
}
[Test]
public void ContractResolverInsideConverter()
{
var test = new MultipleDataTablesJsonTest
{
TableWrapper1 = new DataTableWrapper { DataTableProperty = CreateDataTable(3, "Table1Col") },
TableWrapper2 = new DataTableWrapper { DataTableProperty = CreateDataTable(3, "Table2Col") }
};
string json = JsonConvert.SerializeObject(test, Formatting.Indented, new LowercaseDataTableConverter());
Assert.AreEqual(@"{
""TableWrapper1"": {
""DataTableProperty"": [
{
""table1col1"": ""1"",
""table1col2"": ""2"",
""table1col3"": ""3""
}
],
""StringProperty"": null,
""IntProperty"": 0
},
""TableWrapper2"": {
""DataTableProperty"": [
{
""table2col1"": ""1"",
""table2col2"": ""2"",
""table2col3"": ""3""
}
],
""StringProperty"": null,
""IntProperty"": 0
}
}", json);
}
private static DataTable CreateDataTable(int cols, string colNamePrefix)
{
var table = new DataTable();
for (int i = 1; i <= cols; i++)
{
table.Columns.Add(new DataColumn() { ColumnName = colNamePrefix + i, DefaultValue = i });
}
table.Rows.Add(table.NewRow());
return table;
}
public class DataTableWrapper
{
public DataTable DataTableProperty { get; set; }
public String StringProperty { get; set; }
public Int32 IntProperty { get; set; }
}
public class MultipleDataTablesJsonTest
{
public DataTableWrapper TableWrapper1 { get; set; }
public DataTableWrapper TableWrapper2 { get; set; }
}
public class LowercaseDataTableConverter : DataTableConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dataTableSerializer = new JsonSerializer { ContractResolver = new LowercaseContractResolver() };
base.WriteJson(writer, value, dataTableSerializer);
}
}
public class LowercaseContractResolver : DefaultContractResolver
{
protected internal override string ResolvePropertyName(string propertyName)
{
return propertyName.ToLower();
}
}
}
}
@@ -241,6 +241,7 @@
<Compile Include="Schema\JsonSchemaNodeTests.cs" />
<Compile Include="Schema\JsonSchemaSpecTests.cs" />
<Compile Include="Schema\JsonSchemaTests.cs" />
<Compile Include="Schema\PerformanceTests.cs" />
<Compile Include="Serialization\CamelCasePropertyNamesContractResolverTests.cs" />
<Compile Include="Serialization\ConstructorHandlingTests.cs" />
<Compile Include="Serialization\ContractResolverTests.cs" />
@@ -76,4 +76,4 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.7.18007")]
[assembly: AssemblyFileVersion("6.0.7.18022")]
@@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Schema;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests.Schema
{
[TestFixture]
public class PerformanceTests : TestFixtureBase
{
[Test]
public void ReaderPerformance()
{
string json = @"[
{
""id"": 2,
""name"": ""An ice sculpture"",
""price"": 12.50,
""tags"": [""cold"", ""ice""],
""dimensions"": {
""length"": 7.0,
""width"": 12.0,
""height"": 9.5
},
""warehouseLocation"": {
""latitude"": -78.75,
""longitude"": 20.4
}
},
{
""id"": 3,
""name"": ""A blue mouse"",
""price"": 25.50,
""dimensions"": {
""length"": 3.1,
""width"": 1.0,
""height"": 1.0
},
""warehouseLocation"": {
""latitude"": 54.4,
""longitude"": -32.7
}
}
]";
JsonSchema schema = JsonSchema.Parse(@"{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""title"": ""Product set"",
""type"": ""array"",
""items"": {
""title"": ""Product"",
""type"": ""object"",
""properties"": {
""id"": {
""description"": ""The unique identifier for a product"",
""type"": ""number"",
""required"": true
},
""name"": {
""type"": ""string"",
""required"": true
},
""price"": {
""type"": ""number"",
""minimum"": 0,
""exclusiveMinimum"": true,
""required"": true
},
""tags"": {
""type"": ""array"",
""items"": {
""type"": ""string""
},
""minItems"": 1,
""uniqueItems"": true
},
""dimensions"": {
""type"": ""object"",
""properties"": {
""length"": {""type"": ""number"",""required"": true},
""width"": {""type"": ""number"",""required"": true},
""height"": {""type"": ""number"",""required"": true}
}
},
""warehouseLocation"": {
""description"": ""A geographical coordinate"",
""type"": ""object"",
""properties"": {
""latitude"": { ""type"": ""number"" },
""longitude"": { ""type"": ""number"" }
}
}
}
}
}");
using (var tester = new PerformanceTester("Reader"))
{
for (int i = 0; i < 5000; i++)
{
JsonTextReader reader = new JsonTextReader(new StringReader(json));
JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
validatingReader.Schema = schema;
while (validatingReader.Read())
{
}
}
}
}
}
public class PerformanceTester : IDisposable
{
private readonly Stopwatch _stopwatch = new Stopwatch();
private readonly Action<TimeSpan> _callback;
public PerformanceTester(string description)
: this(ts => Console.WriteLine(description + ": " + ts.TotalSeconds))
{
}
public PerformanceTester(Action<TimeSpan> callback)
{
_callback = callback;
_stopwatch.Start();
}
public static PerformanceTester Start(Action<TimeSpan> callback)
{
return new PerformanceTester(callback);
}
public void Dispose()
{
_stopwatch.Stop();
if (_callback != null)
_callback(Result);
}
public TimeSpan Result
{
get { return _stopwatch.Elapsed; }
}
}
}
@@ -368,6 +368,20 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual("fff", obj.Field1);
}
#endif
[Test]
public void PopulateTest()
{
var test = JsonConvert.DeserializeObject<PopulateWithNullJsonTest>("{\"IntValue\":null}");
Console.WriteLine("IntValue:{0}", test.IntValue);
}
public class PopulateWithNullJsonTest
{
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate, NullValueHandling = NullValueHandling.Ignore)]
[DefaultValue(6)]
public int IntValue { get; set; }
}
}
#if !NET20
@@ -8374,6 +8374,46 @@ Path '', line 1, position 1.");
}
#endif
[Test]
public void ParameterizedConstructorWithBasePrivateProperties()
{
var original = new DerivedConstructorType("Base", "Derived");
var serializerSettings = new JsonSerializerSettings();
var jsonCopy = JsonConvert.SerializeObject(original, serializerSettings);
Console.WriteLine(original);
var clonedObject = JsonConvert.DeserializeObject<DerivedConstructorType>(jsonCopy, serializerSettings);
Assert.AreEqual("Base", clonedObject.BaseProperty);
Assert.AreEqual("Derived", clonedObject.DerivedProperty);
}
public class DerivedConstructorType : BaseConstructorType
{
public DerivedConstructorType(string baseProperty, string derivedProperty)
: base(baseProperty)
{
DerivedProperty = derivedProperty;
}
[JsonProperty]
public string DerivedProperty { get; private set; }
}
public class BaseConstructorType
{
[JsonProperty]
public string BaseProperty { get; private set; }
public BaseConstructorType(string baseProperty)
{
BaseProperty = baseProperty;
}
}
public class ErroringJsonConverter : JsonConverter
{
public ErroringJsonConverter(string s)
@@ -23,6 +23,11 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
#if !(PORTABLE || PORTABLE40)
using System.Collections.ObjectModel;
#if !(NET35 || NET20)
@@ -1836,6 +1841,55 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(1UL, item.WantedUnitID);
}
#endif
#if !(NET20 || NET35)
[Test]
public void GenericItemTypeCollection()
{
DataType data = new DataType();
data.Rows.Add("key", new List<MyInterfaceImplementationType> { new MyInterfaceImplementationType() { SomeProperty = "property" } });
string serialized = JsonConvert.SerializeObject(data, Formatting.Indented);
StringAssert.AreEqual(@"{
""Rows"": {
""key"": {
""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.Serialization.MyInterfaceImplementationType, Newtonsoft.Json.Tests]], mscorlib"",
""$values"": [
{
""SomeProperty"": ""property""
}
]
}
}
}", serialized);
DataType deserialized = JsonConvert.DeserializeObject<DataType>(serialized);
Assert.AreEqual("property", deserialized.Rows["key"].First().SomeProperty);
}
#endif
}
public class DataType
{
public DataType()
{
Rows = new Dictionary<string, IEnumerable<IMyInterfaceType>>();
}
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto, TypeNameHandling = TypeNameHandling.Auto)]
public Dictionary<string, IEnumerable<IMyInterfaceType>> Rows { get; private set; }
}
public interface IMyInterfaceType
{
string SomeProperty { get; set; }
}
public class MyInterfaceImplementationType : IMyInterfaceType
{
public string SomeProperty { get; set; }
}
#if !(NETFX_CORE || ASPNETCORE50)
+5 -2
View File
@@ -261,11 +261,14 @@ namespace Newtonsoft.Json.Tests
protected void TestSetup()
#endif
{
//#if !NETFX_CORE
#if !NETFX_CORE
// CultureInfo turkey = CultureInfo.CreateSpecificCulture("tr");
// Thread.CurrentThread.CurrentCulture = turkey;
// Thread.CurrentThread.CurrentUICulture = turkey;
//#endif
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
#endif
JsonConvert.DefaultSettings = null;
}