Skip to content

Commit

Permalink
Merge pull request #94 from MASSHUU12/improve-default-messages
Browse files Browse the repository at this point in the history
Improve default messages
  • Loading branch information
MASSHUU12 authored Jun 29, 2024
2 parents cdd51ad + 5b20c90 commit 227e645
Show file tree
Hide file tree
Showing 19 changed files with 408 additions and 139 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ All notable changes to this project will be documented in this file.

### Changed

- Improved default messages for ConfirmRangeExtensions.
- Improved default messages.

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions addons/confirma/src/extensions/ConfirmActionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static Action ConfirmCompletesWithin(this Action action, TimeSpan timeSpa
{
throw new ConfirmAssertException(
message ??
$"Expected action to complete within {timeSpan.TotalMilliseconds} ms, but it did not."
$"Action did not complete within {timeSpan.TotalMilliseconds} ms."
);
}

Expand All @@ -32,7 +32,7 @@ public static Action ConfirmDoesNotCompleteWithin(this Action action, TimeSpan t

throw new ConfirmAssertException(
message ??
$"Expected action to not complete within {timeSpan.TotalMilliseconds} ms, but it did."
$"Action completed within {timeSpan.TotalMilliseconds} ms, but it should not have."
);
}
}
16 changes: 11 additions & 5 deletions addons/confirma/src/extensions/ConfirmArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,40 @@ public static T[] ConfirmSize<T>(this T[] array, int expectedSize, string? messa
{
if (array.Length == expectedSize) return array;

throw new ConfirmAssertException(message ?? $"Array size is {array.Length} but expected {expectedSize}.");
throw new ConfirmAssertException(
message ??
$"Array size is {array.Length}, but expected {expectedSize}."
);
}

public static T[] ConfirmEmpty<T>(this T[] array, string? message = null)
{
if (array.Length == 0) return array;

throw new ConfirmAssertException(message ?? "Expected array to be empty.");
throw new ConfirmAssertException(message ?? "Array is not empty.");
}

public static T[] ConfirmNotEmpty<T>(this T[] array, string? message = null)
{
if (array.Length > 0) return array;

throw new ConfirmAssertException(message ?? "Expected array to not be empty.");
throw new ConfirmAssertException(message ?? "Array is empty.");
}

public static T[] ConfirmContains<T>(this T[] array, T expected, string? message = null)
{
if (Array.IndexOf(array, expected) != -1) return array;

throw new ConfirmAssertException(message ?? $"Expected array to contain '{expected}'.");
throw new ConfirmAssertException(
message ??
$"Array does not contain '{expected}'."
);
}

public static T[] ConfirmNotContains<T>(this T[] array, T expected, string? message = null)
{
if (Array.IndexOf(array, expected) == -1) return array;

throw new ConfirmAssertException(message ?? $"Expected array to not contain '{expected}'.");
throw new ConfirmAssertException(message ?? $"Array contains '{expected}'.");
}
}
10 changes: 8 additions & 2 deletions addons/confirma/src/extensions/ConfirmAttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public static Type ConfirmIsDecoratedWith(this Type actual, Type expected, strin
{
if (actual.IsDefined(expected, false)) return actual;

throw new ConfirmAssertException(message ?? $"Expected '{actual.Name}' to be decorated with '{expected.Name}' but was not.");
throw new ConfirmAssertException(
message ??
$"{actual.Name} is not decorated with {expected.Name}."
);
}

public static Type ConfirmIsDecoratedWith<T>(this Type actual, string? message = null)
Expand All @@ -25,7 +28,10 @@ public static Type ConfirmIsNotDecoratedWith(this Type actual, Type expected, st
{
if (!actual.IsDefined(expected, false)) return actual;

throw new ConfirmAssertException(message ?? $"Expected '{actual.Name}' to not be decorated with '{expected.Name}' but was.");
throw new ConfirmAssertException(
message ??
$"{actual.Name} is decorated with {expected.Name}."
);
}

public static Type ConfirmIsNotDecoratedWith<T>(this Type actual, string? message = null)
Expand Down
8 changes: 4 additions & 4 deletions addons/confirma/src/extensions/ConfirmDateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ public static DateTime ConfirmIsBefore(this DateTime actual, DateTime dateTime,
{
return actual.ConfirmLessThan(
dateTime,
message ?? $"Expected {actual.ToUniversalTime()} to be before {dateTime.ToUniversalTime()}."
message ?? $"{actual.ToUniversalTime()} is not before {dateTime.ToUniversalTime()}."
);
}

public static DateTime ConfirmIsOnOrBefore(this DateTime actual, DateTime dateTime, string? message = null)
{
return actual.ConfirmLessThanOrEqual(
dateTime,
message ?? $"Expected {actual.ToUniversalTime()} to be on or before {dateTime.ToUniversalTime()}."
message ?? $"{actual.ToUniversalTime()} is not on or before {dateTime.ToUniversalTime()}."
);
}

public static DateTime ConfirmIsAfter(this DateTime actual, DateTime dateTime, string? message = null)
{
return actual.ConfirmGreaterThan(
dateTime,
message ?? $"Expected {actual.ToUniversalTime()} to be after {dateTime.ToUniversalTime()}."
message ?? $"{actual.ToUniversalTime()} is not after {dateTime.ToUniversalTime()}."
);
}

public static DateTime ConfirmIsOnOrAfter(this DateTime actual, DateTime dateTime, string? message = null)
{
return actual.ConfirmGreaterThanOrEqual(
dateTime,
message ?? $"Expected {actual.ToUniversalTime()} to be on or after {dateTime.ToUniversalTime()}."
message ?? $"{actual.ToUniversalTime()} is not on or after {dateTime.ToUniversalTime()}."
);
}
}
104 changes: 84 additions & 20 deletions addons/confirma/src/extensions/ConfirmDictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,123 @@ namespace Confirma.Extensions;

public static class ConfirmDictionaryExtensions
{
public static IDictionary<TKey, TValue> ConfirmContainsKey<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, string? message = null)
public static IDictionary<TKey, TValue> ConfirmContainsKey<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
string? message = null
)
{
if (dictionary.ContainsKey(key)) return dictionary;

throw new ConfirmAssertException(message ?? $"Expected dictionary to contain key '{key}' but it did not.");
throw new ConfirmAssertException(
message ??
$"Dictionary does not contain key '{key}'."
);
}

public static IDictionary<TKey, TValue> ConfirmNotContainsKey<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, string? message = null)
public static IDictionary<TKey, TValue> ConfirmNotContainsKey<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
string? message = null
)
{
if (!dictionary.ContainsKey(key)) return dictionary;

throw new ConfirmAssertException(message ?? $"Expected dictionary to not contain key '{key}' but it did.");
throw new ConfirmAssertException(
message ??
$"Dictionary contains unexpected key '{key}'."
);
}

