Skip to content

Commit

Permalink
Add tests on ExpressionJsonConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudleclerc committed Apr 15, 2021
1 parent e6923a9 commit 8af8f0c
Show file tree
Hide file tree
Showing 3 changed files with 330 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/AzureMapsControl.Components/Atlas/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private void WriteExpressionOrNumber(Utf8JsonWriter writer, ExpressionOrNumber v

private void WriteExpressionOrStringArray(Utf8JsonWriter writer, ExpressionOrStringArray value)
{
if (value.Values.Any())
if (value.Values is not null)
{
writer.WriteStartArray();
foreach (var expression in value.Values)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AzureMapsControl.Components.Tests.Animations.Options
{

using AzureMapsControl.Components.Animations.Options;

using Xunit;
Expand Down Expand Up @@ -38,14 +39,14 @@ public class EasingTests
[InlineData("easeInBounce")]
[InlineData("easeOutBounce")]
[InlineData("easeInOutBounce")]
public static void Should_ReturnEasingFromString(string easingType)
public void Should_ReturnEasingFromString(string easingType)
{
var easing = Easing.FromString(easingType);
Assert.Equal(easingType, easing.ToString());
}

[Fact]
public static void Should_ReturnDefaultEasing_IfStringDoesNotMatch()
public void Should_ReturnDefaultEasing_IfStringDoesNotMatch()
{
var easingType = "obviouslyNotAValidOne";
var easing = Easing.FromString(easingType);
Expand Down
326 changes: 326 additions & 0 deletions tests/AzureMapsControl.Components.Tests/Atlas/Expression.cs
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);
}
}
}

0 comments on commit 8af8f0c

Please sign in to comment.