1
0

Erweiterung der UnitTests und kleinere Fixes

This commit is contained in:
2021-11-17 23:30:54 +01:00
parent 3a0732dd22
commit 80fc6ff2b3
16 changed files with 873 additions and 37 deletions

View File

@@ -50,14 +50,14 @@
/// </summary>
/// <param name="str">The string to hash, using UTF-8 encoding.</param>
/// <returns>The SHA-256 hash value, in hexadecimal notation.</returns>
public static string Sha256(string str) => CryptographyHelper.Sha256(str);
public static string Sha256(this string str) => CryptographyHelper.Sha256(str);
/// <summary>
/// Computes a hash from a byte array value using the SHA-256 algorithm.
/// </summary>
/// <param name="bytes">The byte array.</param>
/// <returns>The SHA-256 hash value, in hexadecimal notation.</returns>
public static string Sha256(byte[] bytes) => CryptographyHelper.Sha256(bytes);
public static string Sha256(this byte[] bytes) => CryptographyHelper.Sha256(bytes);
#endregion SHA-256

View File

@@ -5,6 +5,7 @@ namespace System
/// <summary>
/// Provides extension methods for exceptions.
/// </summary>
[Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static class ExceptionExtensions
{
/// <summary>

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Text;
@@ -36,6 +35,34 @@ namespace Newtonsoft.Json
JsonConvert.PopulateObject(json, target, jsonSerializerSettings);
}
/// <summary>
/// Deserializes a JSON string into a new instance.
/// </summary>
/// <typeparam name="T">The type of the instance to deserialize.</typeparam>
/// <param name="json">The JSON string to read the values from.</param>
/// <returns>A new instance of <typeparamref name="T"/> with the deserialized values.</returns>
public static T DeserializeJson<T>(this string json)
=> JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);
/// <summary>
/// Deserializes a JSON string into a new instance or using the fallback value.
/// </summary>
/// <typeparam name="T">The type of the instance to deserialize.</typeparam>
/// <param name="json">The JSON string to read the values from.</param>
/// <param name="fallbackValue">A fallback value when deserialization fails.</param>
/// <returns>A new instance of <typeparamref name="T"/> with the deserialized values or the fallback value.</returns>
public static T DeserializeJson<T>(this string json, T fallbackValue)
{
try
{
return JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);
}
catch
{
return fallbackValue;
}
}
/// <summary>
/// Serializes an instance to a JSON string.
/// </summary>
@@ -63,29 +90,13 @@ namespace Newtonsoft.Json
a.ErrorContext.Handled = true;
};
if (includeType)
serializer.TypeNameHandling = TypeNameHandling.Auto;
serializer.TypeNameHandling = includeType ? TypeNameHandling.All : TypeNameHandling.None;
serializer.Serialize(jw, source, typeof(T));
}
return sb.ToString().Trim();
}
/// <summary>
/// Deserializes a JSON string into a new instance.
/// </summary>
/// <typeparam name="T">The type of the instance to deserialize.</typeparam>
/// <param name="json">The JSON string to read the values from.</param>
/// <param name="fallback">A fallback value.</param>
/// <returns>A new instance of <typeparamref name="T"/> with the deserialized values.</returns>
public static T DeserializeJson<T>(this string json, T fallback = default)
{
if (!string.IsNullOrWhiteSpace(json))
return JsonConvert.DeserializeObject<T>(json, jsonSerializerSettings);
return fallback;
}
/// <summary>
/// Converts an object into a JObject using the custom serializer settings.
/// </summary>
@@ -141,10 +152,7 @@ namespace Newtonsoft.Json
if (lvlObj == null)
return defaultValue;
if (typeof(T) == typeof(string))
return (T)Convert.ChangeType(lvlObj, typeof(T));
return DeepConvert.ChangeType<T>(lvlObj);
return DeepConvert.ChangeType<T>(lvlObj is JValue ? ((JValue)lvlObj).Value : lvlObj.Value<object>());
}
/// <summary>

View File

@@ -1,4 +1,6 @@
namespace System.Threading
//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("AMWD.Common.Tests")]
namespace System.Threading
{
/// <summary>
/// Provides extension methods for the <see cref="ReaderWriterLockSlim"/>.
@@ -18,7 +20,7 @@
if (!rwLock.TryEnterReadLock(timeoutMilliseconds))
throw new TimeoutException("The read lock could not be acquired.");
return new RWLockDisposable(rwLock, 1);
return new DisposableReadWriteLock(rwLock, LockMode.Read);
}
/// <summary>
@@ -37,7 +39,7 @@
if (!rwLock.TryEnterUpgradeableReadLock(timeoutMilliseconds))
throw new TimeoutException("The upgradeable read lock could not be acquired.");
return new RWLockDisposable(rwLock, 2);
return new DisposableReadWriteLock(rwLock, LockMode.Upgradable);
}
/// <summary>
@@ -53,15 +55,15 @@
if (!rwLock.TryEnterWriteLock(timeoutMilliseconds))
throw new TimeoutException("The write lock could not be acquired.");
return new RWLockDisposable(rwLock, 3);
return new DisposableReadWriteLock(rwLock, LockMode.Write);
}
private struct RWLockDisposable : IDisposable
private struct DisposableReadWriteLock : IDisposable
{
private readonly ReaderWriterLockSlim rwLock;
private int lockMode;
private LockMode lockMode;
public RWLockDisposable(ReaderWriterLockSlim rwLock, int lockMode)
public DisposableReadWriteLock(ReaderWriterLockSlim rwLock, LockMode lockMode)
{
this.rwLock = rwLock;
this.lockMode = lockMode;
@@ -69,16 +71,28 @@
public void Dispose()
{
if (lockMode == 1)
if (lockMode == LockMode.Read)
rwLock.ExitReadLock();
if (lockMode == 2 && rwLock.IsWriteLockHeld) // Upgraded with EnterWriteLock alone
if (lockMode == LockMode.Upgradable && rwLock.IsWriteLockHeld) // Upgraded with EnterWriteLock alone
rwLock.ExitWriteLock();
if (lockMode == 2)
if (lockMode == LockMode.Upgradable)
rwLock.ExitUpgradeableReadLock();
if (lockMode == 3)
if (lockMode == LockMode.Write)
rwLock.ExitWriteLock();
lockMode = 0;
lockMode = LockMode.None;
}
}
private enum LockMode
{
None = 0,
Read = 1,
Upgradable = 2,
Write = 3,
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace System
{
@@ -25,6 +26,9 @@ namespace System
if (str.Length % 2 == 1)
yield break;
if (Regex.IsMatch(str, "[^0-9a-fA-F]"))
yield break;
for (int i = 0; i < str.Length; i += 2)
yield return Convert.ToByte(str.Substring(i, 2), 16);
}