-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
299 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
src/cmstar.Tests/Serialization/Json/Contracts/CustomFormatDateTimeContractTests.cs
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
src/cmstar.Tests/Serialization/Json/Contracts/CustomFormatDateTimeOffsetContractTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace cmstar.Serialization.Json.Contracts | ||
{ | ||
[TestFixture] | ||
public class CustomFormatDateTimeOffsetContractTests : DateTimeOffsetContractTests | ||
{ | ||
protected override JsonContract GetContract() | ||
{ | ||
var contract = new CustomFormatDateTimeOffsetContract(); | ||
contract.Format = "yyyy@M@dd HH~~mm~~ss ffffff"; | ||
return contract; | ||
} | ||
|
||
public override void Write() | ||
{ | ||
var date = new DateTime(2013, 1, 25, 12, 26, 33, 123); | ||
var json = DoWrite(date); | ||
Assert.AreEqual("\"2013@1@25 12~~26~~33 123000\"", json); | ||
|
||
date = new DateTime(2012, 3, 15, 6, 25, 35, 152); | ||
json = DoWrite(date); | ||
Assert.AreEqual("\"2012@3@15 06~~25~~35 152000\"", json); | ||
|
||
date = new DateTime(1976, 12, 2, 23, 42, 25, 0); | ||
json = DoWrite(date); | ||
Assert.AreEqual("\"1976@12@02 23~~42~~28 000000\"", json); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/cmstar.Tests/Serialization/Json/Contracts/DateTimeOffsetContractTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using NUnit.Framework; | ||
|
||
namespace cmstar.Serialization.Json.Contracts | ||
{ | ||
[TestFixture] | ||
public class DateTimeOffsetContractTests : ContractTestBase | ||
{ | ||
protected override Type UnderlyingType => typeof(DateTimeOffset); | ||
|
||
protected override bool SupportsNullValue => false; | ||
|
||
[Test] | ||
public virtual void Write() | ||
{ | ||
var date = new DateTimeOffset(2013, 1, 25, 12, 26, 33, 123, TimeSpan.FromHours(0)); | ||
var json = DoWrite(date); | ||
Assert.AreEqual("\"2013-01-25T12:26:33.1230000Z\"", json); | ||
|
||
date = new DateTimeOffset(2012, 3, 15, 6, 25, 35, 152, TimeSpan.FromHours(4)); | ||
json = DoWrite(date); | ||
Assert.AreEqual("\"2012-03-15T06:25:35.1520000+04:00\"", json); | ||
|
||
date = new DateTimeOffset(1976, 12, 2, 23, 42, 25, 765, TimeSpan.FromHours(-6)); | ||
json = DoWrite(date); | ||
Assert.AreEqual("\"1976-12-02T23:42:25.7650000-06:00\"", json); | ||
} | ||
|
||
// The contract can read value in Microsoft JSON datetime format. | ||
[Test] | ||
public void ReadMicrosoftJsonDate() | ||
{ | ||
var result = DoRead("\"\\/Date(1359116793123)\\/\""); | ||
Assert.IsInstanceOf<DateTimeOffset>(result); | ||
var expected = new DateTimeOffset(2013, 1, 25, 12, 26, 33, 123, TimeSpan.FromHours(0)); | ||
Assert.AreEqual(expected, (DateTimeOffset)result); | ||
|
||
result = DoRead("\"/Date(218418145765+0400)/\""); | ||
Assert.IsInstanceOf<DateTimeOffset>(result); | ||
expected = new DateTimeOffset(1976, 12, 3, 3, 42, 25, 765, TimeSpan.FromHours(4)); | ||
Assert.AreEqual(expected, (DateTimeOffset)result); | ||
|
||
result = DoRead("\"/Date(218418145765-0800)/\""); | ||
Assert.IsInstanceOf<DateTimeOffset>(result); | ||
expected = new DateTimeOffset(1976, 12, 2, 15, 42, 25, 765, TimeSpan.FromHours(-8)); | ||
Assert.AreEqual(expected, (DateTimeOffset)result); | ||
} | ||
|
||
[Test] | ||
public void ReadString() | ||
{ | ||
var result = DoRead("\"2013-11-25 18:28:36.213\""); | ||
Assert.IsInstanceOf<DateTimeOffset>(result); | ||
var localTime = DateTime.Parse("2013-11-25 18:28:36.213"); | ||
var expected = new DateTimeOffset(localTime); | ||
Assert.AreEqual(expected, result); | ||
|
||
result = DoRead("\"2013-11-25T18:28:36.213+0000\""); | ||
Assert.IsInstanceOf<DateTimeOffset>(result); | ||
expected = new DateTimeOffset(2013, 11, 25, 18, 28, 36, 213, TimeSpan.FromHours(0)); | ||
Assert.AreEqual(expected, result); | ||
|
||
result = DoRead("\"2013-11-25T18:28:36.213+0300\""); | ||
Assert.IsInstanceOf<DateTimeOffset>(result); | ||
expected = new DateTimeOffset(2013, 11, 25, 18, 28, 36, 213, TimeSpan.FromHours(3)); | ||
Assert.AreEqual(expected, result); | ||
|
||
result = DoRead("\"2013-11-25T18:28:36.213-0600\""); | ||
Assert.IsInstanceOf<DateTimeOffset>(result); | ||
expected = new DateTimeOffset(2013, 11, 25, 18, 28, 36, 213, TimeSpan.FromHours(-6)); | ||
Assert.AreEqual(expected, result); | ||
} | ||
|
||
[Test] | ||
public void ReadIllegal() | ||
{ | ||
Assert.Throws<JsonContractException>(() => DoRead("\"some value that is not a date\"")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
src/cmstar/Serialization/Json/Contracts/CustomFormatDateTimeContract.cs
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/cmstar/Serialization/Json/Contracts/CustomFormatDateTimeOffsetContract.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
|
||
namespace cmstar.Serialization.Json.Contracts | ||
{ | ||
/// <summary> | ||
/// An extension of <see cref="DateTimeOffsetContract"/> that allows to specify | ||
/// the format for serializing the date. | ||
/// </summary> | ||
public class CustomFormatDateTimeOffsetContract : DateTimeOffsetContract | ||
{ | ||
private string _format; | ||
|
||
/// <summary> | ||
/// Gets or sets a value which is used to format the date and time. | ||
/// The format string will be passed to the <see cref="DateTime.ToString()"/> | ||
/// method during the serializing. | ||
/// </summary> | ||
public string Format | ||
{ | ||
get | ||
{ | ||
return _format; | ||
} | ||
set | ||
{ | ||
ArgAssert.NotNull(value, "Format"); | ||
_format = value; | ||
} | ||
} | ||
|
||
protected override string ToStringValue(DateTimeOffset value) | ||
{ | ||
//if _format is null the default format would be used | ||
return value.ToString(_format); | ||
} | ||
} | ||
} |
Oops, something went wrong.