Skip to content

Commit

Permalink
Feature: add unit tests for NeedSnapshot and NeedApplicationEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
nan01ab committed Jan 24, 2025
1 parent d7717e2 commit 1ea6b78
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
// modifications are permitted.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System.Reflection;

namespace Neo.UnitTests.SmartContract.Native
{
Expand All @@ -28,5 +31,48 @@ public void TestConstructorOneArg()

Assert.AreEqual(Hardfork.HF_Aspidochelone, arg.ActiveIn);
}

class NeedSnapshot
{
[ContractMethod]
public bool MethodReadOnlyStoreView(IReadOnlyStoreView view) => view is null;

[ContractMethod]
public bool MethodDataCache(DataCache dataCache) => dataCache is null;
}

class NoNeedSnapshot
{
[ContractMethod]
public bool MethodTwo(ApplicationEngine engine, UInt160 account)
=> engine is null || account is null;

[ContractMethod]
public bool MethodOne(ApplicationEngine engine) => engine is null;
}

[TestMethod]
public void TestNeedSnapshot()
{
var flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
foreach (var member in typeof(NeedSnapshot).GetMembers(flags))
{
foreach (var attribute in member.GetCustomAttributes<ContractMethodAttribute>())
{
var metadata = new ContractMethodMetadata(member, attribute);
Assert.IsTrue(metadata.NeedSnapshot);
}
}

foreach (var member in typeof(NoNeedSnapshot).GetMembers(flags))
{
foreach (var attribute in member.GetCustomAttributes<ContractMethodAttribute>())
{
var metadata = new ContractMethodMetadata(member, attribute);
Assert.IsFalse(metadata.NeedSnapshot);
Assert.IsTrue(metadata.NeedApplicationEngine);
}
}
}
}
}

0 comments on commit 1ea6b78

Please sign in to comment.