Skip to content

Commit 6d39166

Browse files
committed
added test, updated exception messages, added static values
1 parent 917abd4 commit 6d39166

5 files changed

Lines changed: 67 additions & 34 deletions

File tree

src/PolylineAlgorithm.Abstraction/Properties/ExceptionMessageResource.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PolylineAlgorithm.Abstraction/Properties/ExceptionMessageResource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,7 @@
141141
<data name="ArgumentIsOutOfRangeForSpecifiedType" xml:space="preserve">
142142
<value>Value is out of range for the specified type {0}.</value>
143143
</data>
144+
<data name="ArgumentCannotBeNullEmptyOrWhiteSpaceMessage" xml:space="preserve">
145+
<value>Argument cannot be null -or- empty -or- white space.</value>
146+
</data>
144147
</root>

tests/PolylineAlgorithm.Abstraction.Tests/PolylineAlgorithm.Abstraction.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.*" />
3939
</ItemGroup>
4040

41-
<ItemGroup>
42-
<ProjectReference Include="..\..\src\PolylineAlgorithm.Abstraction\PolylineAlgorithm.Abstraction.csproj" />
43-
<ProjectReference Include="..\..\utilities\PolylineAlgorithm.Utility\PolylineAlgorithm.Utility.csproj" />
44-
</ItemGroup>
41+
<ItemGroup>
42+
<ProjectReference Include="..\..\src\PolylineAlgorithm.Abstraction\PolylineAlgorithm.Abstraction.csproj" />
43+
<ProjectReference Include="..\..\utilities\PolylineAlgorithm.Utility\PolylineAlgorithm.Utility.csproj" />
44+
</ItemGroup>
4545

4646
</Project>

tests/PolylineAlgorithm.Abstraction.Tests/PolylineDecoderTest.cs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace PolylineAlgorithm.Abstraction.Tests;
22

33
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using PolylineAlgorithm.Abstraction.Properties;
45
using PolylineAlgorithm.Utility;
56
using System;
67

@@ -14,6 +15,7 @@ public class PolylineDecoderTest {
1415

1516
public static IEnumerable<(double, double)> MinAndMaxCoordinates => StaticValueProvider.Invalid.GetMinAndMaxCoordinates();
1617

18+
public static IEnumerable<object[]> InvalidPolylines => StaticValueProvider.Invalid.GetInvalidPolylines().Select<string, object[]>(p => [p]);
1719

1820
[TestMethod]
1921
public void Constructor_Parameterless_Ok() {
@@ -39,104 +41,117 @@ public void Constructor_ValidOptions_Ok() {
3941
}
4042

4143
[TestMethod]
42-
public void Encode_NullPolyline_Throws_ArgumentException() {
44+
public void Decode_NullPolyline_Throws_ArgumentException() {
4345
// Arrange
44-
void Encode() => _decoder.Decode(null!);
46+
void Decode() => _decoder.Decode(null!).ToList();
4547

4648
// Act
47-
var exception = Assert.ThrowsExactly<ArgumentNullException>(Encode);
49+
var exception = Assert.ThrowsExactly<ArgumentNullException>(Decode);
4850

4951
// Assert
50-
Assert.AreEqual("coordinates", exception.ParamName);
52+
Assert.AreEqual("polyline", exception.ParamName);
5153
Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message));
5254
}
5355

5456
[TestMethod]
55-
public void Encode_EmptyPolyline_Throws_ArgumentException() {
57+
public void Decode_EmptyPolyline_Throws_ArgumentException() {
5658
// Arrange
57-
void Encode() => _decoder.Decode(string.Empty);
59+
void Decode() => _decoder.Decode(string.Empty).ToList();
5860

5961
// Act
60-
var exception = Assert.ThrowsExactly<ArgumentException>(Encode);
62+
var exception = Assert.ThrowsExactly<ArgumentException>(Decode);
6163

6264
// Assert
63-
Assert.AreEqual("coordinates", exception.ParamName);
65+
Assert.AreEqual("polyline", exception.ParamName);
6466
Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message));
6567
}
6668

6769
[TestMethod]
68-
public void Encode_WhitespacePolyline_Throws_ArgumentException() {
70+
public void Decode_WhitespacePolyline_Throws_ArgumentException() {
6971
// Arrange
70-
void Encode() => _decoder.Decode(string.Empty);
72+
void Decode() => _decoder.Decode(" ").ToList();
7173

7274
// Act
73-
var exception = Assert.ThrowsExactly<ArgumentException>(Encode);
75+
var exception = Assert.ThrowsExactly<ArgumentException>(Decode);
7476

7577
// Assert
76-
Assert.AreEqual("coordinates", exception.ParamName);
78+
Assert.AreEqual("polyline", exception.ParamName);
7779
Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message));
7880
}
7981

