|
| 1 | +// |
| 2 | +// Copyright © Pete Sramek. All rights reserved. |
| 3 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +namespace PolylineAlgorithm.Tests.Diagnostics; |
| 7 | + |
| 8 | +using PolylineAlgorithm.Diagnostics; |
| 9 | +using PolylineAlgorithm.Tests.Properties; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Tests for <see cref="InvalidPolylineException"/>. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class InvalidPolylineExceptionTests { |
| 16 | + /// <summary> |
| 17 | + /// Tests that the default constructor creates an exception with default properties. |
| 18 | + /// </summary> |
| 19 | + [TestMethod] |
| 20 | + [TestCategory(Category.Unit)] |
| 21 | + public void Constructor_Default_CreatesExceptionWithDefaultProperties() { |
| 22 | + // Arrange & Act |
| 23 | + var exception = new InvalidPolylineException(); |
| 24 | + |
| 25 | + // Assert |
| 26 | + Assert.IsNotNull(exception); |
| 27 | + Assert.IsInstanceOfType<InvalidPolylineException>(exception); |
| 28 | + Assert.IsInstanceOfType<Exception>(exception); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Tests that the default constructor creates an exception with null message. |
| 33 | + /// </summary> |
| 34 | + [TestMethod] |
| 35 | + [TestCategory(Category.Unit)] |
| 36 | + public void Constructor_Default_CreatesExceptionWithNullMessage() { |
| 37 | + // Arrange & Act |
| 38 | + var exception = new InvalidPolylineException(); |
| 39 | + |
| 40 | + // Assert |
| 41 | + Assert.IsNull(exception.Message); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Tests that the default constructor creates an exception with null inner exception. |
| 46 | + /// </summary> |
| 47 | + [TestMethod] |
| 48 | + [TestCategory(Category.Unit)] |
| 49 | + public void Constructor_Default_CreatesExceptionWithNullInnerException() { |
| 50 | + // Arrange & Act |
| 51 | + var exception = new InvalidPolylineException(); |
| 52 | + |
| 53 | + // Assert |
| 54 | + Assert.IsNull(exception.InnerException); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Tests that the message constructor creates an exception with the specified message. |
| 59 | + /// </summary> |
| 60 | + [TestMethod] |
| 61 | + [TestCategory(Category.Unit)] |
| 62 | + public void Constructor_WithMessage_CreatesExceptionWithSpecifiedMessage() { |
| 63 | + // Arrange |
| 64 | + const string expectedMessage = "Test error message"; |
| 65 | + |
| 66 | + // Act |
| 67 | + var exception = CreateInvalidPolylineExceptionWithMessage(expectedMessage); |
| 68 | + |
| 69 | + // Assert |
| 70 | + Assert.IsNotNull(exception); |
| 71 | + Assert.AreEqual(expectedMessage, exception.Message); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Tests that the message constructor creates an exception with null inner exception. |
| 76 | + /// </summary> |
| 77 | + [TestMethod] |
| 78 | + [TestCategory(Category.Unit)] |
| 79 | + public void Constructor_WithMessage_CreatesExceptionWithNullInnerException() { |
| 80 | + // Arrange |
| 81 | + const string message = "Test error message"; |
| 82 | + |
| 83 | + // Act |
| 84 | + var exception = CreateInvalidPolylineExceptionWithMessage(message); |
| 85 | + |
| 86 | + // Assert |
| 87 | + Assert.IsNull(exception.InnerException); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Tests that the message constructor handles null message. |
| 92 | + /// </summary> |
| 93 | + [TestMethod] |
| 94 | + [TestCategory(Category.Unit)] |
| 95 | + public void Constructor_WithNullMessage_CreatesExceptionWithNullMessage() { |
| 96 | + // Arrange |
| 97 | + const string? message = null; |
| 98 | + |
| 99 | + // Act |
| 100 | + var exception = CreateInvalidPolylineExceptionWithMessage(message!); |
| 101 | + |
| 102 | + // Assert |
| 103 | + Assert.IsNull(exception.Message); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Tests that the message constructor handles empty message. |
| 108 | + /// </summary> |
| 109 | + [TestMethod] |
| 110 | + [TestCategory(Category.Unit)] |
| 111 | + public void Constructor_WithEmptyMessage_CreatesExceptionWithEmptyMessage() { |
| 112 | + // Arrange |
| 113 | + var message = string.Empty; |
| 114 | + |
| 115 | + // Act |
| 116 | + var exception = CreateInvalidPolylineExceptionWithMessage(message); |
| 117 | + |
| 118 | + // Assert |
| 119 | + Assert.AreEqual(string.Empty, exception.Message); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Tests that the message and inner exception constructor creates an exception with specified message and inner exception. |
| 124 | + /// </summary> |
| 125 | + [TestMethod] |
| 126 | + [TestCategory(Category.Unit)] |
| 127 | + public void Constructor_WithMessageAndInnerException_CreatesExceptionWithSpecifiedProperties() { |
| 128 | + // Arrange |
| 129 | + const string expectedMessage = "Outer exception message"; |
| 130 | + var innerException = new InvalidOperationException("Inner exception message"); |
| 131 | + |
| 132 | + // Act |
| 133 | + var exception = new InvalidPolylineException(expectedMessage, innerException); |
| 134 | + |
| 135 | + // Assert |
| 136 | + Assert.IsNotNull(exception); |
| 137 | + Assert.AreEqual(expectedMessage, exception.Message); |
| 138 | + Assert.AreSame(innerException, exception.InnerException); |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Tests that the message and inner exception constructor handles null message. |
| 143 | + /// </summary> |
| 144 | + [TestMethod] |
| 145 | + [TestCategory(Category.Unit)] |
| 146 | + public void Constructor_WithNullMessageAndInnerException_CreatesExceptionWithNullMessage() { |
| 147 | + // Arrange |
| 148 | + const string? message = null; |
| 149 | + var innerException = new InvalidOperationException("Inner exception message"); |
| 150 | + |
| 151 | + // Act |
| 152 | + var exception = new InvalidPolylineException(message!, innerException); |
| 153 | + |
| 154 | + // Assert |
| 155 | + Assert.IsNull(exception.Message); |
| 156 | + Assert.AreSame(innerException, exception.InnerException); |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Tests that the message and inner exception constructor handles null inner exception. |
| 161 | + /// </summary> |
| 162 | + [TestMethod] |
| 163 | + [TestCategory(Category.Unit)] |
| 164 | + public void Constructor_WithMessageAndNullInnerException_CreatesExceptionWithNullInnerException() { |
| 165 | + // Arrange |
| 166 | + const string message = "Test error message"; |
| 167 | + const Exception? innerException = null; |
| 168 | + |
| 169 | + // Act |
| 170 | + var exception = new InvalidPolylineException(message, innerException!); |
| 171 | + |
| 172 | + // Assert |
| 173 | + Assert.AreEqual(message, exception.Message); |
| 174 | + Assert.IsNull(exception.InnerException); |
| 175 | + } |
| 176 | + |
| 177 | + /// <summary> |
| 178 | + /// Tests that the message and inner exception constructor handles empty message. |
| 179 | + /// </summary> |
| 180 | + [TestMethod] |
| 181 | + [TestCategory(Category.Unit)] |
| 182 | + public void Constructor_WithEmptyMessageAndInnerException_CreatesExceptionWithEmptyMessage() { |
| 183 | + // Arrange |
| 184 | + var message = string.Empty; |
| 185 | + var innerException = new InvalidOperationException("Inner exception"); |
| 186 | + |
| 187 | + // Act |
| 188 | + var exception = new InvalidPolylineException(message, innerException); |
| 189 | + |
| 190 | + // Assert |
| 191 | + Assert.AreEqual(string.Empty, exception.Message); |
| 192 | + Assert.AreSame(innerException, exception.InnerException); |
| 193 | + } |
| 194 | + |
| 195 | + /// <summary> |
| 196 | + /// Tests that the message and inner exception constructor handles both null values. |
| 197 | + /// </summary> |
| 198 | + [TestMethod] |
| 199 | + [TestCategory(Category.Unit)] |
| 200 | + public void Constructor_WithNullMessageAndNullInnerException_CreatesExceptionWithNullProperties() { |
| 201 | + // Arrange |
| 202 | + const string? message = null; |
| 203 | + const Exception? innerException = null; |
| 204 | + |
| 205 | + // Act |
| 206 | + var exception = new InvalidPolylineException(message!, innerException!); |
| 207 | + |
| 208 | + // Assert |
| 209 | + Assert.IsNull(exception.Message); |
| 210 | + Assert.IsNull(exception.InnerException); |
| 211 | + } |
| 212 | + |
| 213 | + /// <summary> |
| 214 | + /// Helper method to create an InvalidPolylineException with a message using reflection. |
| 215 | + /// </summary> |
| 216 | + /// <param name="message">The exception message.</param> |
| 217 | + /// <returns>An InvalidPolylineException instance.</returns> |
| 218 | + private static InvalidPolylineException CreateInvalidPolylineExceptionWithMessage(string message) { |
| 219 | + var constructor = typeof(InvalidPolylineException).GetConstructor( |
| 220 | + System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, |
| 221 | + null, |
| 222 | + [typeof(string)], |
| 223 | + null); |
| 224 | + |
| 225 | + return (InvalidPolylineException)constructor!.Invoke([message]); |
| 226 | + } |
| 227 | +} |
0 commit comments