-Fixed MetadataTypeAttribute reflection bug by using CustomAttributeData
-Fixed immutable collections bug by getting type from known assembly -Added support from running tests on aspnetcore50
This commit is contained in:
@@ -23,17 +23,22 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
#if !(NET20 || NET35)
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
#else
|
||||
#if NETFX_CORE
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
|
||||
#elif ASPNETCORE50
|
||||
using Xunit;
|
||||
using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
#endif
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
@@ -48,7 +53,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[Test]
|
||||
public void ConstructorWithRefString()
|
||||
{
|
||||
ConstructorInfo constructor = typeof(OutAndRefTestClass).GetConstructors().Single(c => c.GetParameters().Count() == 1);
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(OutAndRefTestClass)).Single(c => c.GetParameters().Count() == 1);
|
||||
|
||||
var creator = ExpressionReflectionDelegateFactory.Instance.CreateParametrizedConstructor(constructor);
|
||||
|
||||
@@ -61,7 +66,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[Test]
|
||||
public void ConstructorWithRefStringAndOutBool()
|
||||
{
|
||||
ConstructorInfo constructor = typeof(OutAndRefTestClass).GetConstructors().Single(c => c.GetParameters().Count() == 2);
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(OutAndRefTestClass)).Single(c => c.GetParameters().Count() == 2);
|
||||
|
||||
var creator = ExpressionReflectionDelegateFactory.Instance.CreateParametrizedConstructor(constructor);
|
||||
|
||||
@@ -74,7 +79,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[Test]
|
||||
public void ConstructorWithRefStringAndRefBoolAndRefBool()
|
||||
{
|
||||
ConstructorInfo constructor = typeof(OutAndRefTestClass).GetConstructors().Single(c => c.GetParameters().Count() == 3);
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(OutAndRefTestClass)).Single(c => c.GetParameters().Count() == 3);
|
||||
|
||||
var creator = ExpressionReflectionDelegateFactory.Instance.CreateParametrizedConstructor(constructor);
|
||||
|
||||
@@ -121,7 +126,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[Test]
|
||||
public void CreatePropertySetter()
|
||||
{
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetProperty(typeof(Movie), "Name"));
|
||||
|
||||
Movie m = new Movie();
|
||||
|
||||
@@ -133,7 +138,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[Test]
|
||||
public void CreatePropertyGetter()
|
||||
{
|
||||
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
|
||||
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(TestReflectionUtils.GetProperty(typeof(Movie), "Name"));
|
||||
|
||||
Movie m = new Movie();
|
||||
m.Name = "OH HAI!";
|
||||
@@ -146,13 +151,13 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[Test]
|
||||
public void CreateMethodCall()
|
||||
{
|
||||
MethodCall<object, object> method = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(typeof(Movie).GetMethod("ToString"));
|
||||
MethodCall<object, object> method = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(TestReflectionUtils.GetMethod(typeof(Movie), "ToString"));
|
||||
|
||||
Movie m = new Movie();
|
||||
object result = method(m);
|
||||
Assert.AreEqual("Newtonsoft.Json.Tests.TestObjects.Movie", result);
|
||||
|
||||
method = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(typeof(Movie).GetMethod("Equals"));
|
||||
method = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(TestReflectionUtils.GetMethod(typeof(Movie), "Equals"));
|
||||
|
||||
result = method(m, m);
|
||||
Assert.AreEqual(true, result);
|
||||
@@ -180,12 +185,12 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
StaticTestClass.StringField = "Field!";
|
||||
StaticTestClass.StringProperty = "Property!";
|
||||
|
||||
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(StaticTestClass).GetProperty("StringProperty"));
|
||||
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(TestReflectionUtils.GetProperty(typeof(StaticTestClass), "StringProperty"));
|
||||
|
||||
object v = getter(null);
|
||||
Assert.AreEqual(StaticTestClass.StringProperty, v);
|
||||
|
||||
getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(StaticTestClass).GetField("StringField"));
|
||||
getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(TestReflectionUtils.GetField(typeof(StaticTestClass), "StringField"));
|
||||
|
||||
v = getter(null);
|
||||
Assert.AreEqual(StaticTestClass.StringField, v);
|
||||
@@ -194,12 +199,12 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[Test]
|
||||
public void SetStatic()
|
||||
{
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StaticTestClass).GetProperty("StringProperty"));
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetProperty(typeof(StaticTestClass), "StringProperty"));
|
||||
|
||||
setter(null, "New property!");
|
||||
Assert.AreEqual("New property!", StaticTestClass.StringProperty);
|
||||
|
||||
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StaticTestClass).GetField("StringField"));
|
||||
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetField(typeof(StaticTestClass), "StringField"));
|
||||
|
||||
setter(null, "New field!");
|
||||
Assert.AreEqual("New field!", StaticTestClass.StringField);
|
||||
@@ -222,12 +227,12 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
StringField = "String!"
|
||||
};
|
||||
|
||||
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(FieldsTestClass).GetField("StringField"));
|
||||
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(TestReflectionUtils.GetField(typeof(FieldsTestClass), "StringField"));
|
||||
|
||||
object value = getter(c);
|
||||
Assert.AreEqual("String!", value);
|
||||
|
||||
getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(FieldsTestClass).GetField("BoolField"));
|
||||
getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(TestReflectionUtils.GetField(typeof(FieldsTestClass), "BoolField"));
|
||||
|
||||
value = getter(c);
|
||||
Assert.AreEqual(true, value);
|
||||
@@ -238,7 +243,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
{
|
||||
FieldsTestClass c = new FieldsTestClass();
|
||||
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("IntReadOnlyField"));
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetField(typeof(FieldsTestClass), "IntReadOnlyField"));
|
||||
|
||||
setter(c, int.MinValue);
|
||||
Assert.AreEqual(int.MinValue, c.IntReadOnlyField);
|
||||
@@ -249,12 +254,12 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
{
|
||||
FieldsTestClass c = new FieldsTestClass();
|
||||
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("StringField"));
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetField(typeof(FieldsTestClass), "StringField"));
|
||||
|
||||
setter(c, "String!");
|
||||
Assert.AreEqual("String!", c.StringField);
|
||||
|
||||
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("BoolField"));
|
||||
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetField(typeof(FieldsTestClass), "BoolField"));
|
||||
|
||||
setter(c, true);
|
||||
Assert.AreEqual(true, c.BoolField);
|
||||
@@ -265,12 +270,12 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
{
|
||||
object structTest = new StructTest();
|
||||
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetProperty(typeof(StructTest), "StringProperty"));
|
||||
|
||||
setter(structTest, "Hi1");
|
||||
Assert.AreEqual("Hi1", ((StructTest)structTest).StringProperty);
|
||||
|
||||
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetField("StringField"));
|
||||
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetField(typeof(StructTest), "StringField"));
|
||||
|
||||
setter(structTest, "Hi2");
|
||||
Assert.AreEqual("Hi2", ((StructTest)structTest).StringField);
|
||||
@@ -284,7 +289,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
Person p = new Person();
|
||||
p.Name = "Hi";
|
||||
|
||||
Func<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
|
||||
Func<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(TestReflectionUtils.GetProperty(typeof(Movie), "Name"));
|
||||
|
||||
setter(p);
|
||||
},
|
||||
@@ -302,7 +307,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
Person p = new Person();
|
||||
Movie m = new Movie();
|
||||
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetProperty(typeof(Movie), "Name"));
|
||||
|
||||
setter(m, "Hi");
|
||||
|
||||
@@ -325,7 +330,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
() => {
|
||||
Movie m = new Movie();
|
||||
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
|
||||
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(TestReflectionUtils.GetProperty(typeof(Movie), "Name"));
|
||||
|
||||
setter(m, new Version("1.1.1.1"));
|
||||
}, new [] {
|
||||
|
||||
Reference in New Issue
Block a user