-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests on ExpressionJsonConverter
- Loading branch information
1 parent
e6923a9
commit 8af8f0c
Showing
3 changed files
with
330 additions
and
3 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
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
326 changes: 326 additions & 0 deletions
326
tests/AzureMapsControl.Components.Tests/Atlas/Expression.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,326 @@ | ||
namespace AzureMapsControl.Components.Tests.Atlas | ||
{ | ||
using System; | ||
using System.Buffers; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
using AzureMapsControl.Components.Atlas; | ||
|
||
using Xunit; | ||
|
||
public class ExpressionJsonConverterTests | ||
{ | ||
[Fact] | ||
public void Should_WriteJson() | ||
{ | ||
var json = "[\"get\",\"Confirmed\"]"; | ||
var expression = new Expression(JsonDocument.Parse(json)); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedBytes = Encoding.UTF8.GetBytes(json); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_WriteExpressions() | ||
{ | ||
var json = "[\"get\",\"Confirmed\"]"; | ||
var child = new Expression(JsonDocument.Parse(json)); | ||
var expression = new Expression(new[] { child }); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedJson = "[[\"get\",\"Confirmed\"]]"; | ||
var expectedBytes = Encoding.UTF8.GetBytes(expectedJson); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
} | ||
|
||
public class ExpressionOrNumberJsonConverterTests | ||
{ | ||
[Fact] | ||
public void Should_WriteJson() | ||
{ | ||
var json = "[\"get\",\"Confirmed\"]"; | ||
var expression = new ExpressionOrNumber(JsonDocument.Parse(json)); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrNumberJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedBytes = Encoding.UTF8.GetBytes(json); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_WriteExpressions() | ||
{ | ||
var json = "[\"get\",\"Confirmed\"]"; | ||
var child = new Expression(JsonDocument.Parse(json)); | ||
var expression = new ExpressionOrNumber(new[] { child }); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrNumberJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedJson = "[[\"get\",\"Confirmed\"]]"; | ||
var expectedBytes = Encoding.UTF8.GetBytes(expectedJson); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_WriteNumberValue() | ||
{ | ||
var value = 1; | ||
var expression = new ExpressionOrNumber(value); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrNumberJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedBytes = Encoding.UTF8.GetBytes(value.ToString()); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_NotWriteNumberValue() | ||
{ | ||
double? value = null; | ||
var expression = new ExpressionOrNumber(value); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrNumberJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
Assert.Equal(0, buffer.WrittenCount); | ||
} | ||
} | ||
|
||
public class ExpressionOrStringJsonConverterTests | ||
{ | ||
[Fact] | ||
public void Should_WriteJson() | ||
{ | ||
var json = "[\"get\",\"Confirmed\"]"; | ||
var expression = new ExpressionOrString(JsonDocument.Parse(json)); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrStringJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedBytes = Encoding.UTF8.GetBytes(json); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_WriteExpressions() | ||
{ | ||
var json = "[\"get\",\"Confirmed\"]"; | ||
var child = new Expression(JsonDocument.Parse(json)); | ||
var expression = new ExpressionOrString(new[] { child }); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrStringJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedJson = "[[\"get\",\"Confirmed\"]]"; | ||
var expectedBytes = Encoding.UTF8.GetBytes(expectedJson); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_WriteStringValue() | ||
{ | ||
var value = "value"; | ||
var expression = new ExpressionOrString(value); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrStringJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedBytes = Encoding.UTF8.GetBytes("\"" + value.ToString() + "\""); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
} | ||
|
||
public class ExpressionOrStringArrayConverterTests | ||
{ | ||
[Fact] | ||
public void Should_WriteJson() | ||
{ | ||
var json = "[\"get\",\"Confirmed\"]"; | ||
var expression = new ExpressionOrStringArray(JsonDocument.Parse(json)); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrStringArrayJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedBytes = Encoding.UTF8.GetBytes(json); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_WriteExpressions() | ||
{ | ||
var json = "[\"get\",\"Confirmed\"]"; | ||
var child = new Expression(JsonDocument.Parse(json)); | ||
var expression = new ExpressionOrStringArray(new[] { child }); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrStringArrayJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedJson = "[[\"get\",\"Confirmed\"]]"; | ||
var expectedBytes = Encoding.UTF8.GetBytes(expectedJson); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_WriteStringArray() | ||
{ | ||
var value = "value"; | ||
var expression = new ExpressionOrStringArray(new[] { value }); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrStringArrayJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedBytes = Encoding.UTF8.GetBytes("[\"" + value.ToString() + "\"]"); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
|
||
[Fact] | ||
public void Should_NotWriteStringArray_NullCase() | ||
{ | ||
string[] values = null; | ||
var expression = new ExpressionOrStringArray(values); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrStringArrayJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
Assert.Equal(0, buffer.WrittenCount); | ||
} | ||
|
||
[Fact] | ||
public void Should_WriteStringArray_EmptyCase() | ||
{ | ||
var expression = new ExpressionOrStringArray(Array.Empty<string>()); | ||
var buffer = new ArrayBufferWriter<byte>(); | ||
using var writer = new Utf8JsonWriter(buffer); | ||
|
||
var converter = new ExpressionOrStringArrayJsonConverter(); | ||
converter.Write(writer, expression, null); | ||
|
||
writer.Flush(); | ||
|
||
var expectedBytes = Encoding.UTF8.GetBytes("[]"); | ||
|
||
var expectedBytesSet = new System.Collections.Generic.HashSet<byte>(expectedBytes); | ||
var writterSet = new System.Collections.Generic.HashSet<byte>(buffer.WrittenSpan.ToArray()); | ||
|
||
Assert.Equal(expectedBytes.Length, buffer.WrittenCount); | ||
Assert.Subset(expectedBytesSet, writterSet); | ||
} | ||
} | ||
} |