Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add StackItemAssertions BeEquivalentTo ReadOnlySpan<byte> overload #14

Merged
merged 3 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions src/assertions/StackItemAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public AndConstraint<StackItemAssertions> BeEquivalentTo(object? expected, strin
bool expectedBool => BeEquivalentTo(expectedBool, because, becauseArgs),
UInt160 expectedHash => BeEquivalentTo(expectedHash, because, becauseArgs),
UInt256 expectedHash => BeEquivalentTo(expectedHash, because, becauseArgs),
byte[] expectedByteArray => BeEquivalentTo(expectedByteArray.AsSpan(), because, becauseArgs),
ReadOnlyMemory<byte> expectedMemory => BeEquivalentTo(expectedMemory.Span, because, becauseArgs),
_ => UnsupportedType(expected)
};
}
Expand Down Expand Up @@ -170,5 +172,28 @@ public AndConstraint<StackItemAssertions> BeEquivalentTo(UInt256 expected, strin

return new AndConstraint<StackItemAssertions>(this);
}

public AndConstraint<StackItemAssertions> BeEquivalentTo(ReadOnlySpan<byte> expected, string because = "", params object[] becauseArgs)
{
try
{
var span = Subject.GetSpan();

Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(span.SequenceEqual(expected))
.FailWith("Expected {context:StackItem} to be {0}{reason}, but found {1}.",
Convert.ToHexString(expected), Convert.ToHexString(span));
}
catch (Exception ex)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:StackItem} to support GetString{reason}, but GetString failed with:{0}.", ex.Message);
}

return new AndConstraint<StackItemAssertions>(this);
}

}
}
4 changes: 2 additions & 2 deletions src/test-harness/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void LoadScript<T>(this TestApplicationEngine engine, params Expre
public static Script CreateScript<T>(this DataCache snapshot, params Expression<Action<T>>[] expressions)
where T : class
{
var scriptHash = snapshot.GetContractAddress<T>();
var scriptHash = snapshot.GetContractScriptHash<T>();
using var builder = new ScriptBuilder();
for (int i = 0; i < expressions.Length; i++)
{
Expand Down Expand Up @@ -115,7 +115,7 @@ public static bool TryGetValue(this NeoStorage storage, UInt256 key, [NotNullWhe
public static BigInteger ToBigInteger(this StorageItem storageItem)
=> new BigInteger(storageItem.Value);

public static UInt160 GetContractAddress<T>(this DataCache snapshot)
public static UInt160 GetContractScriptHash<T>(this DataCache snapshot)
where T : class
=> snapshot.GetContract<T>().Hash;

Expand Down