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
2 changes: 1 addition & 1 deletion Pipeline/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ partial class Build : NukeBuild
/// <para />
/// Afterward, you can update the package reference in `Directory.Packages.props` and reset this flag.
/// </summary>
readonly BuildScope BuildScope = BuildScope.Default;
readonly BuildScope BuildScope = BuildScope.CoreOnly;
Comment thread
vbreuss marked this conversation as resolved.

[Parameter("Github Token")] readonly string GithubToken;

Expand Down
2 changes: 1 addition & 1 deletion Source/aweXpect.Core/Core/ExpectationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class ExpectationBuilder
protected ExpectationBuilder(string subjectExpression, ExpectationGrammars grammars = ExpectationGrammars.None)
{
AweXpectInitialization.EnsureInitialized();
Subject = subjectExpression;
Subject = subjectExpression.TrimCommonWhiteSpace();
ExpectationGrammars = grammars;
}

Expand Down
51 changes: 51 additions & 0 deletions Source/aweXpect.Core/Core/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;

namespace aweXpect.Core.Helpers;

Expand Down Expand Up @@ -90,4 +91,54 @@ public static string SubstringUntilFirst(this string name, char c)
const char ellipsis = '\u2026';
return $"{value.Substring(0, indexOfWordBoundary)}{ellipsis}";
}

public static string TrimCommonWhiteSpace(this string value)
{
string[] lines = value.Split('\n');
if (lines.Length <= 1)
{
return value;
}

StringBuilder sb = new();
foreach (char c in lines[1])
Comment thread
vbreuss marked this conversation as resolved.
{
if (char.IsWhiteSpace(c))
{
sb.Append(c);
}
else
{
break;
}
}

string commonWhiteSpace = sb.ToString();

for (int l = 2; l < lines.Length; l++)
{
if (lines[l].StartsWith(commonWhiteSpace))
{
continue;
}

for (int i = 0; i < Math.Min(lines[l].Length, commonWhiteSpace.Length); i++)
{
if (lines[l][i] != commonWhiteSpace[i])
{
commonWhiteSpace = commonWhiteSpace[..i];
break;
}
}
}

sb.Clear();
sb.Append(lines[0]);
foreach (string? line in lines.Skip(1))
{
sb.Append('\n').Append(line[commonWhiteSpace.Length..]);
Comment thread
vbreuss marked this conversation as resolved.
}

return sb.ToString();
}
}
23 changes: 23 additions & 0 deletions Tests/aweXpect.Core.Tests/Core/ExpectationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ public async Task ForMember_WithSucceedingExpectation_ShouldReturnSuccessConstra
await That(constraintResult.GetExpectationText()).IsEqualTo("length equal to 3");
}

[Fact]
public async Task WhenSubjectHasMultipleLines_ShouldTrimCommonWhiteSpace()
{
async Task Act() => await That(new[]
{
1, 2, 3,
}).IsEmpty();

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that new[]
{
1, 2, 3,
}
is empty,
but it was [
1,
2,
3
]
""");
}

[Fact]
public async Task WhenTypeImplementsIDescribableSubject_ShouldUseToStringFromIt()
{
Expand Down
Loading
Loading