Skip to content

Commit

Permalink
Small unit tests + light refacto + bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Apolixit committed Sep 15, 2024
1 parent f70b10a commit 2669f8c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 80 deletions.
67 changes: 67 additions & 0 deletions Substrate.NET.Metadata.Tests/Conversion/ConversionUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Substrate.NET.Metadata.Conversion.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Substrate.NET.Metadata.Tests.Conversion
{
internal class ConversionUtilsTests
{
[Test]
public void EndsWithArray_ShouldReturnTrue()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 4, 5 };

var result = ConversionUtils.EndsWithArray(array1, array2);

Assert.That(result);
}

[Test]
public void EndsWithArray_ShouldReturnFalse()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 4, 6 };

var result = ConversionUtils.EndsWithArray(array1, array2);

Assert.That(result, Is.False);
}

[Test]
public void AreArraysIdentical_WhenEquals_ShouldReturnTrue()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 1, 2, 3, 4, 5 };

var result = ConversionUtils.AreArraysIdentical(array1, array2);

Assert.That(result);
}

[Test]
public void AreArraysIdentical_WhenNotSameSize_ShouldReturnFalse()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 1, 2, 3, 4 };

var result = ConversionUtils.AreArraysIdentical(array1, array2);

Assert.That(result, Is.False);
}

[Test]
public void AreArraysIdentical_WhenDifferent_ShouldReturnFalse()
{
var array1 = new[] { 1, 2, 3, 4, 5 };
var array2 = new[] { 1, 2, 3, 4, 6 };

var result = ConversionUtils.AreArraysIdentical(array1, array2);

Assert.That(result, Is.False);
}
}
}
4 changes: 2 additions & 2 deletions Substrate.NET.Metadata.Tests/ImplementationCheckTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public void AllBaseTypeSubclasses_ShouldImplementEncode_ReturnsByteArray()
}

// Ensure the class has the Encode method
MethodInfo encodeMethod = type.GetMethod("Encode");
MethodInfo encodeMethod = type!.GetMethod("Encode");
Assert.That(encodeMethod, Is.Not.Null, $"Class {type.Name} does not implement the Encode() method.");

try
{
_ = (byte[])encodeMethod.Invoke(instance, null);
_ = (byte[])encodeMethod!.Invoke(instance, null);
}
catch (TargetInvocationException ex) when (ex.InnerException is NotImplementedException)
{
Expand Down
2 changes: 1 addition & 1 deletion Substrate.NET.Metadata.Tests/MetadataServiceV13Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void StorageEntryMetadataV13_WhenNMap_ShouldThrowException()

storageEntry.StorageType.Value = StorageType.Type.NMap;

Assert.Throws<MetadataConversionException>(() => storageEntry.ToStorageEntryMetadataV14(new Conversion.Internal.ConversionBuilder(new List<Base.Portable.PortableType>())));
Assert.Throws<MetadataConversionException>(() => storageEntry.ToStorageEntryMetadataV14(new Metadata.Conversion.Internal.ConversionBuilder(new List<Base.Portable.PortableType>())));
}
}
}
2 changes: 1 addition & 1 deletion Substrate.NET.Metadata.Tests/MetadataServiceV14Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void MetadataV14_SpecVersionCompare_V9370_And_V9420_ShouldSucceed()


