-Code reformat

This commit is contained in:
James Newton-King
2013-10-20 15:08:39 +13:00
parent d03963016e
commit bcd6982419
466 changed files with 71645 additions and 71598 deletions
@@ -39,188 +39,188 @@ using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Tests.Utilities
{
[TestFixture]
public class DateTimeUtilsTests : TestFixtureBase
{
[Test]
public void RoundTripDateTimeMinAndMax()
[TestFixture]
public class DateTimeUtilsTests : TestFixtureBase
{
RoundtripDateIso(DateTime.MinValue);
RoundtripDateIso(DateTime.MaxValue);
}
[Test]
public void RoundTripDateTimeMinAndMax()
{
RoundtripDateIso(DateTime.MinValue);
RoundtripDateIso(DateTime.MaxValue);
}
private static void RoundtripDateIso(DateTime value)
{
StringWriter sw = new StringWriter();
DateTimeUtils.WriteDateTimeString(sw, value, DateFormatHandling.IsoDateFormat, null, CultureInfo.InvariantCulture);
string minDateText = sw.ToString();
private static void RoundtripDateIso(DateTime value)
{
StringWriter sw = new StringWriter();
DateTimeUtils.WriteDateTimeString(sw, value, DateFormatHandling.IsoDateFormat, null, CultureInfo.InvariantCulture);
string minDateText = sw.ToString();
object dt;
DateTimeUtils.TryParseDateIso(minDateText, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out dt);
object dt;
DateTimeUtils.TryParseDateIso(minDateText, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out dt);
DateTime parsedDt = (DateTime) dt;
Assert.AreEqual(value, parsedDt);
}
DateTime parsedDt = (DateTime)dt;
Assert.AreEqual(value, parsedDt);
}
[Test]
public void NewDateTimeParse()
{
AssertNewDateTimeParseEqual("999x-12-31T23:59:59");
AssertNewDateTimeParseEqual("9999x12-31T23:59:59");
AssertNewDateTimeParseEqual("9999-1x-31T23:59:59");
AssertNewDateTimeParseEqual("9999-12x31T23:59:59");
AssertNewDateTimeParseEqual("9999-12-3xT23:59:59");
AssertNewDateTimeParseEqual("9999-12-31x23:59:59");
AssertNewDateTimeParseEqual("9999-12-31T2x:59:59");
AssertNewDateTimeParseEqual("9999-12-31T23x59:59");
AssertNewDateTimeParseEqual("9999-12-31T23:5x:59");
AssertNewDateTimeParseEqual("9999-12-31T23:59x59");
AssertNewDateTimeParseEqual("9999-12-31T23:59:5x");
AssertNewDateTimeParseEqual("9999-12-31T23:59:5");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.x");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.99999999");
//AssertNewDateTimeParseEqual("9999-12-31T23:59:59.", null); // DateTime.TryParse is bugged and should return null
[Test]
public void NewDateTimeParse()
{
AssertNewDateTimeParseEqual("999x-12-31T23:59:59");
AssertNewDateTimeParseEqual("9999x12-31T23:59:59");
AssertNewDateTimeParseEqual("9999-1x-31T23:59:59");
AssertNewDateTimeParseEqual("9999-12x31T23:59:59");
AssertNewDateTimeParseEqual("9999-12-3xT23:59:59");
AssertNewDateTimeParseEqual("9999-12-31x23:59:59");
AssertNewDateTimeParseEqual("9999-12-31T2x:59:59");
AssertNewDateTimeParseEqual("9999-12-31T23x59:59");
AssertNewDateTimeParseEqual("9999-12-31T23:5x:59");
AssertNewDateTimeParseEqual("9999-12-31T23:59x59");
AssertNewDateTimeParseEqual("9999-12-31T23:59:5x");
AssertNewDateTimeParseEqual("9999-12-31T23:59:5");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.x");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.99999999");
//AssertNewDateTimeParseEqual("9999-12-31T23:59:59.", null); // DateTime.TryParse is bugged and should return null
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055Z");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055+00:00");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055+23:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055-23:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055+11:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055-11:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055Z");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055+00:00");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055+23:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055-23:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055+11:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03.055-11:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03Z");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03+00:00");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03+23:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03-23:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03+11:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03-11:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03Z");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03+00:00");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03+23:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03-23:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03+11:30");
AssertNewDateTimeParseEqual("2000-12-15T22:11:03-11:30");
AssertNewDateTimeParseEqual("0001-01-01T00:00:00Z");
AssertNewDateTimeParseEqual("0001-01-01T00:00:00"); // this is DateTime.MinDate
//AssertNewDateTimeParseEqual("0001-01-01T00:00:00+00:00"); // when the timezone is negative then this breaks
//AssertNewDateTimeParseEqual("0001-01-01T00:00:00+23:30"); // I don't know why a different value is returned from DateTime.TryParse, I think it is a bug in .NET
AssertNewDateTimeParseEqual("0001-01-01T00:00:00-23:30");
//AssertNewDateTimeParseEqual("0001-01-01T00:00:00+11:30"); // when the timezone is negative then this breaks
AssertNewDateTimeParseEqual("0001-01-01T00:00:00-12:00");
AssertNewDateTimeParseEqual("0001-01-01T00:00:00Z");
AssertNewDateTimeParseEqual("0001-01-01T00:00:00"); // this is DateTime.MinDate
//AssertNewDateTimeParseEqual("0001-01-01T00:00:00+00:00"); // when the timezone is negative then this breaks
//AssertNewDateTimeParseEqual("0001-01-01T00:00:00+23:30"); // I don't know why a different value is returned from DateTime.TryParse, I think it is a bug in .NET
AssertNewDateTimeParseEqual("0001-01-01T00:00:00-23:30");
//AssertNewDateTimeParseEqual("0001-01-01T00:00:00+11:30"); // when the timezone is negative then this breaks
AssertNewDateTimeParseEqual("0001-01-01T00:00:00-12:00");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999Z");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999"); // this is DateTime.MaxDate
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999+00:00", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate in some timezones
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999+23:30");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999-23:30", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate in some timezones
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999+11:30", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate in some timezones
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999-11:30", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate in some timezones
}
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999Z");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999"); // this is DateTime.MaxDate
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999+00:00", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate in some timezones
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999+23:30");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999-23:30", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate in some timezones
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999+11:30", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate in some timezones
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999-11:30", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate in some timezones
}
private void AssertNewDateTimeParseEqual(string text, object oldDate)
{
object oldDt;
if (TryParseDateIso(text, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out oldDt))
{
oldDate = oldDt;
}
private void AssertNewDateTimeParseEqual(string text, object oldDate)
{
object oldDt;
if (TryParseDateIso(text, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out oldDt))
{
oldDate = oldDt;
}
object newDt;
DateTimeUtils.TryParseDateIso(text, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out newDt);
object newDt;
DateTimeUtils.TryParseDateIso(text, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out newDt);
if (!Equals(oldDate, newDt))
{
Assert.AreEqual(oldDate, newDt, "DateTime parse not equal. Text: '{0}' Old ticks: {1} New ticks: {2}".FormatWith(
CultureInfo.InvariantCulture,
text,
oldDate != null ? ((DateTime)oldDate).Ticks : (long?)null,
newDt != null ? ((DateTime)newDt).Ticks : (long?)null
));
}
}
if (!Equals(oldDate, newDt))
{
Assert.AreEqual(oldDate, newDt, "DateTime parse not equal. Text: '{0}' Old ticks: {1} New ticks: {2}".FormatWith(
CultureInfo.InvariantCulture,
text,
oldDate != null ? ((DateTime)oldDate).Ticks : (long?)null,
newDt != null ? ((DateTime)newDt).Ticks : (long?)null
));
}
}
private void AssertNewDateTimeParseEqual(string text)
{
object oldDt;
TryParseDateIso(text, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out oldDt);
private void AssertNewDateTimeParseEqual(string text)
{
object oldDt;
TryParseDateIso(text, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out oldDt);
AssertNewDateTimeParseEqual(text, oldDt);
}
AssertNewDateTimeParseEqual(text, oldDt);
}
#if !NET20
[Test]
public void NewDateTimeOffsetParse()
{
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00");
[Test]
public void NewDateTimeOffsetParse()
{
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055Z");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055+00:00");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055+13:30");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055-13:30");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055Z");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055+00:00");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055+13:30");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03.055-13:30");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03Z");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03+00:00");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03+13:30");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03-13:30");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03Z");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03+00:00");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03+13:30");
AssertNewDateTimeOffsetParseEqual("2000-12-15T22:11:03-13:30");
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00Z");
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00+00:00");
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00+13:30");
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00-13:30");
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00Z");
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00+00:00");
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00+13:30");
AssertNewDateTimeOffsetParseEqual("0001-01-01T00:00:00-13:30");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999Z");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999+00:00");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999+13:30");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999-13:30");
}
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999Z");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999+00:00");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999+13:30");
AssertNewDateTimeOffsetParseEqual("9999-12-31T23:59:59.9999999-13:30");
}
private void AssertNewDateTimeOffsetParseEqual(string text)
{
object oldDt;
object newDt;
TryParseDateIso(text, DateParseHandling.DateTimeOffset, DateTimeZoneHandling.Unspecified, out oldDt);
DateTimeUtils.TryParseDateIso(text, DateParseHandling.DateTimeOffset, DateTimeZoneHandling.Unspecified, out newDt);
private void AssertNewDateTimeOffsetParseEqual(string text)
{
object oldDt;
object newDt;
TryParseDateIso(text, DateParseHandling.DateTimeOffset, DateTimeZoneHandling.Unspecified, out oldDt);
DateTimeUtils.TryParseDateIso(text, DateParseHandling.DateTimeOffset, DateTimeZoneHandling.Unspecified, out newDt);
if (!Equals(oldDt, newDt))
{
Assert.AreEqual(oldDt, newDt, "DateTimeOffset parse not equal. Text: '{0}' Old ticks: {1} New ticks: {2}".FormatWith(
CultureInfo.InvariantCulture,
text,
((DateTime)oldDt).Ticks,
((DateTime)newDt).Ticks));
}
}
if (!Equals(oldDt, newDt))
{
Assert.AreEqual(oldDt, newDt, "DateTimeOffset parse not equal. Text: '{0}' Old ticks: {1} New ticks: {2}".FormatWith(
CultureInfo.InvariantCulture,
text,
((DateTime)oldDt).Ticks,
((DateTime)newDt).Ticks));
}
}
#endif
internal static bool TryParseDateIso(string text, DateParseHandling dateParseHandling, DateTimeZoneHandling dateTimeZoneHandling, out object dt)
{
const string isoDateFormat = "yyyy-MM-ddTHH:mm:ss.FFFFFFFK";
internal static bool TryParseDateIso(string text, DateParseHandling dateParseHandling, DateTimeZoneHandling dateTimeZoneHandling, out object dt)
{
const string isoDateFormat = "yyyy-MM-ddTHH:mm:ss.FFFFFFFK";
#if !NET20
if (dateParseHandling == DateParseHandling.DateTimeOffset)
{
DateTimeOffset dateTimeOffset;
if (DateTimeOffset.TryParseExact(text, isoDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset))
{
dt = dateTimeOffset;
return true;
}
}
else
if (dateParseHandling == DateParseHandling.DateTimeOffset)
{
DateTimeOffset dateTimeOffset;
if (DateTimeOffset.TryParseExact(text, isoDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset))
{
dt = dateTimeOffset;
return true;
}
}
else
#endif
{
DateTime dateTime;
if (DateTime.TryParseExact(text, isoDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTime))
{
dateTime = DateTimeUtils.EnsureDateTime(dateTime, dateTimeZoneHandling);
{
DateTime dateTime;
if (DateTime.TryParseExact(text, isoDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTime))
{
dateTime = DateTimeUtils.EnsureDateTime(dateTime, dateTimeZoneHandling);
dt = dateTime;
return true;
dt = dateTime;
return true;
}
}
dt = null;
return false;
}
}
dt = null;
return false;
}
}
}
@@ -41,92 +41,93 @@ using Newtonsoft.Json.Tests.Serialization;
namespace Newtonsoft.Json.Tests.Utilities
{
[TestFixture]
public class DynamicReflectionDelegateFactoryTests : TestFixtureBase
{
[Test]
public void CreateGetWithBadObjectTarget()
[TestFixture]
public class DynamicReflectionDelegateFactoryTests : TestFixtureBase
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
p.Name = "Hi";
[Test]
public void CreateGetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
p.Name = "Hi";
Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
setter(p);
});
setter(p);
});
}
[Test]
public void CreateSetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
Movie m = new Movie();
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, "Hi");
Assert.AreEqual(m.Name, "Hi");
setter(p, "Hi");
Assert.AreEqual(p.Name, "Hi");
});
}
[Test]
public void CreateSetWithBadTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Specified cast is not valid.",
() =>
{
object structTest = new StructTest();
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
setter(structTest, "Hi");
Assert.AreEqual("Hi", ((StructTest)structTest).StringProperty);
setter(new TimeSpan(), "Hi");
});
}
[Test]
public void CreateSetWithBadObjectValue()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.",
() =>
{
Movie m = new Movie();
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, new Version("1.1.1.1"));
});
}
[Test]
public void CreateStaticMethodCall()
{
MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) });
Assert.IsNotNull(castMethodInfo);
MethodCall<object, object> call = DynamicReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo);
object result = call(null, "First!");
Assert.IsNotNull(result);
JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey)result;
Assert.AreEqual("First!", key.Value);
}
}
[Test]
public void CreateSetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
Movie m = new Movie();
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, "Hi");
Assert.AreEqual(m.Name, "Hi");
setter(p, "Hi");
Assert.AreEqual(p.Name, "Hi");
});
}
[Test]
public void CreateSetWithBadTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Specified cast is not valid.",
() =>
{
object structTest = new StructTest();
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
setter(structTest, "Hi");
Assert.AreEqual("Hi", ((StructTest)structTest).StringProperty);
setter(new TimeSpan(), "Hi");
});
}
[Test]
public void CreateSetWithBadObjectValue()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.",
() =>
{
Movie m = new Movie();
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, new Version("1.1.1.1"));
});
}
[Test]
public void CreateStaticMethodCall()
{
MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) });
Assert.IsNotNull(castMethodInfo);
MethodCall<object, object> call = DynamicReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo);
object result = call(null, "First!");
Assert.IsNotNull(result);
JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey) result;
Assert.AreEqual("First!", key.Value);
}
}
}
#endif
@@ -41,262 +41,263 @@ using Newtonsoft.Json.Tests.Serialization;
namespace Newtonsoft.Json.Tests.Utilities
{
[TestFixture]
public class ExpressionReflectionDelegateFactoryTests : TestFixtureBase
{
[Test]
public void DefaultConstructor()
[TestFixture]
public class ExpressionReflectionDelegateFactoryTests : TestFixtureBase
{
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Movie));
[Test]
public void DefaultConstructor()
{
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Movie));
Movie m = (Movie)create();
Assert.IsNotNull(m);
Movie m = (Movie)create();
Assert.IsNotNull(m);
}
[Test]
public void DefaultConstructor_Struct()
{
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(StructTest));
StructTest m = (StructTest)create();
Assert.IsNotNull(m);
}
[Test]
public void DefaultConstructor_Abstract()
{
ExceptionAssert.Throws<Exception>("Cannot create an abstract class.",
() =>
{
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Type));
create();
});
}
[Test]
public void CreatePropertySetter()
{
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
Movie m = new Movie();
setter(m, "OH HAI!");
Assert.AreEqual("OH HAI!", m.Name);
}
[Test]
public void CreatePropertyGetter()
{
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
Movie m = new Movie();
m.Name = "OH HAI!";
object value = getter(m);
Assert.AreEqual("OH HAI!", value);
}
[Test]
public void CreateMethodCall()
{
MethodCall<object, object> method = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(typeof(Movie).GetMethod("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"));
result = method(m, m);
Assert.AreEqual(true, result);
}
[Test]
public void CreateMethodCall_Constructor()
{
MethodCall<object, object> method = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(typeof(Movie).GetConstructor(new Type[0]));
object result = method(null);
Assert.IsTrue(result is Movie);
}
public static class StaticTestClass
{
public static string StringField;
public static string StringProperty { get; set; }
}
[Test]
public void GetStatic()
{
StaticTestClass.StringField = "Field!";
StaticTestClass.StringProperty = "Property!";
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(StaticTestClass).GetProperty("StringProperty"));
object v = getter(null);
Assert.AreEqual(StaticTestClass.StringProperty, v);
getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(StaticTestClass).GetField("StringField"));
v = getter(null);
Assert.AreEqual(StaticTestClass.StringField, v);
}
[Test]
public void SetStatic()
{
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StaticTestClass).GetProperty("StringProperty"));
setter(null, "New property!");
Assert.AreEqual("New property!", StaticTestClass.StringProperty);
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StaticTestClass).GetField("StringField"));
setter(null, "New field!");
Assert.AreEqual("New field!", StaticTestClass.StringField);
}
public class FieldsTestClass
{
public string StringField;
public bool BoolField;
public readonly int IntReadOnlyField = int.MaxValue;
}
[Test]
public void CreateGetField()
{
FieldsTestClass c = new FieldsTestClass
{
BoolField = true,
StringField = "String!"
};
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(FieldsTestClass).GetField("StringField"));
object value = getter(c);
Assert.AreEqual("String!", value);
getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(FieldsTestClass).GetField("BoolField"));
value = getter(c);
Assert.AreEqual(true, value);
}
[Test]
public void CreateSetField_ReadOnly()
{
FieldsTestClass c = new FieldsTestClass();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("IntReadOnlyField"));
setter(c, int.MinValue);
Assert.AreEqual(int.MinValue, c.IntReadOnlyField);
}
[Test]
public void CreateSetField()
{
FieldsTestClass c = new FieldsTestClass();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("StringField"));
setter(c, "String!");
Assert.AreEqual("String!", c.StringField);
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("BoolField"));
setter(c, true);
Assert.AreEqual(true, c.BoolField);
}
[Test]
public void SetOnStruct()
{
object structTest = new StructTest();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
setter(structTest, "Hi1");
Assert.AreEqual("Hi1", ((StructTest)structTest).StringProperty);
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetField("StringField"));
setter(structTest, "Hi2");
Assert.AreEqual("Hi2", ((StructTest)structTest).StringField);
}
[Test]
public void CreateGetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
p.Name = "Hi";
Func<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
setter(p);
});
}
[Test]
public void CreateSetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
Movie m = new Movie();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, "Hi");
Assert.AreEqual(m.Name, "Hi");
setter(p, "Hi");
Assert.AreEqual(p.Name, "Hi");
});
}
[Test]
public void CreateSetWithBadObjectValue()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.",
() =>
{
Movie m = new Movie();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, new Version("1.1.1.1"));
});
}
[Test]
public void CreateStaticMethodCall()
{
MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) });
Assert.IsNotNull(castMethodInfo);
MethodCall<object, object> call = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo);
object result = call(null, "First!");
Assert.IsNotNull(result);
JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey)result;
Assert.AreEqual("First!", key.Value);
}
}
[Test]
public void DefaultConstructor_Struct()
{
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(StructTest));
StructTest m = (StructTest)create();
Assert.IsNotNull(m);
}
[Test]
public void DefaultConstructor_Abstract()
{
ExceptionAssert.Throws<Exception>("Cannot create an abstract class.",
() =>
{
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Type));
create();
});
}
[Test]
public void CreatePropertySetter()
{
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
Movie m = new Movie();
setter(m, "OH HAI!");
Assert.AreEqual("OH HAI!", m.Name);
}
[Test]
public void CreatePropertyGetter()
{
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
Movie m = new Movie();
m.Name = "OH HAI!";
object value = getter(m);
Assert.AreEqual("OH HAI!", value);
}
[Test]
public void CreateMethodCall()
{
MethodCall<object, object> method = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(typeof(Movie).GetMethod("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"));
result = method(m, m);
Assert.AreEqual(true, result);
}
[Test]
public void CreateMethodCall_Constructor()
{
MethodCall<object, object> method = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(typeof(Movie).GetConstructor(new Type[0]));
object result = method(null);
Assert.IsTrue(result is Movie);
}
public static class StaticTestClass
{
public static string StringField;
public static string StringProperty { get; set; }
}
[Test]
public void GetStatic()
{
StaticTestClass.StringField = "Field!";
StaticTestClass.StringProperty = "Property!";
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(StaticTestClass).GetProperty("StringProperty"));
object v = getter(null);
Assert.AreEqual(StaticTestClass.StringProperty, v);
getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(StaticTestClass).GetField("StringField"));
v = getter(null);
Assert.AreEqual(StaticTestClass.StringField, v);
}
[Test]
public void SetStatic()
{
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StaticTestClass).GetProperty("StringProperty"));
setter(null, "New property!");
Assert.AreEqual("New property!", StaticTestClass.StringProperty);
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StaticTestClass).GetField("StringField"));
setter(null, "New field!");
Assert.AreEqual("New field!", StaticTestClass.StringField);
}
public class FieldsTestClass
{
public string StringField;
public bool BoolField;
public readonly int IntReadOnlyField = int.MaxValue;
}
[Test]
public void CreateGetField()
{
FieldsTestClass c = new FieldsTestClass
{
BoolField = true,
StringField = "String!"
};
Func<object, object> getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(FieldsTestClass).GetField("StringField"));
object value = getter(c);
Assert.AreEqual("String!", value);
getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(FieldsTestClass).GetField("BoolField"));
value = getter(c);
Assert.AreEqual(true, value);
}
[Test]
public void CreateSetField_ReadOnly()
{
FieldsTestClass c = new FieldsTestClass();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("IntReadOnlyField"));
setter(c, int.MinValue);
Assert.AreEqual(int.MinValue, c.IntReadOnlyField);
}
[Test]
public void CreateSetField()
{
FieldsTestClass c = new FieldsTestClass();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("StringField"));
setter(c, "String!");
Assert.AreEqual("String!", c.StringField);
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(FieldsTestClass).GetField("BoolField"));
setter(c, true);
Assert.AreEqual(true, c.BoolField);
}
[Test]
public void SetOnStruct()
{
object structTest = new StructTest();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
setter(structTest, "Hi1");
Assert.AreEqual("Hi1", ((StructTest)structTest).StringProperty);
setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetField("StringField"));
setter(structTest, "Hi2");
Assert.AreEqual("Hi2", ((StructTest)structTest).StringField);
}
[Test]
public void CreateGetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
p.Name = "Hi";
Func<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
setter(p);
});
}
[Test]
public void CreateSetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
Movie m = new Movie();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, "Hi");
Assert.AreEqual(m.Name, "Hi");
setter(p, "Hi");
Assert.AreEqual(p.Name, "Hi");
});
}
[Test]
public void CreateSetWithBadObjectValue()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'System.Version' to type 'System.String'.",
() =>
{
Movie m = new Movie();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, new Version("1.1.1.1"));
});
}
[Test]
public void CreateStaticMethodCall()
{
MethodInfo castMethodInfo = typeof(JsonSerializerTest.DictionaryKey).GetMethod("op_Implicit", new[] { typeof(string) });
Assert.IsNotNull(castMethodInfo);
MethodCall<object, object> call = ExpressionReflectionDelegateFactory.Instance.CreateMethodCall<object>(castMethodInfo);
object result = call(null, "First!");
Assert.IsNotNull(result);
JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey) result;
Assert.AreEqual("First!", key.Value);
}
}
}
#endif
@@ -38,26 +38,27 @@ using global::Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Tests.Utilities
{
[TestFixture]
public class ReflectionUtilsTests : TestFixtureBase
{
[Test]
public void GetTypeNameSimpleForGenericTypes()
[TestFixture]
public class ReflectionUtilsTests : TestFixtureBase
{
string typeName;
[Test]
public void GetTypeNameSimpleForGenericTypes()
{
string typeName;
typeName = ReflectionUtils.GetTypeName(typeof(IList<Type>), FormatterAssemblyStyle.Simple, null);
Assert.AreEqual("System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IList<Type>), FormatterAssemblyStyle.Simple, null);
Assert.AreEqual("System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<IList<Type>, IList<Type>>), FormatterAssemblyStyle.Simple, null);
Assert.AreEqual("System.Collections.Generic.IDictionary`2[[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib],[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib]], mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<IList<Type>, IList<Type>>), FormatterAssemblyStyle.Simple, null);
Assert.AreEqual("System.Collections.Generic.IDictionary`2[[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib],[System.Collections.Generic.IList`1[[System.Type, mscorlib]], mscorlib]], mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IList<>), FormatterAssemblyStyle.Simple, null);
Assert.AreEqual("System.Collections.Generic.IList`1, mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IList<>), FormatterAssemblyStyle.Simple, null);
Assert.AreEqual("System.Collections.Generic.IList`1, mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<,>), FormatterAssemblyStyle.Simple, null);
Assert.AreEqual("System.Collections.Generic.IDictionary`2, mscorlib", typeName);
typeName = ReflectionUtils.GetTypeName(typeof(IDictionary<,>), FormatterAssemblyStyle.Simple, null);
Assert.AreEqual("System.Collections.Generic.IDictionary`2, mscorlib", typeName);
}
}
}
}
#endif
@@ -34,23 +34,23 @@ using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Tests.Utilities
{
[TestFixture]
public class StringUtilsTests : TestFixtureBase
{
[Test]
public void ToCamelCaseTest()
[TestFixture]
public class StringUtilsTests : TestFixtureBase
{
Assert.AreEqual("urlValue", StringUtils.ToCamelCase("URLValue"));
Assert.AreEqual("url", StringUtils.ToCamelCase("URL"));
Assert.AreEqual("id", StringUtils.ToCamelCase("ID"));
Assert.AreEqual("i", StringUtils.ToCamelCase("I"));
Assert.AreEqual("", StringUtils.ToCamelCase(""));
Assert.AreEqual(null, StringUtils.ToCamelCase(null));
Assert.AreEqual("iPhone", StringUtils.ToCamelCase("iPhone"));
Assert.AreEqual("person", StringUtils.ToCamelCase("Person"));
Assert.AreEqual("iPhone", StringUtils.ToCamelCase("IPhone"));
Assert.AreEqual("i Phone", StringUtils.ToCamelCase("I Phone"));
Assert.AreEqual(" IPhone", StringUtils.ToCamelCase(" IPhone"));
[Test]
public void ToCamelCaseTest()
{
Assert.AreEqual("urlValue", StringUtils.ToCamelCase("URLValue"));
Assert.AreEqual("url", StringUtils.ToCamelCase("URL"));
Assert.AreEqual("id", StringUtils.ToCamelCase("ID"));
Assert.AreEqual("i", StringUtils.ToCamelCase("I"));
Assert.AreEqual("", StringUtils.ToCamelCase(""));
Assert.AreEqual(null, StringUtils.ToCamelCase(null));
Assert.AreEqual("iPhone", StringUtils.ToCamelCase("iPhone"));
Assert.AreEqual("person", StringUtils.ToCamelCase("Person"));
Assert.AreEqual("iPhone", StringUtils.ToCamelCase("IPhone"));
Assert.AreEqual("i Phone", StringUtils.ToCamelCase("I Phone"));
Assert.AreEqual(" IPhone", StringUtils.ToCamelCase(" IPhone"));
}
}
}
}