|
| 1 | +namespace PolylineAlgorithm.Abstraction.Tests; |
| 2 | + |
| 3 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 4 | +using PolylineAlgorithm.Utility; |
| 5 | +using System; |
| 6 | + |
| 7 | +[TestClass] |
| 8 | +public class PolylineDecoderTest { |
| 9 | + private static readonly PolylineDecoder _decoder = new PolylineDecoder(); |
| 10 | + |
| 11 | + public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100], [1_000]]; |
| 12 | + |
| 13 | + public static IEnumerable<(double, double)> NotANumberAndInfinityCoordinates => StaticValueProvider.Invalid.GetNotANumberAndInfinityCoordinates() ; |
| 14 | + |
| 15 | + public static IEnumerable<(double, double)> MinAndMaxCoordinates => StaticValueProvider.Invalid.GetMinAndMaxCoordinates(); |
| 16 | + |
| 17 | + |
| 18 | + [TestMethod] |
| 19 | + public void Constructor_Parameterless_Ok() { |
| 20 | + // Arrange && Act |
| 21 | + var decoder = new PolylineDecoder(); |
| 22 | + |
| 23 | + // Assert |
| 24 | + Assert.IsNotNull(decoder); |
| 25 | + Assert.IsNotNull(decoder.Options); |
| 26 | + } |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public void Constructor_ValidOptions_Ok() { |
| 30 | + // Arrange |
| 31 | + var options = new PolylineEncodingOptions(); |
| 32 | + |
| 33 | + // Act |
| 34 | + var decoder = new PolylineDecoder(options); |
| 35 | + |
| 36 | + // Assert |
| 37 | + Assert.IsNotNull(decoder); |
| 38 | + Assert.AreSame(options, decoder.Options); |
| 39 | + } |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void Encode_NullPolyline_Throws_ArgumentException() { |
| 43 | + // Arrange |
| 44 | + void Encode() => _decoder.Decode(null!); |
| 45 | + |
| 46 | + // Act |
| 47 | + var exception = Assert.ThrowsExactly<ArgumentNullException>(Encode); |
| 48 | + |
| 49 | + // Assert |
| 50 | + Assert.AreEqual("coordinates", exception.ParamName); |
| 51 | + Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message)); |
| 52 | + } |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public void Encode_EmptyPolyline_Throws_ArgumentException() { |
| 56 | + // Arrange |
| 57 | + void Encode() => _decoder.Decode(string.Empty); |
| 58 | + |
| 59 | + // Act |
| 60 | + var exception = Assert.ThrowsExactly<ArgumentException>(Encode); |
| 61 | + |
| 62 | + // Assert |
| 63 | + Assert.AreEqual("coordinates", exception.ParamName); |
| 64 | + Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message)); |
| 65 | + } |
| 66 | + |
| 67 | + [TestMethod] |
| 68 | + public void Encode_WhitespacePolyline_Throws_ArgumentException() { |
| 69 | + // Arrange |
| 70 | + void Encode() => _decoder.Decode(string.Empty); |
| 71 | + |
| 72 | + // Act |
| 73 | + var exception = Assert.ThrowsExactly<ArgumentException>(Encode); |
| 74 | + |
| 75 | + // Assert |
| 76 | + Assert.AreEqual("coordinates", exception.ParamName); |
| 77 | + Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message)); |
| 78 | + } |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + public void Encode_BufferTooSmall_Throws_InternalBufferOverflowException() { |
| 82 | + // Arrange |
| 83 | + PolylineDecoder decoder = new PolylineDecoder(new PolylineEncodingOptions { BufferSize = 12}); |
| 84 | + string polyline = RandomValueProvider.GetPolyline(2); |
| 85 | + |
| 86 | + // Act |
| 87 | + var exception = Assert.ThrowsExactly<InternalBufferOverflowException>(() => decoder.Decode(polyline)); |
| 88 | + |
| 89 | + // Assert |
| 90 | + Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message)); |
| 91 | + } |
| 92 | + |
| 93 | + //[TestMethod] |
| 94 | + //[DynamicData(nameof(NotANumberAndInfinityCoordinates))] |
| 95 | + //public void Encode_NotANumberAndInfinityCoordinate_Throws_ArgumentOutOfRangeException((double, double) coordinate) { |
| 96 | + // // Arrange |
| 97 | + |
| 98 | + // // Act |
| 99 | + // var exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _decoder.Encode([coordinate])); |
| 100 | + |
| 101 | + // // Assert |
| 102 | + // Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message)); |
| 103 | + //} |
| 104 | + |
| 105 | + //[TestMethod] |
| 106 | + //[DynamicData(nameof(MinAndMaxCoordinates))] |
| 107 | + //public void Encode_MinAndMaxCoordinate_Throws_ArgumentOutOfRangeException((double, double) coordinate) { |
| 108 | + // // Arrange |
| 109 | + |
| 110 | + // // Act |
| 111 | + // var exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _decoder.Encode([coordinate])); |
| 112 | + |
| 113 | + // // Assert |
| 114 | + // Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message)); |
| 115 | + //} |
| 116 | + |
| 117 | + //[TestMethod] |
| 118 | + //[DynamicData(nameof(CoordinateCount))] |
| 119 | + //public void Encode_RandomValue_ValidInput_Ok(int count) { |
| 120 | + // // Arrange |
| 121 | + // IEnumerable<(double Latitude, double Longitude)> coordinates = RandomValueProvider.GetCoordinates(count); |
| 122 | + // string expected = RandomValueProvider.GetPolyline(count); |
| 123 | + |
| 124 | + // // Act |
| 125 | + // var result = _decoder.Encode(coordinates); |
| 126 | + |
| 127 | + // // Assert |
| 128 | + // Assert.AreEqual(expected.Length, result.Length); |
| 129 | + // Assert.IsTrue(expected.Equals(result)); |
| 130 | + //} |
| 131 | + |
| 132 | + //[TestMethod] |
| 133 | + //public void Encode_StaticValue_ValidInput_Ok() { |
| 134 | + // // Arrange |
| 135 | + // IEnumerable<(double Latitude, double Longitude)> coordinates = StaticValueProvider.Valid.GetCoordinates(); |
| 136 | + // string expected = StaticValueProvider.Valid.GetPolyline(); |
| 137 | + |
| 138 | + // // Act |
| 139 | + // var result = _decoder.Encode(coordinates); |
| 140 | + |
| 141 | + // // Assert |
| 142 | + // Assert.AreEqual(expected.Length, result.Length); |
| 143 | + // Assert.IsTrue(expected.Equals(result)); |
| 144 | + //} |
| 145 | + |
| 146 | + public class PolylineDecoder : PolylineDecoder<string, (double Latitude, double Longitude)> { |
| 147 | + public PolylineDecoder() |
| 148 | + : base() { } |
| 149 | + |
| 150 | + public PolylineDecoder(PolylineEncodingOptions options) |
| 151 | + : base(options) { } |
| 152 | + |
| 153 | + protected override (double Latitude, double Longitude) CreateCoordinate(double latitude, double longitude) { |
| 154 | + return (latitude, longitude); |
| 155 | + } |
| 156 | + |
| 157 | + protected override ReadOnlyMemory<char> GetReadOnlyMemory(string? polyline) { |
| 158 | + if(string.IsNullOrWhiteSpace(polyline)) { |
| 159 | + throw new ArgumentException(); |
| 160 | + } |
| 161 | + |
| 162 | + return polyline.AsMemory(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments