Skip to content

Commit

Permalink
Applied suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Jul 30, 2023
1 parent 68a0cff commit a1b928a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
14 changes: 6 additions & 8 deletions Src/FluentAssertions/Specialized/AssemblyAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ public AndConstraint<AssemblyAssertions> Reference(Assembly assembly, string bec
public AndConstraint<AssemblyAssertions> BeUnsigned(string because = "", params object[] becauseArgs)
{
bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith("Expected assembly to be unsigned{reason}, but {context:assembly} is <null>.");
.FailWith("Can't check for assembly signing if {context:assembly} reference is <null>.");

if (success)
{
Expand Down Expand Up @@ -186,22 +185,21 @@ public AndConstraint<AssemblyAssertions> BeSignedWithPublicKey(string publicKey,
Guard.ThrowIfArgumentIsNullOrEmpty(publicKey);

bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith("Expected assembly to have public key {0}{reason}, but {context:assembly} is <null>.", publicKey);
.ForCondition(Subject is not null)
.FailWith("Can't check for assembly signing if {context:assembly} reference is <null>.");

if (success)
{
var bytes = Subject.GetName().GetPublicKey();
var bytes = Subject.GetName().GetPublicKey() ?? Array.Empty<byte>();
var assemblyKey = BitConverter.ToString(bytes).Replace("-", string.Empty, StringComparison.Ordinal);

Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected assembly {0} to have public key {1}{because}, ", Subject.FullName, publicKey)
.ForCondition(bytes.Length != 0)
.FailWith("but it unsigned.")
.FailWith("but it is unsigned.")
.Then
.ForCondition(assemblyKey == publicKey)
.ForCondition(string.Equals(assemblyKey, publicKey, StringComparison.OrdinalIgnoreCase))
.FailWith("but it has {0} instead.", assemblyKey)
.Then
.ClearExpectation();
Expand Down
20 changes: 12 additions & 8 deletions Tests/FluentAssertions.Specs/FindAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ private static byte[] FromHexString(string chars)
#else
private static byte[] FromHexString(string chars)
{
var bytes = new byte[chars.Length / 2];
if (chars.Length % 2 != 0)
{
throw new ArgumentException("Input string length must be even.");
}

for (int i = 0, j = 0; j < bytes.Length; j++, i += 2)
{
bytes[j] = (byte)((Hex(chars[i]) << 4) | Hex(chars[i + 1]));
var bytes = new byte[chars.Length / 2];

static byte Hex(char c) => (byte)(c > '9' ? (c & 0x5F) - 55 : c - '0');
}
for (var i = 0; i < bytes.Length; i++)
{
var bits = chars.Substring(i * 2, 2);
bytes[i] = Convert.ToByte(bits, 16);
}

return bytes;
}
return bytes;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public void Throws_for_null_subject()

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected assembly to be unsigned, but nullAssembly is <null>.");
.WithMessage("Can't check for assembly signing if nullAssembly reference is <null>.");
}
}

Expand Down Expand Up @@ -328,7 +328,7 @@ public void Throws_for_unsigned_assembly()

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected assembly * to have public key *, but it unsigned.");
.WithMessage("Expected assembly * to have public key *, but it is unsigned.");
}

[Fact]
Expand Down Expand Up @@ -356,7 +356,7 @@ public void Throws_for_null_assembly()

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected assembly to have public key *, but nullAssembly is <null>.");
.WithMessage("Can't check for assembly signing if nullAssembly reference is <null>.");
}
}
}
Expand Down

0 comments on commit a1b928a

Please sign in to comment.