-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add byte.AsString and Map UTs (#303)
* add byte.AsString and Map UTs * fix * Update src/Neo.SmartContract.Framework/Helper.cs Co-authored-by: Erik Zhang <[email protected]> * fix * fix * fix Enum and Iterator Test * update neo * fix * fix map key and UT Co-authored-by: Erik Zhang <[email protected]> Co-authored-by: Shargon <[email protected]>
- Loading branch information
1 parent
ec552fe
commit b143a7d
Showing
9 changed files
with
438 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.Compiler.MSIL.UnitTests.Utils; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
|
||
namespace Neo.SmartContract.Framework.UnitTests | ||
{ | ||
[TestClass] | ||
public class MapTest | ||
{ | ||
private TestEngine _engine; | ||
|
||
[TestInitialize] | ||
public void Init() | ||
{ | ||
_engine = new TestEngine(); | ||
_engine.AddEntryScript("./TestClasses/Contract_Map.cs"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestByteArrayMap() | ||
{ | ||
Assert.ThrowsException<System.Exception>(() => _engine.AddEntryScript("./TestClasses/Contract_MapException.cs")); | ||
} | ||
|
||
[TestMethod] | ||
public void TestByteArray() | ||
{ | ||
_engine.Reset(); | ||
StackItem key = System.Text.Encoding.ASCII.GetBytes("a"); | ||
var result = _engine.ExecuteTestCaseStandard("testByteArray", key); | ||
Assert.AreEqual(VMState.HALT, _engine.State); | ||
Assert.AreEqual(1, result.Count); | ||
|
||
var item = result.Pop(); | ||
Assert.IsInstanceOfType(item, typeof(ByteString)); | ||
// Except: {"a":"teststring2"} | ||
Assert.AreEqual("7b2261223a2274657374737472696e6732227d", (item as ByteString).GetSpan().ToHexString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestByteArray2() | ||
{ | ||
_engine.Reset(); | ||
var result = _engine.ExecuteTestCaseStandard("testByteArray2"); | ||
Assert.AreEqual(VMState.HALT, _engine.State); | ||
Assert.AreEqual(1, result.Count); | ||
|
||
var item = result.Pop(); | ||
Assert.IsInstanceOfType(item, typeof(ByteString)); | ||
// Except: {"\u0001\u0001":"\u0022\u0022"} | ||
Assert.AreEqual("7b225c75303030315c7530303031223a225c75303032325c7530303232227d", (item as ByteString).GetSpan().ToHexString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestUnicode() | ||
{ | ||
_engine.Reset(); | ||
StackItem key = System.Text.Encoding.UTF8.GetBytes("中"); | ||
var result = _engine.ExecuteTestCaseStandard("testUnicode", key); | ||
Assert.AreEqual(VMState.HALT, _engine.State); | ||
Assert.AreEqual(1, result.Count); | ||
|
||
var item = result.Pop(); | ||
Assert.IsInstanceOfType(item, typeof(ByteString)); | ||
// Except: {"\u4E2D":"129840test10022939"} | ||
Assert.AreEqual("7b225c7534453244223a22313239383430746573743130303232393339227d", (item as ByteString).GetSpan().ToHexString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestUnicodeValue() | ||
{ | ||
_engine.Reset(); | ||
StackItem value = System.Text.Encoding.UTF8.GetBytes("文"); | ||
var result = _engine.ExecuteTestCaseStandard("testUnicodeValue", value); | ||
Assert.AreEqual(VMState.HALT, _engine.State); | ||
Assert.AreEqual(1, result.Count); | ||
|
||
var item = result.Pop(); | ||
Assert.IsInstanceOfType(item, typeof(ByteString)); | ||
// Except: {"ab":"\u6587"} | ||
Assert.AreEqual("7b226162223a225c7536353837227d", (item as ByteString).GetSpan().ToHexString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestUnicodeKeyValue() | ||
{ | ||
_engine.Reset(); | ||
StackItem key = System.Text.Encoding.UTF8.GetBytes("中"); | ||
StackItem value = System.Text.Encoding.UTF8.GetBytes("文"); | ||
var result = _engine.ExecuteTestCaseStandard("testUnicodeKeyValue", key, value); | ||
Assert.AreEqual(VMState.HALT, _engine.State); | ||
Assert.AreEqual(1, result.Count); | ||
|
||
var item = result.Pop(); | ||
Assert.IsInstanceOfType(item, typeof(ByteString)); | ||
// Except: {"\u4E2D":"\u6587"} | ||
Assert.AreEqual("7b225c7534453244223a225c7536353837227d", (item as ByteString).GetSpan().ToHexString()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestInt() | ||
{ | ||
_engine.Reset(); | ||
StackItem key = 1; | ||
_engine.ExecuteTestCaseStandard("testInt", key); | ||
// Int cannot be used as the key for serializing Map | ||
Assert.AreEqual(VMState.FAULT, _engine.State); | ||
} | ||
|
||
[TestMethod] | ||
public void TestBool() | ||
{ | ||
_engine.Reset(); | ||
StackItem key = true; | ||
_engine.ExecuteTestCaseStandard("testBool", key); | ||
// Bool cannot be used as the key for serializing Map | ||
Assert.AreEqual(VMState.FAULT, _engine.State); | ||
} | ||
|
||
[TestMethod] | ||
public void TestDeserialize() | ||
{ | ||
_engine.Reset(); | ||
var result = _engine.ExecuteTestCaseStandard("testDeserialize", "a"); | ||
Assert.AreEqual(VMState.HALT, _engine.State); | ||
Assert.AreEqual(1, result.Count); | ||
|
||
var item = result.Pop(); | ||
Assert.IsInstanceOfType(item, typeof(Map)); | ||
var map = item as Map; | ||
Assert.AreEqual(1, map.Count); | ||
Assert.IsTrue(map.ContainsKey("a")); | ||
Assert.AreEqual((ByteString)"testdeserialize", map["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
Oops, something went wrong.