Skip to content

Commit 2f51c5d

Browse files
authored
Merge pull request #101 from ngdenterprise/gentests
Generate a skeleton unit test project whenever a new C# contract is created
2 parents d03e9b3 + 5542af2 commit 2f51c5d

17 files changed

+302
-70
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
88

99
### Changed
1010

11+
- Updated C# smart contract template to provide example unit tests
1112
- Fixed an issue preventing contracts from being deployed to TestNet.
13+
- Updated Neo Express to latest RC3 build
1214

1315
## [2.1.47] - 2021-06-08
1416

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@
321321
},
322322
"scripts": {
323323
"bundle-nxp": "npm run bundle-nxp-download && npm run bundle-nxp-extract",
324-
"bundle-nxp-download": "shx rm -rf deps/nxp && shx mkdir -p deps/nxp && nwget \"https://github.com/neo-project/neo-express/releases/download/2.0.37-preview/Neo.Express.2.0.37-preview.nupkg\" -O deps/nxp/nxp.nupkg",
324+
"bundle-nxp-download": "shx rm -rf deps/nxp && shx mkdir -p deps/nxp && nwget \"https://github.com/neo-project/neo-express/releases/download/2.0.39-preview/Neo.Express.2.0.39-preview.nupkg\" -O deps/nxp/nxp.nupkg",
325325
"bundle-nxp-extract": "cd deps/nxp && extract-zip nxp.nupkg",
326326
"compile": "npm run compile-ext && npm run compile-panel",
327327
"compile-ext": "webpack --config src/extension/webpack.config.js --mode development",

resources/new-contract/csharp/Directory.Build.Props.template.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Projects Include="src/$_CLASSNAME_$.csproj" />
5+
<Projects Include="test/$_CLASSNAME_$Tests.csproj" />
6+
</ItemGroup>
7+
<Target Name="build">
8+
<MSBuild Projects="@(Projects)" Targets="build"/>
9+
</Target>
10+
<Target Name="restore">
11+
<MSBuild Projects="@(Projects)" Targets="restore"/>
12+
</Target>
13+
<Target Name="VSTest">
14+
<MSBuild Projects="@(Projects)" Targets="VSTest"/>
15+
</Target>
16+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"neo.express": {
6+
"version": "2.0.37-preview",
7+
"commands": [
8+
"neoxp"
9+
]
10+
},
11+
"neo.compiler.csharp": {
12+
"version": "3.0.0-rc3",
13+
"commands": [
14+
"nccs"
15+
]
16+
}
17+
}
18+
}

resources/new-contract/csharp/$_CLASSNAME_$.csproj.template.txt renamed to resources/new-contract/csharp/src/$_CLASSNAME_$.csproj.template.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>$_CONTRACTNAME_$</RootNamespace>
56
</PropertyGroup>
67

78
<ItemGroup>
8-
<PackageReference Include="Neo.SmartContract.Framework" Version="3.0.0-rc2" />
9+
<PackageReference Include="Neo.SmartContract.Framework" Version="3.0.0-rc3" />
910
</ItemGroup>
1011

1112
<Target
@@ -17,7 +18,7 @@
1718
<_NccsDebugArguments Condition="'$(Configuration)'=='Debug'">--debug --no-optimize</_NccsDebugArguments>
1819
<_NccsDebugArguments Condition="'$(Configuration)'!='Debug'"></_NccsDebugArguments>
1920
</PropertyGroup>
20-
<Exec WorkingDirectory="$(ProjectDir)" Command="nccs $(_NccsDebugArguments) $(MSBuildProjectFullPath)" />
21+
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet nccs $(_NccsDebugArguments) &quot;$(MSBuildProjectFullPath)&quot;" />
2122
</Target>
2223

