Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,20 @@ internal FormattingCustomization(IAwexpectCustomization awexpectCustomization)
{
MinimumNumberOfCharactersAfterStringDifference = v,
}));
MaximumStringLength = new CustomizationValue<int>(
() => Get().MaximumStringLength,
v => Update(p => p with
{
MaximumStringLength = v,
}));
}

/// <inheritdoc cref="FormattingCustomizationValue.MaximumNumberOfCollectionItems" />
public ICustomizationValueSetter<int> MaximumNumberOfCollectionItems { get; }

/// <inheritdoc cref="FormattingCustomizationValue.MaximumStringLength" />
public ICustomizationValueSetter<int> MaximumStringLength { get; }

/// <inheritdoc cref="FormattingCustomizationValue.MinimumNumberOfCharactersAfterStringDifference" />
public ICustomizationValueSetter<int> MinimumNumberOfCharactersAfterStringDifference { get; }

Expand All @@ -65,6 +74,11 @@ public record FormattingCustomizationValue
/// The maximum number of displayed items in a collection.
/// </summary>
public int MaximumNumberOfCollectionItems { get; init; } = 10;

/// <summary>
/// The maximum length of a <see langword="string" /> before it gets truncated.
/// </summary>
public int MaximumStringLength { get; init; } = 100;

/// <summary>
/// The minimum number of characters included after the first mismatch in the string difference.
Expand Down
7 changes: 4 additions & 3 deletions Source/aweXpect.Core/Formatting/ValueFormatters.String.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using aweXpect.Core.Helpers;
using aweXpect.Customization;

namespace aweXpect.Formatting;

Expand All @@ -22,9 +23,9 @@ public static string Format(
return (options.UseLineBreaks, options.IncludeType) switch
{
(true, true) => $"string \"{value}\"",
(false, true) => $"string \"{value.DisplayWhitespace().TruncateWithEllipsis(100)}\"",
(false, true) => $"string \"{value.DisplayWhitespace().TruncateWithEllipsis(Customize.aweXpect.Formatting().MaximumStringLength.Get())}\"",
(true, false) => $"\"{value}\"",
(false, false) => $"\"{value.DisplayWhitespace().TruncateWithEllipsis(100)}\"",
(false, false) => $"\"{value.DisplayWhitespace().TruncateWithEllipsis(Customize.aweXpect.Formatting().MaximumStringLength.Get())}\"",
};
}

Expand Down Expand Up @@ -53,7 +54,7 @@ public static void Format(
stringBuilder.Append('\"');
if (!options.UseLineBreaks)
{
stringBuilder.Append(value.DisplayWhitespace().TruncateWithEllipsis(100));
stringBuilder.Append(value.DisplayWhitespace().TruncateWithEllipsis(Customize.aweXpect.Formatting().MaximumStringLength.Get()));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using aweXpect.Core;
using aweXpect.Core.Helpers;
using aweXpect.Customization;

namespace aweXpect.Options;

Expand Down Expand Up @@ -59,38 +60,44 @@ public string GetExtendedFailure(string it, string? actual, string? expected,
int indexOfFirstMismatch = stringDifference.IndexOfFirstMismatch(StringDifference.MatchType.Equality);
if (indexOfFirstMismatch == 0 && comparer.Equals(actual.TrimStart(), expected))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which has unexpected whitespace (\"{actual.Substring(0, GetIndexOfFirstMatch(actual, expected, comparer)).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the beginning)";
$"{prefix} which has unexpected whitespace (\"{actual.Substring(0, GetIndexOfFirstMatch(actual, expected, comparer)).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the beginning)";
}

if (indexOfFirstMismatch == 0 && comparer.Equals(actual, expected.TrimStart()))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which misses some whitespace (\"{expected.Substring(0, GetIndexOfFirstMatch(expected, actual, comparer)).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the beginning)";
$"{prefix} which misses some whitespace (\"{expected.Substring(0, GetIndexOfFirstMatch(expected, actual, comparer)).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the beginning)";
}

if (indexOfFirstMismatch == minCommonLength && comparer.Equals(actual.TrimEnd(), expected))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which has unexpected whitespace (\"{actual.Substring(indexOfFirstMismatch).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the end)";
$"{prefix} which has unexpected whitespace (\"{actual.Substring(indexOfFirstMismatch).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the end)";
}

if (indexOfFirstMismatch == minCommonLength && comparer.Equals(actual, expected.TrimEnd()))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which misses some whitespace (\"{expected.Substring(indexOfFirstMismatch).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the end)";
$"{prefix} which misses some whitespace (\"{expected.Substring(indexOfFirstMismatch).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the end)";
}

if (actual.Length < expected.Length && indexOfFirstMismatch == actual.Length)
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} with a length of {actual.Length} which is shorter than the expected length of {expected.Length} and misses:{Environment.NewLine} \"{expected.Substring(actual.Length).TruncateWithEllipsis(100)}\"";
$"{prefix} with a length of {actual.Length} which is shorter than the expected length of {expected.Length} and misses:{Environment.NewLine} \"{expected.Substring(actual.Length).TruncateWithEllipsis(maxStringLength)}\"";
}

if (actual.Length > expected.Length && indexOfFirstMismatch == expected.Length)
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} with a length of {actual.Length} which is longer than the expected length of {expected.Length} and has superfluous:{Environment.NewLine} \"{actual.Substring(expected.Length).TruncateWithEllipsis(100)}\"";
$"{prefix} with a length of {actual.Length} which is longer than the expected length of {expected.Length} and has superfluous:{Environment.NewLine} \"{actual.Substring(expected.Length).TruncateWithEllipsis(maxStringLength)}\"";
}

