Skip to content

Commit

Permalink
accelerate tests (#1265)
Browse files Browse the repository at this point in the history
* accelerate tests

* Update src/Neo.SmartContract.Testing/Coverage/CoverageHit.cs

* Update CoverageHit.cs

* bool tryDecodeOperand

* revert to simple description

* refactor CoverageHit

* Update CoverageHit.cs

* Update src/Neo.SmartContract.Testing/Coverage/CoverageHit.cs

---------

Co-authored-by: Shargon <[email protected]>
Co-authored-by: Jimmy <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2024
1 parent d59d0b8 commit 0239e1d
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 37 deletions.
19 changes: 15 additions & 4 deletions src/Neo.SmartContract.Testing/Coverage/CoverageHit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ namespace Neo.SmartContract.Testing.Coverage
/// Constructor
/// </summary>
/// <param name="offset">Offset</param>
/// <param name="description">Decription</param>
/// <param name="instruction">Instruction</param>
/// <param name="outOfScript">Out of script</param>
/// <param name="methodTokens">Method tokens</param>
[DebuggerDisplay("Offset:{Offset}, Description:{Description}, OutOfScript:{OutOfScript}, Hits:{Hits}, GasTotal:{GasTotal}, GasMin:{GasMin}, GasMax:{GasMax}, GasAvg:{GasAvg}")]
public class CoverageHit(int offset, string description, bool outOfScript = false)
public class CoverageHit(int offset, Instruction instruction, bool outOfScript = false, MethodToken[]? methodTokens = null)
{
/// <summary>
/// The covered instruction
/// </summary>
public Instruction Instruction { get; } = instruction;

/// <summary>
/// The instruction offset
/// </summary>
Expand All @@ -24,7 +30,12 @@ public class CoverageHit(int offset, string description, bool outOfScript = fals
/// <summary>
/// The instruction description
/// </summary>
public string Description { get; } = description;
public string Description => DescriptionFromInstruction(Instruction, MethodTokens);

/// <summary>
/// Method tokens
/// </summary>
public MethodToken[]? MethodTokens { get; } = methodTokens;

/// <summary>
/// The instruction is out of the script
Expand Down Expand Up @@ -108,7 +119,7 @@ public void Hit(CoverageHit value)
/// <returns>CoverageData</returns>
public CoverageHit Clone()
{
return new CoverageHit(Offset, Description, OutOfScript)
return new CoverageHit(Offset, Instruction, OutOfScript, MethodTokens)
{
FeeMax = FeeMax,
FeeMin = FeeMin,
Expand Down
7 changes: 5 additions & 2 deletions src/Neo.SmartContract.Testing/Coverage/CoveredContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class CoveredContract : CoverageBase
/// </summary>
public CoveredMethod[] Methods { get; private set; }

public ContractState? ContractState { get; private set; }

/// <summary>
/// Coverage Lines
/// </summary>
Expand All @@ -54,6 +56,7 @@ public CoveredContract(MethodDetectionMechanism mechanism, UInt160 hash, Contrac
{
Hash = hash;
Methods = Array.Empty<CoveredMethod>();
ContractState = state;

if (state is not null)
{
Expand Down Expand Up @@ -86,7 +89,7 @@ internal void GenerateMethods(MethodDetectionMechanism mechanism, ContractState

if (!_lines.ContainsKey(ip))
{
AddLine(instruction, new CoverageHit(ip, CoverageHit.DescriptionFromInstruction(instruction, state.Nef.Tokens), false));
AddLine(instruction, new CoverageHit(ip, instruction, false, instruction.OpCode == OpCode.CALLT ? state.Nef.Tokens : null));
}

if (mechanism == MethodDetectionMechanism.NextMethod)
Expand Down Expand Up @@ -315,7 +318,7 @@ public void Hit(int instructionPointer, Instruction instruction, long fee, bool?
{
// Note: This call is unusual, out of the expected

coverage = new(instructionPointer, CoverageHit.DescriptionFromInstruction(instruction), true);
coverage = new(instructionPointer, instruction, true, instruction.OpCode == OpCode.CALLT ? this.ContractState?.Nef.Tokens : null);
AddLine(instruction, coverage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.IO;

namespace Neo.Compiler.CSharp.UnitTests
namespace Neo.Compiler.CSharp.UnitTests.Peripheral
{
[TestClass]
public class UnitTest_Debug : DebugAndTestBase<Contract_Debug>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO;
using System.Linq;

namespace Neo.Compiler.CSharp.UnitTests
namespace Neo.Compiler.CSharp.UnitTests.Peripheral
{
[TestClass]
public class UnitTest_DebugInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.IO;
using System.Linq;

namespace Neo.Compiler.CSharp.UnitTests
namespace Neo.Compiler.CSharp.UnitTests.Peripheral
{
[TestClass]
public class UnitTest_Optimize : DebugAndTestBase<Contract_Optimize>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace Neo.Compiler.CSharp.UnitTests
namespace Neo.Compiler.CSharp.UnitTests.Peripheral
{
[TestClass]
public class UnitTest_Parameters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Testing;
using System.IO;
using System.Numerics;
using System.Text;

namespace Neo.Compiler.CSharp.UnitTests.Peripheral
{
[TestClass]
public class UnitTest_PrivateMethod : DebugAndTestBase<Contract1>
{
[TestMethod]
public void Test_PrivateMethod()
{
// Optimizer will remove this method
Assert.IsFalse(Encoding.ASCII.GetString(Contract1.Nef.Script.Span).Contains("NEO3"));

// Compile without optimizations

var testContractsPath = new FileInfo("../../../../Neo.Compiler.CSharp.TestContracts/Contract1.cs").FullName;
var results = new CompilationEngine(new CompilationOptions()
{
Debug = CompilationOptions.DebugType.Extended,
CompilerVersion = "TestingEngine",
Optimize = CompilationOptions.OptimizationType.None,
Nullable = Microsoft.CodeAnalysis.NullableContextOptions.Enable
})
.CompileSources(testContractsPath);

Assert.AreEqual(1, results.Count);
Assert.IsTrue(results[0].Success);

var nef = results[0].CreateExecutable();
Assert.IsTrue(Encoding.ASCII.GetString(nef.Script.Span).Contains("NEO3"));
}
}
}
25 changes: 0 additions & 25 deletions tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Contract1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,6 @@ namespace Neo.Compiler.CSharp.UnitTests
[TestClass]
public class UnitTest_Contract1 : DebugAndTestBase<Contract1>
{
[TestMethod]
public void Test_PrivateMethod()
{
// Optimizer will remove this method
Assert.IsFalse(Encoding.ASCII.GetString(Contract1.Nef.Script.Span).Contains("NEO3"));

// Compile without optimizations

var testContractsPath = new FileInfo("../../../../Neo.Compiler.CSharp.TestContracts/Contract1.cs").FullName;
var results = new CompilationEngine(new CompilationOptions()
{
Debug = CompilationOptions.DebugType.Extended,
CompilerVersion = "TestingEngine",
Optimize = CompilationOptions.OptimizationType.None,
Nullable = Microsoft.CodeAnalysis.NullableContextOptions.Enable
})
.CompileSources(testContractsPath);

Assert.AreEqual(1, results.Count);
Assert.IsTrue(results[0].Success);

var nef = results[0].CreateExecutable();
Assert.IsTrue(Encoding.ASCII.GetString(nef.Script.Span).Contains("NEO3"));
}

[TestMethod]
public void Test_ByteArray_New()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ public void TestCoverageByExtension()
[TestMethod]
public void TestHits()
{
var coverage = new CoverageHit(0, "test");
var coverage = new CoverageHit(0, Neo.VM.Instruction.RET);

Assert.AreEqual(0, coverage.Hits);
Assert.AreEqual("test", coverage.Description);
Assert.AreEqual("RET", coverage.Description);
Assert.AreEqual(0, coverage.FeeAvg);
Assert.AreEqual(0, coverage.FeeMax);
Assert.AreEqual(0, coverage.FeeMin);
Expand Down

0 comments on commit 0239e1d

Please sign in to comment.