-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
262 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace Neo.Compiler.MSIL.UnitTests.TestClasses | ||
{ | ||
public class Contract1 : SmartContract.Framework.SmartContract | ||
{ | ||
public static byte[] unitTest_001() | ||
{ | ||
var nb = new byte[] { 1, 2, 3, 4 }; | ||
return nb; | ||
} | ||
|
||
public static void testVoid() | ||
{ | ||
var nb = new byte[] { 1, 2, 3, 4 }; | ||
} | ||
|
||
public static byte[] testArgs1(byte a) | ||
{ | ||
var nb = new byte[] { 1, 2, 3, 3 }; | ||
nb[3] = a; | ||
return nb; | ||
} | ||
|
||
public static object testArgs2(byte[] a) | ||
{ | ||
return a; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.ComponentModel; | ||
|
||
namespace Neo.Compiler.MSIL.UnitTests.TestClasses | ||
{ | ||
public class Contract2 : SmartContract.Framework.SmartContract | ||
{ | ||
[DisplayName("event")] | ||
public static event Action<object> notify; | ||
|
||
public static byte UnitTest_002(object arg1, object arg2) | ||
{ | ||
notify(arg1); | ||
notify(arg2); | ||
var nb = new byte[] { 1, 2, 3, 4 }; | ||
return nb[2]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="TestClasses\Contract1.cs" /> | ||
<Compile Remove="TestClasses\Contract2.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="TestClasses\Contract1.cs"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Include="TestClasses\Contract2.cs"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\TestEngine\TestEngine.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.IO.Json; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System.IO; | ||
using TestingEngine; | ||
using Compiler = Neo.Compiler.Program; | ||
|
||
namespace TestEngine.UnitTests | ||
{ | ||
[TestClass] | ||
public class UnitTest_Invoke | ||
{ | ||
[TestInitialize] | ||
public void Init() | ||
{ | ||
string path = Directory.GetCurrentDirectory(); | ||
var option = new Compiler.Options() | ||
{ | ||
File = path + "/TestClasses/Contract1.cs" | ||
}; | ||
Compiler.Compile(option); | ||
|
||
//Compile changes the path, reseting so that other UT won't break | ||
Directory.SetCurrentDirectory(path); | ||
Engine.Instance.Reset(); | ||
} | ||
|
||
[TestMethod] | ||
public void Test_Missing_Arguments() | ||
{ | ||
var args = new string[] { | ||
"./TestClasses/Contract1.nef" | ||
}; | ||
var result = Program.Run(args); | ||
|
||
Assert.IsTrue(result.ContainsProperty("error")); | ||
Assert.AreEqual(result["error"].AsString(), "One or more arguments are missing"); | ||
} | ||
|
||
[TestMethod] | ||
public void Test_Method_Without_Parameters_Void() | ||
{ | ||
var args = new string[] { | ||
"./TestClasses/Contract1.nef", | ||
"testVoid" | ||
}; | ||
var result = Program.Run(args); | ||
|
||
// mustn't have errors | ||
Assert.IsTrue(result.ContainsProperty("error")); | ||
Assert.IsNull(result["error"]); | ||
|
||
// test state | ||
Assert.IsTrue(result.ContainsProperty("vm_state")); | ||
Assert.AreEqual(result["vm_state"].AsString(), VMState.HALT.ToString()); | ||
|
||
// test result | ||
Assert.IsTrue(result.ContainsProperty("result_stack")); | ||
Assert.IsInstanceOfType(result["result_stack"], typeof(JArray)); | ||
|
||
var resultStack = result["result_stack"] as JArray; | ||
Assert.IsTrue(resultStack.Count == 0); | ||
} | ||
|
||
[TestMethod] | ||
public void Test_Method_Without_Parameters_With_Return() | ||
{ | ||
var args = new string[] { | ||
"./TestClasses/Contract1.nef", | ||
"unitTest_001" | ||
}; | ||
var result = Program.Run(args); | ||
|
||
// mustn't have errors | ||
Assert.IsTrue(result.ContainsProperty("error")); | ||
Assert.IsNull(result["error"]); | ||
|
||
// test state | ||
Assert.IsTrue(result.ContainsProperty("vm_state")); | ||
Assert.AreEqual(result["vm_state"].AsString(), VMState.HALT.ToString()); | ||
|
||
// test result | ||
StackItem wantresult = new byte[] { 1, 2, 3, 4 }; | ||
Assert.IsTrue(result.ContainsProperty("result_stack")); | ||
Assert.IsInstanceOfType(result["result_stack"], typeof(JArray)); | ||
|
||
var resultStack = result["result_stack"] as JArray; | ||
Assert.IsTrue(resultStack.Count == 1); | ||
Assert.IsTrue(resultStack[0].ContainsProperty("value")); | ||
Assert.AreEqual(resultStack[0]["value"].AsString(), wantresult.ToJson()["value"].AsString()); | ||
} | ||
|
||
[TestMethod] | ||
public void Test_Method_With_Parameters() | ||
{ | ||
StackItem arguments = 16; | ||
var args = new string[] { | ||
"./TestClasses/Contract1.nef", | ||
"testArgs1", | ||
arguments.ToJson().ToString() | ||
}; | ||
var result = Program.Run(args); | ||
|
||
// mustn't have errors | ||
Assert.IsTrue(result.ContainsProperty("error")); | ||
Assert.IsNull(result["error"]); | ||
|
||
// test state | ||
Assert.IsTrue(result.ContainsProperty("vm_state")); | ||
Assert.AreEqual(result["vm_state"].AsString(), VMState.HALT.ToString()); | ||
|
||
// test result | ||
StackItem wantresult = new byte[] { 1, 2, 3, 16 }; | ||
Assert.IsTrue(result.ContainsProperty("result_stack")); | ||
Assert.IsInstanceOfType(result["result_stack"], typeof(JArray)); | ||
|
||
var resultStack = result["result_stack"] as JArray; | ||
Assert.IsTrue(resultStack.Count == 1); | ||
Assert.IsTrue(resultStack[0].ContainsProperty("value")); | ||
Assert.AreEqual(resultStack[0]["value"].AsString(), wantresult.ToJson()["value"].AsString()); | ||
} | ||
|
||
[TestMethod] | ||
public void Test_Method_With_Parameters_Missing() | ||
{ | ||
var args = new string[] { | ||
"./TestClasses/Contract1.nef", | ||
"testArgs1" | ||
}; | ||
var result = Program.Run(args); | ||
|
||
// mustn have an error | ||
Assert.IsTrue(result.ContainsProperty("error")); | ||
Assert.IsNotNull(result["error"]); | ||
|
||
// vm state must've faulted | ||
Assert.IsTrue(result.ContainsProperty("vm_state")); | ||
Assert.AreEqual(result["vm_state"].AsString(), VMState.FAULT.ToString()); | ||
|
||
// result stack must be empty | ||
Assert.IsTrue(result.ContainsProperty("result_stack")); | ||
Assert.IsInstanceOfType(result["result_stack"], typeof(JArray)); | ||
|
||
var resultStack = result["result_stack"] as JArray; | ||
Assert.IsTrue(resultStack.Count == 0); | ||
} | ||
} | ||
} |