return $"{prefix} which {stringDifference}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using aweXpect.Core;
using aweXpect.Core.Helpers;
using aweXpect.Customization;

namespace aweXpect.Options;

Expand Down Expand Up @@ -63,21 +64,24 @@ public string GetExtendedFailure(string it, string? actual, string? expected,
int commonLength = Math.Min(trimmedActual.Length, expected.Length);
if (comparer.Equals(trimmedActual[..commonLength], expected[..commonLength]))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which has unexpected whitespace (\"{actual.Substring(0, GetIndexOfFirstMatch(actual, expected, comparer)).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the beginning)";
$"{prefix} which has unexpected whitespace (\"{actual.Substring(0, GetIndexOfFirstMatch(actual, expected, comparer)).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the beginning)";
}
}

if (indexOfFirstMismatch == 0 && comparer.Equals(actual, expected.TrimStart()))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which misses some whitespace (\"{expected.Substring(0, GetIndexOfFirstMatch(expected, actual, comparer)).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the beginning)";
$"{prefix} which misses some whitespace (\"{expected.Substring(0, GetIndexOfFirstMatch(expected, actual, comparer)).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the beginning)";
}

if (actual.Length < expected.Length && indexOfFirstMismatch == actual.Length)
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} with a length of {actual.Length} which is shorter than the expected length of {expected.Length} and misses:{Environment.NewLine} \"{expected.Substring(actual.Length).TruncateWithEllipsis(100)}\"";
$"{prefix} with a length of {actual.Length} which is shorter than the expected length of {expected.Length} and misses:{Environment.NewLine} \"{expected.Substring(actual.Length).TruncateWithEllipsis(maxStringLength)}\"";
}

return $"{prefix} which {stringDifference}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using aweXpect.Core;
using aweXpect.Core.Helpers;
using aweXpect.Customization;

namespace aweXpect.Options;

Expand Down Expand Up @@ -60,8 +61,9 @@ public string GetExtendedFailure(string it, string? actual, string? expected,
int indexOfFirstMismatch = stringDifference.IndexOfFirstMismatch(StringDifference.MatchType.Suffix);
if (indexOfFirstMismatch == 0 && comparer.Equals(actual, expected.TrimStart()))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which misses some whitespace (\"{expected.Substring(0, GetIndexOfFirstMatch(expected, actual, comparer)).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the beginning)";
$"{prefix} which misses some whitespace (\"{expected.Substring(0, GetIndexOfFirstMatch(expected, actual, comparer)).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the beginning)";
}

if (indexOfFirstMismatch == actual.Length)
Expand All @@ -70,21 +72,24 @@ public string GetExtendedFailure(string it, string? actual, string? expected,
int commonLength = Math.Min(trimmedActual.Length, expected.Length);
if (comparer.Equals(trimmedActual[^commonLength..], expected[^commonLength..]))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which has unexpected whitespace (\"{actual.Substring(trimmedActual.Length).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the end)";
$"{prefix} which has unexpected whitespace (\"{actual.Substring(trimmedActual.Length).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the end)";
}
}

if (indexOfFirstMismatch == minCommonLength && comparer.Equals(actual, expected.TrimEnd()))
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} which misses some whitespace (\"{expected.Substring(indexOfFirstMismatch).DisplayWhitespace().TruncateWithEllipsis(100)}\" at the end)";
$"{prefix} which misses some whitespace (\"{expected.Substring(indexOfFirstMismatch).DisplayWhitespace().TruncateWithEllipsis(maxStringLength)}\" at the end)";
}

if (actual.Length < expected.Length && indexOfFirstMismatch == actual.Length)
{
int maxStringLength = Customize.aweXpect.Formatting().MaximumStringLength.Get();
return
$"{prefix} with a length of {actual.Length} which is shorter than the expected length of {expected.Length} and misses:{Environment.NewLine} \"{expected.Substring(actual.Length).TruncateWithEllipsis(100)}\"";
$"{prefix} with a length of {actual.Length} which is shorter than the expected length of {expected.Length} and misses:{Environment.NewLine} \"{expected.Substring(actual.Length).TruncateWithEllipsis(maxStringLength)}\"";
}

