diff --git a/src/AzureMapsControl.Components/Atlas/Expression.cs b/src/AzureMapsControl.Components/Atlas/Expression.cs index 4ec7b29..a487461 100644 --- a/src/AzureMapsControl.Components/Atlas/Expression.cs +++ b/src/AzureMapsControl.Components/Atlas/Expression.cs @@ -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) diff --git a/tests/AzureMapsControl.Components.Tests/Animations/Options/Easing.cs b/tests/AzureMapsControl.Components.Tests/Animations/Options/Easing.cs index 3477c59..aae91b4 100644 --- a/tests/AzureMapsControl.Components.Tests/Animations/Options/Easing.cs +++ b/tests/AzureMapsControl.Components.Tests/Animations/Options/Easing.cs @@ -1,5 +1,6 @@ namespace AzureMapsControl.Components.Tests.Animations.Options { + using AzureMapsControl.Components.Animations.Options; using Xunit; @@ -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); diff --git a/tests/AzureMapsControl.Components.Tests/Atlas/Expression.cs b/tests/AzureMapsControl.Components.Tests/Atlas/Expression.cs new file mode 100644 index 0000000..d5d15b7 --- /dev/null +++ b/tests/AzureMapsControl.Components.Tests/Atlas/Expression.cs @@ -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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(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(); + 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()); + var buffer = new ArrayBufferWriter(); + 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(expectedBytes); + var writterSet = new System.Collections.Generic.HashSet(buffer.WrittenSpan.ToArray()); + + Assert.Equal(expectedBytes.Length, buffer.WrittenCount); + Assert.Subset(expectedBytesSet, writterSet); + } + } +}