Skip to content

Commit

Permalink
added slices to DateTime conversion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alanisaac committed Feb 26, 2020
1 parent 7a6e0cb commit 823f147
Showing 1 changed file with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -904,53 +904,75 @@ public static void TestMultiSegmentStringConversionToDateTime()
{
string jsonString = "\"1997-07-16\"";
string expectedString = "1997-07-16";
int expectedTokenLength = 10;
DateTime expectedDateTime = DateTime.Parse(expectedString);

byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
byte[] utf8 = Encoding.UTF8.GetBytes(jsonString);

ReadOnlySequence<byte> sequence = JsonTestHelper.CreateSegments(dataUtf8);
ReadOnlySequence<byte> sequence = JsonTestHelper.CreateSegments(utf8);

var json = new Utf8JsonReader(sequence, isFinalBlock: false, state: default);
while (json.Read())
for (int j = 0; j < utf8.Length; j++)
{
if (json.TokenType == JsonTokenType.String)
{
DateTime expected = DateTime.Parse(expectedString);
var utf8JsonReader = new Utf8JsonReader(sequence.Slice(0, j), isFinalBlock: false, default);
ReadDateTimeHelper(ref utf8JsonReader, expectedDateTime, expectedTokenLength);

Assert.True(json.TryGetDateTime(out DateTime actual));
Assert.Equal(expected, actual);
Assert.Equal(0, utf8JsonReader.TokenStartIndex);

Assert.Equal(expected, json.GetDateTime());
}
long consumed = utf8JsonReader.BytesConsumed;
utf8JsonReader = new Utf8JsonReader(sequence.Slice(consumed), isFinalBlock: true, utf8JsonReader.CurrentState);
ReadDateTimeHelper(ref utf8JsonReader, expectedDateTime, expectedTokenLength);
}
}

Assert.Equal(dataUtf8.Length, json.BytesConsumed);
private static void ReadDateTimeHelper(ref Utf8JsonReader jsonReader, DateTime expectedValue, long expectedTokenLength)
{
while (jsonReader.Read())
{
if (jsonReader.TokenType == JsonTokenType.String)
{
long tokenLength = jsonReader.HasValueSequence ? jsonReader.ValueSequence.Length : jsonReader.ValueSpan.Length;
Assert.Equal(expectedTokenLength, tokenLength);
Assert.Equal(expectedValue, jsonReader.GetDateTime());
}
}
}

[Fact]
public static void TestMultiSegmentStringConversionToDateTimeOffset()
{
string jsonString = "\"1997-07-16\"";
string expectedString = "1997-07-16";
int expectedTokenLength = 10;
DateTimeOffset expectedDateTimeOffset = DateTimeOffset.Parse(expectedString);

byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
byte[] utf8 = Encoding.UTF8.GetBytes(jsonString);

ReadOnlySequence<byte> sequence = JsonTestHelper.CreateSegments(dataUtf8);
ReadOnlySequence<byte> sequence = JsonTestHelper.CreateSegments(utf8);

var json = new Utf8JsonReader(sequence, isFinalBlock: false, state: default);
while (json.Read())
for (int j = 0; j < utf8.Length; j++)
{
if (json.TokenType == JsonTokenType.String)
{
DateTimeOffset expected = DateTimeOffset.Parse(expectedString);
var utf8JsonReader = new Utf8JsonReader(sequence.Slice(0, j), isFinalBlock: false, default);
ReadDateTimeOffsetHelper(ref utf8JsonReader, expectedDateTimeOffset, expectedTokenLength);

Assert.True(json.TryGetDateTimeOffset(out DateTimeOffset actual));
Assert.Equal(expected, actual);
Assert.Equal(0, utf8JsonReader.TokenStartIndex);

Assert.Equal(expected, json.GetDateTimeOffset());
}
long consumed = utf8JsonReader.BytesConsumed;
utf8JsonReader = new Utf8JsonReader(sequence.Slice(consumed), isFinalBlock: true, utf8JsonReader.CurrentState);
ReadDateTimeOffsetHelper(ref utf8JsonReader, expectedDateTimeOffset, expectedTokenLength);
}
}

Assert.Equal(dataUtf8.Length, json.BytesConsumed);
private static void ReadDateTimeOffsetHelper(ref Utf8JsonReader jsonReader, DateTimeOffset expectedValue, long expectedTokenLength)
{
while (jsonReader.Read())
{
if (jsonReader.TokenType == JsonTokenType.String)
{
long tokenLength = jsonReader.HasValueSequence ? jsonReader.ValueSequence.Length : jsonReader.ValueSpan.Length;
Assert.Equal(expectedTokenLength, tokenLength);
Assert.Equal(expectedValue, jsonReader.GetDateTimeOffset());
}
}
}

private static void SpanSequenceStatesAreEqualInvalidJson(byte[] dataUtf8, ReadOnlySequence<byte> sequence, int maxDepth, JsonCommentHandling commentHandling)
Expand Down

0 comments on commit 823f147

Please sign in to comment.