8082
[TestMethod]
81-
public void Encode_BufferTooSmall_Throws_InternalBufferOverflowException() {
83+
[DynamicData(nameof(InvalidPolylines), DynamicDataSourceType.Property)]
84+
public void Decode_InvalidPolyline_Throws_InvalidPolylineException(string polyline) {
8285
// Arrange
83-
PolylineDecoder decoder = new PolylineDecoder(new PolylineEncodingOptions { BufferSize = 12});
84-
string polyline = RandomValueProvider.GetPolyline(2);
86+
void Decode() => _decoder.Decode(polyline).ToList();
8587

8688
// Act
87-
var exception = Assert.ThrowsExactly<InternalBufferOverflowException>(() => decoder.Decode(polyline));
89+
var exception = Assert.ThrowsExactly<InvalidPolylineException>(Decode);
90+
91+
// Assert
92+
Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message));
93+
}
94+
95+
96+
[TestMethod]
97+
public void Decode_ShortPolyline_Throws_InvalidPolylineException() {
98+
// Arrange
99+
void Decode() => _decoder.Decode("?").ToList();
100+
101+
// Act
102+
var exception = Assert.ThrowsExactly<ArgumentException>(Decode);
88103

89104
// Assert
90105
Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message));
91106
}
92107

93108
//[TestMethod]
94109
//[DynamicData(nameof(NotANumberAndInfinityCoordinates))]
95-
//public void Encode_NotANumberAndInfinityCoordinate_Throws_ArgumentOutOfRangeException((double, double) coordinate) {
110+
//public void Decode_NotANumberAndInfinityCoordinate_Throws_ArgumentOutOfRangeException((double, double) coordinate) {
96111
// // Arrange
97-
112+
// void Decode() => _decoder.Decode([coordinate])
98113
// // Act
99-
// var exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _decoder.Encode([coordinate]));
114+
// var exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(Decode);
100115

101116
// // Assert
102117
// Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message));
103118
//}
104119

105120
//[TestMethod]
106121
//[DynamicData(nameof(MinAndMaxCoordinates))]
107-
//public void Encode_MinAndMaxCoordinate_Throws_ArgumentOutOfRangeException((double, double) coordinate) {
122+
//public void Decode_MinAndMaxCoordinate_Throws_ArgumentOutOfRangeException((double, double) coordinate) {
108123
// // Arrange
109124

110125
// // Act
111-
// var exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _decoder.Encode([coordinate]));
126+
// var exception = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _decoder.Decode([coordinate]));
112127

113128
// // Assert
114129
// Assert.IsFalse(string.IsNullOrWhiteSpace(exception.Message));
115130
//}
116131

117132
//[TestMethod]
118133
//[DynamicData(nameof(CoordinateCount))]
119-
//public void Encode_RandomValue_ValidInput_Ok(int count) {
134+
//public void Decode_RandomValue_ValidInput_Ok(int count) {
120135
// // Arrange
121136
// IEnumerable<(double Latitude, double Longitude)> coordinates = RandomValueProvider.GetCoordinates(count);
122137
// string expected = RandomValueProvider.GetPolyline(count);
123138

124139
// // Act
125-
// var result = _decoder.Encode(coordinates);
140+
// var result = _decoder.Decode(coordinates);
126141

127142
// // Assert
128143
// Assert.AreEqual(expected.Length, result.Length);
129144
// Assert.IsTrue(expected.Equals(result));
130145
//}
131146

132147
//[TestMethod]
133-
//public void Encode_StaticValue_ValidInput_Ok() {
148+
//public void Decode_StaticValue_ValidInput_Ok() {
134149
// // Arrange
135150
// IEnumerable<(double Latitude, double Longitude)> coordinates = StaticValueProvider.Valid.GetCoordinates();
136151
// string expected = StaticValueProvider.Valid.GetPolyline();
137152

138153
// // Act
139-
// var result = _decoder.Encode(coordinates);
154+
// var result = _decoder.Decode(coordinates);
140155

141156
// // Assert
142157
// Assert.AreEqual(expected.Length, result.Length);
@@ -155,8 +170,8 @@ protected override (double Latitude, double Longitude) CreateCoordinate(double l
155170
}
156171

157172
protected override ReadOnlyMemory<char> GetReadOnlyMemory(string? polyline) {
158-
if(string.IsNullOrWhiteSpace(polyline)) {
159-
throw new ArgumentException();
173+
if (string.IsNullOrWhiteSpace(polyline)) {
174+
throw new ArgumentException(ExceptionMessageResource.ArgumentCannotBeNullEmptyOrWhiteSpaceMessage, nameof(polyline));
160175
}
161176

162177
return polyline.AsMemory();

utilities/PolylineAlgorithm.Utility/StaticValueProvider.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,23 @@ public static string GetPolyline() {
4949
}
5050

5151
internal static class Invalid {
52-
//public static string GetMalformedPolyline() {
53-
// return ((char)(127)).ToString();
54-
//}
52+
public static IEnumerable<string> GetInvalidPolylines() {
53+
yield return "??␡";
54+
yield return "???";
55+
yield return "_gsia";
56+
yield return "??_gsia@_cid";
57+
}
5558

5659
public static IEnumerable<(double, double)> GetNotANumberAndInfinityCoordinates() {
5760
yield return (double.NaN, 0);
5861
yield return (0, double.NaN);
62+
yield return (double.NaN, double.NaN);
5963
yield return (double.PositiveInfinity, 0);
6064
yield return (0, double.PositiveInfinity);
65+
yield return (double.PositiveInfinity, double.PositiveInfinity);
6166
yield return (double.NegativeInfinity, 0);
6267
yield return (0, double.NegativeInfinity);
68+
yield return (double.NegativeInfinity, double.NegativeInfinity);
6369
}
6470

6571
public static IEnumerable<(double, double)> GetMinAndMaxCoordinates() {

0 commit comments

Comments
 (0)