Skip to content

Commit

Permalink
Clean unit test and follow some visual studio hints (#792)
Browse files Browse the repository at this point in the history
* Indentantions

* Follow some Visual studio hints

* Clean code
  • Loading branch information
shargon authored May 31, 2019
1 parent e365f00 commit a8ada56
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 59 deletions.
32 changes: 8 additions & 24 deletions neo.UnitTests/UT_Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ public void TestSetup()
public void Size_Get()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRootVal;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
TestUtils.SetupHeaderWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal);
TestUtils.SetupHeaderWithValues(uut, val256, out _, out _, out _, out _, out _);
// blockbase 4 + 32 + 32 + 4 + 4 + 20 + 3
// header 1
uut.Size.Should().Be(100);
Expand All @@ -35,11 +31,7 @@ public void Size_Get()
public void Deserialize()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRoot;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
TestUtils.SetupHeaderWithValues(new Header(), val256, out merkRoot, out val160, out timestampVal, out indexVal, out scriptVal);
TestUtils.SetupHeaderWithValues(new Header(), val256, out UInt256 merkRoot, out UInt160 val160, out uint timestampVal, out uint indexVal, out Witness scriptVal);

uut.MerkleRoot = merkRoot; // need to set for deserialise to be valid

Expand All @@ -65,7 +57,7 @@ private void assertStandardHeaderTestVals(UInt256 val256, UInt256 merkRoot, UInt
uut.NextConsensus.Should().Be(val160);
uut.Witness.InvocationScript.Length.Should().Be(0);
uut.Witness.Size.Should().Be(scriptVal.Size);
uut.Witness.VerificationScript[0].Should().Be(scriptVal.VerificationScript[0]);
uut.Witness.VerificationScript[0].Should().Be(scriptVal.VerificationScript[0]);
}

[TestMethod]
Expand All @@ -80,18 +72,14 @@ public void Equals_SameHeader()
{
uut.Equals(uut).Should().BeTrue();
}

[TestMethod]
public void Equals_SameHash()
{
Header newHeader = new Header();
Header newHeader = new Header();
UInt256 prevHash = new UInt256(TestUtils.GetByteArray(32, 0x42));
UInt256 merkRoot;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
TestUtils.SetupHeaderWithValues(newHeader, prevHash, out merkRoot, out val160, out timestampVal, out indexVal, out scriptVal);
TestUtils.SetupHeaderWithValues(uut, prevHash, out merkRoot, out val160, out timestampVal, out indexVal, out scriptVal);
TestUtils.SetupHeaderWithValues(newHeader, prevHash, out _, out _, out _, out _, out _);
TestUtils.SetupHeaderWithValues(uut, prevHash, out _, out _, out _, out _, out _);

uut.Equals(newHeader).Should().BeTrue();
}
Expand All @@ -106,11 +94,7 @@ public void Equals_SameObject()
public void Serialize()
{
UInt256 val256 = UInt256.Zero;
UInt256 merkRootVal;
UInt160 val160;
uint timestampVal, indexVal;
Witness scriptVal;
TestUtils.SetupHeaderWithValues(uut, val256, out merkRootVal, out val160, out timestampVal, out indexVal, out scriptVal);
TestUtils.SetupHeaderWithValues(uut, val256, out _, out _, out _, out _, out _);

byte[] data;
using (MemoryStream stream = new MemoryStream())
Expand Down
6 changes: 3 additions & 3 deletions neo.UnitTests/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void GetHashData()
TestVerifiable verifiable = new TestVerifiable();
byte[] res = verifiable.GetHashData();
res.Length.Should().Be(8);
byte[] requiredData = new byte[] {7, 116, 101, 115, 116, 83, 116, 114};
byte[] requiredData = new byte[] { 7, 116, 101, 115, 116, 83, 116, 114 };
for (int i = 0; i < requiredData.Length; i++)
{
res[i].Should().Be(requiredData[i]);
Expand All @@ -26,14 +26,14 @@ public void GetHashData()
public void Sign()
{
TestVerifiable verifiable = new TestVerifiable();
byte[] res = verifiable.Sign(new KeyPair(TestUtils.GetByteArray(32,0x42)));
byte[] res = verifiable.Sign(new KeyPair(TestUtils.GetByteArray(32, 0x42)));
res.Length.Should().Be(64);
}

[TestMethod]
public void ToScriptHash()
{
byte[] testByteArray = TestUtils.GetByteArray(64,0x42);
byte[] testByteArray = TestUtils.GetByteArray(64, 0x42);
UInt160 res = testByteArray.ToScriptHash();
res.Should().Be(UInt160.Parse("2d3b96ae1bcc5a585e075e3b81920210dec16302"));
}
Expand Down
2 changes: 0 additions & 2 deletions neo.UnitTests/UT_InteropPrices.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM;
using System.Reflection;

namespace Neo.UnitTests
{
Expand Down
2 changes: 0 additions & 2 deletions neo.UnitTests/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public void TestSetup()
_unit.Count.ShouldBeEquivalentTo(0);
}



long LongRandom(long min, long max, Random rand)
{
// Only returns positive random long values.
Expand Down
2 changes: 0 additions & 2 deletions neo.UnitTests/UT_P2PMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void Serialize_Deserialize_WithoutPayload_ByteString()
buffer.Count.Should().Be(length);
}


[TestMethod]
public void Compression()
{
Expand Down Expand Up @@ -111,7 +110,6 @@ public void Compression()
payloadCopy.Capabilities.Length.Should().Be(1);
((ServerCapability)payloadCopy.Capabilities[0]).Type.Should().Be(NodeCapabilityType.TcpServer);
((ServerCapability)payloadCopy.Capabilities[0]).Port.Should().Be(25);

}
}
}
2 changes: 1 addition & 1 deletion neo.UnitTests/UT_Policy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void Check_Block_UnblockAccount()
ret.Should().BeOfType<VM.Types.Array>();
((VM.Types.Array)ret).Count.Should().Be(1);
((VM.Types.Array)ret)[0].GetByteArray().ShouldBeEquivalentTo(UInt160.Zero.ToArray());

