Skip to content

Commit

Permalink
add StackItemAssertions BeEquivalentTo ReadOnlySpan<byte> overload (#14)
Browse files Browse the repository at this point in the history
* add StackItemAssertions BeEquivalentTo ReadOnlySpan<byte> overload

* GetContractAddress -> GetContractScriptHash

* update Neo.BlockchainToolkit3 Version

Co-authored-by: Harry <harrypierson@outlook.com>
  • Loading branch information
devhawk and Harry committed Jun 22, 2021
1 parent 90c3f1f commit 4f35fc0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
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
2 changes: 1 addition & 1 deletion src/test-harness/test-harness.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Neo.BlockchainToolkit3" Version="1.0.51-preview" />
<PackageReference Include="Neo.BlockchainToolkit3" Version="1.0.52-preview" />
</ItemGroup>
</Project>

0 comments on commit 4f35fc0

Please sign in to comment.