Skip to content

Commit

Permalink
Add ut and Rename
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Nov 26, 2019
1 parent 0295ec1 commit 84698fe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/neo/SmartContract/InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private static bool Enumerator_Create(ApplicationEngine engine)
enumerator = new ArrayWrapper(array);
break;
case PrimitiveType primitive:
enumerator = new ByteArrayWrapper(primitive);
enumerator = new PrimitiveWrapper(primitive);
break;
default:
return false;
Expand Down Expand Up @@ -320,7 +320,7 @@ private static bool Iterator_Create(ApplicationEngine engine)
iterator = new MapWrapper(map);
break;
case PrimitiveType primitive:
iterator = new ByteArrayWrapper(primitive);
iterator = new PrimitiveWrapper(primitive);
break;
default:
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Neo.SmartContract.Iterators
{
internal class ByteArrayWrapper : IIterator
internal class PrimitiveWrapper : IIterator
{
private readonly byte[] array;
private int index = -1;

public ByteArrayWrapper(PrimitiveType value)
public PrimitiveWrapper(PrimitiveType value)
{
this.array = value.ToByteArray().ToArray();
}
Expand Down
49 changes: 49 additions & 0 deletions tests/neo.UnitTests/SmartContract/Iterators/UT_PrimitiveWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Iterators;
using Neo.VM;
using Neo.VM.Types;
using System;

namespace Neo.UnitTests.SmartContract.Iterators
{
[TestClass]
public class UT_PrimitiveWrapper
{
[TestMethod]
public void TestGeneratorAndDispose()
{
PrimitiveWrapper arrayWrapper = new PrimitiveWrapper(new ByteArray(new byte[0]));
Assert.IsNotNull(arrayWrapper);
Action action = () => arrayWrapper.Dispose();
action.Should().NotThrow<Exception>();
}

[TestMethod]
public void TestKeyAndValue()
{
PrimitiveWrapper arrayWrapper = new PrimitiveWrapper(new byte[] { 0x01, 0x02 });
Action action1 = () => arrayWrapper.Key();
action1.Should().Throw<InvalidOperationException>();
Action action2 = () => arrayWrapper.Value();
action2.Should().Throw<InvalidOperationException>();
arrayWrapper.Next();
Assert.AreEqual(0x00, arrayWrapper.Key().GetBigInteger());
Assert.AreEqual(0x01, arrayWrapper.Value());
arrayWrapper.Next();
Assert.AreEqual(0x01, arrayWrapper.Key().GetBigInteger());
Assert.AreEqual(0x02, arrayWrapper.Value());
}

[TestMethod]
public void TestNext()
{
PrimitiveWrapper arrayWrapper = new PrimitiveWrapper(new byte[] { 0x01, 0x02 });
Assert.AreEqual(true, arrayWrapper.Next());
Assert.AreEqual(0x01, arrayWrapper.Value());
Assert.AreEqual(true, arrayWrapper.Next());
Assert.AreEqual(0x02, arrayWrapper.Value());
Assert.AreEqual(false, arrayWrapper.Next());
}
}
}

0 comments on commit 84698fe

Please sign in to comment.