Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
habbes committed Aug 7, 2023
1 parent 1b74100 commit 7dded2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/Microsoft.OData.Core/Json/JsonValueUtilsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ private static Task WriteQuotedAsync(this TextWriter writer, string text)
/// Writes an escaped string to the buffer.
/// </summary>
/// <param name="writer">The text writer to write the output.</param>

/// <param name="inputString">Input string value.</param>
/// <param name="currentIndex">The index in the string at which copying should begin.</param>
/// <param name="buffer">Char buffer to use for streaming data.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public JsonValueUtilsAsyncTests()
public async Task WriteEmptyStringShouldWork(ODataStringEscapeOption stringEscapeOption)
{
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Empty, stringEscapeOption, this.buffer);
Assert.Equal("\"\"", await this.StreamToString());
Assert.Equal("\"\"", await this.StreamToStringAsync());
}

[Theory]
Expand All @@ -68,7 +68,7 @@ public async Task WriteEmptyStringShouldWork(ODataStringEscapeOption stringEscap
public async Task WriteNonSpecialCharactersShouldWork(ODataStringEscapeOption stringEscapeOption)
{
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "abcdefg123", stringEscapeOption, this.buffer);
Assert.Equal("\"abcdefg123\"", await this.StreamToString());
Assert.Equal("\"abcdefg123\"", await this.StreamToStringAsync());
}

[Theory]
Expand All @@ -77,21 +77,21 @@ public async Task WriteNonSpecialCharactersShouldWork(ODataStringEscapeOption st
public async Task WriteLowSpecialCharactersShouldWorkForEscapeOption(ODataStringEscapeOption stringEscapeOption)
{
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "cA_\n\r\b", stringEscapeOption, this.buffer);
Assert.Equal("\"cA_\\n\\r\\b\"", await this.StreamToString());
Assert.Equal("\"cA_\\n\\r\\b\"", await this.StreamToStringAsync());
}

[Fact]
public async Task WriteHighSpecialCharactersShouldWorkForEscapeNonAsciiOption()
{
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "cA_Россия", ODataStringEscapeOption.EscapeNonAscii, this.buffer);
Assert.Equal("\"cA_\\u0420\\u043e\\u0441\\u0441\\u0438\\u044f\"", await this.StreamToString());
Assert.Equal("\"cA_\\u0420\\u043e\\u0441\\u0441\\u0438\\u044f\"", await this.StreamToStringAsync());
}

[Fact]
public async Task WriteHighSpecialCharactersShouldWorkForEscapeOnlyControlsOption()
{
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "cA_Россия", ODataStringEscapeOption.EscapeOnlyControls, this.buffer);
Assert.Equal("\"cA_Россия\"", await this.StreamToString());
Assert.Equal("\"cA_Россия\"", await this.StreamToStringAsync());
}