2324
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
using FluentAssertions;
5+
using Neo.Assertions;
6+
using Neo.BlockchainToolkit;
7+
using Neo.BlockchainToolkit.Models;
8+
using Neo.BlockchainToolkit.SmartContract;
9+
using Neo.SmartContract;
10+
using Neo.VM;
11+
using NeoTestHarness;
12+
using Xunit;
13+
14+
namespace $_CONTRACTNAME_$Tests
15+
{
16+
[CheckpointPath("test/bin/checkpoints/contract-deployed.neoxp-checkpoint")]
17+
public class $_CLASSNAME_$Tests : IClassFixture<CheckpointFixture<$_CLASSNAME_$Tests>>
18+
{
19+
readonly CheckpointFixture fixture;
20+
readonly ExpressChain chain;
21+
22+
public $_CLASSNAME_$Tests(CheckpointFixture<$_CLASSNAME_$Tests> fixture)
23+
{
24+
this.fixture = fixture;
25+
this.chain = fixture.FindChain("$_CONTRACTNAME_$Tests.neo-express");
26+
}
27+
28+
[Fact]
29+
public void contract_owner_in_storage()
30+
{
31+
var settings = chain.GetProtocolSettings();
32+
var owner = chain.GetDefaultAccount("owner").ToScriptHash(settings.AddressVersion);
33+
34+
using var snapshot = fixture.GetSnapshot();
35+
36+
// check to make sure contract owner stored in contract storage
37+
var storages = snapshot.GetContractStorages<$_CLASSNAME_$>();
38+
storages.Count().Should().Be(1);
39+
storages.TryGetValue("MetadataOwner", out var item).Should().BeTrue();
40+
item!.Should().Be(owner);
41+
}
42+
43+
[Fact]
44+
public void can_change_number()
45+
{
46+
var settings = chain.GetProtocolSettings();
47+
var alice = chain.GetDefaultAccount("alice").ToScriptHash(settings.AddressVersion);
48+
49+
using var snapshot = fixture.GetSnapshot();
50+
51+
// ExecuteScript converts the provided expression(s) into a Neo script
52+
// loads them into the engine and executes it
53+
using var engine = new TestApplicationEngine(snapshot, settings, alice);
54+
55+
engine.ExecuteScript<$_CLASSNAME_$>(c => c.changeNumber(42));
56+
57+
engine.State.Should().Be(VMState.HALT);
58+
engine.ResultStack.Should().HaveCount(1);
59+
engine.ResultStack.Peek(0).Should().BeTrue();
60+
61+
// ensure that notification is triggered
62+
engine.Notifications.Should().HaveCount(1);
63+
engine.Notifications[0].EventName.Should().Be("NumberChanged");
64+
engine.Notifications[0].State[0].Should().BeEquivalentTo(alice);
65+
engine.Notifications[0].State[1].Should().BeEquivalentTo(42);
66+
67+
// ensure correct storage item was created
68+
var storages = snapshot.GetContractStorages<$_CLASSNAME_$>();
69+
var contractStorage = storages.StorageMap("$_CLASSNAME_$");
70+
contractStorage.TryGetValue(alice, out var item).Should().BeTrue();
71+
item!.Should().Be(42);
72+
}
73+
}
74+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<NeoContractManifestPath>..\src\bin\sc\$_CLASSNAME_$.manifest.json</NeoContractManifestPath>
6+
<NeoTestPackagesVersion>1.0.37-preview</NeoTestPackagesVersion>
7+
<RootNamespace>$_CONTRACTNAME_$Tests</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\src\$_CLASSNAME_$.csproj">
12+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
13+
</ProjectReference>
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="coverlet.collector" Version="3.0.3">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
22+
<PackageReference Include="Moq" Version="4.16.1" />
23+
<PackageReference Include="Neo.Assertions" Version="$(NeoTestPackagesVersion)" />
24+
<PackageReference Include="Neo.BuildTasks" Version="$(NeoTestPackagesVersion)" PrivateAssets="All" />
25+
<PackageReference Include="Neo.Test.Harness" Version="$(NeoTestPackagesVersion)" />
26+
<PackageReference Include="xunit" Version="2.4.1" />
27+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
28+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
29+
<PrivateAssets>all</PrivateAssets>
30+
</PackageReference>
31+
</ItemGroup>
32+
33+
<Target Name="ConfigureNeoContractInterface" BeforeTargets="ExecuteNeoContractInterface">
34+
<PropertyGroup>
35+
<_NeoContractManifestPath>$([MSBuild]::NormalizePath('$(MSBuildProjectDirectory)', '$(NeoContractManifestPath)'))</_NeoContractManifestPath>
36+
<_NeoContractInterfacePath>$([MSBuild]::NormalizePath('$(IntermediateOutputPath)', 'contract-interface.cs'))</_NeoContractInterfacePath>
37+
</PropertyGroup>
38+
<ItemGroup>
39+
<Compile Include="$(_NeoContractInterfacePath)" />
40+
</ItemGroup>
41+
</Target>
42+
43+
<Target
44+
Name="ExecuteNeoContractInterface"
45+
BeforeTargets="ResolveProjectReferences"
46+
Inputs="$(_NeoContractManifestPath)"
47+
Outputs="$(_NeoContractInterfacePath)">
48+
<NeoContractInterface
49+
ManifestFile="$(_NeoContractManifestPath)"
50+
OutputFile="$(_NeoContractInterfacePath)"
51+
RootNamespace="$(RootNamespace)"/>
52+
</Target>
53+
54+
<Target
55+
Name="ExecuteCreateNeoExpressInstance"
56+
AfterTargets="Build"
57+
Inputs="$(MSBuildProjectFullPath);@(Compile);"
58+
Outputs="$(ProjectDir)/bin/$_CONTRACTNAME_$Tests.neo-express">
59+
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet neoxp create -f bin/$_CONTRACTNAME_$Tests.neo-express" />
60+
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet neoxp wallet create -i bin/$_CONTRACTNAME_$Tests.neo-express owner" />
61+
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet neoxp wallet create -i bin/$_CONTRACTNAME_$Tests.neo-express alice" />
62+
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet neoxp wallet create -i bin/$_CONTRACTNAME_$Tests.neo-express bob" />
63+
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet neoxp batch -i bin/$_CONTRACTNAME_$Tests.neo-express setup-test-chain.batch" />
64+
</Target>
65+
66+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
transfer 10000 gas genesis owner
2+
transfer 10000 gas genesis alice
3+
transfer 10000 gas genesis bob
4+
contract deploy ../src/bin/sc/$_CLASSNAME_$.nef owner
5+
checkpoint create ./bin/checkpoints/contract-deployed -f

0 commit comments

Comments
 (0)