Skip to content

Commit

Permalink
Don't throw ArgumentException
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsm committed Mar 28, 2023
1 parent 90d8268 commit f414dfd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
8 changes: 0 additions & 8 deletions Source/Millistream.Streaming/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,11 @@ public bool AddNumeric(Field tag, string value) =>
/// <param name="value">The scaled and signed 64-bit integer.</param>
/// <param name="decimals">The number of decimals.</param>
/// <returns><see langword = "true" /> if the field was successfully added, or <see langword="false" /> if the value could not be added (because there was no more memory, the message handle does not contain any messages, or the supplied value is not of the type specified).</returns>
/// <exception cref="ArgumentException"><paramref name="decimals"/> is not between 0 and 19.</exception>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_add_int function.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="Message"/> instance has been disposed.</exception>
/// <remarks>The corresponding native function is mdf_message_add_int.</remarks>
public bool AddInt64(uint tag, long value, int decimals)
{
if (decimals < 0 || decimals > 19)
throw new ArgumentException($"{nameof(decimals)} cannot be smaller than 0 or greater than 19.", nameof(decimals));
ThrowIfDisposed();
ThrowIfNativeFunctionIsMissing(_nativeImplementation.mdf_message_add_int, nameof(_nativeImplementation.mdf_message_add_int));
return _nativeImplementation.mdf_message_add_int(Handle, tag, value, decimals) == 1;
Expand All @@ -258,7 +255,6 @@ public bool AddInt64(uint tag, long value, int decimals)
/// <param name="value">The scaled and signed 64-bit integer.</param>
/// <param name="decimals">The number of decimals.</param>
/// <returns><see langword = "true" /> if the field was successfully added, or <see langword="false" /> if the value could not be added (because there was no more memory, the message handle does not contain any messages, or the supplied value is not of the type specified).</returns>
/// <exception cref="ArgumentException"><paramref name="decimals"/> is not between 0 and 19.</exception>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_add_int function.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="Message"/> instance has been disposed.</exception>
/// <remarks>The corresponding native function is mdf_message_add_int.</remarks>
Expand All @@ -272,14 +268,11 @@ public bool AddInt64(Field tag, long value, int decimals) =>
/// <param name="value">The scaled and unsigned 64-bit integer.</param>
/// <param name="decimals">The number of decimals.</param>
/// <returns><see langword = "true" /> if the field was successfully added, or <see langword="false" /> if the value could not be added (because there was no more memory, the message handle does not contain any messages, or the supplied value is not of the type specified).</returns>
/// <exception cref="ArgumentException"><paramref name="decimals"/> is not between 0 and 19.</exception>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_add_uint function.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="Message"/> instance has been disposed.</exception>
/// <remarks>The corresponding native function is mdf_message_add_uint.</remarks>
public bool AddUInt64(uint tag, ulong value, int decimals)
{
if (decimals < 0 || decimals > 19)
throw new ArgumentException($"{nameof(decimals)} cannot be smaller than 0 or greater than 19.", nameof(decimals));
ThrowIfDisposed();
ThrowIfNativeFunctionIsMissing(_nativeImplementation.mdf_message_add_uint, nameof(_nativeImplementation.mdf_message_add_uint));
return _nativeImplementation.mdf_message_add_uint(Handle, tag, value, decimals) == 1;
Expand All @@ -292,7 +285,6 @@ public bool AddUInt64(uint tag, ulong value, int decimals)
/// <param name="value">The scaled and unsigned 64-bit integer.</param>
/// <param name="decimals">The number of decimals.</param>
/// <returns><see langword = "true" /> if the field was successfully added, or <see langword="false" /> if the value could not be added (because there was no more memory, the message handle does not contain any messages, or the supplied value is not of the type specified).</returns>
/// <exception cref="ArgumentException"><paramref name="decimals"/> is not between 0 and 19.</exception>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_add_uint function.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="Message"/> instance has been disposed.</exception>
/// <remarks>The corresponding native function is mdf_message_add_uint.</remarks>
Expand Down
28 changes: 11 additions & 17 deletions Tests/Millistream.Streaming.UnitTests/MessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,21 @@ public void AddInt64AndUInt64Test()
_nativeImplementation.Verify(x => x.mdf_message_add_int(It.IsAny<IntPtr>(), (uint)Field, SignedValue, decimals), Times.Exactly(2));
_nativeImplementation.Verify(x => x.mdf_message_add_uint(It.IsAny<IntPtr>(), (uint)Field, UnsignedValue, decimals), Times.Exactly(2));

TestWithAnInvalidNumberOfDecimals(Field, SignedValue, -1, message.AddInt64);
TestWithAnInvalidNumberOfDecimals((uint)Field, SignedValue, -1, message.AddInt64);
Expression<Func<int, bool>> match = i => i < 0 || i > 19;
_nativeImplementation.Setup(x => x.mdf_message_add_int(It.IsAny<IntPtr>(), It.IsAny<uint>(), It.IsAny<long>(), It.Is<int>(match))).Returns(0);
_nativeImplementation.Setup(x => x.mdf_message_add_uint(It.IsAny<IntPtr>(), It.IsAny<uint>(), It.IsAny<ulong>(), It.Is<int>(match))).Returns(0);

TestWithAnInvalidNumberOfDecimals(Field, UnsignedValue, -1, message.AddUInt64);
TestWithAnInvalidNumberOfDecimals((uint)Field, UnsignedValue, -1, message.AddUInt64);
Assert.IsFalse(message.AddInt64(Field, SignedValue, -1));
Assert.IsFalse(message.AddInt64((uint)Field, SignedValue, -1));

TestWithAnInvalidNumberOfDecimals(Field, SignedValue, 20, message.AddInt64);
TestWithAnInvalidNumberOfDecimals((uint)Field, SignedValue, 20, message.AddInt64);
Assert.IsFalse(message.AddUInt64(Field, UnsignedValue, -1));
Assert.IsFalse(message.AddUInt64((uint)Field, UnsignedValue, -1));

TestWithAnInvalidNumberOfDecimals(Field, UnsignedValue, 20, message.AddUInt64);
TestWithAnInvalidNumberOfDecimals((uint)Field, UnsignedValue, 20, message.AddUInt64);
Assert.IsFalse(message.AddInt64(Field, SignedValue, 20));
Assert.IsFalse(message.AddInt64((uint)Field, SignedValue, 20));

static void TestWithAnInvalidNumberOfDecimals<TField, TValue>(TField field, TValue value, int decimals, Func<TField, TValue, int, bool> method)
{
try
{
method(field, value, decimals);
Assert.Fail();
}
catch (ArgumentException) { }
}
Assert.IsFalse(message.AddUInt64(Field, UnsignedValue, 20));
Assert.IsFalse(message.AddUInt64((uint)Field, UnsignedValue, 20));
}

[TestMethod]
Expand Down

0 comments on commit f414dfd

Please sign in to comment.