[Fact]
Expand All @@ -102,7 +102,7 @@ public async Task WriteSpecialCharactersShouldWork()
this.Reset();
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("{0}", specialChar),
ODataStringEscapeOption.EscapeNonAscii, this.buffer);
Assert.Equal(string.Format("\"{0}\"", this.escapedCharMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"{0}\"", this.escapedCharMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -114,7 +114,7 @@ public async Task WriteSpecialCharactersAtStartOfStringShouldWork()
this.Reset();
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("{0}MiddleEnd", specialChar),
ODataStringEscapeOption.EscapeNonAscii, this.buffer);
Assert.Equal(string.Format("\"{0}MiddleEnd\"", this.escapedCharMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"{0}MiddleEnd\"", this.escapedCharMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -126,7 +126,7 @@ public async Task WriteSpecialCharactersAtMiddleOfStringShouldWork()
this.Reset();
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("Start{0}End", specialChar),
ODataStringEscapeOption.EscapeNonAscii, this.buffer);
Assert.Equal(string.Format("\"Start{0}End\"", this.escapedCharMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"Start{0}End\"", this.escapedCharMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -138,7 +138,7 @@ public async Task WriteSpecialCharactersAtEndOfStringShouldWork()
this.Reset();
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("StartMiddle{0}", specialChar),
ODataStringEscapeOption.EscapeNonAscii, this.buffer);
Assert.Equal(string.Format("\"StartMiddle{0}\"", this.escapedCharMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"StartMiddle{0}\"", this.escapedCharMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -150,7 +150,7 @@ public async Task WriteMultipleSpecialCharactersShouldWork()
this.Reset();
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("{0}Start{0}Middle{0}End", specialChar),
ODataStringEscapeOption.EscapeNonAscii, this.buffer);
Assert.Equal(string.Format("\"{0}Start{0}Middle{0}End\"", this.escapedCharMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"{0}Start{0}Middle{0}End\"", this.escapedCharMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -163,7 +163,7 @@ public async Task WriteSpecialCharactersAtStartOfBufferLengthShouldWork()
Ref<char[]> charBuffer = new Ref<char[]>(new char[10]);
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("StartMiddle{0}End", specialChar),
ODataStringEscapeOption.EscapeNonAscii, charBuffer);
Assert.Equal(string.Format("\"StartMiddle{0}End\"", this.escapedCharMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"StartMiddle{0}End\"", this.escapedCharMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -176,7 +176,7 @@ public async Task WriteMultipleSpecialCharactersAtEndOfBufferLengthShouldWork()
Ref<char[]> charBuffer = new Ref<char[]>(new char[6]);
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("Start{0}Middle{0}End{0}", specialChar),
ODataStringEscapeOption.EscapeNonAscii, charBuffer);
Assert.Equal(string.Format("\"Start{0}Middle{0}End{0}\"", this.escapedCharMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"Start{0}Middle{0}End{0}\"", this.escapedCharMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -189,7 +189,7 @@ public async Task WriteSpecialCharactersAtEndOfBufferLengthShouldWork()
Ref<char[]> charBuffer = new Ref<char[]>(new char[6]);
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("Start{0}", specialChar),
ODataStringEscapeOption.EscapeNonAscii, charBuffer);
Assert.Equal(string.Format("\"Start{0}\"", this.escapedCharMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"Start{0}\"", this.escapedCharMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -204,7 +204,7 @@ public async Task WriteControllCharactersWithStringEscapeOptionShouldWork(ODataS
Ref<char[]> charBuffer = new Ref<char[]>(new char[6]);
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, string.Format("S{0}M{0}E{0}", specialChar),
escapeOption, charBuffer);
Assert.Equal(string.Format("\"S{0}M{0}E{0}\"", this.controlCharsMap[specialChar]), await this.StreamToString());
Assert.Equal(string.Format("\"S{0}M{0}E{0}\"", this.controlCharsMap[specialChar]), await this.StreamToStringAsync());
}
}

Expand All @@ -217,7 +217,7 @@ public async Task WriteLongEscapedStringShouldWork()
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, value,
ODataStringEscapeOption.EscapeNonAscii, charBuffer);
string expected = "\"" + string.Join("\\t", Enumerable.Repeat("\\n", 95000)) + "\"";
Assert.Equal(expected, await this.StreamToString());
Assert.Equal(expected, await this.StreamToStringAsync());
}

[Fact]
Expand All @@ -234,7 +234,7 @@ await JsonValueUtils.WriteEscapedCharArrayAsync(
charBuffer,
null);
string expected = string.Join("\\t", Enumerable.Repeat("\\n", 95000));
Assert.Equal(expected, await this.StreamToString());
Assert.Equal(expected, await this.StreamToStringAsync());
}

[Theory]
Expand All @@ -245,7 +245,7 @@ public async Task WriteStringWithNoSpecialCharShouldLeaveBufferUntouched(ODataSt
this.Reset();
Ref<char[]> charBuffer = new Ref<char[]>(null);
await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "StartMiddleEnd", stringEscapeOption, charBuffer);
Assert.Equal("\"StartMiddleEnd\"", await this.StreamToString());
Assert.Equal("\"StartMiddleEnd\"", await this.StreamToStringAsync());
Assert.Null(charBuffer.Value);
}

Expand All @@ -261,15 +261,15 @@ public async Task WriteStringShouldIgnoreExistingContentsOfTheBuffer(ODataString
}

await JsonValueUtils.WriteEscapedJsonStringAsync(this.writer, "StartVeryVeryLongMiddleEnd", stringEscapeOption, charBuffer);
Assert.Equal("\"StartVeryVeryLongMiddleEnd\"", await this.StreamToString());
Assert.Equal("\"StartVeryVeryLongMiddleEnd\"", await this.StreamToStringAsync());
}

[Fact]
public async Task WriteByteShouldWork()
{
var byteArray = new byte[] { 1, 2, 3 };
await JsonValueUtils.WriteValueAsync(this.writer, byteArray, this.buffer);
Assert.Equal("\"AQID\"", await this.StreamToString());
Assert.Equal("\"AQID\"", await this.StreamToStringAsync());
}

[Fact]
Expand All @@ -284,7 +284,7 @@ public async Task WriteLongBytesShouldWork()

// Act, Get Base64 string from OData library
await JsonValueUtils.WriteValueAsync(this.writer, byteArray, this.buffer);
string convertedBase64String = await this.StreamToString();
string convertedBase64String = await this.StreamToStringAsync();

// Get Base64 string directly calling the converter.
string actualBase64String = JsonConstants.QuoteCharacter + Convert.ToBase64String(byteArray) + JsonConstants.QuoteCharacter;
Expand Down Expand Up @@ -313,7 +313,7 @@ public async Task WriteBytesLengthSameAsBufferSizeShouldWork()

// Act, Get Base64 string from OData library
await JsonValueUtils.WriteValueAsync(this.writer, byteArray, this.buffer);
string convertedBase64String = await this.StreamToString();
string convertedBase64String = await this.StreamToStringAsync();

// Get Base64 string directly calling the converter.
string actualBase64String = JsonConstants.QuoteCharacter + Convert.ToBase64String(byteArray) + JsonConstants.QuoteCharacter;
Expand All @@ -328,14 +328,14 @@ public async Task WriteEmptyByteShouldWork()
{
var byteArray = new byte[] { };
await JsonValueUtils.WriteValueAsync(this.writer, byteArray, this.buffer);
Assert.Equal("\"\"", await this.StreamToString());
Assert.Equal("\"\"", await this.StreamToStringAsync());
}

[Fact]
public async Task WriteNullByteShouldWork()
{
await JsonValueUtils.WriteValueAsync(this.writer, (byte[])null, this.buffer);
Assert.Equal("null", await this.StreamToString());
Assert.Equal("null", await this.StreamToStringAsync());
}

private void Reset()
Expand All @@ -346,7 +346,7 @@ private void Reset()
this.writer = new NonIndentedTextWriter(innerWriter);
}

private async Task<string> StreamToString()
private async Task<string> StreamToStringAsync()
{
await this.writer.FlushAsync();
this.stream.Position = 0;
Expand Down

0 comments on commit 7dded2f

Please sign in to comment.