forked from neo-project/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit Test for Smartcontract Module (neo-project#1090)
* submit ut * fix * Enhance functions in TestDataCache * git amend blockchain * fix test related to TestDataCache * dotnet format * dotnet format * add blank line * fix test * Optimize random * Optimize Random * fix test * add decimal test * fix * 2019/9/25 16:54 change format * Fixes events * update assertion sentence * update UT following code change * format * add type check * recommit * recommit
- Loading branch information
Showing
32 changed files
with
3,620 additions
and
2 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
neo.UnitTests/SmartContract/Enumerators/UT_ConcatenatedEnumerator.cs
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,55 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.SmartContract.Enumerators; | ||
using Neo.SmartContract.Iterators; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.UnitTests.SmartContract.Enumerators | ||
{ | ||
[TestClass] | ||
public class UT_ConcatenatedEnumerator | ||
{ | ||
[TestMethod] | ||
public void TestConcatenatedIteratorAndDispose() | ||
{ | ||
List<StackItem> list1 = new List<StackItem>(); | ||
StackItem stackItem1 = new Integer(0); | ||
list1.Add(stackItem1); | ||
List<StackItem> list2 = new List<StackItem>(); | ||
StackItem stackItem2 = new Integer(0); | ||
list2.Add(stackItem2); | ||
ArrayWrapper arrayWrapper1 = new ArrayWrapper(list1); | ||
ArrayWrapper arrayWrapper2 = new ArrayWrapper(list2); | ||
IteratorKeysWrapper it1 = new IteratorKeysWrapper(arrayWrapper1); | ||
IteratorKeysWrapper it2 = new IteratorKeysWrapper(arrayWrapper2); | ||
ConcatenatedEnumerator uut = new ConcatenatedEnumerator(it1, it2); | ||
Assert.IsNotNull(uut); | ||
Action action = () => uut.Dispose(); | ||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNextAndValue() | ||
{ | ||
List<StackItem> list1 = new List<StackItem>(); | ||
StackItem stackItem1 = new Integer(1); | ||
list1.Add(stackItem1); | ||
List<StackItem> list2 = new List<StackItem>(); | ||
StackItem stackItem2 = new Integer(0); | ||
list2.Add(stackItem2); | ||
ArrayWrapper arrayWrapper1 = new ArrayWrapper(list1); | ||
ArrayWrapper arrayWrapper2 = new ArrayWrapper(list2); | ||
IteratorKeysWrapper it1 = new IteratorKeysWrapper(arrayWrapper1); | ||
IteratorKeysWrapper it2 = new IteratorKeysWrapper(arrayWrapper2); | ||
ConcatenatedEnumerator uut = new ConcatenatedEnumerator(it1, it2); | ||
Assert.AreEqual(true, uut.Next()); | ||
Assert.AreEqual(new Integer(0), uut.Value()); | ||
Assert.AreEqual(true, uut.Next()); | ||
Assert.AreEqual(new Integer(0), uut.Value()); | ||
Assert.AreEqual(false, uut.Next()); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs
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,36 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.SmartContract.Enumerators; | ||
using Neo.SmartContract.Iterators; | ||
using Neo.VM; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.UnitTests.SmartContract.Enumerators | ||
{ | ||
[TestClass] | ||
public class UT_IteratorKeysWrapper | ||
{ | ||
[TestMethod] | ||
public void TestGeneratorAndDispose() | ||
{ | ||
IteratorKeysWrapper iteratorKeysWrapper = new IteratorKeysWrapper(new ArrayWrapper(new List<StackItem>())); | ||
Assert.IsNotNull(iteratorKeysWrapper); | ||
Action action = () => iteratorKeysWrapper.Dispose(); | ||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNextAndValue() | ||
{ | ||
StackItem stackItem = new VM.Types.Boolean(true); | ||
List<StackItem> list = new List<StackItem>(); | ||
list.Add(stackItem); | ||
ArrayWrapper wrapper = new ArrayWrapper(list); | ||
IteratorKeysWrapper iteratorKeysWrapper = new IteratorKeysWrapper(wrapper); | ||
Action action = () => iteratorKeysWrapper.Next(); | ||
action.Should().NotThrow<Exception>(); | ||
Assert.AreEqual(new VM.Types.Integer(0), iteratorKeysWrapper.Value()); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs
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,37 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.SmartContract.Enumerators; | ||
using Neo.SmartContract.Iterators; | ||
using Neo.VM; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.UnitTests.SmartContract.Enumerators | ||
{ | ||
|
||
[TestClass] | ||
public class UT_IteratorValuesWrapper | ||
{ | ||
[TestMethod] | ||
public void TestGeneratorAndDispose() | ||
{ | ||
IteratorValuesWrapper iteratorValuesWrapper = new IteratorValuesWrapper(new ArrayWrapper(new List<StackItem>())); | ||
Assert.IsNotNull(iteratorValuesWrapper); | ||
Action action = () => iteratorValuesWrapper.Dispose(); | ||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNextAndValue() | ||
{ | ||
StackItem stackItem = new VM.Types.Boolean(true); | ||
List<StackItem> list = new List<StackItem>(); | ||
list.Add(stackItem); | ||
ArrayWrapper wrapper = new ArrayWrapper(list); | ||
IteratorValuesWrapper iteratorValuesWrapper = new IteratorValuesWrapper(wrapper); | ||
Action action = () => iteratorValuesWrapper.Next(); | ||
action.Should().NotThrow<Exception>(); | ||
Assert.AreEqual(stackItem, iteratorValuesWrapper.Value()); | ||
} | ||
} | ||
} |
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,50 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.SmartContract.Iterators; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.UnitTests.SmartContract.Iterators | ||
{ | ||
[TestClass] | ||
public class UT_ArrayWrapper | ||
{ | ||
[TestMethod] | ||
public void TestGeneratorAndDispose() | ||
{ | ||
ArrayWrapper arrayWrapper = new ArrayWrapper(new List<StackItem>()); | ||
Assert.IsNotNull(arrayWrapper); | ||
Action action = () => arrayWrapper.Dispose(); | ||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestKeyAndValue() | ||
{ | ||
List<StackItem> list = new List<StackItem>(); | ||
StackItem stackItem = new Integer(0); | ||
list.Add(stackItem); | ||
ArrayWrapper arrayWrapper = new ArrayWrapper(list); | ||
Action action1 = () => arrayWrapper.Key(); | ||
action1.Should().Throw<InvalidOperationException>(); | ||
Action action2 = () => arrayWrapper.Value(); | ||
action2.Should().Throw<InvalidOperationException>(); | ||
arrayWrapper.Next(); | ||
Assert.AreEqual(stackItem, arrayWrapper.Key()); | ||
Assert.AreEqual(stackItem, arrayWrapper.Value()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNext() | ||
{ | ||
List<StackItem> list = new List<StackItem>(); | ||
ArrayWrapper arrayWrapper = new ArrayWrapper(list); | ||
Assert.AreEqual(false, arrayWrapper.Next()); | ||
StackItem stackItem = new Integer(0); | ||
list.Add(stackItem); | ||
Assert.AreEqual(true, arrayWrapper.Next()); | ||
} | ||
} | ||
} |
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,43 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.SmartContract.Iterators; | ||
using Neo.VM; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.UnitTests.SmartContract.Iterators | ||
{ | ||
[TestClass] | ||
public class UT_MapWrapper | ||
{ | ||
[TestMethod] | ||
public void TestGeneratorAndDispose() | ||
{ | ||
MapWrapper mapWrapper = new MapWrapper(new List<KeyValuePair<StackItem, StackItem>>()); | ||
Assert.IsNotNull(mapWrapper); | ||
Action action = () => mapWrapper.Dispose(); | ||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestKeyAndValue() | ||
{ | ||
List<KeyValuePair<StackItem, StackItem>> list = new List<KeyValuePair<StackItem, StackItem>>(); | ||
StackItem stackItem1 = new Integer(0); | ||
StackItem stackItem2 = new Integer(1); | ||
list.Add(new KeyValuePair<StackItem, StackItem>(stackItem1, stackItem2)); | ||
MapWrapper mapWrapper = new MapWrapper(list); | ||
mapWrapper.Next(); | ||
Assert.AreEqual(stackItem1, mapWrapper.Key()); | ||
Assert.AreEqual(stackItem2, mapWrapper.Value()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNext() | ||
{ | ||
MapWrapper mapWrapper = new MapWrapper(new List<KeyValuePair<StackItem, StackItem>>()); | ||
Assert.AreEqual(false, mapWrapper.Next()); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
neo.UnitTests/SmartContract/Iterators/UT_StorageIterator.cs
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,38 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.Ledger; | ||
using Neo.SmartContract.Iterators; | ||
using Neo.VM.Types; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.UnitTests.SmartContract.Iterators | ||
{ | ||
[TestClass] | ||
public class UT_StorageIterator | ||
{ | ||
[TestMethod] | ||
public void TestGeneratorAndDispose() | ||
{ | ||
StorageIterator storageIterator = new StorageIterator(new List<KeyValuePair<StorageKey, StorageItem>>().GetEnumerator()); | ||
Assert.IsNotNull(storageIterator); | ||
Action action = () => storageIterator.Dispose(); | ||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestKeyAndValueAndNext() | ||
{ | ||
List<KeyValuePair<StorageKey, StorageItem>> list = new List<KeyValuePair<StorageKey, StorageItem>>(); | ||
StorageKey storageKey = new StorageKey(); | ||
storageKey.Key = new byte[1]; | ||
StorageItem storageItem = new StorageItem(); | ||
storageItem.Value = new byte[1]; | ||
list.Add(new KeyValuePair<StorageKey, StorageItem>(storageKey, storageItem)); | ||
StorageIterator storageIterator = new StorageIterator(list.GetEnumerator()); | ||
storageIterator.Next(); | ||
Assert.AreEqual(new ByteArray(new byte[1]), storageIterator.Key()); | ||
Assert.AreEqual(new ByteArray(new byte[1]), storageIterator.Value()); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
neo.UnitTests/SmartContract/Manifest/UT_ContractEventDescriptor.cs
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,22 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.SmartContract.Manifest; | ||
|
||
namespace Neo.UnitTests.SmartContract.Manifest | ||
{ | ||
[TestClass] | ||
public class UT_ContractEventDescriptor | ||
{ | ||
[TestMethod] | ||
public void TestFromJson() | ||
{ | ||
ContractEventDescriptor expected = new ContractEventDescriptor | ||
{ | ||
Name = "AAA", | ||
Parameters = new ContractParameterDefinition[0] | ||
}; | ||
ContractEventDescriptor actual = ContractEventDescriptor.FromJson(expected.ToJson()); | ||
Assert.AreEqual(expected.Name, actual.Name); | ||
Assert.AreEqual(0, actual.Parameters.Length); | ||
} | ||
} | ||
} |
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,42 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.Cryptography; | ||
using Neo.Cryptography.ECC; | ||
using Neo.SmartContract.Manifest; | ||
using Neo.Wallets; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Neo.UnitTests.SmartContract.Manifest | ||
{ | ||
[TestClass] | ||
public class UT_ContractGroup | ||
{ | ||
[TestMethod] | ||
public void TestIsValid() | ||
{ | ||
Random random = new Random(); | ||
byte[] privateKey = new byte[32]; | ||
random.NextBytes(privateKey); | ||
KeyPair keyPair = new KeyPair(privateKey); | ||
ContractGroup contractGroup = new ContractGroup | ||
{ | ||
PubKey = keyPair.PublicKey, | ||
Signature = new byte[20] | ||
}; | ||
Assert.AreEqual(false, contractGroup.IsValid(UInt160.Zero)); | ||
|
||
|
||
byte[] message = new byte[] { 0x01,0x01,0x01,0x01,0x01, | ||
0x01,0x01,0x01,0x01,0x01, | ||
0x01,0x01,0x01,0x01,0x01, | ||
0x01,0x01,0x01,0x01,0x01 }; | ||
byte[] signature = Crypto.Default.Sign(message, keyPair.PrivateKey, keyPair.PublicKey.EncodePoint(false).Skip(1).ToArray()); | ||
contractGroup = new ContractGroup | ||
{ | ||
PubKey = keyPair.PublicKey, | ||
Signature = signature | ||
}; | ||
Assert.AreEqual(true, contractGroup.IsValid(new UInt160(message))); | ||
} | ||
} | ||
} |
Oops, something went wrong.