public static IDictionary<TKey, TValue> ConfirmContainsValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value, string? message = null)
public static IDictionary<TKey, TValue> ConfirmContainsValue<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TValue value,
string? message = null
)
{
if (dictionary.Values.Contains(value)) return dictionary;

throw new ConfirmAssertException(message ?? $"Expected dictionary to contain value '{value}' but it did not.");
throw new ConfirmAssertException(
message ??
$"Dictionary does not contain value '{value}'."
);
}

public static IDictionary<TKey, TValue> ConfirmNotContainsValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value, string? message = null)
public static IDictionary<TKey, TValue> ConfirmNotContainsValue<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TValue value,
string? message = null
)
{
if (!dictionary.Values.Contains(value)) return dictionary;

throw new ConfirmAssertException(message ?? $"Expected dictionary to not contain value '{value}' but it did.");
throw new ConfirmAssertException(
message ??
$"Dictionary contains unexpected value '{value}'."
);
}

public static IDictionary<TKey, TValue> ConfirmContainsKeyValuePair<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value, string? message = null)
public static IDictionary<TKey, TValue> ConfirmContainsKeyValuePair<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
TValue? value,
string? message = null
)
{
if (dictionary.TryGetValue(key, out TValue? v) && v?.Equals(value) == true) return dictionary;
if (dictionary.TryGetValue(key, out TValue? v) && v?.Equals(value) == true)
return dictionary;

throw new ConfirmAssertException(message ?? $"Expected dictionary to contain key-value pair '{key}': '{value}' but it did not.");
throw new ConfirmAssertException(
message ??
$"Dictionary does not contain key-value pair '{key}': '{value}'."
);
}

public static IDictionary<Variant, Variant> ConfirmContainsKeyValuePair(this IDictionary<Variant, Variant> dictionary, Variant key, Variant value, string? message = null)
public static IDictionary<Variant, Variant> ConfirmContainsKeyValuePair(
this IDictionary<Variant, Variant> dictionary,
Variant key,
Variant value,
string? message = null
)
{
if (dictionary.TryGetValue(key, out var val) && val.VariantEquals(value)) return dictionary;
if (dictionary.TryGetValue(key, out var val) && val.VariantEquals(value))
return dictionary;

throw new ConfirmAssertException(message ?? $"Expected dictionary to contain key-value pair '{key}': '{value}' but it did not.");
throw new ConfirmAssertException(
message ??
$"Dictionary does not contain key-value pair '{key}': '{value}'."
);
}