// Some calls has been added and one has been removed
var callsMethodChanged = palletBalance.Calls.LookupDifferentialType.TypeVariant.Elems;
var callsMethodChanged = palletBalance!.Calls.LookupDifferentialType.TypeVariant.Elems;
Assert.That(callsMethodChanged.Count(), Is.GreaterThan(1));
Assert.That(callsMethodChanged.Any(x => x.Item1 == CompareStatus.Added && x.Item2.Name.Value == "set_balance_deprecated"));
Assert.That(callsMethodChanged.Any(x => x.Item1 == CompareStatus.Removed && x.Item2.Name.Value == "set_balance"));
Expand Down
8 changes: 5 additions & 3 deletions Substrate.NET.Metadata/Base/StorageEntryTypeNMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Substrate.NetApi.Model.Types.Primitive;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -12,14 +13,15 @@ namespace Substrate.NET.Metadata.Base
/// Represent a "NMap" storage with generic Hasher
/// </summary>
/// <typeparam name="THasher"></typeparam>
[ExcludeFromCodeCoverage] // Should never happened. Here just for consistency
public class StorageEntryTypeNMap<THasher> : BaseType
where THasher : Enum
{
public BaseVec<BaseEnum<THasher>> Hashers { get; private set; }
public BaseVec<BaseEnum<THasher>> Hashers { get; private set; } = default!;

public BaseVec<Str> KeyVec { get; private set; }
public BaseVec<Str> KeyVec { get; private set; } = default!;

public Str Value { get; private set; }
public Str Value { get; private set; } = default!;

public override byte[] Encode()
{
Expand Down
13 changes: 2 additions & 11 deletions Substrate.NET.Metadata/Conversion/Internal/ConversionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public ConversionBuilder(List<PortableType> portableTypes)
/// <returns></returns>
public U32 GetNewIndex()
{
if (!PortableTypes.Any()) return new U32(START_INDEX);
if (PortableTypes.Count == 0) return new U32(START_INDEX);
return new U32(Math.Max(START_INDEX, PortableTypes.Max(x => x.Id.Value) + 1));
}

Expand Down Expand Up @@ -326,15 +326,6 @@ private U32 AddRuntimeLookup(string objType, string palletName, Variant[] varian
return index;
}

public PalletEventMetadataV14 ConvertToEventV14(string eventName, string eventType)
{
var res = new PalletEventMetadataV14();

res.ElemType = TType.From(BuildPortableTypes(eventType).Value);

return res;
}

public void CreateEventBlockchainRuntimeEvent()
{
var node = new TypeDefVariant();
Expand Down Expand Up @@ -403,7 +394,7 @@ public PortableType CreatePortableTypeFromNode(BaseType node, List<string>? path
portableType.Ty.Path.Create(new Str[0]);
}

portableType.Ty.TypeParams = new BaseVec<TypeParameter>(new TypeParameter[0]);
portableType.Ty.TypeParams = new BaseVec<TypeParameter>([]);

if (node is TypeDefTuple tdt)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using Substrate.NET.Metadata.Base;
using Substrate.NET.Metadata.Base.Portable;
using Substrate.NetApi.Model.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Substrate.NET.Metadata.Conversion.Internal
{
Expand Down Expand Up @@ -42,15 +35,13 @@ internal static NodeBuilderType Build(NodeBuilderType nodeBuilderType)
if (ExtractPrimitive(nodeBuilderType) is NodeBuilderTypePrimitive primitive && nodeBuilderType is NodeBuilderTypeUndefined)
{
nodeBuilderType = primitive!;
//return primitive;
}

if (nodeBuilderType is NodeBuilderTypeUndefined)
{
var content = HarmonizeTypeName(nodeBuilderType.Adapted);
var hardBinding = SearchV14.HardBinding(content, nodeBuilderType.PalletContext);

//if(!hardBinding.Equals(nodeBuilderType.Raw, StringComparison.CurrentCultureIgnoreCase))
if (!string.IsNullOrEmpty(hardBinding) && hardBinding != content)
{
nodeBuilderType = Build(new NodeBuilderTypeUndefined(hardBinding, nodeBuilderType.Raw, nodeBuilderType.PalletContext));
Expand Down
45 changes: 0 additions & 45 deletions Substrate.NET.Metadata/Conversion/Internal/StorageClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,6 @@ public StorageClass(StorageType type, string moduleName, string propName, string
public string PropName { get; set; }
public string ClassName { get; set; }

private string? classNameMapped = null;
public string ClassNameMapped
{
get
{
if (classNameMapped is null)
classNameMapped = CustomMapping(ClassName);

return classNameMapped;
}
}

public string CustomMapping(string className)
{
return className switch
{
"TaskAddress<BlockNumber>" => "TaskAddress<T::BlockNumber>",
"Vec<IdentificationTuple>" => "Vec<IdentificationTuple<T>>",
"Vec<AccountId>" => "Vec<T::AccountId>",
"Vec<(AccountId, Balance)>" => "Vec<(<T as frame_system::Config>::AccountId, BalanceOf<T>)>",
"sp_std::marker::PhantomData<(AccountId, Event)>" => "BaseVoid", // Pas sur de moi
"Timepoint<BlockNumber>" => "Timepoint<T::BlockNumber>",
"limits::BlockWeights" => "BlockWeights",
"limits::BlockLength" => "BlockLength",
"TransactionPriority" => "u64",
"& 'static[u8]" => "vec<u8>",
"&[u8]" => "vec<u8>",
"Moment" => "u64",
"ModuleId" => "PalletId",
"LeasePeriod" => "LeasePeriodOf<T>",
_ => className
};
}

public void SetToFoundInV14(int index)
{
FoundInV14 = true;
Index_v14_9110 = index;
}

public static string WriteStoragePlain(Str plain) => WriteStoragePlain(plain.Value);
public static string WriteStoragePlain(string plain)
{
Expand Down Expand Up @@ -149,11 +109,6 @@ public static string WriteStorageNMap(StorageEntryTypeNMapV13 nMap)
return $"[Hashers = [{hasher} / Keys = {keyVec} / Value = {nMap.Value.Value}";
}

public override string ToString()
{
return $"{ModuleName}|{Type}|{PropName}|{ClassName}|{(FoundInV14 ? ClassNameMapped : "")}|{FoundInV14}|{(Index_v14_9110 is not null ? Index_v14_9110.Value : "")}";
}

public enum StorageType
{
Call,
Expand Down
2 changes: 1 addition & 1 deletion Substrate.NET.Metadata/Substrate.NET.Metadata.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<Version>0.1.4</Version>
<Version>0.1.5</Version>
<Description>Substrate metadata version from V9 to V15 with .NET</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
14 changes: 7 additions & 7 deletions Substrate.NET.Metadata/V13/ModuleMetadataV13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace Substrate.NET.Metadata.V13
{
public class ModuleMetadataV13 : BaseType, IMetadataName
{
public Str Name { get; private set; }
public BaseOpt<PalletStorageMetadataV13> Storage { get; private set; }
public BaseOpt<BaseVec<PalletCallMetadataV13>> Calls { get; private set; }
public BaseOpt<BaseVec<PalletEventMetadataV13>> Events { get; private set; }
public BaseVec<PalletConstantMetadataV13> Constants { get; private set; }
public BaseVec<PalletErrorMetadataV13> Errors { get; private set; }
public U8 Index { get; private set; }
public Str Name { get; private set; } = default!;
public BaseOpt<PalletStorageMetadataV13> Storage { get; private set; } = default!;
public BaseOpt<BaseVec<PalletCallMetadataV13>> Calls { get; private set; } = default!;
public BaseOpt<BaseVec<PalletEventMetadataV13>> Events { get; private set; } = default!;
public BaseVec<PalletConstantMetadataV13> Constants { get; private set; } = default!;
public BaseVec<PalletErrorMetadataV13> Errors { get; private set; } = default!;
public U8 Index { get; private set; } = default!;

public override void Decode(byte[] byteArray, ref int p)
{
Expand Down

0 comments on commit 2669f8c

Please sign in to comment.