Skip to content

Commit

Permalink
-Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Apr 25, 2013
1 parent 2004377 commit 31a6a87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
47 changes: 37 additions & 10 deletions Src/Newtonsoft.Json.Tests/Utilities/DateTimeUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
#if !NETFX_CORE
using global::NUnit.Framework;
Expand All @@ -41,6 +42,26 @@ namespace Newtonsoft.Json.Tests.Utilities
[TestFixture]
public class DateTimeUtilsTests : TestFixtureBase
{
[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();

object dt;
DateTimeUtils.TryParseDateIso(minDateText, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out dt);

DateTime parsedDt = (DateTime) dt;
Assert.AreEqual(value, parsedDt);
}

[Test]
public void NewDateTimeParse()
{
Expand All @@ -58,7 +79,7 @@ public void NewDateTimeParse()
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("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");
Expand All @@ -77,24 +98,30 @@ public void NewDateTimeParse()
AssertNewDateTimeParseEqual("2000-12-15T22:11:03-11:30");

AssertNewDateTimeParseEqual("0001-01-01T00:00:00Z");
AssertNewDateTimeParseEqual("0001-01-01T00:00:00");
AssertNewDateTimeParseEqual("0001-01-01T00:00:00+00:00");
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");
AssertNewDateTimeParseEqual("0001-01-01T00:00:00-11: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");
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999+00:00", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate
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
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999+11:30", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate
AssertNewDateTimeParseEqual("9999-12-31T23:59:59.9999999-11:30", DateTime.MaxValue); // DateTime.TryParse fails instead of returning MaxDate
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;
}

object newDt;
DateTimeUtils.TryParseDateIso(text, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out newDt);

Expand Down
1 change: 1 addition & 0 deletions Src/Newtonsoft.Json/Newtonsoft.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<DocumentationFile>bin\Debug\Net45\Newtonsoft.Json.xml</DocumentationFile>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>Newtonsoft.Json.ruleset</CodeAnalysisRuleSet>
<UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down

0 comments on commit 31a6a87

Please sign in to comment.