public static IDictionary<TKey, TValue> ConfirmNotContainsKeyValuePair<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value, string? message = null)
public static IDictionary<TKey, TValue> ConfirmNotContainsKeyValuePair<TKey, TValue>(
this IDictionary<TKey, TValue> dictionary,
TKey key,
TValue? value,
string? message = null
)
{
if (!dictionary.TryGetValue(key, out TValue? v) || v?.Equals(value) == false) return dictionary;
if (!dictionary.TryGetValue(key, out TValue? v) || v?.Equals(value) == false)
return dictionary;

throw new ConfirmAssertException(message ?? $"Expected dictionary to not contain key-value pair '{key}': '{value}' but it did.");
throw new ConfirmAssertException(
message ??
$"Dictionary contains unexpected key-value pair '{key}': '{value}'."
);
}

public static IDictionary<Variant, Variant> ConfirmNotContainsKeyValuePair(this IDictionary<Variant, Variant> dictionary, Variant key, Variant value, string? message = null)
public static IDictionary<Variant, Variant> ConfirmNotContainsKeyValuePair(
this IDictionary<Variant, Variant> dictionary,
Variant key,
Variant value,
string? message = null
)
{
if (!dictionary.TryGetValue(key, out var val) || val.VariantEquals(value) == false) return dictionary;
if (!dictionary.TryGetValue(key, out var val) || val.VariantEquals(value) == false)
return dictionary;

throw new ConfirmAssertException(message ?? $"Expected dictionary to not contain key-value pair '{key}': '{value}' but it did.");
throw new ConfirmAssertException(
message ??
$"Dictionary contains unexpected key-value pair '{key}': '{value}'."
);
}
}
20 changes: 16 additions & 4 deletions addons/confirma/src/extensions/ConfirmEqualExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public static class ConfirmEqualExtensions
{
if (!(actual?.Equals(expected)) ?? false)
{
throw new ConfirmAssertException(message ?? $"Expected '{expected}' but was '{actual}'.");
throw new ConfirmAssertException(
message ??
$"Expected '{expected}' but got '{actual}'."
);
}

return actual;
Expand All @@ -19,14 +22,20 @@ public static class ConfirmEqualExtensions
{
if (actual.SequenceEqual(expected)) return actual;

throw new ConfirmAssertException(message ?? $"Expected '{expected}' but was '{actual}'.");
throw new ConfirmAssertException(
message ??
$"Expected '{string.Join(", ", expected)}' but got '{string.Join(", ", actual)}'."
);
}

public static T? ConfirmNotEqual<T>(this T? actual, T? expected, string? message = null)
{
if (actual?.Equals(expected) ?? false)
{
throw new ConfirmAssertException(message ?? $"Expected not '{expected}' but was '{actual}'.");
throw new ConfirmAssertException(
message ??
$"Expected not '{expected}' but got '{actual}'."
);
}

return actual;
Expand All @@ -36,6 +45,9 @@ public static class ConfirmEqualExtensions
{
if (!actual.SequenceEqual(expected)) return actual;

throw new ConfirmAssertException(message ?? $"Expected not '{expected}' but was '{actual}'.");
throw new ConfirmAssertException(
message ??
$"Expected not '{string.Join(", ", expected)}' but got '{string.Join(", ", actual)}'."
);
}
}
4 changes: 2 additions & 2 deletions addons/confirma/src/extensions/ConfirmEventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static Action ConfirmRaisesEvent<TEventArgs>(
{
throw new ConfirmAssertException(
message ??
$"Expected {eventHandler?.GetType().Name ?? "event"} to be raised, but it was not."
$"{eventHandler?.GetType().Name ?? "event"} was not raised."
);
}

Expand Down Expand Up @@ -62,7 +62,7 @@ public static Action ConfirmDoesNotRaiseEvent<TEventArgs>(
{
throw new ConfirmAssertException(
message ??
$"Expected {eventHandler?.GetType().Name ?? "event"} to not be raised, but it was."
$"{eventHandler?.GetType().Name ?? "event"} was raised."
);
}

Expand Down
Loading

0 comments on commit 227e645

Please sign in to comment.