// Unblock without signature

ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(null),
Expand Down
4 changes: 2 additions & 2 deletions neo.UnitTests/UT_PoolItem.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Neo.Ledger;
using FluentAssertions;
using Neo.Network.P2P.Payloads;
using System;

namespace Neo.UnitTests
{
Expand Down
28 changes: 17 additions & 11 deletions neo.UnitTests/UT_StorageKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ public void Equals_SameHash_SameKey()
{
UInt160 val = new UInt160(TestUtils.GetByteArray(20, 0x42));
byte[] keyVal = TestUtils.GetByteArray(10, 0x42);
StorageKey newSk = new StorageKey();
newSk.ScriptHash = val;
newSk.Key = keyVal;
StorageKey newSk = new StorageKey
{
ScriptHash = val,
Key = keyVal
};
uut.ScriptHash = val;
uut.Key = keyVal;

Expand All @@ -76,10 +78,12 @@ public void Equals_DiffHash_SameKey()
{
UInt160 val = new UInt160(TestUtils.GetByteArray(20, 0x42));
byte[] keyVal = TestUtils.GetByteArray(10, 0x42);
StorageKey newSk = new StorageKey();
newSk.ScriptHash = val;
newSk.Key = keyVal;
uut.ScriptHash = new UInt160(TestUtils.GetByteArray(20, 0x88));
StorageKey newSk = new StorageKey
{
ScriptHash = val,
Key = keyVal
};
uut.ScriptHash = new UInt160(TestUtils.GetByteArray(20, 0x88));
uut.Key = keyVal;

uut.Equals(newSk).Should().BeFalse();
Expand All @@ -91,11 +95,13 @@ public void Equals_SameHash_DiffKey()
{
UInt160 val = new UInt160(TestUtils.GetByteArray(20, 0x42));
byte[] keyVal = TestUtils.GetByteArray(10, 0x42);
StorageKey newSk = new StorageKey();
newSk.ScriptHash = val;
newSk.Key = keyVal;
StorageKey newSk = new StorageKey
{
ScriptHash = val,
Key = keyVal
};
uut.ScriptHash = val;
uut.Key = TestUtils.GetByteArray(10, 0x88);
uut.Key = TestUtils.GetByteArray(10, 0x88);

uut.Equals(newSk).Should().BeFalse();
}
Expand Down
2 changes: 1 addition & 1 deletion neo.UnitTests/UT_Transaction.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Json;
using Neo.IO;
using Neo.IO.Json;
using Neo.Network.P2P.Payloads;

namespace Neo.UnitTests
Expand Down
8 changes: 0 additions & 8 deletions neo.UnitTests/UT_UIntBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ private byte[] RandomBytes(int count)
return (elapsed, result);
}

/* Could do this also so just pass the method to benchmark, but overhead of delegate call might affect benchmark
public delegate int ComparisonMethod(byte[] b1, byte[] b2);
public int BechmarkComparisonMethod(ComparisonMethod compareMethod)
{
}
*/

[TestMethod]
public void Benchmark_CompareTo_UInt256()
{
Expand Down
5 changes: 2 additions & 3 deletions neo.UnitTests/UT_Witness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void InvocationScript_Set()
byte[] dataArray = new byte[] { 0, 32, 32, 20, 32, 32 };
uut.InvocationScript = dataArray;
uut.InvocationScript.Length.Should().Be(6);
Assert.AreEqual(uut.InvocationScript.ToHexString(), "002020142020");
Assert.AreEqual(uut.InvocationScript.ToHexString(), "002020142020");
}

private void setupWitnessWithValues(Witness uut, int lenghtInvocation, int lengthVerification, out byte[] invocationScript, out byte[] verificationScript)
Expand Down Expand Up @@ -68,10 +68,9 @@ public void ToJson()

JObject json = uut.ToJson();
Assert.IsTrue(json.ContainsProperty("invocation"));
Assert.IsTrue(json.ContainsProperty("verification"));
Assert.IsTrue(json.ContainsProperty("verification"));
Assert.AreEqual(json["invocation"].AsString(), "2020");
Assert.AreEqual(json["verification"].AsString(), "202020");

}
}
}

0 comments on commit a8ada56

Please sign in to comment.