return $"{prefix} which {stringDifference}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ namespace aweXpect.Customization
public class FormattingCustomization : aweXpect.Customization.ICustomizationValueUpdater<aweXpect.Customization.AwexpectCustomization.FormattingCustomizationValue>
{
public aweXpect.Customization.ICustomizationValueSetter<int> MaximumNumberOfCollectionItems { get; }
public aweXpect.Customization.ICustomizationValueSetter<int> MaximumStringLength { get; }
public aweXpect.Customization.ICustomizationValueSetter<int> MinimumNumberOfCharactersAfterStringDifference { get; }
public aweXpect.Customization.AwexpectCustomization.FormattingCustomizationValue Get() { }
public aweXpect.Customization.CustomizationLifetime Update(System.Func<aweXpect.Customization.AwexpectCustomization.FormattingCustomizationValue, aweXpect.Customization.AwexpectCustomization.FormattingCustomizationValue> update) { }
Expand All @@ -410,6 +411,7 @@ namespace aweXpect.Customization
{
public FormattingCustomizationValue() { }
public int MaximumNumberOfCollectionItems { get; init; }
public int MaximumStringLength { get; init; }
public int MinimumNumberOfCharactersAfterStringDifference { get; init; }
}
public class ReflectionCustomization : aweXpect.Customization.ICustomizationValueUpdater<aweXpect.Customization.AwexpectCustomization.ReflectionCustomizationValue>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ namespace aweXpect.Customization
public class FormattingCustomization : aweXpect.Customization.ICustomizationValueUpdater<aweXpect.Customization.AwexpectCustomization.FormattingCustomizationValue>
{
public aweXpect.Customization.ICustomizationValueSetter<int> MaximumNumberOfCollectionItems { get; }
public aweXpect.Customization.ICustomizationValueSetter<int> MaximumStringLength { get; }
public aweXpect.Customization.ICustomizationValueSetter<int> MinimumNumberOfCharactersAfterStringDifference { get; }
public aweXpect.Customization.AwexpectCustomization.FormattingCustomizationValue Get() { }
public aweXpect.Customization.CustomizationLifetime Update(System.Func<aweXpect.Customization.AwexpectCustomization.FormattingCustomizationValue, aweXpect.Customization.AwexpectCustomization.FormattingCustomizationValue> update) { }
Expand All @@ -396,6 +397,7 @@ namespace aweXpect.Customization
{
public FormattingCustomizationValue() { }
public int MaximumNumberOfCollectionItems { get; init; }
public int MaximumStringLength { get; init; }
public int MinimumNumberOfCharactersAfterStringDifference { get; init; }
}
public class ReflectionCustomization : aweXpect.Customization.ICustomizationValueUpdater<aweXpect.Customization.AwexpectCustomization.ReflectionCustomizationValue>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,34 @@ public async Task MaximumNumberOfCollectionItems_ShouldBeUsedInFormatter()
int[] items = Enumerable.Range(1, 6).ToArray();
using (IDisposable _ = Customize.aweXpect.Formatting().MaximumNumberOfCollectionItems.Set(3))
{
await That(Formatter.Format(items)).IsEqualTo("[1, 2, 3, …]");
await That(ValueFormatters.Format(Formatter, items)).IsEqualTo("[1, 2, 3, …]");
}

using (IDisposable _ = Customize.aweXpect.Formatting().MaximumNumberOfCollectionItems.Set(5))
{
await That(Formatter.Format(items)).IsEqualTo("[1, 2, 3, 4, 5, …]");
await That(ValueFormatters.Format(Formatter, items)).IsEqualTo("[1, 2, 3, 4, 5, …]");
}

await That(Formatter.Format(items)).IsEqualTo("[1, 2, 3, 4, 5, 6]");
await That(ValueFormatters.Format(Formatter, items)).IsEqualTo("[1, 2, 3, 4, 5, 6]");
}

[Fact]
public async Task MaximumStringLength_ShouldBeUsedInFormatter()
{
string stringWith100Chars =
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt util";
using (IDisposable _ = Customize.aweXpect.Formatting().MaximumStringLength.Set(6))
{
await That(Formatter.Format(stringWith100Chars)).IsEqualTo("\"Lorem …\"");
}

using (IDisposable _ = Customize.aweXpect.Formatting().MaximumStringLength.Set(99))
{
await That(Formatter.Format(stringWith100Chars)).IsEqualTo(
"\"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt uti…\"");
Comment thread
vbreuss marked this conversation as resolved.
}

await That(Formatter.Format(stringWith100Chars)).IsEqualTo($"\"{stringWith100Chars}\"");
}

[Fact]
Expand Down
Loading